Skip to content

Commit aba3950

Browse files
authored
I guess arguments work now (magefile#326)
* I guess arguments work now * add mg.F
1 parent 3730191 commit aba3950

File tree

15 files changed

+982
-494
lines changed

15 files changed

+982
-494
lines changed

mage/args_test.go

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package mage
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
)
7+
8+
func TestArgs(t *testing.T) {
9+
stderr := &bytes.Buffer{}
10+
stdout := &bytes.Buffer{}
11+
inv := Invocation{
12+
Dir: "./testdata/args",
13+
Stderr: stderr,
14+
Stdout: stdout,
15+
Args: []string{"status", "say", "hi", "bob", "count", "5", "status", "wait", "5ms", "cough", "false"},
16+
}
17+
code := Invoke(inv)
18+
if code != 0 {
19+
t.Log(stderr.String())
20+
t.Fatalf("expected 1, but got %v", code)
21+
}
22+
actual := stdout.String()
23+
expected := `status
24+
saying hi bob
25+
01234
26+
status
27+
waiting 5ms
28+
not coughing
29+
`
30+
if actual != expected {
31+
t.Fatalf("output is not expected:\n%q", actual)
32+
}
33+
}
34+
35+
func TestBadIntArg(t *testing.T) {
36+
stderr := &bytes.Buffer{}
37+
stdout := &bytes.Buffer{}
38+
inv := Invocation{
39+
Dir: "./testdata/args",
40+
Stderr: stderr,
41+
Stdout: stdout,
42+
Args: []string{"count", "abc123"},
43+
}
44+
code := Invoke(inv)
45+
if code != 2 {
46+
t.Log("stderr:", stderr)
47+
t.Log("stdout:", stdout)
48+
t.Fatalf("expected code 2, but got %v", code)
49+
}
50+
actual := stderr.String()
51+
expected := "can't convert argument \"abc123\" to int\n"
52+
53+
if actual != expected {
54+
t.Fatalf("output is not expected:\n%q", actual)
55+
}
56+
}
57+
58+
func TestBadBoolArg(t *testing.T) {
59+
stderr := &bytes.Buffer{}
60+
stdout := &bytes.Buffer{}
61+
inv := Invocation{
62+
Dir: "./testdata/args",
63+
Stderr: stderr,
64+
Stdout: stdout,
65+
Args: []string{"cough", "abc123"},
66+
}
67+
code := Invoke(inv)
68+
if code != 2 {
69+
t.Log("stderr:", stderr)
70+
t.Log("stdout:", stdout)
71+
t.Fatalf("expected code 2, but got %v", code)
72+
}
73+
actual := stderr.String()
74+
expected := "can't convert argument \"abc123\" to bool\n"
75+
76+
if actual != expected {
77+
t.Fatalf("output is not expected:\n%q", actual)
78+
}
79+
}
80+
81+
func TestBadDurationArg(t *testing.T) {
82+
stderr := &bytes.Buffer{}
83+
stdout := &bytes.Buffer{}
84+
inv := Invocation{
85+
Dir: "./testdata/args",
86+
Stderr: stderr,
87+
Stdout: stdout,
88+
Args: []string{"wait", "abc123"},
89+
}
90+
code := Invoke(inv)
91+
if code != 2 {
92+
t.Log("stderr:", stderr)
93+
t.Log("stdout:", stdout)
94+
t.Fatalf("expected code 2, but got %v", code)
95+
}
96+
actual := stderr.String()
97+
expected := "can't convert argument \"abc123\" to time.Duration\n"
98+
99+
if actual != expected {
100+
t.Fatalf("output is not expected:\n%q", actual)
101+
}
102+
}
103+
104+
func TestMissingArgs(t *testing.T) {
105+
stderr := &bytes.Buffer{}
106+
stdout := &bytes.Buffer{}
107+
inv := Invocation{
108+
Dir: "./testdata/args",
109+
Stderr: stderr,
110+
Stdout: stdout,
111+
Args: []string{"say", "hi"},
112+
}
113+
code := Invoke(inv)
114+
if code != 2 {
115+
t.Log("stderr:", stderr)
116+
t.Log("stdout:", stdout)
117+
t.Fatalf("expected code 2, but got %v", code)
118+
}
119+
actual := stderr.String()
120+
expected := "not enough arguments for target \"Say\", expected 2, got 1\n"
121+
122+
if actual != expected {
123+
t.Fatalf("output is not expected:\n%q", actual)
124+
}
125+
}
126+
127+
func TestDocs(t *testing.T) {
128+
stderr := &bytes.Buffer{}
129+
stdout := &bytes.Buffer{}
130+
inv := Invocation{
131+
Dir: "./testdata/args",
132+
Stderr: stderr,
133+
Stdout: stdout,
134+
Help: true,
135+
Args: []string{"say"},
136+
}
137+
code := Invoke(inv)
138+
if code != 0 {
139+
t.Log("stderr:", stderr)
140+
t.Log("stdout:", stdout)
141+
t.Fatalf("expected code 0, but got %v", code)
142+
}
143+
actual := stdout.String()
144+
expected := `Say says something. It's pretty cool. I think you should try it.
145+
146+
Usage:
147+
148+
mage say <msg> <name>
149+
150+
Aliases: speak
151+
152+
`
153+
if actual != expected {
154+
t.Fatalf("output is not expected:\n%q", actual)
155+
}
156+
}
157+
158+
func TestMgF(t *testing.T) {
159+
stderr := &bytes.Buffer{}
160+
stdout := &bytes.Buffer{}
161+
inv := Invocation{
162+
Dir: "./testdata/args",
163+
Stderr: stderr,
164+
Stdout: stdout,
165+
Args: []string{"HasDep"},
166+
}
167+
code := Invoke(inv)
168+
if code != 0 {
169+
t.Log("stderr:", stderr)
170+
t.Log("stdout:", stdout)
171+
t.Fatalf("expected code 0, but got %v", code)
172+
}
173+
actual := stdout.String()
174+
expected := "saying hi Susan\n"
175+
if actual != expected {
176+
t.Fatalf("output is not expected: %q", actual)
177+
}
178+
}

mage/main_test.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -826,10 +826,10 @@ func TestBadSecondTargets(t *testing.T) {
826826
}
827827
code := Invoke(inv)
828828
if code != 2 {
829-
t.Errorf("expected 0, but got %v", code)
829+
t.Errorf("expected 2, but got %v", code)
830830
}
831831
actual := stderr.String()
832-
expected := "Unknown target specified: NotGonnaWork\n"
832+
expected := "Unknown target specified: \"NotGonnaWork\"\n"
833833
if actual != expected {
834834
t.Errorf("expected %q, but got %q", expected, actual)
835835
}
@@ -967,7 +967,7 @@ func TestHelpTarget(t *testing.T) {
967967
t.Errorf("expected to exit with code 0, but got %v", code)
968968
}
969969
actual := stdout.String()
970-
expected := "mage panics:\n\nFunction that panics.\n\n"
970+
expected := "Function that panics.\n\nUsage:\n\n\tmage panics\n\n"
971971
if actual != expected {
972972
t.Fatalf("expected %q, but got %q", expected, actual)
973973
}
@@ -987,7 +987,7 @@ func TestHelpAlias(t *testing.T) {
987987
t.Errorf("expected to exit with code 0, but got %v", code)
988988
}
989989
actual := stdout.String()
990-
expected := "mage status:\n\nPrints status.\n\nAliases: st, stat\n\n"
990+
expected := "Prints status.\n\nUsage:\n\n\tmage status\n\nAliases: st, stat\n\n"
991991
if actual != expected {
992992
t.Fatalf("expected %q, but got %q", expected, actual)
993993
}
@@ -1004,7 +1004,7 @@ func TestHelpAlias(t *testing.T) {
10041004
t.Errorf("expected to exit with code 0, but got %v", code)
10051005
}
10061006
actual = stdout.String()
1007-
expected = "mage checkout:\n\nAliases: co\n\n"
1007+
expected = "Usage:\n\n\tmage checkout\n\nAliases: co\n\n"
10081008
if actual != expected {
10091009
t.Fatalf("expected %q, but got %q", expected, actual)
10101010
}
@@ -1053,10 +1053,10 @@ func TestInvalidAlias(t *testing.T) {
10531053
}
10541054
code := Invoke(inv)
10551055
if code != 2 {
1056-
t.Errorf("expected to exit with code 1, but got %v", code)
1056+
t.Errorf("expected to exit with code 2, but got %v", code)
10571057
}
10581058
actual := stderr.String()
1059-
expected := "Unknown target specified: co\n"
1059+
expected := "Unknown target specified: \"co\"\n"
10601060
if actual != expected {
10611061
t.Fatalf("expected %q, but got %q", expected, actual)
10621062
}
@@ -1121,7 +1121,7 @@ func TestCompiledFlags(t *testing.T) {
11211121
t.Fatal(err)
11221122
}
11231123
got := strings.TrimSpace(stdout.String())
1124-
want := filepath.Base(name) + " deploy:\n\nThis is the synopsis for Deploy. This part shouldn't show up."
1124+
want := "This is the synopsis for Deploy. This part shouldn't show up.\n\nUsage:\n\n\t" + filepath.Base(name) + " deploy"
11251125
if got != want {
11261126
t.Errorf("got %q, want %q", got, want)
11271127
}
@@ -1206,8 +1206,8 @@ func TestCompiledEnvironmentVars(t *testing.T) {
12061206
if err := run(stdout, stderr, name, "MAGEFILE_HELP=1", "deploy"); err != nil {
12071207
t.Fatal(err)
12081208
}
1209-
got := strings.TrimSpace(stdout.String())
1210-
want := filepath.Base(name) + " deploy:\n\nThis is the synopsis for Deploy. This part shouldn't show up."
1209+
got := stdout.String()
1210+
want := "This is the synopsis for Deploy. This part shouldn't show up.\n\nUsage:\n\n\t" + filepath.Base(name) + " deploy\n\n"
12111211
if got != want {
12121212
t.Errorf("got %q, want %q", got, want)
12131213
}
@@ -1519,8 +1519,6 @@ func TestAliasToImport(t *testing.T) {
15191519

15201520
}
15211521

1522-
var wrongDepRx = regexp.MustCompile("^Error: Invalid type for dependent function.*@ main.FooBar .*magefile.go")
1523-
15241522
func TestWrongDependency(t *testing.T) {
15251523
stderr := &bytes.Buffer{}
15261524
inv := Invocation{
@@ -1532,9 +1530,10 @@ func TestWrongDependency(t *testing.T) {
15321530
if code != 1 {
15331531
t.Fatalf("expected 1, but got %v", code)
15341532
}
1533+
expected := "Error: argument 0 (complex128), is not a supported argument type\n"
15351534
actual := stderr.String()
1536-
if !wrongDepRx.MatchString(actual) {
1537-
t.Fatalf("expected matching %q, but got %q", wrongDepRx, actual)
1535+
if actual != expected {
1536+
t.Fatalf("expected %q, but got %q", expected, actual)
15381537
}
15391538
}
15401539

0 commit comments

Comments
 (0)