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