This repository has been archived by the owner on May 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
bunchfile_test.go
126 lines (93 loc) · 4.64 KB
/
bunchfile_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
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestFilterCommonBasePackages(t *testing.T) {
depList := []string{"github.com/a/b/c", "github.com/d/e/f", "github.com/a/b"}
result := filterCommonBasePackages(depList, "github.com/my/package")
expectedResult := []string{"github.com/d/e/f", "github.com/a/b"}
assert.Equal(t, result, expectedResult, "packages with a common base should be filtered out")
}
func TestCreateBunchfile(t *testing.T) {
assert.Equal(t, &BunchFile{}, createBunchfile(), "create bunchfile returns a bunchfile object")
}
func TestRawIndex(t *testing.T) {
bunch := createBunchfile()
bunch.Raw = []string{"github.com/a/b", "github.com/a/b/c", "github.com/d 123456"}
rawIndex, present := bunch.RawIndex("github.com/a/b")
assert.Equal(t, rawIndex, 0, "found github.com/a/b at first position in bunchfile")
assert.Equal(t, present, true, "found github.com/a/b at first position in bunchfile")
rawIndex, present = bunch.RawIndex("github.com/d")
assert.Equal(t, rawIndex, 2, "found github.com/d at third position in bunchfile")
assert.Equal(t, present, true, "found github.com/a/b at first position in bunchfile")
rawIndex, present = bunch.RawIndex("github.com/a/b/c")
assert.Equal(t, rawIndex, 1, "found github.com/a/b/c at second position in bunchfile")
assert.Equal(t, present, true, "found github.com/a/b/c in bunchfile")
_, present = bunch.RawIndex("github.com/unknown")
assert.Equal(t, present, false, "did not find github.com/unknown in bunchfile")
}
func TestPackageIndex(t *testing.T) {
bunch := createBunchfile()
bunch.Packages = []Package{
Package{
Repo: "github.com/a/b",
Version: "123",
},
Package{
Repo: "github.com/def",
},
Package{
Repo: "github.com/a/b/c",
Version: "xyz",
},
}
packageIndex, present := bunch.PackageIndex("github.com/a/b")
assert.Equal(t, packageIndex, 0, "found github.com/a/b in first position in bunchfile package list")
assert.Equal(t, present, true, "found github.com/a/b in bunchfile package list")
packageIndex, present = bunch.PackageIndex("github.com/def")
assert.Equal(t, packageIndex, 1, "found github.com/def at second position in bunchfile package list")
assert.Equal(t, present, true, "found github.com/def in bunchfile package list")
packageIndex, present = bunch.PackageIndex("github.com/a/b/c")
assert.Equal(t, packageIndex, 2, "found github.com/a/b/c at third position in bunchfile package list")
assert.Equal(t, present, true, "found github.com/a/b/c in bunchfile package list")
_, present = bunch.PackageIndex("github.com/unknown")
assert.Equal(t, present, false, "did not find github.com/unknown in bunchfile package list")
}
func TestAddPackageNew(t *testing.T) {
bunch := createBunchfile()
err := bunch.AddPackage("github.com/a/b/c")
assert.Nil(t, err, "did not error on adding package")
packageIndex, present := bunch.PackageIndex("github.com/a/b/c")
assert.Equal(t, packageIndex, 0, "found github.com/a/b/c in first position in bunchfile package list")
assert.Equal(t, present, true, "found github.com/a/b/c in bunchfile package list")
err = bunch.AddPackage("github.com/a/b/d@v1.2.0")
assert.Nil(t, err, "did not error on adding package")
packageIndex, present = bunch.PackageIndex("github.com/a/b/d")
assert.Equal(t, packageIndex, 1, "found github.com/a/b/d in second position in bunchfile package list")
assert.Equal(t, present, true, "found github.com/a/b/d in bunchfile package list")
}
func TestAddPackageUpdate(t *testing.T) {
bunch := createBunchfile()
err := bunch.AddPackage("github.com/a/b/c")
assert.Nil(t, err, "did not error on adding package")
pack := bunch.Packages[0]
assert.Equal(t, len(bunch.Packages), 1, "should only be one package in bunchfile")
assert.Equal(t, "", pack.Version, "version should not be set yet")
err = bunch.AddPackage("github.com/a/b/c@v1.2.1")
assert.Nil(t, err, "did not error on adding package")
pack = bunch.Packages[0]
assert.Equal(t, len(bunch.Packages), 1, "should still be only one package in bunchfile")
assert.Equal(t, pack.Version, "v1.2.1", "version should now be set")
}
func TestRemovePackage(t *testing.T) {
bunch := createBunchfile()
err := bunch.AddPackage("github.com/a/b/c")
assert.Nil(t, err, "did not error on adding package")
assert.Equal(t, len(bunch.Raw), 1, "should be one packages in Bunchfile")
assert.Equal(t, len(bunch.Packages), 1, "should be one package in bunchfile package list")
err = bunch.RemovePackage("github.com/a/b/c")
assert.Nil(t, err, "did not error on removing package")
assert.Equal(t, len(bunch.Raw), 0, "should be no packages in Bunchfile")
assert.Equal(t, len(bunch.Packages), 0, "should be no packages in Bunchfile package list")
}