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) {
|
func (b *Build) updateGoModFile() (updateFlag bool, newModFile []byte, err error) {
|
||||||
tempModfile := filepath.Join(b.TmpDir, "go.mod")
|
tempModfile := filepath.Join(b.TmpDir, "go.mod")
|
||||||
buf, err := ioutil.ReadFile(tempModfile)
|
buf, err := ioutil.ReadFile(tempModfile)
|
||||||
@ -64,19 +72,19 @@ func (b *Build) updateGoModFile() (updateFlag bool, newModFile []byte, err error
|
|||||||
if newVersion == "" && !filepath.IsAbs(newPath) {
|
if newVersion == "" && !filepath.IsAbs(newPath) {
|
||||||
var absPath string
|
var absPath string
|
||||||
fullPath := filepath.Join(b.ModRoot, newPath)
|
fullPath := filepath.Join(b.ModRoot, newPath)
|
||||||
absPath, err = filepath.Abs(fullPath)
|
absPath, _ = filepath.Abs(fullPath)
|
||||||
if err != nil {
|
// DropReplace & AddReplace will not return error
|
||||||
return
|
// so no need to check the error
|
||||||
}
|
|
||||||
_ = oriGoModFile.DropReplace(oldPath, oldVersion)
|
_ = oriGoModFile.DropReplace(oldPath, oldVersion)
|
||||||
_ = oriGoModFile.AddReplace(oldPath, oldVersion, absPath, newVersion)
|
_ = oriGoModFile.AddReplace(oldPath, oldVersion, absPath, newVersion)
|
||||||
updateFlag = true
|
updateFlag = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
oriGoModFile.Cleanup()
|
oriGoModFile.Cleanup()
|
||||||
newModFile, err = oriGoModFile.Format()
|
// Format will not return error, so ignore the returned error
|
||||||
if err != nil {
|
// func (f *File) Format() ([]byte, error) {
|
||||||
return
|
// return Format(f.Syntax), nil
|
||||||
}
|
// }
|
||||||
|
newModFile, _ = oriGoModFile.Format()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -79,6 +79,7 @@ func TestUpdateModFileIfContainsReplace(t *testing.T) {
|
|||||||
|
|
||||||
// test wrong go mod file
|
// test wrong go mod file
|
||||||
func TestWithWrongGoModFile(t *testing.T) {
|
func TestWithWrongGoModFile(t *testing.T) {
|
||||||
|
// go.mod not exist
|
||||||
workingDir := filepath.Join(baseDir, "../../tests/samples/xxxxxxxxxxxx/a")
|
workingDir := filepath.Join(baseDir, "../../tests/samples/xxxxxxxxxxxx/a")
|
||||||
b := &Build{
|
b := &Build{
|
||||||
TmpDir: workingDir,
|
TmpDir: workingDir,
|
||||||
|
@ -76,10 +76,9 @@ func (b *Build) mvProjectsToTmp() error {
|
|||||||
// Create a new tmp folder
|
// Create a new tmp folder
|
||||||
err := os.MkdirAll(filepath.Join(b.TmpDir, "src"), os.ModePerm)
|
err := os.MkdirAll(filepath.Join(b.TmpDir, "src"), os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Fail to create the temporary build directory. The err is: %v", err)
|
return fmt.Errorf("Fail to create the temporary build directory. The err is: %v", err)
|
||||||
return 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
|
// traverse pkg list to get project meta info
|
||||||
b.IsMod, b.Root, err = b.traversePkgsList()
|
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
|
// we should get corresponding working directory in temporary directory
|
||||||
b.TmpWorkingDir, err = b.getTmpwd()
|
b.TmpWorkingDir, err = b.getTmpwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("fail to get workding directory in temporary directory: %v", err)
|
|
||||||
return fmt.Errorf("getTmpwd failed with error: %w", err)
|
return fmt.Errorf("getTmpwd failed with error: %w", err)
|
||||||
}
|
}
|
||||||
// issue #14
|
// issue #14
|
||||||
@ -108,7 +106,7 @@ func (b *Build) mvProjectsToTmp() error {
|
|||||||
log.Infof("go.mod needs rewrite? %v", updated)
|
log.Infof("go.mod needs rewrite? %v", updated)
|
||||||
if updated {
|
if updated {
|
||||||
tmpModFile := filepath.Join(b.TmpDir, "go.mod")
|
tmpModFile := filepath.Join(b.TmpDir, "go.mod")
|
||||||
err := ioutil.WriteFile(tmpModFile, newGoModContent, 0666)
|
err := ioutil.WriteFile(tmpModFile, newGoModContent, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("fail to update go.mod: %v", err)
|
return fmt.Errorf("fail to update go.mod: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -74,3 +74,16 @@ setup() {
|
|||||||
|
|
||||||
wait $profile_pid
|
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