add integration test
This commit is contained in:
parent
98b32214fc
commit
5890daa417
@ -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
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
7
tests/samples/gomod_replace_library/bar.go
Normal file
7
tests/samples/gomod_replace_library/bar.go
Normal file
@ -0,0 +1,7 @@
|
||||
package foo
|
||||
|
||||
import "fmt"
|
||||
|
||||
func Bar() {
|
||||
fmt.Println("foo bar")
|
||||
}
|
4
tests/samples/gomod_replace_library/go.mod
Normal file
4
tests/samples/gomod_replace_library/go.mod
Normal file
@ -0,0 +1,4 @@
|
||||
module qiniu.com/foo
|
||||
|
||||
|
||||
go 1.11
|
7
tests/samples/gomod_replace_project/go.mod
Normal file
7
tests/samples/gomod_replace_project/go.mod
Normal file
@ -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
|
9
tests/samples/gomod_replace_project/main.go
Normal file
9
tests/samples/gomod_replace_project/main.go
Normal file
@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"qiniu.com/foo"
|
||||
)
|
||||
|
||||
func main() {
|
||||
foo.Bar()
|
||||
}
|
Loading…
Reference in New Issue
Block a user