The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/pkg/registry/apis/provisioning/filepath_test.go

79 lines
1.6 KiB

package provisioning
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestPathAfterPrefix(t *testing.T) {
tests := []struct {
name string
urlPath string
prefix string
want string
expectError bool
}{
{
name: "basic path with prefix",
urlPath: "/a/b/prefix/c",
prefix: "/prefix",
want: "c",
expectError: false,
},
{
name: "path with multiple prefix occurrences",
urlPath: "/a/prefix/b/prefix/c",
prefix: "/prefix",
want: "b/prefix/c",
expectError: false,
},
{
name: "path without prefix",
urlPath: "/a/b/c",
prefix: "/prefix",
want: "",
expectError: true,
},
{
name: "empty path",
urlPath: "",
prefix: "/prefix",
want: "",
expectError: true,
},
{
name: "empty prefix",
urlPath: "/a/b/c/d",
prefix: "",
want: "a/b/c/d",
expectError: false,
},
{
name: "prefix at start of path",
urlPath: "/prefix/rest/of/path",
prefix: "/prefix",
want: "rest/of/path",
expectError: false,
},
{
name: "prefix in middle with special chars",
urlPath: "/a/b-c/prefix/d_e/f",
prefix: "/prefix",
want: "d_e/f",
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := pathAfterPrefix(tt.urlPath, tt.prefix)
if tt.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, tt.want, got)
}
})
}
}