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

feat: allow mage:import alias to be defined for multiple imports #398

Merged
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
54 changes: 54 additions & 0 deletions mage/import_test.go
Expand Up @@ -312,3 +312,57 @@ Error parsing magefiles: error running "go list -f {{.Dir}}||{{.Name}} github.co
t.Fatalf("expected:\n%s\n\ngot:\n%s", expected, actualShortened)
}
}

func TestMageImportsSameNamespaceUniqueTargets(t *testing.T) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
inv := Invocation{
Dir: "./testdata/mageimport/samenamespace/uniquetargets",
Stdout: stdout,
Stderr: stderr,
List: true,
}

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 := `
Targets:
samenamespace:build1
samenamespace:build2
`[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 TestMageImportsSameNamespaceDupTargets(t *testing.T) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
inv := Invocation{
Dir: "./testdata/mageimport/samenamespace/duptargets",
Stdout: stdout,
Stderr: stderr,
List: true,
}

code := Invoke(inv)
if code != 1 {
t.Fatalf("expected to exit with code 1, but got %v, stderr:\n%s", code, stderr)
}
actual := stderr.String()
expected := `
Error parsing magefiles: "samenamespace:build" target has multiple definitions: github.com/magefile/mage/mage/testdata/mageimport/samenamespace/duptargets/package1.Build, github.com/magefile/mage/mage/testdata/mageimport/samenamespace/duptargets/package2.Build

`[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)
}
}
10 changes: 10 additions & 0 deletions mage/testdata/mageimport/samenamespace/duptargets/magefile.go
@@ -0,0 +1,10 @@
// +build mage

package sametarget

import (
// mage:import samenamespace
_ "github.com/magefile/mage/mage/testdata/mageimport/samenamespace/duptargets/package1"
// mage:import samenamespace
_ "github.com/magefile/mage/mage/testdata/mageimport/samenamespace/duptargets/package2"
)
@@ -0,0 +1,7 @@
package package1

import "fmt"

func Build() {
fmt.Println("build")
}
@@ -0,0 +1,7 @@
package package2

import "fmt"

func Build() {
fmt.Println("build")
}
10 changes: 10 additions & 0 deletions mage/testdata/mageimport/samenamespace/uniquetargets/magefile.go
@@ -0,0 +1,10 @@
// +build mage

package main

import (
// mage:import samenamespace
_ "github.com/magefile/mage/mage/testdata/mageimport/samenamespace/uniquetargets/package1"
// mage:import samenamespace
_ "github.com/magefile/mage/mage/testdata/mageimport/samenamespace/uniquetargets/package2"
)
@@ -0,0 +1,7 @@
package package1

import "fmt"

func Build1() {
fmt.Println("build")
}
@@ -0,0 +1,7 @@
package package2

import "fmt"

func Build2() {
fmt.Println("build")
}
7 changes: 2 additions & 5 deletions parse/parse.go
Expand Up @@ -267,7 +267,7 @@ func Package(path string, files []string) (*PkgInfo, error) {

func getNamedImports(gocmd string, pkgs map[string]string) ([]*Import, error) {
var imports []*Import
for alias, pkg := range pkgs {
for pkg, alias := range pkgs {
debug.Printf("getting import package %q, alias %q", pkg, alias)
imp, err := getImport(gocmd, pkg, alias)
if err != nil {
Expand Down Expand Up @@ -411,10 +411,7 @@ func setImports(gocmd string, pi *PkgInfo) error {
}
if alias != "" {
debug.Printf("found %s: %s (%s)", importTag, name, alias)
if importNames[alias] != "" {
return fmt.Errorf("duplicate import alias: %q", alias)
}
importNames[alias] = name
importNames[name] = alias
} else {
debug.Printf("found %s: %s", importTag, name)
rootImports = append(rootImports, name)
Expand Down