-
Notifications
You must be signed in to change notification settings - Fork 12
/
cli_test.go
173 lines (162 loc) · 4.23 KB
/
cli_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package goxz
import (
"io/ioutil"
"os"
"reflect"
"sort"
"strings"
"testing"
)
func setup(t *testing.T) string {
tmpd, err := ioutil.TempDir("", "goxz-")
if err != nil {
t.Fatal(err)
}
return tmpd
}
func TestCliRun(t *testing.T) {
testCases := []struct {
name string
input []string
files []string
errStr string
}{
{
name: "simple",
input: []string{"./testdata/hello"},
files: []string{
"goxz_darwin_amd64.zip",
"goxz_darwin_arm64.zip",
"goxz_linux_amd64.tar.gz",
"goxz_linux_arm64.tar.gz",
"goxz_windows_amd64.zip",
"goxz_windows_arm64.zip",
},
},
{
name: "zip always and specify multi arch",
input: []string{"-z", "-os=freebsd,linux", "-arch=386 amd64", "./testdata/hello"},
files: []string{
"goxz_freebsd_amd64.zip",
"goxz_freebsd_386.zip",
"goxz_linux_amd64.zip",
"goxz_linux_386.zip",
},
},
{
name: "zip always, static and specify multi os",
input: []string{"-z", "-static", "-os=darwin,linux,freebsd,windows", "./testdata/hello"},
files: []string{
"goxz_darwin_amd64.zip",
"goxz_darwin_arm64.zip",
"goxz_linux_amd64.zip",
"goxz_linux_arm64.zip",
"goxz_windows_amd64.zip",
"goxz_windows_arm64.zip",
"goxz_freebsd_amd64.zip",
"goxz_freebsd_arm64.zip",
},
},
{
name: "build multiple pakcages with app name",
input: []string{"-n=abc", "-os=linux", "-arch=amd64", "./testdata/hello", "./cmd/goxz"},
files: []string{"abc_linux_amd64.tar.gz"},
},
{
name: "output option with version",
input: []string{"-o=abc", "-C=.", "-pv=0.1.1", "-os=freebsd", "./testdata/hello"},
files: []string{
"goxz_0.1.1_freebsd_amd64.tar.gz",
"goxz_0.1.1_freebsd_arm64.tar.gz",
},
},
{
name: "[error] no resulting object",
input: []string{}, // same as []string{"."}
errStr: `can't build artifact for non main package: "goxz"`,
},
{
name: "[error] multiple packages and -o flag are not compatible",
input: []string{"-o=hoge", "./testdata/hello", "./cmd/goxz"},
errStr: "When building multiple packages",
},
{
name: "[error] package not exists",
input: []string{"-work", "./testdata/hello___"},
errStr: "go list failed with following output",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cl := &cli{outStream: ioutil.Discard, errStream: ioutil.Discard}
tmpd := setup(t)
defer os.RemoveAll(tmpd)
args := append([]string{"-d=" + tmpd}, tc.input...)
err := cl.run(args)
if tc.errStr == "" {
if err != nil {
t.Errorf("%s: error should be nil but: %s", tc.name, err)
}
} else {
if err == nil {
t.Errorf("%s: error should be occured but nil", tc.name)
} else if !strings.Contains(err.Error(), tc.errStr) {
t.Errorf("%s: error should be contains %q, but %q", tc.name, tc.errStr, err)
}
}
files, err := ioutil.ReadDir(tmpd)
if err != nil {
t.Fatal(err)
}
var outs []string
for _, f := range files {
if !f.IsDir() {
outs = append(outs, f.Name())
}
}
sort.Strings(tc.files)
sort.Strings(outs)
if !reflect.DeepEqual(tc.files, outs) {
t.Errorf("%s: files are not built correctly\n out: %v\nexpect: %v",
tc.name, outs, tc.files)
}
})
}
}
func TestCliRun_projDir(t *testing.T) {
if err := os.Chdir("./testdata"); err != nil {
t.Fatal(err)
}
defer os.Chdir("../")
input := []string{"-o=abc", "-C=../", "-pv=0.1.1", "-os=freebsd", "./testdata/hello"}
builtFiles := []string{
"goxz_0.1.1_freebsd_amd64.tar.gz",
"goxz_0.1.1_freebsd_arm64.tar.gz",
}
cl := &cli{outStream: ioutil.Discard, errStream: ioutil.Discard}
tmpd := setup(t)
// This deletion process is performed to check whether goxz itself creates
// a directory correctly
if err := os.RemoveAll(tmpd); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpd)
args := append([]string{"-d=" + tmpd}, input...)
err := cl.run(args)
if err != nil {
t.Errorf("error should be nil but: %s", err)
}
files, err := ioutil.ReadDir(tmpd)
if err != nil {
t.Fatal(err)
}
var outs []string
for _, f := range files {
if !f.IsDir() {
outs = append(outs, f.Name())
}
}
if !reflect.DeepEqual(builtFiles, outs) {
t.Errorf("files are not built correctly\n out: %v\nexpect: %v", outs, builtFiles)
}
}