Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions mage/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ func TestArgs(t *testing.T) {
Dir: "./testdata/args",
Stderr: stderr,
Stdout: stdout,
Args: []string{"status", "say", "hi", "bob", "count", "5", "status", "wait", "5ms", "cough", "false"},
Args: []string{"status", "say", "hi", "bob", "count", "5", "status", "wait", "5ms", "cough", "false", "doubleIt", "3.1"},
}
code := Invoke(inv)
if code != 0 {
t.Log(stderr.String())
t.Fatalf("expected 1, but got %v", code)
t.Fatalf("expected 0, but got %v", code)
}
actual := stdout.String()
expected := `status
Expand All @@ -26,6 +26,7 @@ saying hi bob
status
waiting 5ms
not coughing
3.1 * 2 = 6.2
`
if actual != expected {
t.Fatalf("output is not expected:\n%q", actual)
Expand Down Expand Up @@ -101,6 +102,29 @@ func TestBadDurationArg(t *testing.T) {
}
}

func TestBadFloat64Arg(t *testing.T) {
stderr := &bytes.Buffer{}
stdout := &bytes.Buffer{}
inv := Invocation{
Dir: "./testdata/args",
Stderr: stderr,
Stdout: stdout,
Args: []string{"doubleIt", "abc123"},
}
code := Invoke(inv)
if code != 2 {
t.Log("stderr:", stderr)
t.Log("stdout:", stdout)
t.Fatalf("expected code 2, but got %v", code)
}
actual := stderr.String()
expected := "can't convert argument \"abc123\" to float64\n"

if actual != expected {
t.Fatalf("output is not expected:\n%q", actual)
}
}

func TestMissingArgs(t *testing.T) {
stderr := &bytes.Buffer{}
stdout := &bytes.Buffer{}
Expand Down
4 changes: 4 additions & 0 deletions mage/testdata/args/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ func Cough(ctx context.Context, b bool) error {
func HasDep() {
mg.Deps(mg.F(Say, "hi", "Susan"))
}

func DoubleIt(f float64) {
fmt.Printf("%.1f * 2 = %.1f\n", f, f*2)
}
9 changes: 9 additions & 0 deletions parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ func (f Function) ExecCode() string {
os.Exit(2)
}
x++`, x)
case "float64":
parseargs += fmt.Sprintf(`
arg%d, err := strconv.ParseFloat(args.Args[x], 64)
if err != nil {
logger.Printf("can't convert argument %%q to float64\n", args.Args[x])
os.Exit(2)
}
x++`, x)
case "bool":
parseargs += fmt.Sprintf(`
arg%d, err := strconv.ParseBool(args.Args[x])
Expand Down Expand Up @@ -845,6 +853,7 @@ func toOneLine(s string) string {
var argTypes = map[string]string{
"string": "string",
"int": "int",
"float64": "float64",
"&{time Duration}": "time.Duration",
"bool": "bool",
}