-
Notifications
You must be signed in to change notification settings - Fork 1
/
dependencies_test.go
114 lines (92 loc) · 2.49 KB
/
dependencies_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"reflect"
"testing"
"github.com/hashicorp/go-version"
)
func TestGraph_Solve(t *testing.T) {
oldPkgDir := pkgDir
defer func() {
pkgDir = oldPkgDir
}()
pkgDir = "testdata/manifests/valid-manifests"
d, err := LoadDB()
if err != nil {
t.Fatalf("unexpected error: %+v", err)
}
outputChan := make(chan string)
defer close(outputChan)
go func() {
for _ = range outputChan {
}
}()
g := NewGraph(&d, &StateDB{}, outputChan)
for _, test := range []struct {
name string
pkg string
version string
expectTaskIDs []string
expectError bool
}{
{"installing non-existent package", "non-such", "= 1.0.0", []string{}, true},
{"installing non-existent version", "app-utils", ">3", []string{}, true},
{"happy path, with re-calculate logic", "sample-app", "1.0.0", []string{"app-utils 1.0.3", "some-security-library 1.8.9", "user-lib 1.5.0", "sample-app 1.0.0"}, false},
{"happy path, with re-calculate logic, 'latest'", "sample-app", "", []string{"app-utils 1.0.3", "some-security-library 1.8.9", "user-lib 1.5.0", "sample-app 1.0.0"}, false},
} {
t.Run(test.name, func(t *testing.T) {
var (
constraint version.Constraints
err error
)
if test.version == "" {
constraint = nil
} else {
constraint, err = version.NewConstraint(test.version)
if err != nil {
t.Fatalf("unexpected error: %+v", err)
}
}
_, err = g.Solve("default", test.pkg, constraint)
if !test.expectError && err != nil {
t.Errorf("unexpected error: %+v", err)
}
if test.expectError && err == nil {
t.Errorf("expected error, received none")
}
tasks := make([]string, len(g.tasks))
for idx, m := range g.tasks {
tasks[idx] = m.ID
}
if !reflect.DeepEqual(test.expectTaskIDs, tasks) {
t.Errorf("expected %#v, received %#v", test.expectTaskIDs, tasks)
}
})
}
}
func TestGraph_Solve_Circular(t *testing.T) {
oldPkgDir := pkgDir
defer func() {
pkgDir = oldPkgDir
}()
pkgDir = "testdata/manifests/circular"
d, err := LoadDB()
if err != nil {
t.Fatalf("unexpected error: %+v", err)
}
outputChan := make(chan string)
defer close(outputChan)
go func() {
for _ = range outputChan {
}
}()
g := NewGraph(&d, &StateDB{}, outputChan)
_, err = g.Solve("default", "app-1", nil)
if err == nil {
t.Fatalf("expected error")
}
errStr := err.Error()
expect := `circular dependency: "app-3" -> "app-1"`
if expect != errStr {
t.Fatalf("expected %q, received %q", expect, errStr)
}
}