|
| 1 | +package trace |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + depgraph "github.com/openshift/origin/tools/depcheck/pkg/graph" |
| 7 | +) |
| 8 | + |
| 9 | +var pkgs = &PackageList{ |
| 10 | + Packages: []Package{ |
| 11 | + { |
| 12 | + Dir: "/path/to/github.com/test/repo/root", |
| 13 | + ImportPath: "github.com/test/repo/root", |
| 14 | + Imports: []string{ |
| 15 | + "github.com/test/repo/pkg/one", |
| 16 | + }, |
| 17 | + }, |
| 18 | + { |
| 19 | + Dir: "/path/to/github.com/test/repo/pkg/one", |
| 20 | + ImportPath: "github.com/test/repo/pkg/one", |
| 21 | + Imports: []string{ |
| 22 | + "github.com/test/repo/pkg/two", |
| 23 | + "github.com/test/repo/pkg/three", |
| 24 | + "github.com/test/repo/pkg/depends_on_fmt", |
| 25 | + }, |
| 26 | + }, |
| 27 | + { |
| 28 | + Dir: "/path/to/github.com/test/repo/pkg/two", |
| 29 | + ImportPath: "github.com/test/repo/pkg/two", |
| 30 | + Imports: []string{ |
| 31 | + "github.com/test/repo/vendor/github.com/testvendor/vendor_one", |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + Dir: "/path/to/github.com/test/repo/pkg/three", |
| 36 | + ImportPath: "github.com/test/repo/pkg/three", |
| 37 | + Imports: []string{ |
| 38 | + "github.com/test/repo/shared/shared_one", |
| 39 | + }, |
| 40 | + }, |
| 41 | + { |
| 42 | + Dir: "/path/to/github.com/test/repo/pkg/depends_on_fmt", |
| 43 | + ImportPath: "github.com/test/repo/pkg/depends_on_fmt", |
| 44 | + Imports: []string{ |
| 45 | + "fmt", |
| 46 | + "github.com/test/repo/unique/unique_nonvendored_one", |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + Dir: "/path/to/github.com/test/repo/unique/unique_nonvendored_one", |
| 51 | + ImportPath: "github.com/test/repo/unique/unique_nonvendored_one", |
| 52 | + Imports: []string{}, |
| 53 | + }, |
| 54 | + { |
| 55 | + Dir: "/path/to/github.com/test/repo/shared/shared_one", |
| 56 | + ImportPath: "github.com/test/repo/shared/shared_one", |
| 57 | + Imports: []string{}, |
| 58 | + }, |
| 59 | + { |
| 60 | + Dir: "/path/to/github.com/test/repo/vendor/github.com/testvendor/vendor_one", |
| 61 | + ImportPath: "github.com/test/repo/vendor/github.com/testvendor/vendor_one", |
| 62 | + Imports: []string{ |
| 63 | + "github.com/test/repo/unique/unique_vendor_one", |
| 64 | + "github.com/test/repo/shared/shared_one", |
| 65 | + }, |
| 66 | + }, |
| 67 | + { |
| 68 | + Dir: "/path/to/github.com/test/repo/unique/unique_vendor_one", |
| 69 | + ImportPath: "github.com/test/repo/unique/unique_vendor_one", |
| 70 | + Imports: []string{}, |
| 71 | + }, |
| 72 | + }, |
| 73 | +} |
| 74 | + |
| 75 | +func TestBuildGraphCreatesExpectedNodesAndEdges(t *testing.T) { |
| 76 | + invalidImports := map[string]bool{ |
| 77 | + "fmt": true, |
| 78 | + } |
| 79 | + |
| 80 | + g, err := BuildGraph(pkgs, []string{}) |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("unexpected error: %v", err) |
| 83 | + } |
| 84 | + |
| 85 | + if len(g.Nodes()) != len(pkgs.Packages) { |
| 86 | + t.Fatalf("node count mismatch. Expected %v nodes but got %v.", len(pkgs.Packages), len(g.Nodes())) |
| 87 | + } |
| 88 | + |
| 89 | + for _, pkg := range pkgs.Packages { |
| 90 | + from, exists := g.NodeByName(pkg.ImportPath) |
| 91 | + if !exists || !g.Has(from) { |
| 92 | + t.Fatalf("expected node with name to exist for given package with ImportPath %q", pkg.ImportPath) |
| 93 | + } |
| 94 | + |
| 95 | + for _, dep := range pkg.Imports { |
| 96 | + if _, skip := invalidImports[dep]; skip { |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + to, exists := g.NodeByName(dep) |
| 101 | + if !exists || !g.Has(to) { |
| 102 | + t.Fatalf("expected node with name ") |
| 103 | + } |
| 104 | + |
| 105 | + if !g.HasEdgeFromTo(from, to) { |
| 106 | + t.Fatalf("expected edge to exist between nodes %v and %v", from, to) |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestBuildGraphExcludesNodes(t *testing.T) { |
| 113 | + excludes := []string{ |
| 114 | + "github.com/test/repo/pkg/three", |
| 115 | + "github.com/test/repo/pkg/depends_on_fmt", |
| 116 | + } |
| 117 | + |
| 118 | + g, err := BuildGraph(pkgs, excludes) |
| 119 | + if err != nil { |
| 120 | + t.Fatalf("unexpected error: %v", err) |
| 121 | + } |
| 122 | + |
| 123 | + for _, n := range g.Nodes() { |
| 124 | + node, ok := n.(*depgraph.Node) |
| 125 | + if !ok { |
| 126 | + t.Fatalf("expected node to be of type *depgraph.Node") |
| 127 | + } |
| 128 | + |
| 129 | + for _, exclude := range excludes { |
| 130 | + if node.UniqueName == exclude { |
| 131 | + t.Fatalf("expected node with unique name %q to have been excluded from the graph", node.UniqueName) |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | +} |
| 137 | + |
| 138 | +func TestPackagesWithInvalidPathsAreOmitted(t *testing.T) { |
| 139 | + pkgList := &PackageList{ |
| 140 | + Packages: []Package{ |
| 141 | + { |
| 142 | + Dir: "/path/to/github.com/test/repo/invalid", |
| 143 | + ImportPath: "invalid/import/path1", |
| 144 | + Imports: []string{ |
| 145 | + "fmt", |
| 146 | + "invalid.import.path2", |
| 147 | + "invalid.import.path3", |
| 148 | + }, |
| 149 | + }, |
| 150 | + { |
| 151 | + Dir: "/path/to/github.com/test/repo/invalid", |
| 152 | + ImportPath: "invalid.import.path2", |
| 153 | + Imports: []string{ |
| 154 | + "net", |
| 155 | + "encoding/json", |
| 156 | + }, |
| 157 | + }, |
| 158 | + { |
| 159 | + Dir: "/path/to/github.com/test/repo/invalid", |
| 160 | + ImportPath: "invalid3", |
| 161 | + }, |
| 162 | + }, |
| 163 | + } |
| 164 | + |
| 165 | + g, err := BuildGraph(pkgList, []string{}) |
| 166 | + if err != nil { |
| 167 | + t.Fatalf("unexpected error: %v", err) |
| 168 | + } |
| 169 | + |
| 170 | + if len(g.Nodes()) != 0 { |
| 171 | + t.Fatalf("expected no nodes to have been created for an invalid package list. Saw %v unexpected nodes.", len(g.Nodes())) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func TestLabelNamesForVendoredNodes(t *testing.T) { |
| 176 | + pkgList := &PackageList{ |
| 177 | + Packages: []Package{ |
| 178 | + { |
| 179 | + Dir: "/path/to/github.com/test/repo/vendor/github.com/testvendor/vendor_one", |
| 180 | + ImportPath: "github.com/test/repo/vendor/github.com/testvendor/vendor_one", |
| 181 | + }, |
| 182 | + }, |
| 183 | + } |
| 184 | + |
| 185 | + expectedLabelName := "github.com/testvendor/vendor_one" |
| 186 | + |
| 187 | + g, err := BuildGraph(pkgList, []string{}) |
| 188 | + if err != nil { |
| 189 | + t.Fatalf("unexpected error: %v", err) |
| 190 | + } |
| 191 | + |
| 192 | + if len(g.Nodes()) != 1 { |
| 193 | + t.Fatalf("expected graph of size 1, but got graph with %v nodes", len(g.Nodes())) |
| 194 | + } |
| 195 | + |
| 196 | + node, ok := g.Nodes()[0].(*depgraph.Node) |
| 197 | + if !ok { |
| 198 | + t.Fatalf("expected node %v to be of type *depgraph.Node", node) |
| 199 | + } |
| 200 | + |
| 201 | + if node.LabelName != expectedLabelName { |
| 202 | + t.Fatalf("expected node label name to be %q but was %q", expectedLabelName, node.LabelName) |
| 203 | + } |
| 204 | +} |
0 commit comments