forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocatortype_test.go
138 lines (127 loc) · 3.81 KB
/
locatortype_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
127
128
129
130
131
132
133
134
135
136
137
138
package buildpack_test
import (
"fmt"
"path/filepath"
"testing"
"github.com/heroku/color"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
"github.com/buildpacks/pack/internal/buildpack"
"github.com/buildpacks/pack/internal/dist"
h "github.com/buildpacks/pack/testhelpers"
)
func TestGetLocatorType(t *testing.T) {
color.Disable(true)
defer color.Disable(false)
spec.Run(t, "testGetLocatorType", testGetLocatorType, spec.Parallel(), spec.Report(report.Terminal{}))
}
func testGetLocatorType(t *testing.T, when spec.G, it spec.S) {
type testCase struct {
locator string
builderBPs []dist.BuildpackInfo
expectedType buildpack.LocatorType
expectedErr string
localPath string
}
var localPath = func(path string) string {
return filepath.Join("testdata", path)
}
for _, tc := range []testCase{
{
locator: "from=builder",
expectedType: buildpack.FromBuilderLocator,
},
{
locator: "from=builder:some-bp",
builderBPs: []dist.BuildpackInfo{{ID: "some-bp", Version: "some-version"}},
expectedType: buildpack.IDLocator,
},
{
locator: "from=builder:some-bp",
builderBPs: nil,
expectedErr: "'from=builder:some-bp' is not a valid identifier",
},
{
locator: "from=builder:some-bp@some-other-version",
builderBPs: []dist.BuildpackInfo{{ID: "some-bp", Version: "some-version"}},
expectedErr: "'from=builder:some-bp@some-other-version' is not a valid identifier",
},
{
locator: "some-bp",
builderBPs: []dist.BuildpackInfo{{ID: "some-bp", Version: "any-version"}},
expectedType: buildpack.IDLocator,
},
{
locator: localPath("some-bp"),
builderBPs: []dist.BuildpackInfo{{ID: localPath("some-bp"), Version: "some-version"}},
localPath: localPath("some-bp"),
expectedType: buildpack.URILocator,
},
{
locator: "https://example.com/buildpack.tgz",
expectedType: buildpack.URILocator,
},
{
locator: "cnbs/some-bp",
builderBPs: nil,
localPath: "",
expectedType: buildpack.PackageLocator,
},
{
locator: "cnbs/some-bp@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
expectedType: buildpack.PackageLocator,
},
{
locator: "cnbs/some-bp:some-tag",
expectedType: buildpack.PackageLocator,
},
{
locator: "cnbs/some-bp:some-tag@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
expectedType: buildpack.PackageLocator,
},
{
locator: "registry.com/cnbs/some-bp",
expectedType: buildpack.PackageLocator,
},
{
locator: "registry.com/cnbs/some-bp@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
expectedType: buildpack.PackageLocator,
},
{
locator: "registry.com/cnbs/some-bp:some-tag",
expectedType: buildpack.PackageLocator,
},
{
locator: "registry.com/cnbs/some-bp:some-tag@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
expectedType: buildpack.PackageLocator,
},
{
locator: "urn:cnb:registry:example/foo:1.0.0",
expectedType: buildpack.RegistryLocator,
},
} {
tc := tc
desc := fmt.Sprintf("locator is %s", tc.locator)
if len(tc.builderBPs) > 0 {
var names []string
for _, bp := range tc.builderBPs {
names = append(names, bp.FullName())
}
desc += fmt.Sprintf(" and builder has buildpacks %s", names)
}
if tc.localPath != "" {
desc += fmt.Sprintf(" and a local path exists at '%s'", tc.localPath)
}
when(desc, func() {
it(fmt.Sprintf("should return '%s'", tc.expectedType), func() {
actualType, actualErr := buildpack.GetLocatorType(tc.locator, tc.builderBPs)
if tc.expectedErr == "" {
h.AssertNil(t, actualErr)
} else {
h.AssertError(t, actualErr, tc.expectedErr)
}
h.AssertEq(t, actualType, tc.expectedType)
})
})
}
}