Skip to content

Commit 0477cd6

Browse files
authored
Merge pull request #16 from oalders/refactor-tests
Refactor some tests
2 parents 2f7adf3 + ceb1be8 commit 0477cd6

File tree

2 files changed

+63
-84
lines changed

2 files changed

+63
-84
lines changed

api.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ type UserCmd struct {
6161
Sudoer string `arg:"" required:"" default:"sudoer" enum:"sudoer" help:"is current user a passwordless sudoer. e.g. \"is user sudoer\""`
6262
}
6363

64+
type KnownCLI struct {
65+
Attr string `arg:"" name:"attribute" required:"" enum:"version"`
66+
Name string `arg:"" required:""`
67+
}
68+
6469
// KnownCmd type is configuration for printing environment info.
6570
//
6671
//nolint:lll
@@ -71,13 +76,10 @@ type KnownCmd struct {
7176
OS struct {
7277
Attr string `arg:"" required:"" name:"attribute" help:"[id|id-like|pretty-name|name|version|version-codename]"`
7378
} `cmd:"" help:"Print without check. e.g. \"is known os name\""`
74-
CLI struct {
75-
Attr string `arg:"" name:"attribute" required:"" enum:"version"`
76-
Name string `arg:"" required:""`
77-
} `cmd:"" help:"Print without check. e.g. \"is known cli version git\""`
78-
Major bool `xor:"Major,Minor,Patch" help:"Only print the major OS or CLI version (e.g. major.minor.patch)"`
79-
Minor bool `xor:"Major,Minor,Patch" help:"Only print the minor OS or CLI version (e.g. major.minor.patch)"`
80-
Patch bool `xor:"Major,Minor,Patch" help:"Only print the patch OS or CLI version (e.g. major.minor.patch)"`
79+
CLI KnownCLI `cmd:"" help:"Print without check. e.g. \"is known cli version git\""`
80+
Major bool `xor:"Major,Minor,Patch" help:"Only print the major OS or CLI version (e.g. major.minor.patch)"`
81+
Minor bool `xor:"Major,Minor,Patch" help:"Only print the minor OS or CLI version (e.g. major.minor.patch)"`
82+
Patch bool `xor:"Major,Minor,Patch" help:"Only print the patch OS or CLI version (e.g. major.minor.patch)"`
8183
}
8284

8385
// ThereCmd is configuration for finding executables.

known_test.go

Lines changed: 54 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"runtime"
56
"testing"
67

@@ -12,109 +13,85 @@ import (
1213
func TestKnownCmd(t *testing.T) {
1314
t.Parallel()
1415
const tmux = "testdata/bin/tmux"
15-
{
16-
ctx := types.Context{Debug: true}
17-
cmd := KnownCmd{}
18-
cmd.OS.Attr = attr.Name
19-
err := cmd.Run(&ctx)
20-
assert.NoError(t, err)
21-
assert.True(t, ctx.Success)
22-
}
23-
if runtime.GOOS == "darwin" {
24-
ctx := types.Context{Debug: true}
25-
cmd := KnownCmd{}
26-
cmd.OS.Attr = attr.Version
27-
err := cmd.Run(&ctx)
28-
assert.NoError(t, err)
29-
assert.True(t, ctx.Success)
30-
}
31-
{
32-
ctx := types.Context{Debug: true}
33-
cmd := KnownCmd{}
34-
cmd.OS.Attr = attr.Version
35-
err := cmd.Run(&ctx)
36-
assert.NoError(t, err)
37-
assert.True(t, ctx.Success)
16+
type testableOS struct {
17+
Attr string
18+
Error bool
19+
Success bool
3820
}
3921

40-
{
41-
ctx := types.Context{Debug: true}
42-
cmd := KnownCmd{}
43-
cmd.OS.Attr = "tmuxxx"
44-
err := cmd.Run(&ctx)
45-
assert.NoError(t, err)
46-
assert.False(t, ctx.Success, "No success")
22+
osTests := []testableOS{
23+
{attr.Name, false, true},
24+
{attr.Version, false, true},
25+
{"tmuxxx", false, false},
26+
{"tmuxxx", false, false},
4727
}
4828

49-
{
50-
ctx := types.Context{Debug: true}
51-
cmd := KnownCmd{}
52-
cmd.CLI.Attr = attr.Version
53-
cmd.CLI.Name = "gitzzz"
54-
err := cmd.Run(&ctx)
55-
assert.NoError(t, err)
56-
assert.False(t, ctx.Success, "No success")
29+
if runtime.GOOS == "darwin" {
30+
osTests = append(osTests, testableOS{attr.Version, false, true})
5731
}
5832

59-
{
33+
for _, test := range osTests {
6034
ctx := types.Context{Debug: true}
6135
cmd := KnownCmd{}
62-
cmd.Arch.Attr = "arch"
36+
cmd.OS.Attr = test.Attr
6337
err := cmd.Run(&ctx)
64-
assert.NoError(t, err)
65-
assert.True(t, ctx.Success, "success")
38+
name := fmt.Sprintf("%s err: %t success: %t", test.Attr, test.Error, test.Success)
39+
if test.Error {
40+
assert.Error(t, err, name)
41+
} else {
42+
assert.NoError(t, err, name)
43+
}
44+
if test.Success {
45+
assert.True(t, ctx.Success, name)
46+
} else {
47+
assert.False(t, ctx.Success, name)
48+
}
6649
}
6750

68-
{
69-
ctx := types.Context{Debug: true}
70-
cmd := KnownCmd{}
71-
cmd.CLI.Attr = attr.Version
72-
cmd.CLI.Name = tmux
73-
cmd.Major = true
74-
err := cmd.Run(&ctx)
75-
assert.NoError(t, err)
76-
assert.True(t, ctx.Success, "success")
51+
type testableCLI struct {
52+
Cmd KnownCmd
53+
Error bool
54+
Success bool
7755
}
78-
79-
{
80-
ctx := types.Context{Debug: true}
81-
cmd := KnownCmd{}
82-
cmd.CLI.Attr = attr.Version
83-
cmd.CLI.Name = tmux
84-
cmd.Minor = true
85-
err := cmd.Run(&ctx)
86-
assert.NoError(t, err)
87-
assert.True(t, ctx.Success, "success")
56+
cliTests := []testableCLI{
57+
{KnownCmd{CLI: KnownCLI{attr.Version, "gitzzz"}}, false, false},
58+
{KnownCmd{CLI: KnownCLI{attr.Version, tmux}}, false, true},
59+
{KnownCmd{CLI: KnownCLI{attr.Version, tmux}, Major: true}, false, true},
60+
{KnownCmd{CLI: KnownCLI{attr.Version, tmux}, Minor: true}, false, true},
61+
{KnownCmd{CLI: KnownCLI{attr.Version, tmux}, Patch: true}, false, true},
8862
}
8963

90-
{
64+
for _, test := range cliTests {
9165
ctx := types.Context{Debug: true}
92-
cmd := KnownCmd{}
93-
cmd.CLI.Attr = attr.Version
94-
cmd.CLI.Name = tmux
95-
cmd.Patch = true
96-
err := cmd.Run(&ctx)
97-
assert.NoError(t, err)
98-
assert.True(t, ctx.Success, "success")
66+
err := test.Cmd.Run(&ctx)
67+
68+
switch test.Error {
69+
case true:
70+
assert.Error(t, err)
71+
default:
72+
assert.NoError(t, err)
73+
}
74+
75+
switch test.Success {
76+
case true:
77+
assert.True(t, ctx.Success)
78+
default:
79+
assert.False(t, ctx.Success)
80+
}
9981
}
10082

10183
{
10284
ctx := types.Context{Debug: true}
10385
cmd := KnownCmd{}
104-
cmd.CLI.Attr = attr.Version
105-
cmd.CLI.Name = tmux
106-
cmd.Major = true
86+
cmd.Arch.Attr = "arch"
10787
err := cmd.Run(&ctx)
10888
assert.NoError(t, err)
10989
assert.True(t, ctx.Success, "success")
11090
}
111-
11291
{
11392
ctx := types.Context{Debug: true}
114-
cmd := KnownCmd{}
115-
cmd.CLI.Attr = attr.Name
116-
cmd.CLI.Name = tmux
117-
cmd.Major = true
93+
cmd := KnownCmd{Major: true}
94+
cmd.OS.Attr = "name"
11895
err := cmd.Run(&ctx)
11996
assert.Error(t, err)
12097
assert.False(t, ctx.Success, "success")

0 commit comments

Comments
 (0)