github.com/golang/dep@v0.5.4/internal/importers/gvt/importer_test.go (about)

     1  // Copyright 2017 The Go 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 gvt
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"log"
    11  	"path/filepath"
    12  	"testing"
    13  
    14  	"github.com/golang/dep"
    15  	"github.com/golang/dep/gps"
    16  	"github.com/golang/dep/internal/importers/importertest"
    17  	"github.com/golang/dep/internal/test"
    18  	"github.com/pkg/errors"
    19  )
    20  
    21  func TestGvtConfig_Convert(t *testing.T) {
    22  	testCases := map[string]struct {
    23  		importertest.TestCase
    24  		gvtConfig gvtManifest
    25  	}{
    26  		"package with master branch": {
    27  			importertest.TestCase{
    28  				WantConstraint: importertest.V1Constraint,
    29  				WantRevision:   importertest.V1Rev,
    30  				WantVersion:    importertest.V1Tag,
    31  			},
    32  			gvtManifest{
    33  				Deps: []gvtPkg{
    34  					{
    35  						ImportPath: importertest.Project,
    36  						Revision:   importertest.V1Rev,
    37  						Branch:     "master",
    38  					},
    39  				},
    40  			},
    41  		},
    42  		"package with non-master branch": {
    43  			importertest.TestCase{
    44  				WantConstraint: importertest.V2Branch,
    45  				WantRevision:   importertest.V2PatchRev,
    46  				WantVersion:    importertest.V2PatchTag,
    47  			},
    48  			gvtManifest{
    49  				Deps: []gvtPkg{
    50  					{
    51  						ImportPath: importertest.Project,
    52  						Revision:   importertest.V2PatchRev,
    53  						Branch:     importertest.V2Branch,
    54  					},
    55  				},
    56  			},
    57  		},
    58  		"package with HEAD branch": {
    59  			importertest.TestCase{
    60  				WantRevision: importertest.V1Rev,
    61  				WantVersion:  importertest.V1Tag,
    62  			},
    63  			gvtManifest{
    64  				Deps: []gvtPkg{
    65  					{
    66  						ImportPath: importertest.Project,
    67  						Revision:   importertest.V1Rev,
    68  						Branch:     "HEAD",
    69  					},
    70  				},
    71  			},
    72  		},
    73  		"package with alternate repository": {
    74  			importertest.TestCase{
    75  				WantConstraint: importertest.V1Constraint,
    76  				WantRevision:   importertest.V1Rev,
    77  				WantVersion:    importertest.V1Tag,
    78  				WantSourceRepo: importertest.ProjectSrc,
    79  			},
    80  			gvtManifest{
    81  				Deps: []gvtPkg{
    82  					{
    83  						ImportPath: importertest.Project,
    84  						Repository: importertest.ProjectSrc,
    85  						Revision:   importertest.V1Rev,
    86  						Branch:     "master",
    87  					},
    88  				},
    89  			},
    90  		},
    91  		"missing package name": {
    92  			importertest.TestCase{
    93  				WantWarning: "Warning: Skipping project. Invalid gvt configuration, ImportPath is required",
    94  			},
    95  			gvtManifest{
    96  				Deps: []gvtPkg{{ImportPath: ""}},
    97  			},
    98  		},
    99  		"missing revision": {
   100  			importertest.TestCase{
   101  				WantWarning: fmt.Sprintf(
   102  					"Warning: Invalid gvt configuration, Revision not found for ImportPath %q",
   103  					importertest.Project,
   104  				),
   105  			},
   106  			gvtManifest{
   107  				Deps: []gvtPkg{
   108  					{
   109  						ImportPath: importertest.Project,
   110  					},
   111  				},
   112  			},
   113  		},
   114  	}
   115  
   116  	for name, testCase := range testCases {
   117  		name := name
   118  		testCase := testCase
   119  		t.Run(name, func(t *testing.T) {
   120  			err := testCase.Execute(t, func(logger *log.Logger, sm gps.SourceManager) (*dep.Manifest, *dep.Lock) {
   121  				g := NewImporter(logger, true, sm)
   122  				g.gvtConfig = testCase.gvtConfig
   123  				return g.convert(importertest.RootProject)
   124  			})
   125  			if err != nil {
   126  				t.Fatalf("%#v", err)
   127  			}
   128  		})
   129  	}
   130  }
   131  
   132  func TestGvtConfig_Import(t *testing.T) {
   133  	h := test.NewHelper(t)
   134  	defer h.Cleanup()
   135  
   136  	cacheDir := "gps-repocache"
   137  	h.TempDir(cacheDir)
   138  	h.TempDir("src")
   139  	h.TempDir(filepath.Join("src", importertest.RootProject))
   140  	h.TempCopy(filepath.Join(importertest.RootProject, gvtPath), "manifest")
   141  
   142  	projectRoot := h.Path(importertest.RootProject)
   143  	sm, err := gps.NewSourceManager(gps.SourceManagerConfig{
   144  		Cachedir: h.Path(cacheDir),
   145  		Logger:   log.New(test.Writer{TB: t}, "", 0),
   146  	})
   147  	h.Must(err)
   148  	defer sm.Release()
   149  
   150  	// Capture stderr so we can verify output
   151  	verboseOutput := &bytes.Buffer{}
   152  	logger := log.New(verboseOutput, "", 0)
   153  
   154  	g := NewImporter(logger, false, sm) // Disable verbose so that we don't print values that change each test run
   155  	if !g.HasDepMetadata(projectRoot) {
   156  		t.Fatal("Expected the importer to detect gvt configuration file")
   157  	}
   158  
   159  	m, l, err := g.Import(projectRoot, importertest.RootProject)
   160  	h.Must(err)
   161  
   162  	if m == nil {
   163  		t.Fatal("Expected the manifest to be generated")
   164  	}
   165  
   166  	if l == nil {
   167  		t.Fatal("Expected the lock to be generated")
   168  	}
   169  
   170  	goldenFile := "golden.txt"
   171  	got := verboseOutput.String()
   172  	want := h.GetTestFileString(goldenFile)
   173  	if want != got {
   174  		if *test.UpdateGolden {
   175  			if err := h.WriteTestFile(goldenFile, got); err != nil {
   176  				t.Fatalf("%+v", errors.Wrapf(err, "Unable to write updated golden file %s", goldenFile))
   177  			}
   178  		} else {
   179  			t.Fatalf("want %s, got %s", want, got)
   180  		}
   181  	}
   182  }
   183  
   184  func TestGvtConfig_JsonLoad(t *testing.T) {
   185  	// This is same as testdata/manifest
   186  	wantConfig := gvtManifest{
   187  		Deps: []gvtPkg{
   188  			{
   189  				ImportPath: "github.com/sdboyer/deptest",
   190  				Revision:   "3f4c3bea144e112a69bbe5d8d01c1b09a544253f",
   191  				Branch:     "HEAD",
   192  			},
   193  			{
   194  				ImportPath: "github.com/sdboyer/deptestdos",
   195  				Revision:   "5c607206be5decd28e6263ffffdcee067266015e",
   196  				Branch:     "master",
   197  			},
   198  			{
   199  				ImportPath: "github.com/carolynvs/deptest-importers",
   200  				Revision:   "b79bc9482da8bb7402cdc3e3fd984db250718dd7",
   201  				Branch:     "v2",
   202  			},
   203  		},
   204  	}
   205  
   206  	h := test.NewHelper(t)
   207  	defer h.Cleanup()
   208  
   209  	ctx := importertest.NewTestContext(h)
   210  
   211  	h.TempCopy(filepath.Join(importertest.RootProject, gvtPath), "manifest")
   212  
   213  	projectRoot := h.Path(importertest.RootProject)
   214  
   215  	g := NewImporter(ctx.Err, true, nil)
   216  	err := g.load(projectRoot)
   217  	if err != nil {
   218  		t.Fatalf("Error while loading... %v", err)
   219  	}
   220  
   221  	if !equalImports(g.gvtConfig.Deps, wantConfig.Deps) {
   222  		t.Fatalf("Expected imports to be equal. \n\t(GOT): %v\n\t(WNT): %v", g.gvtConfig.Deps, wantConfig.Deps)
   223  	}
   224  }
   225  
   226  // equalImports compares two slices of gvtPkg and checks if they are
   227  // equal.
   228  func equalImports(a, b []gvtPkg) bool {
   229  	if a == nil && b == nil {
   230  		return true
   231  	}
   232  
   233  	if a == nil || b == nil {
   234  		return false
   235  	}
   236  
   237  	if len(a) != len(b) {
   238  		return false
   239  	}
   240  
   241  	for i := range a {
   242  		if a[i] != b[i] {
   243  			return false
   244  		}
   245  	}
   246  
   247  	return true
   248  }