compiler needs to link vendored packages

Этот коммит содержится в:
gedi 2019-03-15 16:46:41 +02:00
родитель f19201d47b
коммит 8e7bc15e74
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 56604CDCCC201556
2 изменённых файлов: 103 добавлений и 7 удалений

Просмотреть файл

@ -159,13 +159,32 @@ func Build(bin string) error {
return err return err
} }
// godog package may be vendored and may need importmap
vendored := maybeVendoredGodog()
// compile godog testmain package archive // compile godog testmain package archive
// we do not depend on CGO so a lot of checks are not necessary // 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") testMainPkgOut := filepath.Join(testdir, "main.a")
args := []string{ args := []string{
"-o", testMainPkgOut, "-o", testMainPkgOut,
"-importcfg", cfg, "-importcfg", compilerCfg,
"-p", "main", "-p", "main",
"-complete", "-complete",
} }
@ -181,7 +200,7 @@ func Build(bin string) error {
// link test suite executable // link test suite executable
args = []string{ args = []string{
"-o", bin, "-o", bin,
"-importcfg", cfg, "-importcfg", linkerCfg,
"-buildmode=exe", "-buildmode=exe",
} }
args = append(args, testMainPkgOut) args = append(args, testMainPkgOut)
@ -199,6 +218,26 @@ func Build(bin string) error {
return nil 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 { func importPackage(dir string) *build.Package {
pkg, _ := build.ImportDir(dir, 0) pkg, _ := build.ImportDir(dir, 0)

Просмотреть файл

@ -7,6 +7,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
) )
@ -140,6 +141,17 @@ func buildTestCommand(t *testing.T, args ...string) *exec.Cmd {
return exec.Command(bin, args...) 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) { func TestGodogBuildWithSourceNotInGoPath(t *testing.T) {
dir := filepath.Join(os.TempDir(), "godogs") dir := filepath.Join(os.TempDir(), "godogs")
err := buildTestPackage(dir, map[string]string{ err := buildTestPackage(dir, map[string]string{
@ -298,8 +310,8 @@ func TestGodogBuildWithinGopath(t *testing.T) {
} }
} }
func TestGodogBuildWithVendoredGodog(t *testing.T) { func TestGodogBuildWithVendoredGodogAndMod(t *testing.T) {
gopath := filepath.Join(os.TempDir(), "_gp") gopath := filepath.Join(os.TempDir(), "_gpc")
dir := filepath.Join(gopath, "src", "godogs") dir := filepath.Join(gopath, "src", "godogs")
err := buildTestPackage(dir, map[string]string{ err := buildTestPackage(dir, map[string]string{
"godogs.feature": builderFeatureFile, "godogs.feature": builderFeatureFile,
@ -338,8 +350,53 @@ func TestGodogBuildWithVendoredGodog(t *testing.T) {
var stdout, stderr bytes.Buffer var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout cmd.Stdout = &stdout
cmd.Stderr = &stderr cmd.Stderr = &stderr
cmd.Env = os.Environ() cmd.Env = append(envVarsWithoutGopath(), "GOPATH="+gopath)
cmd.Env = append(cmd.Env, "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 { if err := cmd.Run(); err != nil {
t.Log(stdout.String()) t.Log(stdout.String())