|
|
|
@ -11,6 +11,7 @@ import ( |
|
|
|
|
"github.com/grafana/grafana/pkg/plugins" |
|
|
|
|
"github.com/grafana/grafana/pkg/plugins/config" |
|
|
|
|
"github.com/grafana/grafana/pkg/plugins/manager/fakes" |
|
|
|
|
"github.com/grafana/grafana/pkg/plugins/pluginassets" |
|
|
|
|
"github.com/grafana/grafana/pkg/plugins/pluginscdn" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
@ -44,7 +45,7 @@ func TestService(t *testing.T) { |
|
|
|
|
"two": {}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
svc := ProvideService(cfg, pluginscdn.ProvideService(cfg)) |
|
|
|
|
svc := ProvideService(cfg, pluginscdn.ProvideService(cfg), fakes.NewFakeAssetProvider()) |
|
|
|
|
|
|
|
|
|
tableOldFS := fakes.NewFakePluginFS("/grafana/public/app/plugins/panel/table-old") |
|
|
|
|
jsonData := map[string]plugins.JSONData{ |
|
|
|
@ -61,22 +62,22 @@ func TestService(t *testing.T) { |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("Base", func(t *testing.T) { |
|
|
|
|
base, err := svc.Base(NewPluginInfo(jsonData["one"], plugins.ClassExternal, pluginFS("one"), nil)) |
|
|
|
|
base, err := svc.Base(pluginassets.NewPluginInfo(jsonData["one"], plugins.ClassExternal, pluginFS("one"), nil)) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
oneCDNURL, err := url.JoinPath(tc.cdnBaseURL, "/one/1.0.0/public/plugins/one") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, oneCDNURL, base) |
|
|
|
|
|
|
|
|
|
base, err = svc.Base(NewPluginInfo(jsonData["one"], plugins.ClassCDN, pluginFS(oneCDNURL), nil)) |
|
|
|
|
base, err = svc.Base(pluginassets.NewPluginInfo(jsonData["one"], plugins.ClassCDN, pluginFS(oneCDNURL), nil)) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, oneCDNURL, base) |
|
|
|
|
|
|
|
|
|
base, err = svc.Base(NewPluginInfo(jsonData["two"], plugins.ClassExternal, pluginFS("two"), nil)) |
|
|
|
|
base, err = svc.Base(pluginassets.NewPluginInfo(jsonData["two"], plugins.ClassExternal, pluginFS("two"), nil)) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, "public/plugins/two", base) |
|
|
|
|
|
|
|
|
|
base, err = svc.Base(NewPluginInfo(jsonData["table-old"], plugins.ClassCore, tableOldFS, nil)) |
|
|
|
|
base, err = svc.Base(pluginassets.NewPluginInfo(jsonData["table-old"], plugins.ClassCore, tableOldFS, nil)) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, "public/plugins/table-old", base) |
|
|
|
|
|
|
|
|
@ -84,8 +85,8 @@ func TestService(t *testing.T) { |
|
|
|
|
parentFS.RelFunc = func(_ string) (string, error) { |
|
|
|
|
return "child-plugins/two", nil |
|
|
|
|
} |
|
|
|
|
parent := NewPluginInfo(jsonData["one"], plugins.ClassExternal, parentFS, nil) |
|
|
|
|
child := NewPluginInfo(jsonData["two"], plugins.ClassExternal, fakes.NewFakePluginFS(""), &parent) |
|
|
|
|
parent := pluginassets.NewPluginInfo(jsonData["one"], plugins.ClassExternal, parentFS, nil) |
|
|
|
|
child := pluginassets.NewPluginInfo(jsonData["two"], plugins.ClassExternal, fakes.NewFakePluginFS(""), &parent) |
|
|
|
|
base, err = svc.Base(child) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
@ -95,7 +96,7 @@ func TestService(t *testing.T) { |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("Module", func(t *testing.T) { |
|
|
|
|
module, err := svc.Module(NewPluginInfo(jsonData["one"], plugins.ClassExternal, pluginFS("one"), nil)) |
|
|
|
|
module, err := svc.Module(pluginassets.NewPluginInfo(jsonData["one"], plugins.ClassExternal, pluginFS("one"), nil)) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
oneCDNURL, err := url.JoinPath(tc.cdnBaseURL, "/one/1.0.0/public/plugins/one") |
|
|
|
@ -106,15 +107,15 @@ func TestService(t *testing.T) { |
|
|
|
|
require.Equal(t, oneCDNModuleURL, module) |
|
|
|
|
|
|
|
|
|
fs := pluginFS("one") |
|
|
|
|
module, err = svc.Module(NewPluginInfo(jsonData["one"], plugins.ClassCDN, fs, nil)) |
|
|
|
|
module, err = svc.Module(pluginassets.NewPluginInfo(jsonData["one"], plugins.ClassCDN, fs, nil)) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, path.Join(fs.Base(), "module.js"), module) |
|
|
|
|
|
|
|
|
|
module, err = svc.Module(NewPluginInfo(jsonData["two"], plugins.ClassExternal, pluginFS("two"), nil)) |
|
|
|
|
module, err = svc.Module(pluginassets.NewPluginInfo(jsonData["two"], plugins.ClassExternal, pluginFS("two"), nil)) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, "public/plugins/two/module.js", module) |
|
|
|
|
|
|
|
|
|
module, err = svc.Module(NewPluginInfo(jsonData["table-old"], plugins.ClassCore, tableOldFS, nil)) |
|
|
|
|
module, err = svc.Module(pluginassets.NewPluginInfo(jsonData["table-old"], plugins.ClassCore, tableOldFS, nil)) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, "core:plugin/table-old", module) |
|
|
|
|
|
|
|
|
@ -122,8 +123,8 @@ func TestService(t *testing.T) { |
|
|
|
|
parentFS.RelFunc = func(_ string) (string, error) { |
|
|
|
|
return "child-plugins/two", nil |
|
|
|
|
} |
|
|
|
|
parent := NewPluginInfo(jsonData["one"], plugins.ClassExternal, parentFS, nil) |
|
|
|
|
child := NewPluginInfo(jsonData["two"], plugins.ClassExternal, fakes.NewFakePluginFS(""), &parent) |
|
|
|
|
parent := pluginassets.NewPluginInfo(jsonData["one"], plugins.ClassExternal, parentFS, nil) |
|
|
|
|
child := pluginassets.NewPluginInfo(jsonData["two"], plugins.ClassExternal, fakes.NewFakePluginFS(""), &parent) |
|
|
|
|
module, err = svc.Module(child) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
@ -142,29 +143,29 @@ func TestService(t *testing.T) { |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
u, err := svc.RelativeURL(NewPluginInfo(pluginsMap["one"].JSONData, plugins.ClassExternal, pluginFS("one"), nil), "") |
|
|
|
|
u, err := svc.RelativeURL(pluginassets.NewPluginInfo(pluginsMap["one"].JSONData, plugins.ClassExternal, pluginFS("one"), nil), "") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
// given an empty path, base URL will be returned
|
|
|
|
|
baseURL, err := svc.Base(NewPluginInfo(pluginsMap["one"].JSONData, plugins.ClassExternal, pluginFS("one"), nil)) |
|
|
|
|
baseURL, err := svc.Base(pluginassets.NewPluginInfo(pluginsMap["one"].JSONData, plugins.ClassExternal, pluginFS("one"), nil)) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, baseURL, u) |
|
|
|
|
|
|
|
|
|
u, err = svc.RelativeURL(NewPluginInfo(pluginsMap["one"].JSONData, plugins.ClassExternal, pluginFS("one"), nil), "path/to/file.txt") |
|
|
|
|
u, err = svc.RelativeURL(pluginassets.NewPluginInfo(pluginsMap["one"].JSONData, plugins.ClassExternal, pluginFS("one"), nil), "path/to/file.txt") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, strings.TrimRight(tc.cdnBaseURL, "/")+"/one/1.0.0/public/plugins/one/path/to/file.txt", u) |
|
|
|
|
|
|
|
|
|
u, err = svc.RelativeURL(NewPluginInfo(pluginsMap["two"].JSONData, plugins.ClassExternal, pluginFS("two"), nil), "path/to/file.txt") |
|
|
|
|
u, err = svc.RelativeURL(pluginassets.NewPluginInfo(pluginsMap["two"].JSONData, plugins.ClassExternal, pluginFS("two"), nil), "path/to/file.txt") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, "public/plugins/two/path/to/file.txt", u) |
|
|
|
|
|
|
|
|
|
u, err = svc.RelativeURL(NewPluginInfo(pluginsMap["two"].JSONData, plugins.ClassExternal, pluginFS("two"), nil), "default") |
|
|
|
|
u, err = svc.RelativeURL(pluginassets.NewPluginInfo(pluginsMap["two"].JSONData, plugins.ClassExternal, pluginFS("two"), nil), "default") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, "public/plugins/two/default", u) |
|
|
|
|
|
|
|
|
|
oneCDNURL, err := url.JoinPath(tc.cdnBaseURL, "/one/1.0.0/public/plugins/one") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
u, err = svc.RelativeURL(NewPluginInfo(pluginsMap["one"].JSONData, plugins.ClassCDN, pluginFS(oneCDNURL), nil), "path/to/file.txt") |
|
|
|
|
u, err = svc.RelativeURL(pluginassets.NewPluginInfo(pluginsMap["one"].JSONData, plugins.ClassCDN, pluginFS(oneCDNURL), nil), "path/to/file.txt") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
oneCDNRelativeURL, err := url.JoinPath(oneCDNURL, "path/to/file.txt") |
|
|
|
@ -175,8 +176,8 @@ func TestService(t *testing.T) { |
|
|
|
|
parentFS.RelFunc = func(_ string) (string, error) { |
|
|
|
|
return "child-plugins/two", nil |
|
|
|
|
} |
|
|
|
|
parent := NewPluginInfo(jsonData["one"], plugins.ClassExternal, parentFS, nil) |
|
|
|
|
child := NewPluginInfo(jsonData["two"], plugins.ClassExternal, fakes.NewFakePluginFS(""), &parent) |
|
|
|
|
parent := pluginassets.NewPluginInfo(jsonData["one"], plugins.ClassExternal, parentFS, nil) |
|
|
|
|
child := pluginassets.NewPluginInfo(jsonData["two"], plugins.ClassExternal, fakes.NewFakePluginFS(""), &parent) |
|
|
|
|
u, err = svc.RelativeURL(child, "path/to/file.txt") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
@ -192,8 +193,8 @@ func TestService(t *testing.T) { |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("GetTranslations", func(t *testing.T) { |
|
|
|
|
pluginInfo := NewPluginInfo(jsonData["one"], plugins.ClassExternal, pluginFS("one"), nil) |
|
|
|
|
pluginInfo.pluginJSON.Languages = []string{"en-US", "pt-BR"} |
|
|
|
|
pluginInfo := pluginassets.NewPluginInfo(jsonData["one"], plugins.ClassExternal, pluginFS("one"), nil) |
|
|
|
|
pluginInfo.JsonData.Languages = []string{"en-US", "pt-BR"} |
|
|
|
|
translations, err := svc.GetTranslations(pluginInfo) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
oneCDNURL, err := url.JoinPath(tc.cdnBaseURL, "one", "1.0.0", "public", "plugins", "one") |
|
|
|
@ -219,14 +220,14 @@ func TestService_ChildPlugins(t *testing.T) { |
|
|
|
|
tcs := []struct { |
|
|
|
|
name string |
|
|
|
|
cfg *config.PluginManagementCfg |
|
|
|
|
pluginInfo func() PluginInfo |
|
|
|
|
pluginInfo func() pluginassets.PluginInfo |
|
|
|
|
expected expected |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
name: "Local FS external plugin", |
|
|
|
|
cfg: &config.PluginManagementCfg{}, |
|
|
|
|
pluginInfo: func() PluginInfo { |
|
|
|
|
return NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent"), nil) |
|
|
|
|
pluginInfo: func() pluginassets.PluginInfo { |
|
|
|
|
return pluginassets.NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent"), nil) |
|
|
|
|
}, |
|
|
|
|
expected: expected{ |
|
|
|
|
module: "public/plugins/parent/module.js", |
|
|
|
@ -237,9 +238,9 @@ func TestService_ChildPlugins(t *testing.T) { |
|
|
|
|
{ |
|
|
|
|
name: "Local FS external plugin with child", |
|
|
|
|
cfg: &config.PluginManagementCfg{}, |
|
|
|
|
pluginInfo: func() PluginInfo { |
|
|
|
|
parentInfo := NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent"), nil) |
|
|
|
|
childInfo := NewPluginInfo(plugins.JSONData{ID: "child", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent/child"), &parentInfo) |
|
|
|
|
pluginInfo: func() pluginassets.PluginInfo { |
|
|
|
|
parentInfo := pluginassets.NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent"), nil) |
|
|
|
|
childInfo := pluginassets.NewPluginInfo(plugins.JSONData{ID: "child", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent/child"), &parentInfo) |
|
|
|
|
return childInfo |
|
|
|
|
}, |
|
|
|
|
expected: expected{ |
|
|
|
@ -251,8 +252,8 @@ func TestService_ChildPlugins(t *testing.T) { |
|
|
|
|
{ |
|
|
|
|
name: "Local FS core plugin", |
|
|
|
|
cfg: &config.PluginManagementCfg{}, |
|
|
|
|
pluginInfo: func() PluginInfo { |
|
|
|
|
return NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCore, plugins.NewLocalFS("/plugins/parent"), nil) |
|
|
|
|
pluginInfo: func() pluginassets.PluginInfo { |
|
|
|
|
return pluginassets.NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCore, plugins.NewLocalFS("/plugins/parent"), nil) |
|
|
|
|
}, |
|
|
|
|
expected: expected{ |
|
|
|
|
module: "core:plugin/parent", |
|
|
|
@ -263,8 +264,8 @@ func TestService_ChildPlugins(t *testing.T) { |
|
|
|
|
{ |
|
|
|
|
name: "Externally-built Local FS core plugin", |
|
|
|
|
cfg: &config.PluginManagementCfg{}, |
|
|
|
|
pluginInfo: func() PluginInfo { |
|
|
|
|
return NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCore, plugins.NewLocalFS("/plugins/parent/dist"), nil) |
|
|
|
|
pluginInfo: func() pluginassets.PluginInfo { |
|
|
|
|
return pluginassets.NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCore, plugins.NewLocalFS("/plugins/parent/dist"), nil) |
|
|
|
|
}, |
|
|
|
|
expected: expected{ |
|
|
|
|
module: "public/plugins/parent/module.js", |
|
|
|
@ -277,8 +278,8 @@ func TestService_ChildPlugins(t *testing.T) { |
|
|
|
|
cfg: &config.PluginManagementCfg{ |
|
|
|
|
PluginsCDNURLTemplate: "https://cdn.example.com", |
|
|
|
|
}, |
|
|
|
|
pluginInfo: func() PluginInfo { |
|
|
|
|
return NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCDN, pluginFS("https://cdn.example.com/plugins/parent"), nil) |
|
|
|
|
pluginInfo: func() pluginassets.PluginInfo { |
|
|
|
|
return pluginassets.NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCDN, pluginFS("https://cdn.example.com/plugins/parent"), nil) |
|
|
|
|
}, |
|
|
|
|
expected: expected{ |
|
|
|
|
module: "https://cdn.example.com/plugins/parent/module.js", |
|
|
|
@ -291,10 +292,10 @@ func TestService_ChildPlugins(t *testing.T) { |
|
|
|
|
cfg: &config.PluginManagementCfg{ |
|
|
|
|
PluginsCDNURLTemplate: "https://cdn.example.com", |
|
|
|
|
}, |
|
|
|
|
pluginInfo: func() PluginInfo { |
|
|
|
|
pluginInfo: func() pluginassets.PluginInfo { |
|
|
|
|
// Note: fake plugin FS is the most convenient way to mock the plugin FS for CDN plugins
|
|
|
|
|
parentInfo := NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCDN, pluginFS("https://cdn.example.com/parent"), nil) |
|
|
|
|
childInfo := NewPluginInfo(plugins.JSONData{ID: "child", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCDN, pluginFS("https://cdn.example.com/parent/some/other/dir/child"), &parentInfo) |
|
|
|
|
parentInfo := pluginassets.NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCDN, pluginFS("https://cdn.example.com/parent"), nil) |
|
|
|
|
childInfo := pluginassets.NewPluginInfo(plugins.JSONData{ID: "child", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCDN, pluginFS("https://cdn.example.com/parent/some/other/dir/child"), &parentInfo) |
|
|
|
|
return childInfo |
|
|
|
|
}, |
|
|
|
|
expected: expected{ |
|
|
|
@ -311,8 +312,8 @@ func TestService_ChildPlugins(t *testing.T) { |
|
|
|
|
"parent": {"cdn": "true"}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
pluginInfo: func() PluginInfo { |
|
|
|
|
return NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent"), nil) |
|
|
|
|
pluginInfo: func() pluginassets.PluginInfo { |
|
|
|
|
return pluginassets.NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent"), nil) |
|
|
|
|
}, |
|
|
|
|
expected: expected{ |
|
|
|
|
module: "https://cdn.example.com/parent/1.0.0/public/plugins/parent/module.js", |
|
|
|
@ -328,9 +329,9 @@ func TestService_ChildPlugins(t *testing.T) { |
|
|
|
|
"parent": {"cdn": "true"}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
pluginInfo: func() PluginInfo { |
|
|
|
|
parentInfo := NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent"), nil) |
|
|
|
|
childInfo := NewPluginInfo(plugins.JSONData{ID: "child", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent/child"), &parentInfo) |
|
|
|
|
pluginInfo: func() pluginassets.PluginInfo { |
|
|
|
|
parentInfo := pluginassets.NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent"), nil) |
|
|
|
|
childInfo := pluginassets.NewPluginInfo(plugins.JSONData{ID: "child", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent/child"), &parentInfo) |
|
|
|
|
return childInfo |
|
|
|
|
}, |
|
|
|
|
expected: expected{ |
|
|
|
@ -342,7 +343,7 @@ func TestService_ChildPlugins(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
for _, tc := range tcs { |
|
|
|
|
t.Run(tc.name, func(t *testing.T) { |
|
|
|
|
svc := ProvideService(tc.cfg, pluginscdn.ProvideService(tc.cfg)) |
|
|
|
|
svc := ProvideService(tc.cfg, pluginscdn.ProvideService(tc.cfg), fakes.NewFakeAssetProvider()) |
|
|
|
|
|
|
|
|
|
module, err := svc.Module(tc.pluginInfo()) |
|
|
|
|
require.NoError(t, err) |
|
|
|
@ -358,3 +359,72 @@ func TestService_ChildPlugins(t *testing.T) { |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestService_AssetProviderPrecedence(t *testing.T) { |
|
|
|
|
cfg := &config.PluginManagementCfg{ |
|
|
|
|
PluginsCDNURLTemplate: "https://cdn.example.com", |
|
|
|
|
PluginSettings: map[string]map[string]string{ |
|
|
|
|
"test-plugin": {"cdn": "true"}, |
|
|
|
|
}, |
|
|
|
|
Features: config.Features{PluginAssetProvider: true}, |
|
|
|
|
} |
|
|
|
|
cdn := pluginscdn.ProvideService(cfg) |
|
|
|
|
|
|
|
|
|
pluginInfo := pluginassets.NewPluginInfo( |
|
|
|
|
plugins.JSONData{ID: "test-plugin", Info: plugins.Info{Version: "1.0.0"}}, |
|
|
|
|
plugins.ClassExternal, |
|
|
|
|
pluginFS("/plugins/test-plugin"), |
|
|
|
|
nil, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
t.Run("Asset provider enabled takes precedence when feature enabled", func(t *testing.T) { |
|
|
|
|
assetProvider := fakes.NewFakeAssetProvider() |
|
|
|
|
assetProvider.AssetPathFunc = func(n pluginassets.PluginInfo, pathElems ...string) (string, error) { |
|
|
|
|
return "from-asset-provider", nil |
|
|
|
|
} |
|
|
|
|
assetProvider.ModuleFunc = func(n pluginassets.PluginInfo) (string, error) { |
|
|
|
|
return "module-from-asset-provider", nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
svc := ProvideService(cfg, cdn, assetProvider) |
|
|
|
|
|
|
|
|
|
base, err := svc.Base(pluginInfo) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, "from-asset-provider", base) |
|
|
|
|
|
|
|
|
|
module, err := svc.Module(pluginInfo) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, "module-from-asset-provider", module) |
|
|
|
|
|
|
|
|
|
relURL, err := svc.RelativeURL(pluginInfo, "path/to/file.txt") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, "from-asset-provider", relURL) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("GetTranslations uses asset provider when feature enabled", func(t *testing.T) { |
|
|
|
|
assetProvider := fakes.NewFakeAssetProvider() |
|
|
|
|
assetProvider.AssetPathFunc = func(n pluginassets.PluginInfo, pathElems ...string) (string, error) { |
|
|
|
|
return path.Join("translation-path", path.Join(pathElems...)), nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pluginWithLangs := pluginassets.NewPluginInfo( |
|
|
|
|
plugins.JSONData{ |
|
|
|
|
ID: "multilang-plugin", |
|
|
|
|
Info: plugins.Info{Version: "1.0.0"}, |
|
|
|
|
Languages: []string{"en-US", "es-ES", "fr-FR"}, |
|
|
|
|
}, |
|
|
|
|
plugins.ClassExternal, |
|
|
|
|
pluginFS("/plugins/multilang-plugin"), |
|
|
|
|
nil, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
svc := ProvideService(cfg, cdn, assetProvider) |
|
|
|
|
translations, err := svc.GetTranslations(pluginWithLangs) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Len(t, translations, 3) |
|
|
|
|
// All translation paths should come from the asset provider
|
|
|
|
|
require.Equal(t, "translation-path/locales/en-US/multilang-plugin.json", translations["en-US"]) |
|
|
|
|
require.Equal(t, "translation-path/locales/es-ES/multilang-plugin.json", translations["es-ES"]) |
|
|
|
|
require.Equal(t, "translation-path/locales/fr-FR/multilang-plugin.json", translations["fr-FR"]) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|