-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
134 lines (111 loc) · 3.53 KB
/
main_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
package main_test
import (
"strings"
"testing"
"github.com/alecthomas/kong"
kongyaml "github.com/alecthomas/kong-yaml"
"github.com/dskiff/tko/pkg/cmd"
"gotest.tools/v3/assert"
)
func TestYamlBasic(t *testing.T) {
yaml := `
build:
base-ref: base-image@sha256:1234
platform: custom-os/arch-variant
entrypoint: /entrypoint
destination-path: /destination
destination-chown: false
target-repo: repo/target
author: me
default-annotations:
label1: value1
label2: value2
annotations:
label3: value3
label4: value4
env:
VAR1: value1
VAR2: value2
tmp: /tmp-dir
verbose: true
`
r, err := kongyaml.Loader(strings.NewReader(yaml))
assert.NilError(t, err)
cli := cmd.CLI{}
parser := mustNew(t, &cli, kong.Resolvers(r))
_, err = parser.Parse([]string{"build", "/source"})
assert.NilError(t, err)
assert.Equal(t, "base-image@sha256:1234", cli.Build.BaseRef)
assert.Equal(t, "custom-os/arch-variant", cli.Build.Platform)
assert.Equal(t, "/entrypoint", cli.Build.Entrypoint)
assert.Equal(t, "/destination", cli.Build.DestinationPath)
assert.Equal(t, false, cli.Build.DestinationChown)
assert.Equal(t, "repo/target", cli.Build.TargetRepo)
assert.Equal(t, "me", cli.Build.Author)
assert.Equal(t, "value1", cli.Build.DefaultAnnotations["label1"])
assert.Equal(t, "value2", cli.Build.DefaultAnnotations["label2"])
assert.Equal(t, "value3", cli.Build.Annotations["label3"])
assert.Equal(t, "value4", cli.Build.Annotations["label4"])
assert.Equal(t, "value1", cli.Build.Env["VAR1"])
assert.Equal(t, "value2", cli.Build.Env["VAR2"])
assert.Equal(t, "/tmp-dir", cli.Build.Tmp)
assert.Equal(t, true, cli.Build.Verbose)
}
func TestVersionArgs(t *testing.T) {
cli := cmd.CLI{}
parser := mustNew(t, &cli)
args, err := parser.Parse([]string{"version"})
assert.NilError(t, err)
assert.Equal(t, true, args.Command() == "version")
}
func TestBuildArgs(t *testing.T) {
cli := cmd.CLI{}
parser := mustNew(t, &cli)
_, err := parser.Parse([]string{"build", "/source",
"-b", "base-image@sha256:1234",
"-p", "custom-os/arch-variant",
"--entrypoint", "/entrypoint",
"-d", "/destination",
"--destination-chown=false",
"-t", "repo/target",
"--author", "me",
"-A", "label1=value1",
"-A", "label2=value2",
"-a", "label3=value3",
"-a", "label4=value4",
"-e", "VAR1=value1",
"-e", "VAR2=value2",
"-T", "REMOTE",
"-v",
"--tmp", "/tmp-dir",
})
assert.NilError(t, err)
assert.Equal(t, "base-image@sha256:1234", cli.Build.BaseRef)
assert.Equal(t, "custom-os/arch-variant", cli.Build.Platform)
assert.Equal(t, "/entrypoint", cli.Build.Entrypoint)
assert.Equal(t, "/destination", cli.Build.DestinationPath)
assert.Equal(t, false, cli.Build.DestinationChown)
assert.Equal(t, "repo/target", cli.Build.TargetRepo)
assert.Equal(t, "me", cli.Build.Author)
assert.Equal(t, "value1", cli.Build.DefaultAnnotations["label1"])
assert.Equal(t, "value2", cli.Build.DefaultAnnotations["label2"])
assert.Equal(t, "value3", cli.Build.Annotations["label3"])
assert.Equal(t, "value4", cli.Build.Annotations["label4"])
assert.Equal(t, "value1", cli.Build.Env["VAR1"])
assert.Equal(t, "value2", cli.Build.Env["VAR2"])
assert.Equal(t, "/tmp-dir", cli.Build.Tmp)
assert.Equal(t, true, cli.Build.Verbose)
}
func mustNew(t *testing.T, cli interface{}, options ...kong.Option) *kong.Kong {
t.Helper()
options = append([]kong.Option{
kong.Name("test"),
kong.Exit(func(int) {
t.Helper()
t.Fatalf("unexpected exit()")
}),
}, options...)
parser, err := kong.New(cli, options...)
assert.NilError(t, err)
return parser
}