|
| 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 main |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "log" |
| 10 | + "path/filepath" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/golang/dep" |
| 14 | + "github.com/golang/dep/internal/gps" |
| 15 | + "github.com/golang/dep/internal/test" |
| 16 | + "github.com/pkg/errors" |
| 17 | +) |
| 18 | + |
| 19 | +func TestGvtConfig_Convert(t *testing.T) { |
| 20 | + testCases := map[string]struct { |
| 21 | + convertTestCase |
| 22 | + gvtConfig gvtManifest |
| 23 | + }{ |
| 24 | + "package without comment": { |
| 25 | + convertTestCase{ |
| 26 | + wantConstraint: importerTestV1Constraint, |
| 27 | + wantRevision: importerTestV1Rev, |
| 28 | + wantVersion: importerTestV1Tag, |
| 29 | + }, |
| 30 | + gvtManifest{ |
| 31 | + Deps: []gvtPkg{ |
| 32 | + { |
| 33 | + ImportPath: importerTestProject, |
| 34 | + Revision: importerTestV1Rev, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + "package with non-master branch": { |
| 40 | + convertTestCase{ |
| 41 | + wantConstraint: importerTestV2Branch, |
| 42 | + wantRevision: importerTestV2PatchRev, |
| 43 | + wantVersion: importerTestV2PatchTag, |
| 44 | + }, |
| 45 | + gvtManifest{ |
| 46 | + Deps: []gvtPkg{ |
| 47 | + { |
| 48 | + ImportPath: importerTestProject, |
| 49 | + Revision: importerTestV2PatchRev, |
| 50 | + Branch: importerTestV2Branch, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + "missing package name": { |
| 56 | + convertTestCase{ |
| 57 | + wantConvertErr: true, |
| 58 | + }, |
| 59 | + gvtManifest{ |
| 60 | + Deps: []gvtPkg{{ImportPath: ""}}, |
| 61 | + }, |
| 62 | + }, |
| 63 | + "missing revision": { |
| 64 | + convertTestCase{ |
| 65 | + wantConvertErr: true, |
| 66 | + }, |
| 67 | + gvtManifest{ |
| 68 | + Deps: []gvtPkg{ |
| 69 | + { |
| 70 | + ImportPath: importerTestProject, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + for name, testCase := range testCases { |
| 78 | + name := name |
| 79 | + testCase := testCase |
| 80 | + t.Run(name, func(t *testing.T) { |
| 81 | + t.Parallel() |
| 82 | + |
| 83 | + err := testCase.Exec(t, func(logger *log.Logger, sm gps.SourceManager) (*dep.Manifest, *dep.Lock, error) { |
| 84 | + g := newGvtImporter(logger, true, sm) |
| 85 | + g.gvtConfig = testCase.gvtConfig |
| 86 | + return g.convert(testProjectRoot) |
| 87 | + }) |
| 88 | + if err != nil { |
| 89 | + t.Fatalf("%#v", err) |
| 90 | + } |
| 91 | + }) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestGvtConfig_Import(t *testing.T) { |
| 96 | + h := test.NewHelper(t) |
| 97 | + defer h.Cleanup() |
| 98 | + |
| 99 | + cacheDir := "gps-repocache" |
| 100 | + h.TempDir(cacheDir) |
| 101 | + h.TempDir("src") |
| 102 | + h.TempDir(filepath.Join("src", testProjectRoot)) |
| 103 | + h.TempCopy(filepath.Join(testProjectRoot, gvtPath), "init/gvt/manifest") |
| 104 | + |
| 105 | + projectRoot := h.Path(testProjectRoot) |
| 106 | + sm, err := gps.NewSourceManager(gps.SourceManagerConfig{ |
| 107 | + Cachedir: h.Path(cacheDir), |
| 108 | + Logger: log.New(test.Writer{t}, "", 0), |
| 109 | + }) |
| 110 | + h.Must(err) |
| 111 | + defer sm.Release() |
| 112 | + |
| 113 | + // Capture stderr so we can verify output |
| 114 | + verboseOutput := &bytes.Buffer{} |
| 115 | + logger := log.New(verboseOutput, "", 0) |
| 116 | + |
| 117 | + g := newGvtImporter(logger, false, sm) // Disable verbose so that we don't print values that change each test run |
| 118 | + if !g.HasDepMetadata(projectRoot) { |
| 119 | + t.Fatal("Expected the importer to detect gvt configuration file") |
| 120 | + } |
| 121 | + |
| 122 | + m, l, err := g.Import(projectRoot, testProjectRoot) |
| 123 | + h.Must(err) |
| 124 | + |
| 125 | + if m == nil { |
| 126 | + t.Fatal("Expected the manifest to be generated") |
| 127 | + } |
| 128 | + |
| 129 | + if l == nil { |
| 130 | + t.Fatal("Expected the lock to be generated") |
| 131 | + } |
| 132 | + |
| 133 | + goldenFile := "init/gvt/golden.txt" |
| 134 | + got := verboseOutput.String() |
| 135 | + want := h.GetTestFileString(goldenFile) |
| 136 | + if want != got { |
| 137 | + if *test.UpdateGolden { |
| 138 | + if err := h.WriteTestFile(goldenFile, got); err != nil { |
| 139 | + t.Fatalf("%+v", errors.Wrapf(err, "Unable to write updated golden file %s", goldenFile)) |
| 140 | + } |
| 141 | + } else { |
| 142 | + t.Fatalf("want %s, got %s", want, got) |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func TestGvtConfig_JsonLoad(t *testing.T) { |
| 148 | + // This is same as cmd/dep/testdata/init/gvt/manifest |
| 149 | + wantConfig := gvtManifest{ |
| 150 | + Deps: []gvtPkg{ |
| 151 | + { |
| 152 | + ImportPath: "github.com/sdboyer/deptest", |
| 153 | + Revision: "3f4c3bea144e112a69bbe5d8d01c1b09a544253f", |
| 154 | + }, |
| 155 | + { |
| 156 | + ImportPath: "github.com/sdboyer/deptestdos", |
| 157 | + Revision: "5c607206be5decd28e6263ffffdcee067266015e", |
| 158 | + }, |
| 159 | + { |
| 160 | + ImportPath: "github.com/carolynvs/deptest-importers", |
| 161 | + Revision: "b79bc9482da8bb7402cdc3e3fd984db250718dd7", |
| 162 | + Branch: "v2", |
| 163 | + }, |
| 164 | + }, |
| 165 | + } |
| 166 | + |
| 167 | + h := test.NewHelper(t) |
| 168 | + defer h.Cleanup() |
| 169 | + |
| 170 | + ctx := newTestContext(h) |
| 171 | + |
| 172 | + h.TempCopy(filepath.Join(testProjectRoot, gvtPath), "init/gvt/manifest") |
| 173 | + |
| 174 | + projectRoot := h.Path(testProjectRoot) |
| 175 | + |
| 176 | + g := newGvtImporter(ctx.Err, true, nil) |
| 177 | + err := g.load(projectRoot) |
| 178 | + if err != nil { |
| 179 | + t.Fatalf("Error while loading... %v", err) |
| 180 | + } |
| 181 | + |
| 182 | + if !gvtEqualImports(g.gvtConfig.Deps, wantConfig.Deps) { |
| 183 | + t.Fatalf("Expected imports to be equal. \n\t(GOT): %v\n\t(WNT): %v", g.gvtConfig.Deps, wantConfig.Deps) |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +// gvtEqualImports compares two slices of gvtPkg and checks if they are |
| 188 | +// equal. |
| 189 | +func gvtEqualImports(a, b []gvtPkg) bool { |
| 190 | + if a == nil && b == nil { |
| 191 | + return true |
| 192 | + } |
| 193 | + |
| 194 | + if a == nil || b == nil { |
| 195 | + return false |
| 196 | + } |
| 197 | + |
| 198 | + if len(a) != len(b) { |
| 199 | + return false |
| 200 | + } |
| 201 | + |
| 202 | + for i := range a { |
| 203 | + if a[i] != b[i] { |
| 204 | + return false |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + return true |
| 209 | +} |
0 commit comments