|
| 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/internal/gps" |
| 14 | + "github.com/golang/dep/internal/test" |
| 15 | + "github.com/pkg/errors" |
| 16 | +) |
| 17 | + |
| 18 | +const testGbProjectRoot = "github.com/golang/notexist" |
| 19 | + |
| 20 | +func TestGbConfig_Import(t *testing.T) { |
| 21 | + h := test.NewHelper(t) |
| 22 | + defer h.Cleanup() |
| 23 | + |
| 24 | + ctx := newTestContext(h) |
| 25 | + sm, err := ctx.SourceManager() |
| 26 | + h.Must(err) |
| 27 | + defer sm.Release() |
| 28 | + |
| 29 | + h.TempDir(filepath.Join("src", testGbProjectRoot, "vendor")) |
| 30 | + h.TempCopy(filepath.Join(testGbProjectRoot, "vendor", "manifest"), "gb/manifest") |
| 31 | + projectRoot := h.Path(testGbProjectRoot) |
| 32 | + |
| 33 | + // Capture stderr so we can verify output |
| 34 | + verboseOutput := &bytes.Buffer{} |
| 35 | + ctx.Err = log.New(verboseOutput, "", 0) |
| 36 | + |
| 37 | + g := newGbImporter(ctx.Err, false, sm) // Disable verbose so that we don't print values that change each test run |
| 38 | + if !g.HasDepMetadata(projectRoot) { |
| 39 | + t.Fatal("Expected the importer to detect the gb manifest file") |
| 40 | + } |
| 41 | + |
| 42 | + m, l, err := g.Import(projectRoot, testGbProjectRoot) |
| 43 | + h.Must(err) |
| 44 | + |
| 45 | + if m == nil { |
| 46 | + t.Fatal("Expected the manifest to be generated") |
| 47 | + } |
| 48 | + |
| 49 | + if l == nil { |
| 50 | + t.Fatal("Expected the lock to be generated") |
| 51 | + } |
| 52 | + |
| 53 | + goldenFile := "gb/golden.txt" |
| 54 | + got := verboseOutput.String() |
| 55 | + want := h.GetTestFileString(goldenFile) |
| 56 | + if want != got { |
| 57 | + if *test.UpdateGolden { |
| 58 | + if err := h.WriteTestFile(goldenFile, got); err != nil { |
| 59 | + t.Fatalf("%+v", errors.Wrapf(err, "Unable to write updated golden file %s", goldenFile)) |
| 60 | + } |
| 61 | + } else { |
| 62 | + t.Fatalf("expected %s, got %s", want, got) |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestGbConfig_Convert_Project(t *testing.T) { |
| 68 | + h := test.NewHelper(t) |
| 69 | + defer h.Cleanup() |
| 70 | + |
| 71 | + ctx := newTestContext(h) |
| 72 | + sm, err := ctx.SourceManager() |
| 73 | + h.Must(err) |
| 74 | + defer sm.Release() |
| 75 | + |
| 76 | + pkg := "github.com/sdboyer/deptest" |
| 77 | + repo := "https://github.com/sdboyer/deptest.git" |
| 78 | + |
| 79 | + g := newGbImporter(ctx.Err, true, sm) |
| 80 | + g.manifest = gbManifest{ |
| 81 | + Dependencies: []gbDependency{ |
| 82 | + { |
| 83 | + Importpath: pkg, |
| 84 | + Repository: repo, |
| 85 | + Revision: "ff2948a2ac8f538c4ecd55962e919d1e13e74baf", |
| 86 | + }, |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + manifest, lock, err := g.convert(testGbProjectRoot) |
| 91 | + if err != nil { |
| 92 | + t.Fatal(err) |
| 93 | + } |
| 94 | + |
| 95 | + d, ok := manifest.Constraints[gps.ProjectRoot(pkg)] |
| 96 | + if !ok { |
| 97 | + t.Fatal("Expected the manifest to have a dependency for 'github.com/sdboyer/deptest' but got none") |
| 98 | + } |
| 99 | + |
| 100 | + wantC := "^1.0.0" |
| 101 | + gotC := d.Constraint.String() |
| 102 | + if gotC != wantC { |
| 103 | + t.Fatalf("Expected manifest constraint to be %s, got %s", wantC, gotC) |
| 104 | + } |
| 105 | + |
| 106 | + gotS := d.Source |
| 107 | + if gotS != repo { |
| 108 | + t.Fatalf("Expected manifest source to be %s, got %s", repo, gotS) |
| 109 | + } |
| 110 | + |
| 111 | + wantP := 1 |
| 112 | + gotP := len(lock.P) |
| 113 | + if gotP != 1 { |
| 114 | + t.Fatalf("Expected the lock to contain %d project but got %d", wantP, gotP) |
| 115 | + } |
| 116 | + |
| 117 | + p := lock.P[0] |
| 118 | + gotPr := string(p.Ident().ProjectRoot) |
| 119 | + if gotPr != pkg { |
| 120 | + t.Fatalf("Expected the lock to have a project for %s but got '%s'", pkg, gotPr) |
| 121 | + } |
| 122 | + |
| 123 | + gotS = p.Ident().Source |
| 124 | + if gotS != repo { |
| 125 | + t.Fatalf("Expected locked source to be %s, got '%s'", repo, gotS) |
| 126 | + } |
| 127 | + |
| 128 | + lv := p.Version() |
| 129 | + lpv, ok := lv.(gps.PairedVersion) |
| 130 | + if !ok { |
| 131 | + t.Fatalf("Expected locked version to be a PairedVersion but got %T", lv) |
| 132 | + } |
| 133 | + |
| 134 | + wantRev := "ff2948a2ac8f538c4ecd55962e919d1e13e74baf" |
| 135 | + gotRev := lpv.Revision().String() |
| 136 | + if gotRev != wantRev { |
| 137 | + t.Fatalf("Expected locked revision to be %s, got %s", wantRev, gotRev) |
| 138 | + } |
| 139 | + |
| 140 | + wantV := "v1.0.0" |
| 141 | + gotV := lpv.String() |
| 142 | + if gotV != wantV { |
| 143 | + t.Fatalf("Expected locked version to be %s, got %s", wantV, gotV) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func TestGbConfig_Convert_BadInput_EmptyPackageName(t *testing.T) { |
| 148 | + h := test.NewHelper(t) |
| 149 | + defer h.Cleanup() |
| 150 | + |
| 151 | + ctx := newTestContext(h) |
| 152 | + sm, err := ctx.SourceManager() |
| 153 | + h.Must(err) |
| 154 | + defer sm.Release() |
| 155 | + |
| 156 | + g := newGbImporter(ctx.Err, true, sm) |
| 157 | + g.manifest = gbManifest{ |
| 158 | + Dependencies: []gbDependency{{Importpath: ""}}, |
| 159 | + } |
| 160 | + |
| 161 | + _, _, err = g.convert(testGbProjectRoot) |
| 162 | + if err == nil { |
| 163 | + t.Fatal("Expected conversion to fail because the package name is empty") |
| 164 | + } |
| 165 | +} |
0 commit comments