Skip to content

Commit 7fa2cae

Browse files
committed
fix gomodcheck test
1 parent c1ad240 commit 7fa2cae

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

internal/linters/go/gomodcheck/gomodcheck.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ func goModCheckHandler(ctx context.Context, a lint.Agent) error {
4848
func goModCheckOutput(log *xlog.Logger, a lint.Agent) (map[string][]lint.LinterOutput, error) {
4949
output := make(map[string][]lint.LinterOutput)
5050
extensions := []string{".mod"}
51-
modFiles, err := util.FindFileWithExt(".", extensions)
51+
modFiles, err := util.FindFileWithExt(a.RepoDir, extensions)
5252
if err != nil {
5353
return output, err
5454
}
5555
for _, file := range modFiles {
56-
fName := file
56+
fName := strings.TrimPrefix(file, a.RepoDir+"/")
5757
if !strings.HasSuffix(fName, "go.mod") {
5858
continue
5959
}
6060

61-
goModPath := fName
61+
goModPath := file
6262
file, err := os.ReadFile(goModPath)
6363
if err != nil {
6464
log.Errorf("Error opening %s: %s", goModPath, err)

internal/linters/go/gomodcheck/gomodcheck_test.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package gomodcheck
1818

1919
import (
20-
"context"
2120
"os"
2221
"path/filepath"
2322
"reflect"
@@ -108,14 +107,10 @@ func TestGoModCheck(t *testing.T) {
108107

109108
for _, tc := range tcs {
110109
t.Run(tc.id, func(t *testing.T) {
111-
p, err := lint.NewGithubProvider(context.TODO(), nil, github.PullRequestEvent{}, lint.WithPullRequestChangedFiles(tc.input))
112-
if err != nil {
113-
t.Errorf("Error creating github provider: %v", err)
114-
return
115-
}
110+
116111
// prepare go.mod files
117-
for _, file := range p.GetFiles(nil) {
118-
filename := file
112+
for _, file := range tc.input {
113+
filename := *file.Filename
119114
dir := filepath.Dir(filename)
120115
err := os.MkdirAll(dir, 0o755)
121116
if err != nil {
@@ -131,9 +126,12 @@ func TestGoModCheck(t *testing.T) {
131126
}
132127
}
133128

134-
output, err := goModCheckOutput(&xlog.Logger{}, lint.Agent{
135-
Provider: p,
136-
})
129+
repoDir, err := os.Getwd()
130+
if err != nil {
131+
t.Errorf("Error get current working directory: %v", err)
132+
return
133+
}
134+
output, err := goModCheckOutput(&xlog.Logger{}, lint.Agent{RepoDir: repoDir})
137135
if err != nil {
138136
t.Errorf("Error execute goModCheckOutput : %v", err)
139137
}

server.go

+11
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,17 @@ func (s *Server) handleCodeRequestEvent(ctx context.Context, info *codeRequestIn
430430

431431
func processForCLI(ctx context.Context, path string, o cliOptions) error {
432432
log := util.FromContext(ctx)
433+
// 如果传入的是目录,则运行目录下的所有文件
434+
// todo 如果传入的是文件,提取后缀,并只跑对应的linter
435+
info, err := os.Stat(path)
436+
if err != nil {
437+
return err
438+
}
439+
if !info.IsDir() {
440+
path = filepath.Dir(path)
441+
// ext := filepath.Ext(path)
442+
}
443+
433444
for name, fn := range lint.TotalPullRequestHandlers() {
434445
// check if linter is installed
435446
_, err := exec.LookPath(name)

0 commit comments

Comments
 (0)