github.com/golang/dep@v0.5.4/internal/importers/glock/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 glock 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 TestGlockConfig_Convert(t *testing.T) { 22 testCases := map[string]struct { 23 importertest.TestCase 24 packages []glockPackage 25 }{ 26 "package": { 27 importertest.TestCase{ 28 WantConstraint: importertest.V1Constraint, 29 WantRevision: importertest.V1Rev, 30 WantVersion: importertest.V1Tag, 31 }, 32 []glockPackage{ 33 { 34 importPath: importertest.Project, 35 revision: importertest.V1Rev, 36 }, 37 }, 38 }, 39 "missing package name": { 40 importertest.TestCase{ 41 WantWarning: "Warning: Skipping project. Invalid glock configuration, import path is required", 42 }, 43 []glockPackage{{importPath: ""}}, 44 }, 45 "missing revision": { 46 importertest.TestCase{ 47 WantWarning: fmt.Sprintf( 48 " Warning: Skipping import with empty constraints. "+ 49 "The solve step will add the dependency to the lock if needed: %q", 50 importertest.Project, 51 ), 52 }, 53 []glockPackage{{importPath: importertest.Project}}, 54 }, 55 } 56 57 for name, testCase := range testCases { 58 name := name 59 testCase := testCase 60 t.Run(name, func(t *testing.T) { 61 err := testCase.Execute(t, func(logger *log.Logger, sm gps.SourceManager) (*dep.Manifest, *dep.Lock) { 62 g := NewImporter(logger, true, sm) 63 g.packages = testCase.packages 64 return g.convert(importertest.RootProject) 65 }) 66 if err != nil { 67 t.Fatalf("%#v", err) 68 } 69 }) 70 } 71 } 72 73 func TestGlockConfig_LoadInvalid(t *testing.T) { 74 const testLine = "github.com/sdboyer/deptest 3f4c3bea144e112a69bbe5d8d01c1b09a544253f invalid" 75 _, err := parseGlockLine(testLine) 76 expected := fmt.Errorf("invalid glock configuration: %s", testLine) 77 if err.Error() != expected.Error() { 78 t.Errorf("want error %s, got %s", err, expected) 79 } 80 } 81 82 func TestGlockConfig_LoadEmptyLine(t *testing.T) { 83 pkg, err := parseGlockLine("") 84 if err != nil { 85 t.Fatalf("%#v", err) 86 } 87 if pkg != nil { 88 t.Errorf("want package nil, got %+v", pkg) 89 } 90 } 91 92 func TestGlockConfig_Import(t *testing.T) { 93 h := test.NewHelper(t) 94 defer h.Cleanup() 95 96 ctx := importertest.NewTestContext(h) 97 sm, err := ctx.SourceManager() 98 h.Must(err) 99 defer sm.Release() 100 101 h.TempDir(filepath.Join("src", importertest.RootProject)) 102 h.TempCopy(filepath.Join(importertest.RootProject, glockfile), glockfile) 103 projectRoot := h.Path(importertest.RootProject) 104 105 // Capture stderr so we can verify output 106 verboseOutput := &bytes.Buffer{} 107 ctx.Err = log.New(verboseOutput, "", 0) 108 109 g := NewImporter(ctx.Err, false, sm) // Disable verbose so that we don't print values that change each test run 110 if !g.HasDepMetadata(projectRoot) { 111 t.Fatal("Expected the importer to detect the glock configuration files") 112 } 113 114 m, l, err := g.Import(projectRoot, importertest.RootProject) 115 h.Must(err) 116 117 if m == nil { 118 t.Fatal("Expected the manifest to be generated") 119 } 120 121 if l == nil { 122 t.Fatal("Expected the lock to be generated") 123 } 124 125 goldenFile := "golden.txt" 126 got := verboseOutput.String() 127 want := h.GetTestFileString(goldenFile) 128 if want != got { 129 if *test.UpdateGolden { 130 if err := h.WriteTestFile(goldenFile, got); err != nil { 131 t.Fatalf("%+v", errors.Wrapf(err, "Unable to write updated golden file %s", goldenFile)) 132 } 133 } else { 134 t.Fatalf("want %s, got %s", want, got) 135 } 136 } 137 }