github.com/aca02djr/gb@v0.4.1/install_test.go (about) 1 package gb 2 3 import ( 4 "os" 5 "path/filepath" 6 "runtime" 7 "testing" 8 ) 9 10 func TestStale(t *testing.T) { 11 var tests = []struct { 12 pkgs []string 13 stale map[string]bool 14 }{{ 15 pkgs: []string{"a"}, 16 stale: map[string]bool{ 17 "a": true, 18 }, 19 }, { 20 pkgs: []string{"a", "b"}, 21 stale: map[string]bool{ 22 "a": false, 23 "b": true, 24 }, 25 }, { 26 pkgs: []string{"a", "b"}, 27 stale: map[string]bool{ 28 "a": false, 29 "b": false, 30 }, 31 }} 32 33 root := mktemp(t) 34 defer os.RemoveAll(root) 35 36 proj := Project{ 37 rootdir: root, 38 srcdirs: []Srcdir{{ 39 Root: filepath.Join(getwd(t), "testdata", "src"), 40 }}, 41 } 42 43 newctx := func() *Context { 44 ctx, err := proj.NewContext( 45 GcToolchain(), 46 ) 47 if err != nil { 48 t.Fatal(err) 49 } 50 return ctx 51 } 52 53 resolve := func(ctx *Context, pkg string) *Package { 54 p, err := ctx.ResolvePackage(pkg) 55 if err != nil { 56 t.Fatal(err) 57 } 58 return p 59 } 60 61 for _, tt := range tests { 62 ctx := newctx() 63 ctx.Install = true 64 defer ctx.Destroy() 65 for _, pkg := range tt.pkgs { 66 resolve(ctx, pkg) 67 } 68 69 for p, s := range tt.stale { 70 pkg := resolve(ctx, p) 71 if pkg.Stale != s { 72 t.Errorf("%q.Stale: got %v, want %v", pkg.Name, pkg.Stale, s) 73 } 74 } 75 76 for _, pkg := range tt.pkgs { 77 if err := Build(resolve(ctx, pkg)); err != nil { 78 t.Fatal(err) 79 } 80 } 81 } 82 } 83 84 func TestInstallpath(t *testing.T) { 85 ctx := testContext(t) 86 defer ctx.Destroy() 87 88 tests := []struct { 89 pkg string 90 installpath string 91 }{{ 92 pkg: "a", // from testdata 93 installpath: filepath.Join(ctx.Pkgdir(), "a.a"), 94 }, { 95 pkg: "runtime", // from stdlib 96 installpath: filepath.Join(ctx.Pkgdir(), "runtime.a"), 97 }, { 98 pkg: "unsafe", // synthetic 99 installpath: filepath.Join(ctx.Pkgdir(), "unsafe.a"), 100 }} 101 102 resolve := func(pkg string) *Package { 103 p, err := ctx.ResolvePackage(pkg) 104 if err != nil { 105 t.Fatal(err) 106 } 107 return p 108 } 109 110 for _, tt := range tests { 111 pkg := resolve(tt.pkg) 112 got := installpath(pkg) 113 if got != tt.installpath { 114 t.Errorf("installpath(%q): expected: %v, got %v", tt.pkg, tt.installpath, got) 115 } 116 } 117 } 118 119 func TestPkgpath(t *testing.T) { 120 opts := func(o ...func(*Context) error) []func(*Context) error { return o } 121 gotargetos := "windows" 122 if runtime.GOOS == gotargetos { 123 gotargetos = "linux" 124 } 125 gotargetarch := "arm64" 126 if runtime.GOARCH == "arm64" { 127 gotargetarch = "amd64" 128 } 129 tests := []struct { 130 opts []func(*Context) error 131 pkg string 132 pkgpath func(*Context) string 133 }{{ 134 pkg: "a", // from testdata 135 pkgpath: func(ctx *Context) string { return filepath.Join(ctx.Pkgdir(), "a.a") }, 136 }, { 137 opts: opts(GOOS(gotargetos), GOARCH(gotargetarch)), 138 pkg: "a", // from testdata 139 pkgpath: func(ctx *Context) string { return filepath.Join(ctx.Pkgdir(), "a.a") }, 140 }, { 141 opts: opts(WithRace), 142 pkg: "a", // from testdata 143 pkgpath: func(ctx *Context) string { return filepath.Join(ctx.Pkgdir(), "a.a") }, 144 }, { 145 opts: opts(Tags("foo", "bar")), 146 pkg: "a", // from testdata 147 pkgpath: func(ctx *Context) string { return filepath.Join(ctx.Pkgdir(), "a.a") }, 148 }, { 149 pkg: "runtime", // from stdlib 150 pkgpath: func(ctx *Context) string { 151 return filepath.Join(runtime.GOROOT(), "pkg", ctx.gohostos+"_"+ctx.gohostarch, "runtime.a") 152 }, 153 }, { 154 opts: opts(Tags("foo", "bar")), 155 pkg: "runtime", // from stdlib 156 pkgpath: func(ctx *Context) string { 157 return filepath.Join(runtime.GOROOT(), "pkg", ctx.gohostos+"_"+ctx.gohostarch, "runtime.a") 158 }, 159 }, { 160 opts: opts(WithRace), 161 pkg: "runtime", // from stdlib 162 pkgpath: func(ctx *Context) string { 163 return filepath.Join(runtime.GOROOT(), "pkg", ctx.gohostos+"_"+ctx.gohostarch+"_race", "runtime.a") 164 }, 165 }, { 166 opts: opts(WithRace, Tags("foo", "bar")), 167 pkg: "runtime", // from stdlib 168 pkgpath: func(ctx *Context) string { 169 return filepath.Join(runtime.GOROOT(), "pkg", ctx.gohostos+"_"+ctx.gohostarch+"_race", "runtime.a") 170 }, 171 }, { 172 opts: opts(GOOS(gotargetos), GOARCH(gotargetarch)), 173 pkg: "runtime", // from stdlib 174 pkgpath: func(ctx *Context) string { 175 return filepath.Join(ctx.Pkgdir(), "runtime.a") 176 }, 177 }, { 178 pkg: "unsafe", // synthetic 179 pkgpath: func(ctx *Context) string { 180 return filepath.Join(runtime.GOROOT(), "pkg", ctx.gohostos+"_"+ctx.gohostarch, "unsafe.a") 181 }, 182 }, { 183 pkg: "unsafe", // synthetic 184 opts: opts(GOOS(gotargetos), GOARCH(gotargetarch), WithRace), 185 pkgpath: func(ctx *Context) string { 186 return filepath.Join(ctx.Pkgdir(), "unsafe.a") 187 }, 188 }} 189 190 for _, tt := range tests { 191 ctx := testContext(t, tt.opts...) 192 defer ctx.Destroy() 193 pkg, err := ctx.ResolvePackage(tt.pkg) 194 if err != nil { 195 t.Fatal(err) 196 } 197 got := pkgpath(pkg) 198 want := tt.pkgpath(ctx) 199 if got != want { 200 t.Errorf("pkgpath(%q): expected: %v, got %v", tt.pkg, want, got) 201 } 202 } 203 }