github.com/gsquire/gb@v0.4.4-0.20161112235727-3982dc872064/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  	proj := tempProject(t)
    34  	defer os.RemoveAll(proj.rootdir)
    35  	proj.tempfile("src/a/a.go", `package a
    36  
    37  const A = "A"
    38  `)
    39  
    40  	proj.tempfile("src/b/b.go", `package main
    41  
    42  import "a"
    43  
    44  func main() {
    45          println(a.A)
    46  }
    47  `)
    48  
    49  	newctx := func() *Context {
    50  		ctx, err := NewContext(proj,
    51  			GcToolchain(),
    52  		)
    53  		if err != nil {
    54  			t.Fatal(err)
    55  		}
    56  		return ctx
    57  	}
    58  
    59  	resolve := func(ctx *Context, pkg string) *Package {
    60  		p, err := ctx.ResolvePackage(pkg)
    61  		if err != nil {
    62  			t.Fatal(err)
    63  		}
    64  		return p
    65  	}
    66  
    67  	for _, tt := range tests {
    68  		ctx := newctx()
    69  		ctx.Install = true
    70  		defer ctx.Destroy()
    71  		for _, pkg := range tt.pkgs {
    72  			resolve(ctx, pkg)
    73  		}
    74  
    75  		for p, s := range tt.stale {
    76  			pkg := resolve(ctx, p)
    77  			if pkg.Stale != s {
    78  				t.Errorf("%q.Stale: got %v, want %v", pkg.Name, pkg.Stale, s)
    79  			}
    80  		}
    81  
    82  		for _, pkg := range tt.pkgs {
    83  			if err := Build(resolve(ctx, pkg)); err != nil {
    84  				t.Fatal(err)
    85  			}
    86  		}
    87  	}
    88  }
    89  
    90  func TestInstallpath(t *testing.T) {
    91  	ctx := testContext(t)
    92  	defer ctx.Destroy()
    93  
    94  	tests := []struct {
    95  		pkg         string
    96  		installpath string
    97  	}{{
    98  		pkg:         "a", // from testdata
    99  		installpath: filepath.Join(ctx.Pkgdir(), "a.a"),
   100  	}, {
   101  		pkg:         "runtime", // from stdlib
   102  		installpath: filepath.Join(ctx.Pkgdir(), "runtime.a"),
   103  	}, {
   104  		pkg:         "unsafe", // synthetic
   105  		installpath: filepath.Join(ctx.Pkgdir(), "unsafe.a"),
   106  	}}
   107  
   108  	resolve := func(pkg string) *Package {
   109  		p, err := ctx.ResolvePackage(pkg)
   110  		if err != nil {
   111  			t.Fatal(err)
   112  		}
   113  		return p
   114  	}
   115  
   116  	for _, tt := range tests {
   117  		pkg := resolve(tt.pkg)
   118  		got := installpath(pkg)
   119  		if got != tt.installpath {
   120  			t.Errorf("installpath(%q): expected: %v, got %v", tt.pkg, tt.installpath, got)
   121  		}
   122  	}
   123  }
   124  
   125  func TestPkgpath(t *testing.T) {
   126  	opts := func(o ...func(*Context) error) []func(*Context) error { return o }
   127  	gotargetos := "windows"
   128  	if runtime.GOOS == gotargetos {
   129  		gotargetos = "linux"
   130  	}
   131  	gotargetarch := "arm64"
   132  	if runtime.GOARCH == "arm64" {
   133  		gotargetarch = "amd64"
   134  	}
   135  	tests := []struct {
   136  		opts    []func(*Context) error
   137  		pkg     string
   138  		pkgpath func(*Context) string
   139  	}{{
   140  		pkg:     "a", // from testdata
   141  		pkgpath: func(ctx *Context) string { return filepath.Join(ctx.Pkgdir(), "a.a") },
   142  	}, {
   143  		opts:    opts(GOOS(gotargetos), GOARCH(gotargetarch)),
   144  		pkg:     "a", // from testdata
   145  		pkgpath: func(ctx *Context) string { return filepath.Join(ctx.Pkgdir(), "a.a") },
   146  	}, {
   147  		opts:    opts(WithRace),
   148  		pkg:     "a", // from testdata
   149  		pkgpath: func(ctx *Context) string { return filepath.Join(ctx.Pkgdir(), "a.a") },
   150  	}, {
   151  		opts:    opts(Tags("foo", "bar")),
   152  		pkg:     "a", // from testdata
   153  		pkgpath: func(ctx *Context) string { return filepath.Join(ctx.Pkgdir(), "a.a") },
   154  	}, {
   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(Tags("foo", "bar")),
   161  		pkg:  "runtime", // from stdlib
   162  		pkgpath: func(ctx *Context) string {
   163  			return filepath.Join(runtime.GOROOT(), "pkg", ctx.gohostos+"_"+ctx.gohostarch, "runtime.a")
   164  		},
   165  	}, {
   166  		opts: opts(WithRace),
   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(WithRace, Tags("foo", "bar")),
   173  		pkg:  "runtime", // from stdlib
   174  		pkgpath: func(ctx *Context) string {
   175  			return filepath.Join(runtime.GOROOT(), "pkg", ctx.gohostos+"_"+ctx.gohostarch+"_race", "runtime.a")
   176  		},
   177  	}, {
   178  		opts: opts(GOOS(gotargetos), GOARCH(gotargetarch)),
   179  		pkg:  "runtime", // from stdlib
   180  		pkgpath: func(ctx *Context) string {
   181  			return filepath.Join(ctx.Pkgdir(), "runtime.a")
   182  		},
   183  	}, {
   184  		pkg: "unsafe", // synthetic
   185  		pkgpath: func(ctx *Context) string {
   186  			return filepath.Join(runtime.GOROOT(), "pkg", ctx.gohostos+"_"+ctx.gohostarch, "unsafe.a")
   187  		},
   188  	}, {
   189  		pkg:  "unsafe", // synthetic
   190  		opts: opts(GOOS(gotargetos), GOARCH(gotargetarch), WithRace),
   191  		pkgpath: func(ctx *Context) string {
   192  			return filepath.Join(ctx.Pkgdir(), "unsafe.a")
   193  		},
   194  	}}
   195  
   196  	for _, tt := range tests {
   197  		ctx := testContext(t, tt.opts...)
   198  		defer ctx.Destroy()
   199  		pkg, err := ctx.ResolvePackage(tt.pkg)
   200  		if err != nil {
   201  			t.Fatal(err)
   202  		}
   203  		got := pkgpath(pkg)
   204  		want := tt.pkgpath(ctx)
   205  		if got != want {
   206  			t.Errorf("pkgpath(%q): expected: %v, got %v", tt.pkg, want, got)
   207  		}
   208  	}
   209  }