github.com/golang/dep@v0.5.4/internal/importers/govendor/importer_test.go (about) 1 // Copyright 2016 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 govendor 6 7 import ( 8 "bytes" 9 "log" 10 "path/filepath" 11 "testing" 12 13 "github.com/golang/dep" 14 "github.com/golang/dep/gps" 15 "github.com/golang/dep/internal/importers/importertest" 16 "github.com/golang/dep/internal/test" 17 "github.com/pkg/errors" 18 ) 19 20 const testGovendorProjectRoot = "github.com/golang/notexist" 21 22 func TestGovendorConfig_Import(t *testing.T) { 23 h := test.NewHelper(t) 24 defer h.Cleanup() 25 26 ctx := importertest.NewTestContext(h) 27 sm, err := ctx.SourceManager() 28 h.Must(err) 29 defer sm.Release() 30 31 h.TempDir(filepath.Join("src", testGovendorProjectRoot)) 32 h.TempCopy(filepath.Join(testGovendorProjectRoot, govendorDir, govendorName), "vendor.json") 33 projectRoot := h.Path(testGovendorProjectRoot) 34 35 // Capture stderr so we can verify output 36 verboseOutput := &bytes.Buffer{} 37 ctx.Err = log.New(verboseOutput, "", 0) 38 39 g := NewImporter(ctx.Err, false, sm) // Disable verbose so that we don't print values that change each test run 40 if !g.HasDepMetadata(projectRoot) { 41 t.Fatal("Expected the importer to detect the govendor configuration files") 42 } 43 44 m, l, err := g.Import(projectRoot, testGovendorProjectRoot) 45 h.Must(err) 46 47 if m == nil { 48 t.Fatal("Expected the manifest to be generated") 49 } 50 51 if l == nil { 52 t.Fatal("Expected the lock to be generated") 53 } 54 55 goldenFile := "golden.txt" 56 got := verboseOutput.String() 57 want := h.GetTestFileString(goldenFile) 58 if want != got { 59 if *test.UpdateGolden { 60 if err := h.WriteTestFile(goldenFile, got); err != nil { 61 t.Fatalf("%+v", errors.Wrapf(err, "Unable to write updated golden file %s", goldenFile)) 62 } 63 } else { 64 t.Fatalf("expected %s, got %s", want, got) 65 } 66 } 67 } 68 69 func TestGovendorConfig_Convert(t *testing.T) { 70 testCases := map[string]struct { 71 file govendorFile 72 importertest.TestCase 73 }{ 74 "project": { 75 govendorFile{ 76 Package: []*govendorPackage{ 77 { 78 Path: importertest.Project, 79 Origin: importertest.ProjectSrc, 80 Revision: importertest.V1Rev, 81 }, 82 }, 83 }, 84 importertest.TestCase{ 85 WantSourceRepo: importertest.ProjectSrc, 86 WantConstraint: importertest.V1Constraint, 87 WantRevision: importertest.V1Rev, 88 WantVersion: importertest.V1Tag, 89 }, 90 }, 91 "skipped build tags": { 92 govendorFile{ 93 Ignore: "test linux_amd64", 94 }, 95 importertest.TestCase{ 96 WantIgnored: nil, 97 }, 98 }, 99 "ignored external package": { 100 govendorFile{ 101 Ignore: "github.com/sdboyer/deptest k8s.io/apimachinery", 102 }, 103 importertest.TestCase{ 104 WantIgnored: []string{"github.com/sdboyer/deptest*", "k8s.io/apimachinery*"}, 105 }, 106 }, 107 "ignored internal package": { 108 govendorFile{ 109 Ignore: "samples/ foo/bar", 110 }, 111 importertest.TestCase{ 112 WantIgnored: []string{importertest.RootProject + "/samples*", importertest.RootProject + "/foo/bar*"}, 113 }, 114 }, 115 "missing package path": { 116 govendorFile{ 117 Package: []*govendorPackage{ 118 { 119 Revision: importertest.V2PatchRev, 120 }, 121 }, 122 }, 123 importertest.TestCase{ 124 WantWarning: "Warning: Skipping project. Invalid govendor configuration, Path is required", 125 }, 126 }, 127 "missing package revision doesn't cause an error": { 128 govendorFile{ 129 Package: []*govendorPackage{ 130 { 131 Path: importertest.Project, 132 }, 133 }, 134 }, 135 importertest.TestCase{ 136 WantRevision: "", 137 }, 138 }, 139 } 140 141 for name, testCase := range testCases { 142 name := name 143 testCase := testCase 144 t.Run(name, func(t *testing.T) { 145 err := testCase.Execute(t, func(logger *log.Logger, sm gps.SourceManager) (*dep.Manifest, *dep.Lock) { 146 g := NewImporter(logger, true, sm) 147 g.file = testCase.file 148 return g.convert(importertest.RootProject) 149 }) 150 if err != nil { 151 t.Fatalf("%#v", err) 152 } 153 }) 154 } 155 }