From 8e7bc15e741c4f6c93c0fc2e797ed869cbc1b8f7 Mon Sep 17 00:00:00 2001 From: gedi Date: Fri, 15 Mar 2019 16:46:41 +0200 Subject: [PATCH] compiler needs to link vendored packages --- builder_go110.go | 45 ++++++++++++++++++++++++++++++--- builder_test.go | 65 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 103 insertions(+), 7 deletions(-) diff --git a/builder_go110.go b/builder_go110.go index 81e45bb..7bb63ad 100644 --- a/builder_go110.go +++ b/builder_go110.go @@ -159,13 +159,32 @@ func Build(bin string) error { return err } + // godog package may be vendored and may need importmap + vendored := maybeVendoredGodog() + // compile godog testmain package archive // we do not depend on CGO so a lot of checks are not necessary - cfg := filepath.Join(testdir, "importcfg.link") + linkerCfg := filepath.Join(testdir, "importcfg.link") + compilerCfg := linkerCfg + if vendored != nil { + data, err := ioutil.ReadFile(linkerCfg) + if err != nil { + return err + } + + data = append(data, []byte(fmt.Sprintf("importmap %s=%s\n", godogImportPath, vendored.ImportPath))...) + compilerCfg = filepath.Join(testdir, "importcfg") + + err = ioutil.WriteFile(compilerCfg, data, 0644) + if err != nil { + return err + } + } + testMainPkgOut := filepath.Join(testdir, "main.a") args := []string{ "-o", testMainPkgOut, - "-importcfg", cfg, + "-importcfg", compilerCfg, "-p", "main", "-complete", } @@ -181,7 +200,7 @@ func Build(bin string) error { // link test suite executable args = []string{ "-o", bin, - "-importcfg", cfg, + "-importcfg", linkerCfg, "-buildmode=exe", } args = append(args, testMainPkgOut) @@ -199,6 +218,26 @@ func Build(bin string) error { return nil } +func maybeVendoredGodog() *build.Package { + dir, err := filepath.Abs(".") + if err != nil { + return nil + } + + for _, gopath := range gopaths { + gopath = filepath.Join(gopath, "src") + for strings.HasPrefix(dir, gopath) && dir != gopath { + pkg, err := build.ImportDir(filepath.Join(dir, "vendor", godogImportPath), 0) + if err != nil { + dir = filepath.Dir(dir) + continue + } + return pkg + } + } + return nil +} + func importPackage(dir string) *build.Package { pkg, _ := build.ImportDir(dir, 0) diff --git a/builder_test.go b/builder_test.go index f61ccdd..46a9abd 100644 --- a/builder_test.go +++ b/builder_test.go @@ -7,6 +7,7 @@ import ( "os" "os/exec" "path/filepath" + "strings" "testing" ) @@ -140,6 +141,17 @@ func buildTestCommand(t *testing.T, args ...string) *exec.Cmd { return exec.Command(bin, args...) } +func envVarsWithoutGopath() []string { + var env []string + for _, def := range os.Environ() { + if strings.Index(def, "GOPATH=") == 0 { + continue + } + env = append(env, def) + } + return env +} + func TestGodogBuildWithSourceNotInGoPath(t *testing.T) { dir := filepath.Join(os.TempDir(), "godogs") err := buildTestPackage(dir, map[string]string{ @@ -298,8 +310,8 @@ func TestGodogBuildWithinGopath(t *testing.T) { } } -func TestGodogBuildWithVendoredGodog(t *testing.T) { - gopath := filepath.Join(os.TempDir(), "_gp") +func TestGodogBuildWithVendoredGodogAndMod(t *testing.T) { + gopath := filepath.Join(os.TempDir(), "_gpc") dir := filepath.Join(gopath, "src", "godogs") err := buildTestPackage(dir, map[string]string{ "godogs.feature": builderFeatureFile, @@ -338,8 +350,53 @@ func TestGodogBuildWithVendoredGodog(t *testing.T) { var stdout, stderr bytes.Buffer cmd.Stdout = &stdout cmd.Stderr = &stderr - cmd.Env = os.Environ() - cmd.Env = append(cmd.Env, "GOPATH="+gopath) + cmd.Env = append(envVarsWithoutGopath(), "GOPATH="+gopath) + + if err := cmd.Run(); err != nil { + t.Log(stdout.String()) + t.Log(stderr.String()) + t.Fatal(err) + } +} + +func TestGodogBuildWithVendoredGodogWithoutModule(t *testing.T) { + gopath := filepath.Join(os.TempDir(), "_gp") + dir := filepath.Join(gopath, "src", "godogs") + err := buildTestPackage(dir, map[string]string{ + "godogs.feature": builderFeatureFile, + }) + if err != nil { + os.RemoveAll(gopath) + t.Fatal(err) + } + defer os.RemoveAll(gopath) + + pkg := filepath.Join(dir, "vendor", "github.com", "DATA-DOG") + if err := os.MkdirAll(pkg, 0755); err != nil { + t.Fatal(err) + } + + prevDir, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + + // symlink godog package + if err := os.Symlink(prevDir, filepath.Join(pkg, "godog")); err != nil { + t.Fatal(err) + } + + if err := os.Chdir(dir); err != nil { + t.Fatal(err) + } + defer os.Chdir(prevDir) + + cmd := buildTestCommand(t, "godogs.feature") + + var stdout, stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + cmd.Env = append(envVarsWithoutGopath(), "GOPATH="+gopath) if err := cmd.Run(); err != nil { t.Log(stdout.String())