diff --git a/pkg/build/build.go b/pkg/build/build.go index 052c00d..f4edb55 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -133,8 +133,11 @@ func (b *Build) determineOutputDir(outputDir string) (string, error) { targetName := "" for _, pkg := range b.Pkgs { if pkg.Name == "main" { - _, file := filepath.Split(pkg.Target) - targetName = file + if pkg.Target != ""{ + targetName = filepath.Base(pkg.Target) + }else { + targetName = filepath.Base(pkg.Dir) + } break } } diff --git a/pkg/build/legacy.go b/pkg/build/legacy.go index 8583895..6eca135 100644 --- a/pkg/build/legacy.go +++ b/pkg/build/legacy.go @@ -17,14 +17,13 @@ package build import ( - "os" "path/filepath" + "strings" log "github.com/sirupsen/logrus" "github.com/otiai10/copy" "github.com/qiniu/goc/pkg/cover" - "strings" ) func (b *Build) cpLegacyProject() { @@ -38,13 +37,11 @@ func (b *Build) cpLegacyProject() { continue } - if err := b.copyDir(v, b.TmpDir); err != nil { + if err := b.copyDir(v); err != nil { log.Errorf("Failed to Copy the folder from %v to %v, the error is: %v ", src, dst, err) } visited[src] = true - - //b.cpDepPackages(v, visited) } if b.IsMod { for _, v := range b.Pkgs { @@ -68,35 +65,7 @@ func (b *Build) cpLegacyProject() { } } -// only cp dependency in root(current gopath), -// skip deps in other GOPATHs -// only used for version before go 1.11.4 -func (b *Build) cpDepPackages(pkg *cover.Package, visited map[string]bool) { - gopath := pkg.Root - for _, dep := range pkg.Deps { - src := filepath.Join(gopath, "src", dep) - // Check if copied - if _, ok := visited[src]; ok { - // Skip if already copied - continue - } - // Check if we can found in the root gopath - _, err := os.Stat(src) - if err != nil { - continue - } - - dst := filepath.Join(b.TmpDir, "src", dep) - - if err := copy.Copy(src, dst); err != nil { - log.Errorf("Failed to Copy the folder from %v to %v, the error is: %v ", src, dst, err) - } - - visited[src] = true - } -} - -func (b *Build) copyDir(pkg *cover.Package, tmpDir string) error { +func (b *Build) copyDir(pkg *cover.Package) error { fileList := []string{} dir := pkg.Dir fileList = append(fileList, pkg.GoFiles...) @@ -119,8 +88,8 @@ func (b *Build) copyDir(pkg *cover.Package, tmpDir string) error { } else { root = pkg.Root } - src = strings.TrimPrefix(pkg.Dir, root) // get the relative path of the files - dst := filepath.Join(tmpDir, src, file) // it will adapt the case where src is "" + src = strings.TrimPrefix(pkg.Dir, root) // get the relative path of the files + dst := filepath.Join(b.TmpDir, src, file) // it will adapt the case where src is "" if err := copy.Copy(p, dst); err != nil { log.Errorf("Failed to Copy the folder from %v to %v, the error is: %v ", src, dst, err) return err diff --git a/pkg/build/legacy_test.go b/pkg/build/legacy_test.go index 1c5f6c6..cb786ed 100644 --- a/pkg/build/legacy_test.go +++ b/pkg/build/legacy_test.go @@ -17,7 +17,6 @@ package build import ( - "path/filepath" "strings" "testing" @@ -47,36 +46,15 @@ func TestLegacyProjectCopyWithUnexistedDir(t *testing.T) { assert.Equal(t, strings.Contains(output, "Failed to Copy"), true) } -// copy in cpDepPackages of invalid dst name -func TestDepPackagesCopyWithInvalidDir(t *testing.T) { - gopath := filepath.Join(baseDir, "../../tests/samples/simple_gopath_project") - pkg := &cover.Package{ - Module: &cover.ModulePublic{ - Dir: "not exied, ia mas duser", - }, - Root: gopath, - Deps: []string{"qiniu.com", "ddfee 2344234"}, - } - b := &Build{ - TmpDir: "/", // "/" is invalid dst in Linux, it should fail - } - - output := captureOutput(func() { - visited := make(map[string]bool) - - b.cpDepPackages(pkg, visited) - }) - assert.Equal(t, strings.Contains(output, "Failed to Copy"), true) -} - func TestCopyDir(t *testing.T) { wd, _ := os.Getwd() pkg := &cover.Package{Dir: wd, Root: wd, GoFiles: []string{"build.go", "legacy.go"}, CgoFiles: []string{"run.go"}} tmpDir := "/tmp/test/" b := &Build{ WorkingDir: "empty", + TmpDir: tmpDir, } assert.NoError(t, os.MkdirAll(tmpDir, os.ModePerm)) defer os.RemoveAll(tmpDir) - assert.NoError(t, b.copyDir(pkg, tmpDir)) + assert.NoError(t, b.copyDir(pkg)) } diff --git a/pkg/build/tmpfolder.go b/pkg/build/tmpfolder.go index e4687ef..95b9cf5 100644 --- a/pkg/build/tmpfolder.go +++ b/pkg/build/tmpfolder.go @@ -57,13 +57,6 @@ func (b *Build) MvProjectsToTmp() error { } else { b.NewGOPATH = fmt.Sprintf("%v:%v", b.TmpDir, b.OriGOPATH) } - // fix #14: unable to build project not in GOPATH in legacy mode - // this kind of project does not have a pkg.Root value - // go 1.11, 1.12 has no pkg.Root, - // so add b.IsMod == false as secondary judgement - if b.Root == "" && b.IsMod == false { - b.NewGOPATH = b.OriGOPATH - } log.Infof("New GOPATH: %v", b.NewGOPATH) return nil } diff --git a/pkg/build/tmpfolder_test.go b/pkg/build/tmpfolder_test.go index 2183280..cf311e6 100644 --- a/pkg/build/tmpfolder_test.go +++ b/pkg/build/tmpfolder_test.go @@ -95,7 +95,7 @@ func TestLegacyProjectNotInGoPATH(t *testing.T) { os.Setenv("GO111MODULE", "off") b, _ := NewBuild("", []string{"."}, workingDir, "") - if b.OriGOPATH != b.NewGOPATH { + if !strings.Contains(b.NewGOPATH,b.OriGOPATH) { t.Fatalf("New GOPATH should be same with old GOPATH, for this kind of project. New: %v, old: %v", b.NewGOPATH, b.OriGOPATH) }