update test samples
This commit is contained in:
parent
4dfd0bc735
commit
98b32214fc
@ -19,7 +19,6 @@ package build
|
|||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/otiai10/copy"
|
"github.com/otiai10/copy"
|
||||||
log "github.com/sirupsen/logrus"
|
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")
|
tempModfile := filepath.Join(b.TmpDir, "go.mod")
|
||||||
buf, err := ioutil.ReadFile(tempModfile)
|
buf, err := ioutil.ReadFile(tempModfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return
|
||||||
}
|
}
|
||||||
oriGoModFile, err := modfile.Parse(tempModfile, buf, nil)
|
oriGoModFile, err := modfile.Parse(tempModfile, buf, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateFlag = false
|
||||||
for index := range oriGoModFile.Replace {
|
for index := range oriGoModFile.Replace {
|
||||||
replace := oriGoModFile.Replace[index]
|
replace := oriGoModFile.Replace[index]
|
||||||
oldPath := replace.Old.Path
|
oldPath := replace.Old.Path
|
||||||
oldVersion := replace.Old.Version
|
oldVersion := replace.Old.Version
|
||||||
newPath := replace.New.Path
|
newPath := replace.New.Path
|
||||||
newVersion := replace.New.Version
|
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)
|
fullPath := filepath.Join(b.ModRoot, newPath)
|
||||||
absPath, err := filepath.Abs(fullPath)
|
absPath, err = filepath.Abs(fullPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return
|
||||||
}
|
|
||||||
err = oriGoModFile.DropReplace(oldPath, oldVersion)
|
|
||||||
err = oriGoModFile.AddReplace(oldPath, oldVersion, absPath, newVersion)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
}
|
||||||
|
_ = oriGoModFile.DropReplace(oldPath, oldVersion)
|
||||||
|
_ = oriGoModFile.AddReplace(oldPath, oldVersion, absPath, newVersion)
|
||||||
|
updateFlag = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
oriGoModFile.Cleanup()
|
oriGoModFile.Cleanup()
|
||||||
newGoModFile, err := oriGoModFile.Format()
|
newModFile, err = oriGoModFile.Format()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return
|
||||||
}
|
}
|
||||||
return string(newGoModFile), nil
|
return
|
||||||
}
|
}
|
||||||
|
@ -64,9 +64,17 @@ func TestUpdateModFileIfContainsReplace(t *testing.T) {
|
|||||||
ModRoot: "/aa/bb/cc",
|
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.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
|
// test wrong go mod file
|
||||||
@ -77,8 +85,9 @@ func TestWithWrongGoModFile(t *testing.T) {
|
|||||||
ModRoot: "/aa/bb/cc",
|
ModRoot: "/aa/bb/cc",
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := b.updateGoModFile()
|
updated, _, err := b.updateGoModFile()
|
||||||
assert.Equal(t, errors.Is(err, os.ErrNotExist), true)
|
assert.Equal(t, errors.Is(err, os.ErrNotExist), true)
|
||||||
|
assert.Equal(t, updated, false)
|
||||||
|
|
||||||
// a wrong format go mod
|
// a wrong format go mod
|
||||||
workingDir = filepath.Join(baseDir, "../../tests/samples/gomod_samples/b")
|
workingDir = filepath.Join(baseDir, "../../tests/samples/gomod_samples/b")
|
||||||
@ -87,6 +96,7 @@ func TestWithWrongGoModFile(t *testing.T) {
|
|||||||
ModRoot: "/aa/bb/cc",
|
ModRoot: "/aa/bb/cc",
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = b.updateGoModFile()
|
updated, _, err = b.updateGoModFile()
|
||||||
assert.NotEqual(t, err, nil)
|
assert.NotEqual(t, err, nil)
|
||||||
|
assert.Equal(t, updated, false)
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ import (
|
|||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@ -100,6 +101,18 @@ func (b *Build) mvProjectsToTmp() error {
|
|||||||
b.cpLegacyProject()
|
b.cpLegacyProject()
|
||||||
} else if b.IsMod == true { // go 1.11, 1.12 has no Build.Root
|
} else if b.IsMod == true { // go 1.11, 1.12 has no Build.Root
|
||||||
b.cpGoModulesProject()
|
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 == "" {
|
} else if b.IsMod == false && b.Root == "" {
|
||||||
b.TmpWorkingDir = b.TmpDir
|
b.TmpWorkingDir = b.TmpDir
|
||||||
b.cpNonStandardLegacy()
|
b.cpNonStandardLegacy()
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
module example.com/gg/a
|
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 (
|
require (
|
||||||
github.com/qiniu/bar v1.0.0
|
github.com/qiniu/bar v1.0.0
|
||||||
|
github.com/qiniu/bar2 v1.2.0
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user