Skip to content

Commit

Permalink
fix one line imports as mage:imports (#204)
Browse files Browse the repository at this point in the history
* fix one line imports as mage:imports

fixes #194
  • Loading branch information
natefinch authored Dec 11, 2018
1 parent edea463 commit aedfce6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
21 changes: 21 additions & 0 deletions mage/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,24 @@ func TestMageImportsAliasToNS(t *testing.T) {
t.Fatalf("expected: %q got: %q", expected, actual)
}
}

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

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 := "build\n"
if actual != expected {
t.Fatalf("expected: %q got: %q", expected, actual)
}
}
6 changes: 6 additions & 0 deletions mage/testdata/mageimport/oneline/magefile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// +build mage

package main

// mage:import
import _ "github.com/magefile/mage/mage/testdata/mageimport/oneline/other"
7 changes: 7 additions & 0 deletions mage/testdata/mageimport/oneline/other/other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package other

import "fmt"

func Build() {
fmt.Println("build")
}
34 changes: 22 additions & 12 deletions parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,24 +319,34 @@ func setImports(gocmd string, pi *PkgInfo) error {
importNames := map[string]string{}
rootImports := []string{}
for _, f := range pi.AstPkg.Files {
for _, imp := range f.Imports {
name, alias, ok := getImportPath(imp)
if !ok {
for _, d := range f.Decls {
gen, ok := d.(*ast.GenDecl)
if !ok || gen.Tok != token.IMPORT {
continue
}
if alias != "" {
debug.Printf("found %s: %s (%s)", importTag, name, alias)
if importNames[alias] != "" {
return fmt.Errorf("duplicate import alias: %q", alias)
for j := 0; j < len(gen.Specs); j++ {
spec := gen.Specs[j]
impspec := spec.(*ast.ImportSpec)
if len(gen.Specs) == 1 && gen.Lparen == token.NoPos && impspec.Doc == nil {
impspec.Doc = gen.Doc
}
name, alias, ok := getImportPath(impspec)
if !ok {
continue
}
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
} else {
debug.Printf("found %s: %s", importTag, name)
rootImports = append(rootImports, name)
}
importNames[alias] = name
} else {
debug.Printf("found %s: %s", importTag, name)
rootImports = append(rootImports, name)
}
}
}

imports, err := getNamedImports(gocmd, importNames)
if err != nil {
return err
Expand Down

0 comments on commit aedfce6

Please sign in to comment.