Skip to content

Commit

Permalink
make -h work with imported targets (#335)
Browse files Browse the repository at this point in the history
fixes #249
Forgot to allow help to be called on imported targets. Kinda useful, eh?
  • Loading branch information
natefinch authored Feb 20, 2021
1 parent de7ca6c commit 4cf3cfc
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 2 deletions.
98 changes: 98 additions & 0 deletions mage/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,104 @@ Targets:
}
}

func TestMageImportsHelp(t *testing.T) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
inv := Invocation{
Dir: "./testdata/mageimport",
Stdout: stdout,
Stderr: stderr,
Help: true,
Args: []string{"buildSubdir"},
}

code := Invoke(inv)
if code != 0 {
t.Fatalf("expected to exit with code 0, but got %v, stderr:\n%s", code, stderr)
}
actual := stdout.String()
expected := `
BuildSubdir Builds stuff.
Usage:
mage buildsubdir
`[1:]

if actual != expected {
t.Logf("expected: %q", expected)
t.Logf(" actual: %q", actual)
t.Fatalf("expected:\n%v\n\ngot:\n%v", expected, actual)
}
}

func TestMageImportsHelpNamed(t *testing.T) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
inv := Invocation{
Dir: "./testdata/mageimport",
Stdout: stdout,
Stderr: stderr,
Help: true,
Args: []string{"zz:buildSubdir2"},
}

code := Invoke(inv)
if code != 0 {
t.Fatalf("expected to exit with code 0, but got %v, stderr:\n%s", code, stderr)
}
actual := stdout.String()
expected := `
BuildSubdir2 Builds stuff.
Usage:
mage zz:buildsubdir2
`[1:]

if actual != expected {
t.Logf("expected: %q", expected)
t.Logf(" actual: %q", actual)
t.Fatalf("expected:\n%v\n\ngot:\n%v", expected, actual)
}
}

func TestMageImportsHelpNamedNS(t *testing.T) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
inv := Invocation{
Dir: "./testdata/mageimport",
Stdout: stdout,
Stderr: stderr,
Help: true,
Args: []string{"zz:ns:deploy2"},
}

code := Invoke(inv)
if code != 0 {
t.Fatalf("expected to exit with code 0, but got %v, stderr:\n%s", code, stderr)
}
actual := stdout.String()
expected := `
Deploy2 deploys stuff.
Usage:
mage zz:ns:deploy2
Aliases: nsd2
`[1:]

if actual != expected {
t.Logf("expected: %q", expected)
t.Logf(" actual: %q", actual)
t.Fatalf("expected:\n%v\n\ngot:\n%v", expected, actual)
}
}

func TestMageImportsRoot(t *testing.T) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
Expand Down
25 changes: 23 additions & 2 deletions mage/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,27 @@ Options:
os.Exit(2)
}
switch strings.ToLower(args.Args[0]) {
{{range .Funcs}}case "{{lower .TargetName}}":
{{range .Funcs -}}
case "{{lower .TargetName}}":
{{if ne .Comment "" -}}
fmt.Println({{printf "%q" .Comment}})
fmt.Println()
{{end}}
fmt.Print("Usage:\n\n\t{{$.BinaryName}} {{lower .TargetName}}{{range .Args}} <{{.Name}}>{{end}}\n\n")
var aliases []string
{{- $name := .Name -}}
{{- $recv := .Receiver -}}
{{range $alias, $func := $.Aliases}}
{{if and (eq $name $func.Name) (eq $recv $func.Receiver)}}aliases = append(aliases, "{{$alias}}"){{end -}}
{{- end}}
if len(aliases) > 0 {
fmt.Printf("Aliases: %s\n\n", strings.Join(aliases, ", "))
}
return
{{end -}}
{{range .Imports -}}
{{range .Info.Funcs -}}
case "{{lower .TargetName}}":
{{if ne .Comment "" -}}
fmt.Println({{printf "%q" .Comment}})
fmt.Println()
Expand All @@ -355,7 +375,8 @@ Options:
fmt.Printf("Aliases: %s\n\n", strings.Join(aliases, ", "))
}
return
{{end}}
{{end -}}
{{end -}}
default:
logger.Printf("Unknown target: %q\n", args.Args[0])
os.Exit(2)
Expand Down

0 comments on commit 4cf3cfc

Please sign in to comment.