github.com/johandry/terraform@v0.11.12-beta1/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.exe"),
    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  		"/example/mockos_mockarch/terraform-foo-windowsthing1_v1.0.0.exe",
    63  		"/example/mockos_mockarch/terraform-foo-windowsthing2_v1.0.0_x4.exe",
    64  		"/example/mockos_mockarch/terraform-foo-windowsthing3.exe",
    65  		"/example2/mockos_mockarch/terraform-foo-bar_v0.0.1",
    66  	})
    67  
    68  	want := []PluginMeta{
    69  		{
    70  			Name:    "bar",
    71  			Version: "0.0.1",
    72  			Path:    "/example/mockos_mockarch/terraform-foo-bar_v0.0.1",
    73  		},
    74  		{
    75  			Name:    "baz",
    76  			Version: "0.0.1",
    77  			Path:    "/example/mockos_mockarch/terraform-foo-baz_v0.0.1",
    78  		},
    79  		{
    80  			Name:    "baz",
    81  			Version: "1.0.0",
    82  			Path:    "/example/mockos_mockarch/terraform-foo-baz_v1.0.0",
    83  		},
    84  		{
    85  			Name:    "baz",
    86  			Version: "2.0.0",
    87  			Path:    "/example/mockos_mockarch/terraform-foo-baz_v2.0.0_x4",
    88  		},
    89  		{
    90  			Name:    "upper",
    91  			Version: "2.0.0",
    92  			Path:    "/example/mockos_mockarch/terraform-foo-upper_V2.0.0_X4",
    93  		},
    94  		{
    95  			Name:    "bar",
    96  			Version: "0.0.0",
    97  			Path:    "/example/terraform-foo-bar",
    98  		},
    99  		{
   100  			Name:    "bar",
   101  			Version: "bananas",
   102  			Path:    "/example/mockos_mockarch/terraform-foo-bar_vbananas",
   103  		},
   104  		{
   105  			Name:    "bar",
   106  			Version: "",
   107  			Path:    "/example/mockos_mockarch/terraform-foo-bar_v",
   108  		},
   109  		{
   110  			Name:    "windowsthing1",
   111  			Version: "1.0.0",
   112  			Path:    "/example/mockos_mockarch/terraform-foo-windowsthing1_v1.0.0.exe",
   113  		},
   114  		{
   115  			Name:    "windowsthing2",
   116  			Version: "1.0.0",
   117  			Path:    "/example/mockos_mockarch/terraform-foo-windowsthing2_v1.0.0_x4.exe",
   118  		},
   119  		{
   120  			Name:    "windowsthing3",
   121  			Version: "0.0.0",
   122  			Path:    "/example/mockos_mockarch/terraform-foo-windowsthing3.exe",
   123  		},
   124  	}
   125  
   126  	for p := range got {
   127  		t.Logf("got %#v", p)
   128  	}
   129  
   130  	if got, want := got.Count(), len(want); got != want {
   131  		t.Errorf("got %d items; want %d", got, want)
   132  	}
   133  
   134  	for _, wantMeta := range want {
   135  		if !got.Has(wantMeta) {
   136  			t.Errorf("missing meta %#v", wantMeta)
   137  		}
   138  	}
   139  }