Skip to content

Commit

Permalink
fix(gomodcheck): simply find target path
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlJi committed Sep 20, 2024
1 parent 285f161 commit 05f2473
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 30 deletions.
7 changes: 1 addition & 6 deletions internal/linters/go/gomodcheck/gomodcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/qiniu/reviewbot/internal/linters"
Expand Down Expand Up @@ -50,16 +49,12 @@ func goModCheckOutput(log *xlog.Logger, a linters.Agent) (map[string][]linters.L
log.Errorf("Error parsing %s: %s", goModPath, err)
return output, err
}
re := regexp.MustCompile(`^(?:\.\./)+`)

for _, replace := range mod.Replace {
var parsePath string
if !strings.HasPrefix(replace.New.Path, "../") {
continue
}
matches := re.FindString(replace.New.Path)
parsePath = filepath.Join(filepath.Dir(goModPath), matches)

parsePath := filepath.Join(filepath.Dir(goModPath), replace.New.Path)
isSub, err := isSubdirectory(a.RepoDir, parsePath)
if err != nil {
log.Errorf("failed to compare whether A is a subdirectory of B : %v", err)
Expand Down
75 changes: 51 additions & 24 deletions internal/linters/go/gomodcheck/gomodcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

func TestGoModCheck(t *testing.T) {
modDir := "c/go.mod"
tcs := []struct {
id string
content []byte
Expand All @@ -21,19 +20,18 @@ func TestGoModCheck(t *testing.T) {
}{
{
id: "case1 : cross-repository local replacement ",
content: []byte("replace github.com/xxxxx/xxx v0.0.0 => ../../github.com/xxx/xxxx"),
content: []byte("replace github.com/a/c v0.0.0 => ../../github.com/c/d"),
input: linters.Agent{
RepoDir: "a/b",
PullRequestChangedFiles: []*github.CommitFile{
{
Filename: &modDir,
Filename: github.String("c/go.mod"),
},
},
},
want: map[string][]linters.LinterOutput{
"c/go.mod": {
{
File: modDir,
File: "c/go.mod",
Line: 1,
Column: 1,
Message: "cross-repository local replacement are not allowed[reviewbot]\nfor more information see https://github.com/qiniu/reviewbot/issues/275",
Expand All @@ -43,51 +41,80 @@ func TestGoModCheck(t *testing.T) {
},
{
id: "case2 : valid local replacement ",
content: []byte("replace github.com/xxxxx/xxx v0.0.0 => ../github.com/xxx/xxxx"),
content: []byte("replace github.com/a/b v0.0.0 => ../github.com/c/d"),
input: linters.Agent{
RepoDir: "a/b",
PullRequestChangedFiles: []*github.CommitFile{
{
Filename: &modDir,
Filename: github.String("c/go.mod"),
},
},
},
want: map[string][]linters.LinterOutput{},
},
{
id: "case3 : valid non-local replacement ",
content: []byte("replace github.com/xxxxx/xxx v0.0.0 => github.com/xxx/xxxx v1.1.1"),
content: []byte("replace github.com/a/b v0.0.0 => github.com/c/d v1.1.1"),
input: linters.Agent{
RepoDir: "a/b",
PullRequestChangedFiles: []*github.CommitFile{
{
Filename: &modDir,
Filename: github.String("c/go.mod"),
},
},
},
want: map[string][]linters.LinterOutput{},
},
{
id: "case4 : multiple go.mod files",
content: []byte("replace github.com/a/b v0.0.0 => ../../github.com/c/d"),
input: linters.Agent{
PullRequestChangedFiles: []*github.CommitFile{
{
Filename: github.String("c/go.mod"),
},
{
Filename: github.String("d/go.mod"),
},
},
},
want: map[string][]linters.LinterOutput{
"c/go.mod": {
{
File: "c/go.mod",
Line: 1,
Column: 1,
Message: "cross-repository local replacement are not allowed[reviewbot]\nfor more information see https://github.com/qiniu/reviewbot/issues/275",
},
},
"d/go.mod": {
{
File: "d/go.mod",
Line: 1,
Column: 1,
Message: "cross-repository local replacement are not allowed[reviewbot]\nfor more information see https://github.com/qiniu/reviewbot/issues/275",
},
},
},
},
}

for _, tc := range tcs {
t.Run(tc.id, func(t *testing.T) {
filename := filepath.Join(tc.input.RepoDir, modDir)
err := os.MkdirAll(filepath.Dir(filename), 0o755)
if err != nil {
t.Errorf("Error creating directories:%v", err)
return
}
defer func() {
err = os.RemoveAll(filename)
// prepare go.mod files
for _, file := range tc.input.PullRequestChangedFiles {
filename := file.GetFilename()
dir := filepath.Dir(filename)
err := os.MkdirAll(dir, 0o755)
if err != nil {
t.Errorf("Error creating directories: %v", err)
return
}
defer os.RemoveAll(dir)

err = os.WriteFile(filename, tc.content, 0o600)
if err != nil {
t.Errorf("Error writing to file: %v", err)
return
}
}()
err = os.WriteFile(filename, tc.content, 0o644)
if err != nil {
t.Errorf("Error writing to file: %v", err)
return
}

output, err := goModCheckOutput(&xlog.Logger{}, tc.input)
Expand Down

0 comments on commit 05f2473

Please sign in to comment.