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