github.com/gsquire/gb@v0.4.4-0.20161112235727-3982dc872064/package_test.go (about) 1 package gb 2 3 import ( 4 "fmt" 5 "path/filepath" 6 "reflect" 7 "runtime" 8 "testing" 9 10 "github.com/constabulary/gb/internal/importer" 11 ) 12 13 func testContext(t *testing.T, opts ...func(*Context) error) *Context { 14 ctx, err := NewContext(testProject(t), opts...) 15 if err != nil { 16 t.Fatal(err) 17 } 18 ctx.Force = true 19 return ctx 20 } 21 22 func TestResolvePackage(t *testing.T) { 23 var tests = []struct { 24 pkg string // package name 25 opts []func(*Context) error 26 err error 27 }{{ 28 pkg: "a", 29 }, { 30 pkg: "localimport", 31 err: fmt.Errorf(`import "../localimport": relative import not supported`), 32 }} 33 proj := testProject(t) 34 for _, tt := range tests { 35 ctx, err := NewContext(proj, tt.opts...) 36 defer ctx.Destroy() 37 _, err = ctx.ResolvePackage(tt.pkg) 38 if !reflect.DeepEqual(err, tt.err) { 39 t.Errorf("ResolvePackage(%q): want: %v, got %v", tt.pkg, tt.err, err) 40 } 41 } 42 } 43 44 func TestPackageBinfile(t *testing.T) { 45 opts := func(o ...func(*Context) error) []func(*Context) error { return o } 46 gotargetos := "windows" 47 if runtime.GOOS == "windows" { 48 gotargetos = "linux" 49 } 50 gotargetarch := "386" 51 if runtime.GOARCH == "386" { 52 gotargetarch = "amd64" 53 } 54 var tests = []struct { 55 pkg string // package name 56 opts []func(*Context) error 57 want string // binfile result 58 }{{ 59 pkg: "b", 60 want: "b", 61 }, { 62 pkg: "b", 63 opts: opts(GOOS(gotargetos)), 64 want: fmt.Sprintf("b-%v-%v", gotargetos, runtime.GOARCH), 65 }, { 66 pkg: "b", 67 opts: opts(GOARCH(gotargetarch)), 68 want: fmt.Sprintf("b-%v-%v", runtime.GOOS, gotargetarch), 69 }, { 70 pkg: "b", 71 opts: opts(GOARCH(gotargetarch), GOOS(gotargetos)), 72 want: fmt.Sprintf("b-%v-%v", gotargetos, gotargetarch), 73 }, { 74 pkg: "b", 75 opts: opts(Tags("lol")), 76 want: "b-lol", 77 }, { 78 pkg: "b", 79 opts: opts(GOARCH(gotargetarch), GOOS(gotargetos), Tags("lol")), 80 want: fmt.Sprintf("b-%v-%v-lol", gotargetos, gotargetarch), 81 }} 82 83 proj := testProject(t) 84 for i, tt := range tests { 85 ctx, _ := NewContext(proj, tt.opts...) 86 defer ctx.Destroy() 87 pkg, err := ctx.ResolvePackage(tt.pkg) 88 if err != nil { 89 t.Fatal(err) 90 } 91 got := pkg.Binfile() 92 want := filepath.Join(ctx.Bindir(), tt.want) 93 if pkg.gotargetos == "windows" { 94 want += ".exe" 95 } 96 if want != got { 97 t.Errorf("test %v: (%s).Binfile(): want %s, got %s", i+1, tt.pkg, want, got) 98 } 99 } 100 } 101 102 func TestPackageIsMain(t *testing.T) { 103 var tests = []struct { 104 pkg *Package 105 want bool 106 }{{ 107 pkg: &Package{ 108 Package: &importer.Package{ 109 Name: "main", 110 ImportPath: "main", 111 }, 112 }, 113 want: true, 114 }, { 115 pkg: &Package{ 116 Package: &importer.Package{ 117 Name: "a", 118 ImportPath: "main", 119 }, 120 }, 121 want: false, 122 }, { 123 pkg: &Package{ 124 Package: &importer.Package{ 125 Name: "main", 126 ImportPath: "a", 127 }, 128 }, 129 want: true, 130 }, { 131 pkg: &Package{ 132 Package: &importer.Package{ 133 Name: "main", 134 ImportPath: "testmain", 135 }, 136 }, 137 want: true, 138 }, { 139 pkg: &Package{ 140 Package: &importer.Package{ 141 Name: "main", 142 ImportPath: "main", 143 }, 144 TestScope: true, 145 }, 146 want: false, 147 }, { 148 pkg: &Package{ 149 Package: &importer.Package{ 150 Name: "a", 151 ImportPath: "main", 152 }, 153 TestScope: true, 154 }, 155 want: false, 156 }, { 157 pkg: &Package{ 158 Package: &importer.Package{ 159 Name: "main", 160 ImportPath: "a", 161 }, 162 TestScope: true, 163 }, 164 want: false, 165 }, { 166 pkg: &Package{ 167 Package: &importer.Package{ 168 Name: "main", 169 ImportPath: "testmain", 170 }, 171 TestScope: true, 172 }, 173 want: true, 174 }} 175 176 for _, tt := range tests { 177 got := tt.pkg.isMain() 178 if got != tt.want { 179 t.Errorf("Package{Name:%q, ImportPath: %q, TestScope:%v}.isMain(): got %v, want %v", tt.pkg.Name, tt.pkg.ImportPath, tt.pkg.TestScope, got, tt.want) 180 } 181 } 182 } 183 184 func TestNewPackage(t *testing.T) { 185 tests := []struct { 186 pkg importer.Package 187 want Package 188 }{{ 189 importer.Package{ 190 Name: "C", 191 ImportPath: "C", 192 Standard: true, 193 }, 194 Package{ 195 Stale: false, 196 }, 197 }} 198 proj := testProject(t) 199 for i, tt := range tests { 200 ctx, _ := NewContext(proj) 201 defer ctx.Destroy() 202 203 got, err := ctx.NewPackage(&tt.pkg) 204 if err != nil { 205 t.Error(err) 206 continue 207 } 208 want := tt.want // deep copy 209 want.Package = &tt.pkg 210 want.Context = ctx 211 212 if !reflect.DeepEqual(got, &want) { 213 t.Errorf("%d: pkg: %s: expected %#v, got %#v", i+1, tt.pkg.ImportPath, &want, got) 214 } 215 } 216 }