Skip to content

Commit

Permalink
I guess arguments work now (#326)
Browse files Browse the repository at this point in the history
* I guess arguments work now
* add mg.F
  • Loading branch information
natefinch committed Dec 29, 2020
1 parent 3730191 commit aba3950
Show file tree
Hide file tree
Showing 15 changed files with 982 additions and 494 deletions.
178 changes: 178 additions & 0 deletions mage/args_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package mage

import (
"bytes"
"testing"
)

func TestArgs(t *testing.T) {
stderr := &bytes.Buffer{}
stdout := &bytes.Buffer{}
inv := Invocation{
Dir: "./testdata/args",
Stderr: stderr,
Stdout: stdout,
Args: []string{"status", "say", "hi", "bob", "count", "5", "status", "wait", "5ms", "cough", "false"},
}
code := Invoke(inv)
if code != 0 {
t.Log(stderr.String())
t.Fatalf("expected 1, but got %v", code)
}
actual := stdout.String()
expected := `status
saying hi bob
01234
status
waiting 5ms
not coughing
`
if actual != expected {
t.Fatalf("output is not expected:\n%q", actual)
}
}

func TestBadIntArg(t *testing.T) {
stderr := &bytes.Buffer{}
stdout := &bytes.Buffer{}
inv := Invocation{
Dir: "./testdata/args",
Stderr: stderr,
Stdout: stdout,
Args: []string{"count", "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 int\n"

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

func TestBadBoolArg(t *testing.T) {
stderr := &bytes.Buffer{}
stdout := &bytes.Buffer{}
inv := Invocation{
Dir: "./testdata/args",
Stderr: stderr,
Stdout: stdout,
Args: []string{"cough", "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 bool\n"

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

func TestBadDurationArg(t *testing.T) {
stderr := &bytes.Buffer{}
stdout := &bytes.Buffer{}
inv := Invocation{
Dir: "./testdata/args",
Stderr: stderr,
Stdout: stdout,
Args: []string{"wait", "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 time.Duration\n"

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

func TestMissingArgs(t *testing.T) {
stderr := &bytes.Buffer{}
stdout := &bytes.Buffer{}
inv := Invocation{
Dir: "./testdata/args",
Stderr: stderr,
Stdout: stdout,
Args: []string{"say", "hi"},
}
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 := "not enough arguments for target \"Say\", expected 2, got 1\n"

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

func TestDocs(t *testing.T) {
stderr := &bytes.Buffer{}
stdout := &bytes.Buffer{}
inv := Invocation{
Dir: "./testdata/args",
Stderr: stderr,
Stdout: stdout,
Help: true,
Args: []string{"say"},
}
code := Invoke(inv)
if code != 0 {
t.Log("stderr:", stderr)
t.Log("stdout:", stdout)
t.Fatalf("expected code 0, but got %v", code)
}
actual := stdout.String()
expected := `Say says something. It's pretty cool. I think you should try it.
Usage:
mage say <msg> <name>
Aliases: speak
`
if actual != expected {
t.Fatalf("output is not expected:\n%q", actual)
}
}

func TestMgF(t *testing.T) {
stderr := &bytes.Buffer{}
stdout := &bytes.Buffer{}
inv := Invocation{
Dir: "./testdata/args",
Stderr: stderr,
Stdout: stdout,
Args: []string{"HasDep"},
}
code := Invoke(inv)
if code != 0 {
t.Log("stderr:", stderr)
t.Log("stdout:", stdout)
t.Fatalf("expected code 0, but got %v", code)
}
actual := stdout.String()
expected := "saying hi Susan\n"
if actual != expected {
t.Fatalf("output is not expected: %q", actual)
}
}
27 changes: 13 additions & 14 deletions mage/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,10 +826,10 @@ func TestBadSecondTargets(t *testing.T) {
}
code := Invoke(inv)
if code != 2 {
t.Errorf("expected 0, but got %v", code)
t.Errorf("expected 2, but got %v", code)
}
actual := stderr.String()
expected := "Unknown target specified: NotGonnaWork\n"
expected := "Unknown target specified: \"NotGonnaWork\"\n"
if actual != expected {
t.Errorf("expected %q, but got %q", expected, actual)
}
Expand Down Expand Up @@ -967,7 +967,7 @@ func TestHelpTarget(t *testing.T) {
t.Errorf("expected to exit with code 0, but got %v", code)
}
actual := stdout.String()
expected := "mage panics:\n\nFunction that panics.\n\n"
expected := "Function that panics.\n\nUsage:\n\n\tmage panics\n\n"
if actual != expected {
t.Fatalf("expected %q, but got %q", expected, actual)
}
Expand All @@ -987,7 +987,7 @@ func TestHelpAlias(t *testing.T) {
t.Errorf("expected to exit with code 0, but got %v", code)
}
actual := stdout.String()
expected := "mage status:\n\nPrints status.\n\nAliases: st, stat\n\n"
expected := "Prints status.\n\nUsage:\n\n\tmage status\n\nAliases: st, stat\n\n"
if actual != expected {
t.Fatalf("expected %q, but got %q", expected, actual)
}
Expand All @@ -1004,7 +1004,7 @@ func TestHelpAlias(t *testing.T) {
t.Errorf("expected to exit with code 0, but got %v", code)
}
actual = stdout.String()
expected = "mage checkout:\n\nAliases: co\n\n"
expected = "Usage:\n\n\tmage checkout\n\nAliases: co\n\n"
if actual != expected {
t.Fatalf("expected %q, but got %q", expected, actual)
}
Expand Down Expand Up @@ -1053,10 +1053,10 @@ func TestInvalidAlias(t *testing.T) {
}
code := Invoke(inv)
if code != 2 {
t.Errorf("expected to exit with code 1, but got %v", code)
t.Errorf("expected to exit with code 2, but got %v", code)
}
actual := stderr.String()
expected := "Unknown target specified: co\n"
expected := "Unknown target specified: \"co\"\n"
if actual != expected {
t.Fatalf("expected %q, but got %q", expected, actual)
}
Expand Down Expand Up @@ -1121,7 +1121,7 @@ func TestCompiledFlags(t *testing.T) {
t.Fatal(err)
}
got := strings.TrimSpace(stdout.String())
want := filepath.Base(name) + " deploy:\n\nThis is the synopsis for Deploy. This part shouldn't show up."
want := "This is the synopsis for Deploy. This part shouldn't show up.\n\nUsage:\n\n\t" + filepath.Base(name) + " deploy"
if got != want {
t.Errorf("got %q, want %q", got, want)
}
Expand Down Expand Up @@ -1206,8 +1206,8 @@ func TestCompiledEnvironmentVars(t *testing.T) {
if err := run(stdout, stderr, name, "MAGEFILE_HELP=1", "deploy"); err != nil {
t.Fatal(err)
}
got := strings.TrimSpace(stdout.String())
want := filepath.Base(name) + " deploy:\n\nThis is the synopsis for Deploy. This part shouldn't show up."
got := stdout.String()
want := "This is the synopsis for Deploy. This part shouldn't show up.\n\nUsage:\n\n\t" + filepath.Base(name) + " deploy\n\n"
if got != want {
t.Errorf("got %q, want %q", got, want)
}
Expand Down Expand Up @@ -1519,8 +1519,6 @@ func TestAliasToImport(t *testing.T) {

}

var wrongDepRx = regexp.MustCompile("^Error: Invalid type for dependent function.*@ main.FooBar .*magefile.go")

func TestWrongDependency(t *testing.T) {
stderr := &bytes.Buffer{}
inv := Invocation{
Expand All @@ -1532,9 +1530,10 @@ func TestWrongDependency(t *testing.T) {
if code != 1 {
t.Fatalf("expected 1, but got %v", code)
}
expected := "Error: argument 0 (complex128), is not a supported argument type\n"
actual := stderr.String()
if !wrongDepRx.MatchString(actual) {
t.Fatalf("expected matching %q, but got %q", wrongDepRx, actual)
if actual != expected {
t.Fatalf("expected %q, but got %q", expected, actual)
}
}

Expand Down

0 comments on commit aba3950

Please sign in to comment.