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