github.com/opsmatic/godep@v0.1.5/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 ) 66 67 func TestRewrite(t *testing.T) { 68 var cases = []struct { 69 cwd string 70 paths []string 71 start []*node 72 want []*node 73 werr bool 74 }{ 75 { // simple case, one dependency 76 cwd: "C", 77 paths: []string{"D"}, 78 start: []*node{ 79 {"C/main.go", pkg("main", "D"), nil}, 80 {"C/Godeps/_workspace/src/D/main.go", pkg("D"), nil}, 81 }, 82 want: []*node{ 83 {"C/main.go", pkg("main", "C/Godeps/_workspace/src/D"), nil}, 84 {"C/Godeps/_workspace/src/D/main.go", pkg("D"), nil}, 85 }, 86 }, 87 { // transitive dep 88 cwd: "C", 89 paths: []string{"D", "T"}, 90 start: []*node{ 91 {"C/main.go", pkg("main", "D"), nil}, 92 {"C/Godeps/_workspace/src/D/main.go", pkg("D", "T"), nil}, 93 {"C/Godeps/_workspace/src/T/main.go", pkg("T"), nil}, 94 }, 95 want: []*node{ 96 {"C/main.go", pkg("main", "C/Godeps/_workspace/src/D"), nil}, 97 {"C/Godeps/_workspace/src/D/main.go", pkg("D", "C/Godeps/_workspace/src/T"), nil}, 98 {"C/Godeps/_workspace/src/T/main.go", pkg("T"), nil}, 99 }, 100 }, 101 { // intermediate dep that uses godep save -r 102 cwd: "C", 103 paths: []string{"D", "T"}, 104 start: []*node{ 105 {"C/main.go", pkg("main", "D"), nil}, 106 {"C/Godeps/_workspace/src/D/main.go", pkg("D", "D/Godeps/_workspace/src/T"), nil}, 107 {"C/Godeps/_workspace/src/T/main.go", pkg("T"), nil}, 108 }, 109 want: []*node{ 110 {"C/main.go", pkg("main", "C/Godeps/_workspace/src/D"), nil}, 111 {"C/Godeps/_workspace/src/D/main.go", pkg("D", "C/Godeps/_workspace/src/T"), nil}, 112 {"C/Godeps/_workspace/src/T/main.go", pkg("T"), nil}, 113 }, 114 }, 115 { // don't qualify standard library and local imports 116 cwd: "C", 117 start: []*node{ 118 {"C/main.go", pkg("main", "fmt", "C/D"), nil}, 119 {"C/D/main.go", pkg("D"), nil}, 120 }, 121 want: []*node{ 122 {"C/main.go", pkg("main", "fmt", "C/D"), nil}, 123 {"C/D/main.go", pkg("D"), nil}, 124 }, 125 }, 126 { // simple case, one dependency, -r=false 127 cwd: "C", 128 start: []*node{ 129 {"C/main.go", pkg("main", "D"), nil}, 130 {"C/Godeps/_workspace/src/D/main.go", pkg("D"), nil}, 131 }, 132 want: []*node{ 133 {"C/main.go", pkg("main", "D"), nil}, 134 {"C/Godeps/_workspace/src/D/main.go", pkg("D"), nil}, 135 }, 136 }, 137 { // transitive dep, -r=false 138 cwd: "C", 139 start: []*node{ 140 {"C/main.go", pkg("main", "D"), nil}, 141 {"C/Godeps/_workspace/src/D/main.go", pkg("D", "T"), nil}, 142 {"C/Godeps/_workspace/src/T/main.go", pkg("T"), nil}, 143 }, 144 want: []*node{ 145 {"C/main.go", pkg("main", "D"), nil}, 146 {"C/Godeps/_workspace/src/D/main.go", pkg("D", "T"), nil}, 147 {"C/Godeps/_workspace/src/T/main.go", pkg("T"), nil}, 148 }, 149 }, 150 { // intermediate dep that uses godep save -r, -r=false 151 cwd: "C", 152 start: []*node{ 153 {"C/main.go", pkg("main", "D"), nil}, 154 {"C/Godeps/_workspace/src/D/main.go", pkg("D", "D/Godeps/_workspace/src/T"), nil}, 155 {"C/Godeps/_workspace/src/T/main.go", pkg("T"), nil}, 156 }, 157 want: []*node{ 158 {"C/main.go", pkg("main", "D"), nil}, 159 {"C/Godeps/_workspace/src/D/main.go", pkg("D", "T"), nil}, 160 {"C/Godeps/_workspace/src/T/main.go", pkg("T"), nil}, 161 }, 162 }, 163 { // whitespace 164 cwd: "C", 165 paths: []string{"D"}, 166 start: []*node{ 167 {"C/main.go", whitespace, nil}, 168 }, 169 want: []*node{ 170 {"C/main.go", whitespaceRewritten, nil}, 171 }, 172 }, 173 } 174 175 const gopath = "godeptest" 176 defer os.RemoveAll(gopath) 177 for _, test := range cases { 178 err := os.RemoveAll(gopath) 179 if err != nil { 180 t.Fatal(err) 181 } 182 src := filepath.Join(gopath, "src") 183 makeTree(t, &node{src, "", test.start}, "") 184 err = rewriteTree(filepath.Join(src, test.cwd), test.cwd, test.paths) 185 if g := err != nil; g != test.werr { 186 t.Errorf("save err = %v (%v) want %v", g, err, test.werr) 187 } 188 189 checkTree(t, &node{src, "", test.want}) 190 } 191 }