-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli_test.go
More file actions
79 lines (75 loc) · 1.75 KB
/
cli_test.go
File metadata and controls
79 lines (75 loc) · 1.75 KB
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
package dl_test
import (
"bytes"
"context"
"os"
"testing"
"github.com/task4233/dl/v2"
)
func init() {
once.Do(extractZip)
}
func TestRun(t *testing.T) {
t.Parallel()
tests := map[string]struct {
targetPath string
args []string
wantErr bool
}{
"success with clean": {
args: []string{"clean", "testdata/run/clean"},
wantErr: false,
},
"success with init": {
args: []string{"init", "testdata/run"},
wantErr: false,
},
"success with remove": {
args: []string{"remove", "testdata/run/remove"},
wantErr: false,
},
"success restore": {
args: []string{"restore", "testdata/run/restore"},
wantErr: false,
},
"failed restore with uninited directory": {
args: []string{"restore", "testdata/run/clean-with-uninited"},
wantErr: true,
},
"failed clean with uninited directory": {
args: []string{"clean", "testdata/run/clean-with-uninited"},
wantErr: true,
},
"failed clean with excluded directory": {
args: []string{"clean", "testdata/run/clean/.dl"},
wantErr: true,
},
"failed with unknown command": {
args: []string{"hoge"},
wantErr: true,
},
"failed with no arg": {
args: []string{},
wantErr: true,
},
}
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
t.Parallel()
err := dl.New().Run(context.Background(), "v0.0.0", tt.args)
if (err != nil) != tt.wantErr {
t.Fatalf("unexpected error, wantError=%v, got=%v", tt.wantErr, err)
}
if tt.targetPath != "" {
data, err := os.ReadFile(tt.targetPath)
if err != nil {
t.Fatalf("failed ReadFile: %s", err.Error())
}
if bytes.Contains(data, []byte("dl")) {
t.Fatalf("failed to delete dl from data: \n%s", string(data))
}
}
})
}
}