github.com/jlowellwofford/u-root@v1.0.0/pkg/uroot/uroot_test.go (about)

     1  // Copyright 2018 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package uroot
     6  
     7  import (
     8  	"fmt"
     9  	"path/filepath"
    10  	"reflect"
    11  	"testing"
    12  
    13  	"github.com/u-root/u-root/pkg/golang"
    14  )
    15  
    16  func TestResolvePackagePaths(t *testing.T) {
    17  	defaultEnv := golang.Default()
    18  	gopath1, err := filepath.Abs("test/gopath1")
    19  	if err != nil {
    20  		t.Fatalf("failure to set up test: %v", err)
    21  	}
    22  	gopath2, err := filepath.Abs("test/gopath2")
    23  	if err != nil {
    24  		t.Fatalf("failure to set up test: %v", err)
    25  	}
    26  	gopath1Env := defaultEnv
    27  	gopath1Env.GOPATH = gopath1
    28  	gopath2Env := defaultEnv
    29  	gopath2Env.GOPATH = gopath2
    30  	everythingEnv := defaultEnv
    31  	everythingEnv.GOPATH = gopath1 + ":" + gopath2
    32  	foopath, err := filepath.Abs("test/gopath1/src/foo")
    33  	if err != nil {
    34  		t.Fatalf("failure to set up test: %v", err)
    35  	}
    36  
    37  	for _, tc := range []struct {
    38  		env      golang.Environ
    39  		in       []string
    40  		expected []string
    41  		wantErr  bool
    42  	}{
    43  		// Nonexistent Package
    44  		{
    45  			env:      defaultEnv,
    46  			in:       []string{"fakepackagename"},
    47  			expected: nil,
    48  			wantErr:  true,
    49  		},
    50  		// Single go package import
    51  		{
    52  			env: defaultEnv,
    53  			in:  []string{"github.com/u-root/u-root/cmds/ls"},
    54  			// We expect the full URL format because that's the path in our default GOPATH
    55  			expected: []string{"github.com/u-root/u-root/cmds/ls"},
    56  			wantErr:  false,
    57  		},
    58  		// Single package directory relative to working dir
    59  		{
    60  			env:      defaultEnv,
    61  			in:       []string{"test/gopath1/src/foo"},
    62  			expected: []string{"github.com/u-root/u-root/pkg/uroot/test/gopath1/src/foo"},
    63  			wantErr:  false,
    64  		},
    65  		// Single package directory with absolute path
    66  		{
    67  			env:      defaultEnv,
    68  			in:       []string{foopath},
    69  			expected: []string{"github.com/u-root/u-root/pkg/uroot/test/gopath1/src/foo"},
    70  			wantErr:  false,
    71  		},
    72  		// Single package directory relative to GOPATH
    73  		{
    74  			env: gopath1Env,
    75  			in:  []string{"foo"},
    76  			expected: []string{
    77  				"foo",
    78  			},
    79  			wantErr: false,
    80  		},
    81  		// Package directory glob
    82  		{
    83  			env: defaultEnv,
    84  			in:  []string{"test/gopath2/src/mypkg*"},
    85  			expected: []string{
    86  				"github.com/u-root/u-root/pkg/uroot/test/gopath2/src/mypkga",
    87  				"github.com/u-root/u-root/pkg/uroot/test/gopath2/src/mypkgb",
    88  			},
    89  			wantErr: false,
    90  		},
    91  		// GOPATH glob
    92  		{
    93  			env: gopath2Env,
    94  			in:  []string{"mypkg*"},
    95  			expected: []string{
    96  				"mypkga",
    97  				"mypkgb",
    98  			},
    99  			wantErr: false,
   100  		},
   101  		// Single ambiguous package - exists in both GOROOT and GOPATH
   102  		{
   103  			env: gopath1Env,
   104  			in:  []string{"os"},
   105  			expected: []string{
   106  				"os",
   107  			},
   108  			wantErr: false,
   109  		},
   110  		// Packages from different gopaths
   111  		{
   112  			env: everythingEnv,
   113  			in:  []string{"foo", "mypkga"},
   114  			expected: []string{
   115  				"foo",
   116  				"mypkga",
   117  			},
   118  			wantErr: false,
   119  		},
   120  		// Same package specified twice
   121  		{
   122  			env: defaultEnv,
   123  			in:  []string{"test/gopath2/src/mypkga", "test/gopath2/src/mypkga"},
   124  			// TODO: This returns the package twice. Is this preferred?
   125  			expected: []string{
   126  				"github.com/u-root/u-root/pkg/uroot/test/gopath2/src/mypkga",
   127  				"github.com/u-root/u-root/pkg/uroot/test/gopath2/src/mypkga",
   128  			},
   129  			wantErr: false,
   130  		},
   131  	} {
   132  		t.Run(fmt.Sprintf("%q", tc.in), func(t *testing.T) {
   133  			out, err := ResolvePackagePaths(tc.env, tc.in)
   134  			if (err != nil) != tc.wantErr {
   135  				t.Fatalf("ResolvePackagePaths(%#v, %v) err != nil is %v, want %v\nerr is %v",
   136  					tc.env, tc.in, err != nil, tc.wantErr, err)
   137  			}
   138  			if !reflect.DeepEqual(out, tc.expected) {
   139  				t.Errorf("ResolvePackagePaths(%#v, %v) = %v; want %v",
   140  					tc.env, tc.in, out, tc.expected)
   141  			}
   142  		})
   143  	}
   144  }