-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain_test.go
More file actions
100 lines (90 loc) · 2.62 KB
/
main_test.go
File metadata and controls
100 lines (90 loc) · 2.62 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
91
92
93
94
95
96
97
98
99
100
package main
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/macadmins/carafe/cudo"
"github.com/macadmins/carafe/exec"
"github.com/macadmins/carafe/shell/testshell"
)
func TestValidateFormulaArg_AllowsValid(t *testing.T) {
valid := []string{
"openssl",
"openssl@1.1",
"libxml++",
"qt-5",
"foo_bar.baz",
"gcc@12",
"a",
strings.Repeat("a", 128),
}
for _, v := range valid {
assert.NoError(t, validateFormulaArg(v))
assert.True(t, formulaRe.MatchString(v))
}
}
func TestValidateFormulaArg_RejectsInvalid(t *testing.T) {
invalid := []string{
"",
"homebrew/cask/vlc",
"foo bar",
"naïve",
"foo:bar",
"abc!",
"a\nb",
strings.Repeat("a", 129),
}
for _, v := range invalid {
assert.Error(t, validateFormulaArg(v))
assert.False(t, formulaRe.MatchString(v))
}
}
func TestFormulaRe_DoesNotPartiallyMatch(t *testing.T) {
assert.False(t, formulaRe.MatchString("valid@1.0!")) // '!' not allowed anywhere
assert.False(t, formulaRe.MatchString(" valid")) // leading space
assert.False(t, formulaRe.MatchString("valid ")) // trailing space
}
func newMockConfig() exec.CarafeConfig {
return exec.CarafeConfig{
Arch: "arm64",
CUSudo: &cudo.CUSudo{
CurrentUser: "testuser",
Platform: "darwin",
OSFunc: &cudo.MockOSFunc{},
UserHome: "/Users/testuser",
Executor: testshell.NewExecutor(),
},
}
}
func TestCheckCmd_InvalidCacheTTL(t *testing.T) {
cmd := buildRootCmd(newMockConfig(), "test")
cmd.SetArgs([]string{"check", "--cache-ttl=banana", "git"})
err := cmd.Execute()
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid argument")
}
func TestCheckCmd_NoCacheFlag(t *testing.T) {
// --no-cache should be accepted without error (brew call itself will fail
// with the mock executor, but the flag parsing must succeed).
cmd := buildRootCmd(newMockConfig(), "test")
cmd.SetArgs([]string{"check", "--no-cache", "git"})
// The mock executor has no output configured so brew.Check will error,
// but the error must NOT be a flag-parsing error.
err := cmd.Execute()
if err != nil {
assert.NotContains(t, err.Error(), "invalid argument")
assert.NotContains(t, err.Error(), "unknown flag")
}
}
func TestCheckCmd_CacheTTLFlag(t *testing.T) {
// Valid duration strings must be accepted.
for _, ttl := range []string{"30s", "2m", "1h", "0s"} {
cmd := buildRootCmd(newMockConfig(), "test")
cmd.SetArgs([]string{"check", "--cache-ttl=" + ttl, "--no-cache", "git"})
err := cmd.Execute()
if err != nil {
assert.NotContains(t, err.Error(), "invalid argument", "TTL %q should be valid", ttl)
}
}
}