-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
known_test.go
113 lines (99 loc) · 2.37 KB
/
known_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
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/oalders/is/attr"
"github.com/oalders/is/types"
"github.com/stretchr/testify/assert"
)
//nolint:unparam
func prependPath(path string) string {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
return filepath.Join(wd, path) + string(os.PathListSeparator) + os.Getenv("PATH")
}
//nolint:paralleltest,nolintlint
func TestKnownCmd(t *testing.T) {
t.Setenv("PATH", prependPath("testdata/bin"))
const command = "semver"
type testableOS struct {
Attr string
Error bool
Success bool
}
osTests := []testableOS{
{attr.Name, false, true},
{attr.Version, false, true},
{"tmuxxx", false, false},
{"tmuxxx", false, false},
}
if runtime.GOOS == "darwin" {
osTests = append(osTests, testableOS{attr.Version, false, true})
}
for _, test := range osTests {
ctx := types.Context{Debug: true}
cmd := KnownCmd{}
cmd.OS.Attr = test.Attr
err := cmd.Run(&ctx)
name := fmt.Sprintf("%s err: %t success: %t", test.Attr, test.Error, test.Success)
if test.Error {
assert.Error(t, err, name)
} else {
assert.NoError(t, err, name)
}
if test.Success {
assert.True(t, ctx.Success, name)
} else {
assert.False(t, ctx.Success, name)
}
}
type testableCLI struct {
Cmd KnownCmd
Error bool
Success bool
}
cliTests := []testableCLI{
{KnownCmd{CLI: KnownCLI{attr.Version, "gitzzz"}}, false, false},
{KnownCmd{CLI: KnownCLI{attr.Version, command}}, false, true},
{KnownCmd{CLI: KnownCLI{attr.Version, command}, Major: true}, false, true},
{KnownCmd{CLI: KnownCLI{attr.Version, command}, Minor: true}, false, true},
{KnownCmd{CLI: KnownCLI{attr.Version, command}, Patch: true}, false, true},
}
for _, test := range cliTests {
ctx := types.Context{Debug: true}
err := test.Cmd.Run(&ctx)
switch test.Error {
case true:
assert.Error(t, err)
default:
assert.NoError(t, err)
}
switch test.Success {
case true:
assert.True(t, ctx.Success)
default:
assert.False(t, ctx.Success)
}
}
{
ctx := types.Context{Debug: true}
cmd := KnownCmd{}
cmd.Arch.Attr = "arch"
err := cmd.Run(&ctx)
assert.NoError(t, err)
assert.True(t, ctx.Success, "success")
}
{
ctx := types.Context{Debug: true}
cmd := KnownCmd{Major: true}
cmd.OS.Attr = "name"
err := cmd.Run(&ctx)
assert.Error(t, err)
assert.False(t, ctx.Success, "success")
}
}