2017-04-12 19:39:04 +02:00
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFindPluginPaths(t *testing.T) {
|
|
|
|
got := findPluginPaths(
|
|
|
|
"foo",
|
|
|
|
[]string{
|
2017-06-16 19:58:40 +02:00
|
|
|
"test-fixtures/current-style-plugins/mockos_mockarch",
|
2017-04-12 19:39:04 +02:00
|
|
|
"test-fixtures/legacy-style-plugins",
|
|
|
|
"test-fixtures/non-existent",
|
|
|
|
"test-fixtures/not-a-dir",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
want := []string{
|
2017-06-09 22:05:15 +02:00
|
|
|
filepath.Join("test-fixtures", "current-style-plugins", "mockos_mockarch", "terraform-foo-bar_v0.0.1"),
|
|
|
|
filepath.Join("test-fixtures", "current-style-plugins", "mockos_mockarch", "terraform-foo-bar_v1.0.0"),
|
2017-06-16 21:28:48 +02:00
|
|
|
// un-versioned plugins are still picked up, even in current-style paths
|
|
|
|
filepath.Join("test-fixtures", "current-style-plugins", "mockos_mockarch", "terraform-foo-missing-version"),
|
2017-04-12 19:39:04 +02:00
|
|
|
filepath.Join("test-fixtures", "legacy-style-plugins", "terraform-foo-bar"),
|
|
|
|
filepath.Join("test-fixtures", "legacy-style-plugins", "terraform-foo-baz"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn the paths back into relative paths, since we don't care exactly
|
|
|
|
// where this code is present on the system for the sake of this test.
|
|
|
|
baseDir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
// Should never happen
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for i, absPath := range got {
|
|
|
|
if !filepath.IsAbs(absPath) {
|
|
|
|
t.Errorf("got non-absolute path %s", absPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
got[i], err = filepath.Rel(baseDir, absPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't make %s relative to current directory %s", absPath, baseDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResolvePluginPaths(t *testing.T) {
|
|
|
|
got := ResolvePluginPaths([]string{
|
2017-06-09 22:05:15 +02:00
|
|
|
"/example/mockos_mockarch/terraform-foo-bar_v0.0.1",
|
|
|
|
"/example/mockos_mockarch/terraform-foo-baz_v0.0.1",
|
|
|
|
"/example/mockos_mockarch/terraform-foo-baz_v1.0.0",
|
|
|
|
"/example/mockos_mockarch/terraform-foo-baz_v2.0.0_x4",
|
|
|
|
"/example/mockos_mockarch/terraform-foo-upper_V2.0.0_X4",
|
2017-04-12 19:39:04 +02:00
|
|
|
"/example/terraform-foo-bar",
|
2017-06-09 22:05:15 +02:00
|
|
|
"/example/mockos_mockarch/terraform-foo-bar_vbananas",
|
|
|
|
"/example/mockos_mockarch/terraform-foo-bar_v",
|
|
|
|
"/example2/mockos_mockarch/terraform-foo-bar_v0.0.1",
|
2017-04-12 19:39:04 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
want := []PluginMeta{
|
|
|
|
{
|
|
|
|
Name: "bar",
|
|
|
|
Version: "0.0.1",
|
2017-06-09 22:05:15 +02:00
|
|
|
Path: "/example/mockos_mockarch/terraform-foo-bar_v0.0.1",
|
2017-04-12 19:39:04 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "baz",
|
|
|
|
Version: "0.0.1",
|
2017-06-09 22:05:15 +02:00
|
|
|
Path: "/example/mockos_mockarch/terraform-foo-baz_v0.0.1",
|
2017-04-12 19:39:04 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "baz",
|
|
|
|
Version: "1.0.0",
|
2017-06-09 22:05:15 +02:00
|
|
|
Path: "/example/mockos_mockarch/terraform-foo-baz_v1.0.0",
|
2017-04-12 19:39:04 +02:00
|
|
|
},
|
2017-05-25 02:29:10 +02:00
|
|
|
{
|
|
|
|
Name: "baz",
|
|
|
|
Version: "2.0.0",
|
2017-06-09 22:05:15 +02:00
|
|
|
Path: "/example/mockos_mockarch/terraform-foo-baz_v2.0.0_x4",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "upper",
|
|
|
|
Version: "2.0.0",
|
|
|
|
Path: "/example/mockos_mockarch/terraform-foo-upper_V2.0.0_X4",
|
2017-05-25 02:29:10 +02:00
|
|
|
},
|
2017-04-12 19:39:04 +02:00
|
|
|
{
|
|
|
|
Name: "bar",
|
|
|
|
Version: "0.0.0",
|
|
|
|
Path: "/example/terraform-foo-bar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "bar",
|
|
|
|
Version: "bananas",
|
2017-06-09 22:05:15 +02:00
|
|
|
Path: "/example/mockos_mockarch/terraform-foo-bar_vbananas",
|
2017-04-12 19:39:04 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "bar",
|
|
|
|
Version: "",
|
2017-06-09 22:05:15 +02:00
|
|
|
Path: "/example/mockos_mockarch/terraform-foo-bar_v",
|
2017-04-12 19:39:04 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-06-09 22:05:15 +02:00
|
|
|
for p := range got {
|
|
|
|
t.Logf("got %#v", p)
|
|
|
|
}
|
2017-05-25 02:29:10 +02:00
|
|
|
|
2017-04-12 19:39:04 +02:00
|
|
|
if got, want := got.Count(), len(want); got != want {
|
|
|
|
t.Errorf("got %d items; want %d", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, wantMeta := range want {
|
|
|
|
if !got.Has(wantMeta) {
|
|
|
|
t.Errorf("missing meta %#v", wantMeta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|