Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make -h work with imported targets #335

Merged
merged 1 commit into from Feb 20, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
98 changes: 98 additions & 0 deletions mage/import_test.go
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
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