gopkg.in/tools/godep.v70@v70.0.0-20160520184947-56b9657fec2f/match_test.go (about) 1 package main 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 ) 9 10 func TestMatchPattern(t *testing.T) { 11 // Test cases from $GOROOT/src/cmd/go/match_test.go. 12 cases := []struct { 13 pat string 14 path string 15 want bool 16 }{ 17 {"...", "foo", true}, 18 {"net", "net", true}, 19 {"net", "net/http", false}, 20 {"net/http", "net", false}, 21 {"net/http", "net/http", true}, 22 {"net...", "netchan", true}, 23 {"net...", "net", true}, 24 {"net...", "net/http", true}, 25 {"net...", "not/http", false}, 26 {"net/...", "netchan", false}, 27 {"net/...", "net", true}, 28 {"net/...", "net/http", true}, 29 {"net/...", "not/http", false}, 30 } 31 for _, test := range cases { 32 ok := matchPattern(test.pat)(test.path) 33 if ok != test.want { 34 t.Errorf("matchPackages(%q)(%q) = %v want %v", test.pat, test.path, ok, test.want) 35 } 36 } 37 } 38 39 func TestSubPath(t *testing.T) { 40 cases := []struct { 41 sub string 42 dir string 43 want bool 44 }{ 45 //Basic 46 {`/Users/emuller/go/src/github.com/tools/godep`, `/Users/emuller/go`, true}, 47 //Case insensitive filesystem used in dir 48 {`/Users/emuller/go/src/github.com/tools/godep`, `/Users/Emuller/go`, true}, 49 {`/Users/emuller/go/Src/github.com/tools/godep`, `/Users/Emuller/go`, true}, 50 //spaces 51 {`/Users/e muller/go/Src/github.com/tools/godep`, `/Users/E muller/go`, true}, 52 // () 53 {`/Users/e muller/(Personal)/go/Src/github.com/tools/godep`, `/Users/E muller/(Personal)/go`, true}, 54 //Not even close, but same length 55 {`/foo`, `/bar`, false}, 56 // Same, so not sub path (same path) 57 {`/foo`, `/foo`, false}, 58 // Windows with different cases 59 {`c:\foo\bar`, `C:\foo`, true}, 60 } 61 62 for _, test := range cases { 63 ok := subPath(test.sub, test.dir) 64 if ok != test.want { 65 t.Errorf("subdir(%s,%s) = %v want %v", test.sub, test.dir, ok, test.want) 66 } 67 } 68 } 69 70 func TestIsSameOrNewer(t *testing.T) { 71 cases := []struct { 72 base string 73 check string 74 want bool 75 }{ 76 {`go1.6`, `go1.6`, true}, 77 {`go1.5`, `go1.6`, true}, 78 {`go1.7`, `go1.6`, false}, 79 {`go1.6`, `devel-8f48efb`, true}, // devel versions are always never 80 } 81 82 for _, test := range cases { 83 ok := isSameOrNewer(test.base, test.check) 84 if ok != test.want { 85 t.Errorf("isSameOrNewer(%s,%s) = %v want %v", test.base, test.check, ok, test.want) 86 } 87 } 88 } 89 90 func TestDetermineVersion(t *testing.T) { 91 cases := []struct { 92 v string 93 go15ve string 94 vendorDir []string 95 want bool 96 }{ 97 {"go1.5", "", nil, false}, 98 {"go1.5", "1", nil, true}, 99 {"go1.5", "1", []string{"Godeps", "_workspace"}, false}, 100 {"go1.5", "0", nil, false}, 101 {"go1.6", "", nil, true}, 102 {"go1.6", "1", nil, true}, 103 {"go1.6", "1", []string{"Godeps", "_workspace"}, false}, 104 {"go1.6", "0", nil, false}, 105 {"devel", "", nil, true}, 106 {"devel-12345", "", nil, true}, 107 {"devel", "1", nil, true}, 108 {"devel-12345", "1", nil, true}, 109 {"devel", "1", []string{"Godeps", "_workspace"}, false}, 110 {"devel-12345", "1", []string{"Godeps", "_workspace"}, false}, 111 {"devel", "0", nil, false}, 112 {"devel-12345", "0", nil, false}, 113 } 114 115 wd, err := os.Getwd() 116 if err != nil { 117 t.Fatal(err) 118 } 119 defer func() { 120 os.Chdir(wd) 121 }() 122 123 ove := os.Getenv("GO15VENDOREXPERIMENT") 124 defer func() { 125 os.Setenv("GO15VENDOREXPERIMENT", ove) 126 }() 127 128 for i, test := range cases { 129 os.Setenv("GO15VENDOREXPERIMENT", test.go15ve) 130 tdir, err := ioutil.TempDir("", "godeptest") 131 if err != nil { 132 t.Fatal(err) 133 } 134 defer os.RemoveAll(tdir) 135 os.Chdir(tdir) 136 137 if len(test.vendorDir) > 0 { 138 md := tdir 139 for _, vd := range test.vendorDir { 140 md = filepath.Join(md, vd) 141 if err := os.Mkdir(md, os.ModePerm); err != nil { 142 t.Fatal(err) 143 } 144 } 145 } 146 147 if e := determineVendor(test.v); e != test.want { 148 t.Errorf("%d GO15VENDOREXPERIMENT=%s determineVendor(%s) == %t, but wanted %t\n", i, test.go15ve, test.v, e, test.want) 149 } 150 } 151 }