mirror of
https://github.com/alibaba/open-code-review.git
synced 2026-07-09 17:28:58 +00:00
test: fix golangci-lint errcheck/staticcheck issues in test code (#323)
Some checks are pending
CI / test (push) Waiting to run
Some checks are pending
CI / test (push) Waiting to run
This commit is contained in:
parent
ac1eff5bae
commit
e2d75b732a
23 changed files with 240 additions and 100 deletions
|
|
@ -181,7 +181,7 @@ func TestEmitRunResult_NilQuietHandle(t *testing.T) {
|
|||
|
||||
func TestEmitRunResult_JSONTraceIDFromContext(t *testing.T) {
|
||||
tp := sdktrace.NewTracerProvider()
|
||||
defer tp.Shutdown(context.Background())
|
||||
defer func() { _ = tp.Shutdown(context.Background()) }()
|
||||
otel.SetTracerProvider(tp)
|
||||
|
||||
ctx, span := tp.Tracer("test").Start(context.Background(), "test-root")
|
||||
|
|
@ -211,7 +211,7 @@ func TestEmitRunResult_JSONTraceIDFromContext(t *testing.T) {
|
|||
|
||||
func TestEmitRunResult_JSONNoFilesTraceID(t *testing.T) {
|
||||
tp := sdktrace.NewTracerProvider()
|
||||
defer tp.Shutdown(context.Background())
|
||||
defer func() { _ = tp.Shutdown(context.Background()) }()
|
||||
otel.SetTracerProvider(tp)
|
||||
|
||||
ctx, span := tp.Tracer("test").Start(context.Background(), "test-root")
|
||||
|
|
|
|||
|
|
@ -23,11 +23,17 @@ func initTestGitRepo(t *testing.T) string {
|
|||
}
|
||||
}
|
||||
f := filepath.Join(dir, "README.md")
|
||||
os.WriteFile(f, []byte("hello"), 0o644)
|
||||
if err := os.WriteFile(f, []byte("hello"), 0o644); err != nil {
|
||||
t.Fatalf("write README: %v", err)
|
||||
}
|
||||
cmd := exec.Command("git", "-C", dir, "add", ".")
|
||||
cmd.CombinedOutput()
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("git add: %v: %s", err, out)
|
||||
}
|
||||
cmd = exec.Command("git", "-C", dir, "commit", "-m", "initial commit")
|
||||
cmd.CombinedOutput()
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("git commit: %v: %s", err, out)
|
||||
}
|
||||
return dir
|
||||
}
|
||||
|
||||
|
|
@ -93,9 +99,18 @@ func TestResolveRepoDir_NotGitRepo(t *testing.T) {
|
|||
|
||||
func TestResolveRepoDir_EmptyUsesWd(t *testing.T) {
|
||||
dir := initTestGitRepo(t)
|
||||
origDir, _ := os.Getwd()
|
||||
defer os.Chdir(origDir)
|
||||
os.Chdir(dir)
|
||||
origDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("getwd: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
if err := os.Chdir(origDir); err != nil {
|
||||
t.Errorf("restore chdir: %v", err)
|
||||
}
|
||||
}()
|
||||
if err := os.Chdir(dir); err != nil {
|
||||
t.Fatalf("chdir: %v", err)
|
||||
}
|
||||
|
||||
resolved, err := resolveRepoDir("")
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -169,7 +169,7 @@ func TestOutputJSONWithWarnings_NoCommentsSubtaskError(t *testing.T) {
|
|||
|
||||
warnings := []agent.AgentWarning{{Type: "subtask_error", File: "x.go", Message: "fail"}}
|
||||
err := outputJSONWithWarnings(nil, warnings, 1, 10, 5, 15, 0, 0, time.Second, "", nil, "abc123trace")
|
||||
w.Close()
|
||||
_ = w.Close()
|
||||
os.Stdout = old
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -177,10 +177,12 @@ func TestOutputJSONWithWarnings_NoCommentsSubtaskError(t *testing.T) {
|
|||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
buf.ReadFrom(r)
|
||||
_, _ = buf.ReadFrom(r)
|
||||
|
||||
var out jsonOutput
|
||||
json.Unmarshal(buf.Bytes(), &out)
|
||||
if err := json.Unmarshal(buf.Bytes(), &out); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if out.Status != "completed_with_errors" {
|
||||
t.Errorf("status = %q, want completed_with_errors", out.Status)
|
||||
}
|
||||
|
|
@ -224,7 +226,7 @@ func TestOutputJSON(t *testing.T) {
|
|||
}
|
||||
err := outputJSON(comments)
|
||||
|
||||
w.Close()
|
||||
_ = w.Close()
|
||||
os.Stdout = old
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -232,7 +234,7 @@ func TestOutputJSON(t *testing.T) {
|
|||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
buf.ReadFrom(r)
|
||||
_, _ = buf.ReadFrom(r)
|
||||
|
||||
var out jsonOutput
|
||||
if err := json.Unmarshal(buf.Bytes(), &out); err != nil {
|
||||
|
|
@ -253,7 +255,7 @@ func TestOutputJSON_NoComments(t *testing.T) {
|
|||
|
||||
err := outputJSON(nil)
|
||||
|
||||
w.Close()
|
||||
_ = w.Close()
|
||||
os.Stdout = old
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -261,10 +263,12 @@ func TestOutputJSON_NoComments(t *testing.T) {
|
|||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
buf.ReadFrom(r)
|
||||
_, _ = buf.ReadFrom(r)
|
||||
|
||||
var out jsonOutput
|
||||
json.Unmarshal(buf.Bytes(), &out)
|
||||
if err := json.Unmarshal(buf.Bytes(), &out); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if out.Message == "" {
|
||||
t.Error("expected non-empty message when no comments")
|
||||
}
|
||||
|
|
@ -278,7 +282,7 @@ func TestOutputJSONWithWarnings(t *testing.T) {
|
|||
comments := []model.LlmComment{{Path: "b.go", Content: "test"}}
|
||||
warnings := []agent.AgentWarning{{Type: "subtask_error", File: "c.go", Message: "failed"}}
|
||||
err := outputJSONWithWarnings(comments, warnings, 5, 100, 50, 150, 10, 5, 3*time.Second, "summary", map[string]int64{"file_read": 3}, "trace-xyz-789")
|
||||
w.Close()
|
||||
_ = w.Close()
|
||||
os.Stdout = old
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -286,10 +290,12 @@ func TestOutputJSONWithWarnings(t *testing.T) {
|
|||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
buf.ReadFrom(r)
|
||||
_, _ = buf.ReadFrom(r)
|
||||
|
||||
var out jsonOutput
|
||||
json.Unmarshal(buf.Bytes(), &out)
|
||||
if err := json.Unmarshal(buf.Bytes(), &out); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if out.Status != "completed_with_errors" {
|
||||
t.Errorf("status = %q, want completed_with_errors", out.Status)
|
||||
}
|
||||
|
|
@ -314,7 +320,7 @@ func TestOutputJSONWithWarnings_NoCommentsNoErrors(t *testing.T) {
|
|||
|
||||
warnings := []agent.AgentWarning{{Type: "warning", Message: "something"}}
|
||||
err := outputJSONWithWarnings(nil, warnings, 2, 50, 20, 70, 0, 0, time.Second, "", nil, "")
|
||||
w.Close()
|
||||
_ = w.Close()
|
||||
os.Stdout = old
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -322,10 +328,12 @@ func TestOutputJSONWithWarnings_NoCommentsNoErrors(t *testing.T) {
|
|||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
buf.ReadFrom(r)
|
||||
_, _ = buf.ReadFrom(r)
|
||||
|
||||
var out jsonOutput
|
||||
json.Unmarshal(buf.Bytes(), &out)
|
||||
if err := json.Unmarshal(buf.Bytes(), &out); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if out.Status != "completed_with_warnings" {
|
||||
t.Errorf("status = %q, want completed_with_warnings", out.Status)
|
||||
}
|
||||
|
|
@ -341,7 +349,7 @@ func TestOutputJSONNoFiles(t *testing.T) {
|
|||
|
||||
err := outputJSONNoFiles("test-trace-id-456")
|
||||
|
||||
w.Close()
|
||||
_ = w.Close()
|
||||
os.Stdout = old
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -349,10 +357,12 @@ func TestOutputJSONNoFiles(t *testing.T) {
|
|||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
buf.ReadFrom(r)
|
||||
_, _ = buf.ReadFrom(r)
|
||||
|
||||
var out jsonOutput
|
||||
json.Unmarshal(buf.Bytes(), &out)
|
||||
if err := json.Unmarshal(buf.Bytes(), &out); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if out.Status != "skipped" {
|
||||
t.Errorf("status = %q, want skipped", out.Status)
|
||||
}
|
||||
|
|
@ -370,10 +380,10 @@ func captureStdout(t *testing.T, fn func()) string {
|
|||
}
|
||||
os.Stdout = w
|
||||
fn()
|
||||
w.Close()
|
||||
_ = w.Close()
|
||||
os.Stdout = old
|
||||
var buf bytes.Buffer
|
||||
buf.ReadFrom(r)
|
||||
_, _ = buf.ReadFrom(r)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -93,8 +93,8 @@ func TestSanitizeTerminal(t *testing.T) {
|
|||
{"only control chars", "\x1b\x07\x00\x7f", ""},
|
||||
{"unicode preserved", "代码审查 レビュー 🔍", "代码审查 レビュー 🔍"},
|
||||
{"mixed safe and unsafe", "path\x1b[0m/file.go", "path[0m/file.go"},
|
||||
{"strips C1 CSI (U+009B)", "beforeafter", "beforeafter"},
|
||||
{"strips C1 OSC (U+009D)", "beforeafter", "beforeafter"},
|
||||
{"strips C1 CSI (U+009B)", "before\u009bafter", "beforeafter"},
|
||||
{"strips C1 OSC (U+009D)", "before\u009dafter", "beforeafter"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -364,7 +364,7 @@ func TestPrintWizardCancelled(t *testing.T) {
|
|||
}
|
||||
os.Stdout = w
|
||||
printWizardCancelled(tc.savedInSession, tc.scope)
|
||||
w.Close()
|
||||
_ = w.Close()
|
||||
os.Stdout = old
|
||||
got, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -2169,7 +2169,7 @@ func TestProviderTUI_ApiKeyTypingShowsOneStarPerChar(t *testing.T) {
|
|||
m.loadExistingAPIKey()
|
||||
m.apiKeyInput.Focus()
|
||||
|
||||
for _, ch := range []rune("abc") {
|
||||
for _, ch := range "abc" {
|
||||
result, _ := m.Update(charKey(ch))
|
||||
m = result.(providerTUIModel)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -79,9 +79,18 @@ func TestQuietHandle_IdempotentRestore(t *testing.T) {
|
|||
|
||||
func TestResolveWorkingDir_CurrentDir(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
origDir, _ := os.Getwd()
|
||||
defer os.Chdir(origDir)
|
||||
os.Chdir(dir)
|
||||
origDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("getwd: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
if err := os.Chdir(origDir); err != nil {
|
||||
t.Errorf("restore chdir: %v", err)
|
||||
}
|
||||
}()
|
||||
if err := os.Chdir(dir); err != nil {
|
||||
t.Fatalf("chdir: %v", err)
|
||||
}
|
||||
|
||||
absPath, isGit, err := resolveWorkingDir("", false)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue