From 5890daa417561646ccf4805ea22a3fe9ecc965fa Mon Sep 17 00:00:00 2001 From: lyyyuna Date: Wed, 12 Aug 2020 11:43:50 +0800 Subject: [PATCH] add integration test --- pkg/build/gomodules.go | 24 ++++++++++++++------- pkg/build/gomodules_test.go | 1 + pkg/build/tmpfolder.go | 8 +++---- tests/build.bats | 13 +++++++++++ tests/samples/gomod_replace_library/bar.go | 7 ++++++ tests/samples/gomod_replace_library/go.mod | 4 ++++ tests/samples/gomod_replace_project/go.mod | 7 ++++++ tests/samples/gomod_replace_project/main.go | 9 ++++++++ 8 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 tests/samples/gomod_replace_library/bar.go create mode 100644 tests/samples/gomod_replace_library/go.mod create mode 100644 tests/samples/gomod_replace_project/go.mod create mode 100644 tests/samples/gomod_replace_project/main.go diff --git a/pkg/build/gomodules.go b/pkg/build/gomodules.go index be6240d..32c9b79 100644 --- a/pkg/build/gomodules.go +++ b/pkg/build/gomodules.go @@ -41,6 +41,14 @@ func (b *Build) cpGoModulesProject() { } } +// updateGoModFile rewrites the go.mod file in the temporary directory, +// if it has a 'replace' directive, and the directive has a relative local path +// it will be rewritten with a absolute path. +// ex. +// suppose original project is located at /path/to/aa/bb/cc, go.mod contains a directive: +// 'replace github.com/qiniu/bar => ../home/foo/bar' +// after the project is copied to temporary directory, it should be rewritten as +// 'replace github.com/qiniu/bar => /path/to/aa/bb/home/foo/bar' func (b *Build) updateGoModFile() (updateFlag bool, newModFile []byte, err error) { tempModfile := filepath.Join(b.TmpDir, "go.mod") buf, err := ioutil.ReadFile(tempModfile) @@ -64,19 +72,19 @@ func (b *Build) updateGoModFile() (updateFlag bool, newModFile []byte, err error if newVersion == "" && !filepath.IsAbs(newPath) { var absPath string fullPath := filepath.Join(b.ModRoot, newPath) - absPath, err = filepath.Abs(fullPath) - if err != nil { - return - } + absPath, _ = filepath.Abs(fullPath) + // DropReplace & AddReplace will not return error + // so no need to check the error _ = oriGoModFile.DropReplace(oldPath, oldVersion) _ = oriGoModFile.AddReplace(oldPath, oldVersion, absPath, newVersion) updateFlag = true } } oriGoModFile.Cleanup() - newModFile, err = oriGoModFile.Format() - if err != nil { - return - } + // Format will not return error, so ignore the returned error + // func (f *File) Format() ([]byte, error) { + // return Format(f.Syntax), nil + // } + newModFile, _ = oriGoModFile.Format() return } diff --git a/pkg/build/gomodules_test.go b/pkg/build/gomodules_test.go index 18ccde2..e0e6a6d 100644 --- a/pkg/build/gomodules_test.go +++ b/pkg/build/gomodules_test.go @@ -79,6 +79,7 @@ func TestUpdateModFileIfContainsReplace(t *testing.T) { // test wrong go mod file func TestWithWrongGoModFile(t *testing.T) { + // go.mod not exist workingDir := filepath.Join(baseDir, "../../tests/samples/xxxxxxxxxxxx/a") b := &Build{ TmpDir: workingDir, diff --git a/pkg/build/tmpfolder.go b/pkg/build/tmpfolder.go index 95b4195..cf5a778 100644 --- a/pkg/build/tmpfolder.go +++ b/pkg/build/tmpfolder.go @@ -76,10 +76,9 @@ func (b *Build) mvProjectsToTmp() error { // Create a new tmp folder err := os.MkdirAll(filepath.Join(b.TmpDir, "src"), os.ModePerm) if err != nil { - log.Errorf("Fail to create the temporary build directory. The err is: %v", err) - return err + return fmt.Errorf("Fail to create the temporary build directory. The err is: %v", err) } - log.Printf("Tmp project generated in: %v", b.TmpDir) + log.Infof("Tmp project generated in: %v", b.TmpDir) // traverse pkg list to get project meta info b.IsMod, b.Root, err = b.traversePkgsList() @@ -90,7 +89,6 @@ func (b *Build) mvProjectsToTmp() error { // we should get corresponding working directory in temporary directory b.TmpWorkingDir, err = b.getTmpwd() if err != nil { - log.Errorf("fail to get workding directory in temporary directory: %v", err) return fmt.Errorf("getTmpwd failed with error: %w", err) } // issue #14 @@ -108,7 +106,7 @@ func (b *Build) mvProjectsToTmp() error { log.Infof("go.mod needs rewrite? %v", updated) if updated { tmpModFile := filepath.Join(b.TmpDir, "go.mod") - err := ioutil.WriteFile(tmpModFile, newGoModContent, 0666) + err := ioutil.WriteFile(tmpModFile, newGoModContent, os.ModePerm) if err != nil { return fmt.Errorf("fail to update go.mod: %v", err) } diff --git a/tests/build.bats b/tests/build.bats index e2f17a0..b4d8567 100755 --- a/tests/build.bats +++ b/tests/build.bats @@ -72,5 +72,18 @@ setup() { info build3 output: $output [ "$status" -eq 0 ] + wait $profile_pid +} + +@test "test goc build with go.mod project which contains replace directive" { + cd samples/gomod_replace_project + + wait_profile_backend "build4" & + profile_pid=$! + + run gocc build --debug --debugcisyncfile ci-sync.bak; + info build4 output: $output + [ "$status" -eq 0 ] + wait $profile_pid } \ No newline at end of file diff --git a/tests/samples/gomod_replace_library/bar.go b/tests/samples/gomod_replace_library/bar.go new file mode 100644 index 0000000..7307d25 --- /dev/null +++ b/tests/samples/gomod_replace_library/bar.go @@ -0,0 +1,7 @@ +package foo + +import "fmt" + +func Bar() { + fmt.Println("foo bar") +} diff --git a/tests/samples/gomod_replace_library/go.mod b/tests/samples/gomod_replace_library/go.mod new file mode 100644 index 0000000..b16fdd4 --- /dev/null +++ b/tests/samples/gomod_replace_library/go.mod @@ -0,0 +1,4 @@ +module qiniu.com/foo + + +go 1.11 diff --git a/tests/samples/gomod_replace_project/go.mod b/tests/samples/gomod_replace_project/go.mod new file mode 100644 index 0000000..d92ad34 --- /dev/null +++ b/tests/samples/gomod_replace_project/go.mod @@ -0,0 +1,7 @@ +module example.com/simple-project + +require qiniu.com/foo v0.0.0 + +replace qiniu.com/foo => ../gomod_replace_library + +go 1.11 diff --git a/tests/samples/gomod_replace_project/main.go b/tests/samples/gomod_replace_project/main.go new file mode 100644 index 0000000..d370f07 --- /dev/null +++ b/tests/samples/gomod_replace_project/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "qiniu.com/foo" +) + +func main() { + foo.Bar() +}