From 98b32214fc79fb17029b9feee3a447406b265cb6 Mon Sep 17 00:00:00 2001 From: lyyyuna Date: Wed, 12 Aug 2020 10:42:47 +0800 Subject: [PATCH] update test samples --- pkg/build/gomodules.go | 31 ++++++++++++++-------------- pkg/build/gomodules_test.go | 18 ++++++++++++---- pkg/build/tmpfolder.go | 13 ++++++++++++ tests/samples/gomod_samples/a/go.mod | 6 +++++- 4 files changed, 48 insertions(+), 20 deletions(-) diff --git a/pkg/build/gomodules.go b/pkg/build/gomodules.go index 1c8f2c1..be6240d 100644 --- a/pkg/build/gomodules.go +++ b/pkg/build/gomodules.go @@ -19,7 +19,6 @@ package build import ( "io/ioutil" "path/filepath" - "strings" "github.com/otiai10/copy" log "github.com/sirupsen/logrus" @@ -42,40 +41,42 @@ func (b *Build) cpGoModulesProject() { } } -func (b *Build) updateGoModFile() (string, error) { +func (b *Build) updateGoModFile() (updateFlag bool, newModFile []byte, err error) { tempModfile := filepath.Join(b.TmpDir, "go.mod") buf, err := ioutil.ReadFile(tempModfile) if err != nil { - return "", err + return } oriGoModFile, err := modfile.Parse(tempModfile, buf, nil) if err != nil { - return "", err + return } + updateFlag = false for index := range oriGoModFile.Replace { replace := oriGoModFile.Replace[index] oldPath := replace.Old.Path oldVersion := replace.Old.Version newPath := replace.New.Path newVersion := replace.New.Version - if strings.HasPrefix(replace.New.Path, "..") { + // replace to a local filesystem does not have a version + // absolute path no need to rewrite + if newVersion == "" && !filepath.IsAbs(newPath) { + var absPath string fullPath := filepath.Join(b.ModRoot, newPath) - absPath, err := filepath.Abs(fullPath) + absPath, err = filepath.Abs(fullPath) if err != nil { - return "", err - } - err = oriGoModFile.DropReplace(oldPath, oldVersion) - err = oriGoModFile.AddReplace(oldPath, oldVersion, absPath, newVersion) - if err != nil { - return "", err + return } + _ = oriGoModFile.DropReplace(oldPath, oldVersion) + _ = oriGoModFile.AddReplace(oldPath, oldVersion, absPath, newVersion) + updateFlag = true } } oriGoModFile.Cleanup() - newGoModFile, err := oriGoModFile.Format() + newModFile, err = oriGoModFile.Format() if err != nil { - return "", err + return } - return string(newGoModFile), nil + return } diff --git a/pkg/build/gomodules_test.go b/pkg/build/gomodules_test.go index f59ac91..18ccde2 100644 --- a/pkg/build/gomodules_test.go +++ b/pkg/build/gomodules_test.go @@ -64,9 +64,17 @@ func TestUpdateModFileIfContainsReplace(t *testing.T) { ModRoot: "/aa/bb/cc", } - newmod, err := b.updateGoModFile() + // replace with relative local file path should be rewrite + updated, newmod, err := b.updateGoModFile() assert.Equal(t, err, nil) - assert.Contains(t, newmod, "replace github.com/qiniu/bar => /aa/bb/home/foo/bar") + assert.Equal(t, updated, true) + assert.Contains(t, string(newmod), "replace github.com/qiniu/bar => /aa/bb/home/foo/bar") + + // old replace should be removed + assert.NotContains(t, string(newmod), "github.com/qiniu/bar => ../home/foo/bar") + + // normal replace should not be rewrite + assert.Contains(t, string(newmod), "github.com/qiniu/bar2 => github.com/baniu/bar3 v1.2.3") } // test wrong go mod file @@ -77,8 +85,9 @@ func TestWithWrongGoModFile(t *testing.T) { ModRoot: "/aa/bb/cc", } - _, err := b.updateGoModFile() + updated, _, err := b.updateGoModFile() assert.Equal(t, errors.Is(err, os.ErrNotExist), true) + assert.Equal(t, updated, false) // a wrong format go mod workingDir = filepath.Join(baseDir, "../../tests/samples/gomod_samples/b") @@ -87,6 +96,7 @@ func TestWithWrongGoModFile(t *testing.T) { ModRoot: "/aa/bb/cc", } - _, err = b.updateGoModFile() + updated, _, err = b.updateGoModFile() assert.NotEqual(t, err, nil) + assert.Equal(t, updated, false) } diff --git a/pkg/build/tmpfolder.go b/pkg/build/tmpfolder.go index fa98786..95b4195 100644 --- a/pkg/build/tmpfolder.go +++ b/pkg/build/tmpfolder.go @@ -20,6 +20,7 @@ import ( "crypto/sha256" "errors" "fmt" + "io/ioutil" "os" "path/filepath" "strings" @@ -100,6 +101,18 @@ func (b *Build) mvProjectsToTmp() error { b.cpLegacyProject() } else if b.IsMod == true { // go 1.11, 1.12 has no Build.Root b.cpGoModulesProject() + updated, newGoModContent, err := b.updateGoModFile() + if err != nil { + return fmt.Errorf("fail to generate new go.mod: %v", err) + } + log.Infof("go.mod needs rewrite? %v", updated) + if updated { + tmpModFile := filepath.Join(b.TmpDir, "go.mod") + err := ioutil.WriteFile(tmpModFile, newGoModContent, 0666) + if err != nil { + return fmt.Errorf("fail to update go.mod: %v", err) + } + } } else if b.IsMod == false && b.Root == "" { b.TmpWorkingDir = b.TmpDir b.cpNonStandardLegacy() diff --git a/tests/samples/gomod_samples/a/go.mod b/tests/samples/gomod_samples/a/go.mod index 1bfcf0c..d47b8c2 100644 --- a/tests/samples/gomod_samples/a/go.mod +++ b/tests/samples/gomod_samples/a/go.mod @@ -1,7 +1,11 @@ module example.com/gg/a -replace github.com/qiniu/bar => ../home/foo/bar +replace ( + github.com/qiniu/bar => ../home/foo/bar + github.com/qiniu/bar2 => github.com/baniu/bar3 v1.2.3 +) require ( github.com/qiniu/bar v1.0.0 + github.com/qiniu/bar2 v1.2.0 )