-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathmain_options_test.go
More file actions
90 lines (82 loc) · 2.91 KB
/
Copy pathmain_options_test.go
File metadata and controls
90 lines (82 loc) · 2.91 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
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"strings"
"testing"
"github.com/mvt-project/androidqf/modules"
)
func TestBuildOptionsNoFlagsKeepsInteractiveDefaults(t *testing.T) {
opts, err := buildOptions(false, false, "", "", "", "", "", "")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if *opts != (modules.Options{}) {
t.Fatalf("opts = %+v, want zero value", *opts)
}
}
func TestBuildOptionsInvalidValueFails(t *testing.T) {
tests := []struct {
name string
backup, download, removeTrusted, intrusionLogs, hashFiles string
wantErr string
}{
{"backup", "maybe", "", "", "", "", "invalid -backup value"},
{"download", "", "some", "", "", "", "invalid -download value"},
{"remove-trusted", "", "", "nope", "", "", "invalid -remove-trusted value"},
{"intrusion-logs", "", "", "", "never", "", "invalid -intrusion-logs value"},
{"hash-files", "", "", "", "", "maybe", "invalid -hash-files value"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := buildOptions(false, false, tt.backup, tt.download, tt.removeTrusted, tt.intrusionLogs, tt.hashFiles, "")
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("err = %v, want containing %q", err, tt.wantErr)
}
})
}
}
func TestBuildOptionsNonInteractiveUnknownModuleFails(t *testing.T) {
_, err := buildOptions(false, true, "", "", "", "", "", "typo")
if err == nil || !strings.Contains(err.Error(), "unknown -module value") {
t.Fatalf("err = %v, want unknown -module error", err)
}
}
func TestBuildOptionsNonInteractiveMissingFlagsFails(t *testing.T) {
_, err := buildOptions(false, true, "", "", "", "", "", "")
if err == nil || !strings.Contains(err.Error(), "-non-interactive requires") {
t.Fatalf("err = %v, want missing flags error", err)
}
}
func TestBuildOptionsNonInteractiveModuleFilter(t *testing.T) {
_, err := buildOptions(false, true, "none", "", "", "", "", "backup")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestBuildOptionsFullInvocation(t *testing.T) {
opts, err := buildOptions(true, true, "sms", "all", "no", "no", "yes", "")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !opts.Fast || !opts.NonInteractive {
t.Fatalf("opts = %+v, want Fast and NonInteractive set", *opts)
}
for _, check := range []struct {
got string
parse func(string) (string, error)
token string
}{
{opts.Backup, modules.ParseBackupOption, "sms"},
{opts.Download, modules.ParseDownloadOption, "all"},
{opts.RemoveTrusted, modules.ParseRemoveTrustedOption, "no"},
{opts.IntrusionLogs, modules.ParseIntrusionLogsOption, "no"},
{opts.HashFiles, modules.ParseHashFilesOption, "yes"},
} {
want, err := check.parse(check.token)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if check.got != want {
t.Fatalf("got %q, want %q", check.got, want)
}
}
}