gopkg.in/tools/godep.v39@v39.0.0-20151216234527-2721f686f09e/rewrite_test.go (about) 1 package main 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 ) 8 9 func TestUnqualify(t *testing.T) { 10 var cases = []struct { 11 path string 12 want string 13 }{ 14 {"C", "C"}, 15 {"D/Godeps/_workspace/src/T", "T"}, 16 {"C/Godeps/_workspace/src/D/Godeps/_workspace/src/T", "T"}, 17 } 18 for _, test := range cases { 19 g := unqualify(test.path) 20 if g != test.want { 21 t.Errorf("qualify(%s) = %s want %s", test.path, g, test.want) 22 } 23 } 24 } 25 26 func TestQualify(t *testing.T) { 27 var cases = []struct { 28 path string 29 want string 30 }{ 31 {"C", "C"}, 32 {"C/P", "C/P"}, 33 {"fmt", "fmt"}, 34 {"DP", "DP"}, 35 {"D", "C/Godeps/_workspace/src/D"}, 36 {"D/P", "C/Godeps/_workspace/src/D/P"}, 37 } 38 for _, test := range cases { 39 g := qualify(test.path, "C", []string{"D"}) 40 if g != test.want { 41 t.Errorf("qualify({C}, %s) = %s want %s", test.path, g, test.want) 42 } 43 } 44 } 45 46 const ( 47 whitespace = `package main 48 49 import "D" 50 51 var ( 52 x int 53 abc int 54 ) 55 ` 56 whitespaceRewritten = `package main 57 58 import "C/Godeps/_workspace/src/D" 59 60 var ( 61 x int 62 abc int 63 ) 64 ` 65 sortOrder = `package main 66 67 import ( 68 "E" 69 "C/Godeps/_workspace/src/D" 70 ) 71 ` 72 sortOrderRewritten = `package main 73 74 import ( 75 "C/Godeps/_workspace/src/D" 76 "C/Godeps/_workspace/src/E" 77 ) 78 ` 79 sortOrderPreserveComment = `package main 80 81 import ( 82 "C/Godeps/_workspace/src/E" // src E 83 "D" // src D 84 ) 85 ` 86 sortOrderPreserveCommentRewritten = `package main 87 88 import ( 89 "C/Godeps/_workspace/src/D" // src D 90 "C/Godeps/_workspace/src/E" // src E 91 ) 92 ` 93 ) 94 95 func TestRewrite(t *testing.T) { 96 var cases = []struct { 97 cwd string 98 paths []string 99 start []*node 100 want []*node 101 werr bool 102 }{ 103 { // simple case, one dependency 104 cwd: "C", 105 paths: []string{"D"}, 106 start: []*node{ 107 {"C/main.go", pkg("main", "D"), nil}, 108 {"C/Godeps/_workspace/src/D/main.go", pkg("D"), nil}, 109 }, 110 want: []*node{ 111 {"C/main.go", pkg("main", "C/Godeps/_workspace/src/D"), nil}, 112 {"C/Godeps/_workspace/src/D/main.go", pkg("D"), nil}, 113 }, 114 }, 115 { // transitive dep 116 cwd: "C", 117 paths: []string{"D", "T"}, 118 start: []*node{ 119 {"C/main.go", pkg("main", "D"), nil}, 120 {"C/Godeps/_workspace/src/D/main.go", pkg("D", "T"), nil}, 121 {"C/Godeps/_workspace/src/T/main.go", pkg("T"), nil}, 122 }, 123 want: []*node{ 124 {"C/main.go", pkg("main", "C/Godeps/_workspace/src/D"), nil}, 125 {"C/Godeps/_workspace/src/D/main.go", pkg("D", "C/Godeps/_workspace/src/T"), nil}, 126 {"C/Godeps/_workspace/src/T/main.go", pkg("T"), nil}, 127 }, 128 }, 129 { // intermediate dep that uses godep save -r 130 cwd: "C", 131 paths: []string{"D", "T"}, 132 start: []*node{ 133 {"C/main.go", pkg("main", "D"), nil}, 134 {"C/Godeps/_workspace/src/D/main.go", pkg("D", "D/Godeps/_workspace/src/T"), nil}, 135 {"C/Godeps/_workspace/src/T/main.go", pkg("T"), nil}, 136 }, 137 want: []*node{ 138 {"C/main.go", pkg("main", "C/Godeps/_workspace/src/D"), nil}, 139 {"C/Godeps/_workspace/src/D/main.go", pkg("D", "C/Godeps/_workspace/src/T"), nil}, 140 {"C/Godeps/_workspace/src/T/main.go", pkg("T"), nil}, 141 }, 142 }, 143 { // don't qualify standard library and local imports 144 cwd: "C", 145 start: []*node{ 146 {"C/main.go", pkg("main", "fmt", "C/D"), nil}, 147 {"C/D/main.go", pkg("D"), nil}, 148 }, 149 want: []*node{ 150 {"C/main.go", pkg("main", "fmt", "C/D"), nil}, 151 {"C/D/main.go", pkg("D"), nil}, 152 }, 153 }, 154 { // simple case, one dependency, -r=false 155 cwd: "C", 156 start: []*node{ 157 {"C/main.go", pkg("main", "D"), nil}, 158 {"C/Godeps/_workspace/src/D/main.go", pkg("D"), nil}, 159 }, 160 want: []*node{ 161 {"C/main.go", pkg("main", "D"), nil}, 162 {"C/Godeps/_workspace/src/D/main.go", pkg("D"), nil}, 163 }, 164 }, 165 { // transitive dep, -r=false 166 cwd: "C", 167 start: []*node{ 168 {"C/main.go", pkg("main", "D"), nil}, 169 {"C/Godeps/_workspace/src/D/main.go", pkg("D", "T"), nil}, 170 {"C/Godeps/_workspace/src/T/main.go", pkg("T"), nil}, 171 }, 172 want: []*node{ 173 {"C/main.go", pkg("main", "D"), nil}, 174 {"C/Godeps/_workspace/src/D/main.go", pkg("D", "T"), nil}, 175 {"C/Godeps/_workspace/src/T/main.go", pkg("T"), nil}, 176 }, 177 }, 178 { // intermediate dep that uses godep save -r, -r=false 179 cwd: "C", 180 start: []*node{ 181 {"C/main.go", pkg("main", "D"), nil}, 182 {"C/Godeps/_workspace/src/D/main.go", pkg("D", "D/Godeps/_workspace/src/T"), nil}, 183 {"C/Godeps/_workspace/src/T/main.go", pkg("T"), nil}, 184 }, 185 want: []*node{ 186 {"C/main.go", pkg("main", "D"), nil}, 187 {"C/Godeps/_workspace/src/D/main.go", pkg("D", "T"), nil}, 188 {"C/Godeps/_workspace/src/T/main.go", pkg("T"), nil}, 189 }, 190 }, 191 { // whitespace 192 cwd: "C", 193 paths: []string{"D"}, 194 start: []*node{ 195 {"C/main.go", whitespace, nil}, 196 }, 197 want: []*node{ 198 {"C/main.go", whitespaceRewritten, nil}, 199 }, 200 }, 201 { // sort after rewrite 202 cwd: "C", 203 paths: []string{"D", "E"}, 204 start: []*node{ 205 {"C/main.go", sortOrder, nil}, 206 }, 207 want: []*node{ 208 {"C/main.go", sortOrderRewritten, nil}, 209 }, 210 }, 211 { // sort after rewrite 212 cwd: "C", 213 paths: []string{"D", "E"}, 214 start: []*node{ 215 {"C/main.go", sortOrderPreserveComment, nil}, 216 }, 217 want: []*node{ 218 {"C/main.go", sortOrderPreserveCommentRewritten, nil}, 219 }, 220 }, 221 { // testdata directory is copied unmodified. 222 cwd: "C", 223 paths: []string{"D"}, 224 start: []*node{ 225 {"C/main.go", pkg("main", "D"), nil}, 226 {"C/testdata", "", 227 []*node{ 228 {"badpkg.go", "//", nil}, 229 }, 230 }, 231 }, 232 want: []*node{ 233 {"C/main.go", pkg("main", "C/Godeps/_workspace/src/D"), nil}, 234 {"C/testdata", "", 235 []*node{ 236 {"badpkg.go", "//", nil}, 237 }, 238 }, 239 }, 240 }, 241 } 242 243 const gopath = "godeptest" 244 defer os.RemoveAll(gopath) 245 for pos, test := range cases { 246 clearPkgCache() 247 err := os.RemoveAll(gopath) 248 if err != nil { 249 t.Fatal(err) 250 } 251 src := filepath.Join(gopath, "src") 252 makeTree(t, &node{src, "", test.start}, "") 253 err = rewriteTree(filepath.Join(src, test.cwd), test.cwd, test.paths) 254 if g := err != nil; g != test.werr { 255 t.Errorf("save err = %v (%v) want %v", g, err, test.werr) 256 } 257 tempFiles, err := filepath.Glob(filepath.Join(src, test.cwd) + "/*.temp") 258 if err != nil { 259 t.Errorf("Error while running glob: %s", err.Error()) 260 } 261 if len(tempFiles) != 0 { 262 t.Errorf("Unexpected tempfiles: %+v", tempFiles) 263 } 264 265 checkTree(t, pos, &node{src, "", test.want}) 266 } 267 }