|
| 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 main |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/golang/dep" |
| 11 | + "github.com/golang/dep/internal/gps" |
| 12 | +) |
| 13 | + |
| 14 | +type testRootProjectAnalyzer struct { |
| 15 | + *dep.Manifest |
| 16 | + *dep.Lock |
| 17 | +} |
| 18 | + |
| 19 | +func (a testRootProjectAnalyzer) DeriveRootManifestAndLock(path string, n gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) { |
| 20 | + return a.Manifest, a.Lock, nil |
| 21 | +} |
| 22 | + |
| 23 | +func (a testRootProjectAnalyzer) PostSolveShenanigans(*dep.Manifest, *dep.Lock) { |
| 24 | + // do nothing |
| 25 | +} |
| 26 | + |
| 27 | +func TestCompositeAnalyzer_ManifestDependencies(t *testing.T) { |
| 28 | + pkg := gps.ProjectRoot("github.com/sdboyer/deptest") |
| 29 | + m1 := &dep.Manifest{ |
| 30 | + Dependencies: gps.ProjectConstraints{ |
| 31 | + pkg: gps.ProjectProperties{Constraint: gps.NewVersion("^1.0.0")}, |
| 32 | + }, |
| 33 | + } |
| 34 | + m2 := &dep.Manifest{ |
| 35 | + Dependencies: gps.ProjectConstraints{ |
| 36 | + pkg: gps.ProjectProperties{Constraint: gps.NewVersion("^2.0.0")}, |
| 37 | + }, |
| 38 | + } |
| 39 | + c := compositeAnalyzer{ |
| 40 | + Analyzers: []rootProjectAnalyzer{ |
| 41 | + testRootProjectAnalyzer{Manifest: m1}, |
| 42 | + testRootProjectAnalyzer{Manifest: m2}, |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + rm, _, err := c.DeriveRootManifestAndLock("", "") |
| 47 | + if err != nil { |
| 48 | + t.Fatal(err) |
| 49 | + } |
| 50 | + |
| 51 | + if rm == nil { |
| 52 | + t.Fatal("Expected the root manifest to not be nil") |
| 53 | + } |
| 54 | + |
| 55 | + dep, has := rm.Dependencies[pkg] |
| 56 | + if !has { |
| 57 | + t.Fatal("Expected the root manifest to contain the test project") |
| 58 | + } |
| 59 | + |
| 60 | + wantC := "^2.0.0" |
| 61 | + gotC := dep.Constraint.String() |
| 62 | + if wantC != gotC { |
| 63 | + t.Fatalf("Expected the test project to be constrained to '%s', got '%s'", wantC, gotC) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestCompositeAnalyzer_ManifestOverrides(t *testing.T) { |
| 68 | + pkg := gps.ProjectRoot("github.com/sdboyer/deptest") |
| 69 | + m1 := &dep.Manifest{ |
| 70 | + Ovr: gps.ProjectConstraints{ |
| 71 | + pkg: gps.ProjectProperties{Constraint: gps.NewVersion("^1.0.0")}, |
| 72 | + }, |
| 73 | + } |
| 74 | + m2 := &dep.Manifest{ |
| 75 | + Ovr: gps.ProjectConstraints{ |
| 76 | + pkg: gps.ProjectProperties{Constraint: gps.NewVersion("^2.0.0")}, |
| 77 | + }, |
| 78 | + } |
| 79 | + c := compositeAnalyzer{ |
| 80 | + Analyzers: []rootProjectAnalyzer{ |
| 81 | + testRootProjectAnalyzer{Manifest: m1}, |
| 82 | + testRootProjectAnalyzer{Manifest: m2}, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + rm, _, err := c.DeriveRootManifestAndLock("", "") |
| 87 | + if err != nil { |
| 88 | + t.Fatal(err) |
| 89 | + } |
| 90 | + |
| 91 | + if rm == nil { |
| 92 | + t.Fatal("Expected the root manifest to not be nil") |
| 93 | + } |
| 94 | + |
| 95 | + dep, has := rm.Ovr[pkg] |
| 96 | + if !has { |
| 97 | + t.Fatal("Expected the root manifest to contain the test project override") |
| 98 | + } |
| 99 | + |
| 100 | + wantC := "^2.0.0" |
| 101 | + gotC := dep.Constraint.String() |
| 102 | + if wantC != gotC { |
| 103 | + t.Fatalf("Expected the test project to be overridden to '%s', got '%s'", wantC, gotC) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestCompositeAnalyzer_ManifestRequired(t *testing.T) { |
| 108 | + pkg1 := "github.com/sdboyer/deptest" |
| 109 | + pkg2 := "github.com/sdboyer/deptestdos" |
| 110 | + m1 := &dep.Manifest{ |
| 111 | + Required: []string{pkg1}, |
| 112 | + } |
| 113 | + m2 := &dep.Manifest{ |
| 114 | + Required: []string{pkg2}, |
| 115 | + } |
| 116 | + c := compositeAnalyzer{ |
| 117 | + Analyzers: []rootProjectAnalyzer{ |
| 118 | + testRootProjectAnalyzer{Manifest: m1}, |
| 119 | + testRootProjectAnalyzer{Manifest: m2}, |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + rm, _, err := c.DeriveRootManifestAndLock("", "") |
| 124 | + if err != nil { |
| 125 | + t.Fatal(err) |
| 126 | + } |
| 127 | + |
| 128 | + if rm == nil { |
| 129 | + t.Fatal("Expected the root manifest to not be nil") |
| 130 | + } |
| 131 | + |
| 132 | + if len(rm.Required) != 2 { |
| 133 | + t.Fatalf("Expected the root manifest to contain 2 required packages, got %d", len(rm.Required)) |
| 134 | + } |
| 135 | + |
| 136 | + if rm.Required[0] != pkg1 { |
| 137 | + t.Fatalf("Expected the first required package to be '%s', got '%s'", pkg1, rm.Required[0]) |
| 138 | + } |
| 139 | + |
| 140 | + if rm.Required[1] != pkg2 { |
| 141 | + t.Fatalf("Expected the second required package to be '%s', got '%s'", pkg2, rm.Required[1]) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func TestCompositeAnalyzer_ManifestIgnored(t *testing.T) { |
| 146 | + pkg1 := "github.com/sdboyer/deptest" |
| 147 | + pkg2 := "github.com/sdboyer/deptestdos" |
| 148 | + m1 := &dep.Manifest{ |
| 149 | + Ignored: []string{pkg1}, |
| 150 | + } |
| 151 | + m2 := &dep.Manifest{ |
| 152 | + Ignored: []string{pkg2}, |
| 153 | + } |
| 154 | + c := compositeAnalyzer{ |
| 155 | + Analyzers: []rootProjectAnalyzer{ |
| 156 | + testRootProjectAnalyzer{Manifest: m1}, |
| 157 | + testRootProjectAnalyzer{Manifest: m2}, |
| 158 | + }, |
| 159 | + } |
| 160 | + |
| 161 | + rm, _, err := c.DeriveRootManifestAndLock("", "") |
| 162 | + if err != nil { |
| 163 | + t.Fatal(err) |
| 164 | + } |
| 165 | + |
| 166 | + if rm == nil { |
| 167 | + t.Fatal("Expected the root manifest to not be nil") |
| 168 | + } |
| 169 | + |
| 170 | + if len(rm.Ignored) != 2 { |
| 171 | + t.Fatalf("Expected the root manifest to contain 2 ignored packages, got %d", len(rm.Ignored)) |
| 172 | + } |
| 173 | + |
| 174 | + if rm.Ignored[0] != pkg1 { |
| 175 | + t.Fatalf("Expected the first ignored package to be '%s', got '%s'", pkg1, rm.Ignored[0]) |
| 176 | + } |
| 177 | + |
| 178 | + if rm.Ignored[1] != pkg2 { |
| 179 | + t.Fatalf("Expected the second ignored package to be '%s', got '%s'", pkg2, rm.Ignored[1]) |
| 180 | + } |
| 181 | +} |
0 commit comments