github.com/ticketmaster/terraform@v0.10.0-beta2.0.20170711045249-a12daf5aba4f/plugin/discovery/find_test.go (about) 1 package discovery 2 3 import ( 4 "os" 5 "path/filepath" 6 "reflect" 7 "testing" 8 ) 9 10 func TestFindPluginPaths(t *testing.T) { 11 got := findPluginPaths( 12 "foo", 13 []string{ 14 "test-fixtures/current-style-plugins/mockos_mockarch", 15 "test-fixtures/legacy-style-plugins", 16 "test-fixtures/non-existent", 17 "test-fixtures/not-a-dir", 18 }, 19 ) 20 want := []string{ 21 filepath.Join("test-fixtures", "current-style-plugins", "mockos_mockarch", "terraform-foo-bar_v0.0.1"), 22 filepath.Join("test-fixtures", "current-style-plugins", "mockos_mockarch", "terraform-foo-bar_v1.0.0"), 23 // un-versioned plugins are still picked up, even in current-style paths 24 filepath.Join("test-fixtures", "current-style-plugins", "mockos_mockarch", "terraform-foo-missing-version"), 25 filepath.Join("test-fixtures", "legacy-style-plugins", "terraform-foo-bar"), 26 filepath.Join("test-fixtures", "legacy-style-plugins", "terraform-foo-baz"), 27 } 28 29 // Turn the paths back into relative paths, since we don't care exactly 30 // where this code is present on the system for the sake of this test. 31 baseDir, err := os.Getwd() 32 if err != nil { 33 // Should never happen 34 panic(err) 35 } 36 for i, absPath := range got { 37 if !filepath.IsAbs(absPath) { 38 t.Errorf("got non-absolute path %s", absPath) 39 } 40 41 got[i], err = filepath.Rel(baseDir, absPath) 42 if err != nil { 43 t.Fatalf("Can't make %s relative to current directory %s", absPath, baseDir) 44 } 45 } 46 47 if !reflect.DeepEqual(got, want) { 48 t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, want) 49 } 50 } 51 52 func TestResolvePluginPaths(t *testing.T) { 53 got := ResolvePluginPaths([]string{ 54 "/example/mockos_mockarch/terraform-foo-bar_v0.0.1", 55 "/example/mockos_mockarch/terraform-foo-baz_v0.0.1", 56 "/example/mockos_mockarch/terraform-foo-baz_v1.0.0", 57 "/example/mockos_mockarch/terraform-foo-baz_v2.0.0_x4", 58 "/example/mockos_mockarch/terraform-foo-upper_V2.0.0_X4", 59 "/example/terraform-foo-bar", 60 "/example/mockos_mockarch/terraform-foo-bar_vbananas", 61 "/example/mockos_mockarch/terraform-foo-bar_v", 62 "/example2/mockos_mockarch/terraform-foo-bar_v0.0.1", 63 }) 64 65 want := []PluginMeta{ 66 { 67 Name: "bar", 68 Version: "0.0.1", 69 Path: "/example/mockos_mockarch/terraform-foo-bar_v0.0.1", 70 }, 71 { 72 Name: "baz", 73 Version: "0.0.1", 74 Path: "/example/mockos_mockarch/terraform-foo-baz_v0.0.1", 75 }, 76 { 77 Name: "baz", 78 Version: "1.0.0", 79 Path: "/example/mockos_mockarch/terraform-foo-baz_v1.0.0", 80 }, 81 { 82 Name: "baz", 83 Version: "2.0.0", 84 Path: "/example/mockos_mockarch/terraform-foo-baz_v2.0.0_x4", 85 }, 86 { 87 Name: "upper", 88 Version: "2.0.0", 89 Path: "/example/mockos_mockarch/terraform-foo-upper_V2.0.0_X4", 90 }, 91 { 92 Name: "bar", 93 Version: "0.0.0", 94 Path: "/example/terraform-foo-bar", 95 }, 96 { 97 Name: "bar", 98 Version: "bananas", 99 Path: "/example/mockos_mockarch/terraform-foo-bar_vbananas", 100 }, 101 { 102 Name: "bar", 103 Version: "", 104 Path: "/example/mockos_mockarch/terraform-foo-bar_v", 105 }, 106 } 107 108 for p := range got { 109 t.Logf("got %#v", p) 110 } 111 112 if got, want := got.Count(), len(want); got != want { 113 t.Errorf("got %d items; want %d", got, want) 114 } 115 116 for _, wantMeta := range want { 117 if !got.Has(wantMeta) { 118 t.Errorf("missing meta %#v", wantMeta) 119 } 120 } 121 }