Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ linters:
msg: "do not use os.MkdirTemp() in tests, use t.TempDir()"
- pattern: os\.Setenv()
msg: "do not use os.Setenv() in tests, use t.Setenv()"
- pattern: os\.Chdir()
msg: "do not use os.Chdir() in tests, use t.Chdir()"
- pattern: fmt\.Print.*()
msg: "do not use fmt.Print() or fmt.Println() in tests"
depguard:
Expand Down
14 changes: 2 additions & 12 deletions cmd/root/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,7 @@ func TestCompleteAgentFilename(t *testing.T) {
tt.setup(t, tmpDir)

// Change working directory
origDir, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, os.Chdir(tmpDir))
t.Cleanup(func() {
require.NoError(t, os.Chdir(origDir))
})
t.Chdir(tmpDir)

completions, directive := completeAgentFilename(tt.toComplete)

Expand Down Expand Up @@ -272,12 +267,7 @@ func TestCompleteAliasIncludesYAMLFiles(t *testing.T) {
writeFile(t, tmpDir, "readme.md") // not a yaml file

// Change working directory
origDir, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, os.Chdir(tmpDir))
t.Cleanup(func() {
require.NoError(t, os.Chdir(origDir))
})
t.Chdir(tmpDir)

// Test that completeAlias includes YAML files starting with "go"
completions, directive := completeAlias("go")
Expand Down
7 changes: 1 addition & 6 deletions cmd/root/record_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package root

import (
"os"
"path/filepath"
"strings"
"testing"
Expand All @@ -26,11 +25,7 @@ func TestSetupRecordingProxy_EmptyPath(t *testing.T) {
}

func TestSetupRecordingProxy_AutoGeneratesFilename(t *testing.T) {
tmpDir := t.TempDir()
originalWd, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, os.Chdir(tmpDir))
t.Cleanup(func() { _ = os.Chdir(originalWd) })
t.Chdir(t.TempDir())

var runConfig config.RuntimeConfig

Expand Down
67 changes: 13 additions & 54 deletions pkg/evaluation/save_test.go
Original file line number Diff line number Diff line change
@@ -1,78 +1,37 @@
package evaluation

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"

"github.com/docker/cagent/pkg/session"
)

func TestSaveWithCustomFilename(t *testing.T) {
// Create a temporary directory for tests
tmpDir := t.TempDir()

// Change to temp directory
oldWd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get working directory: %v", err)
}
defer func() {
if err := os.Chdir(oldWd); err != nil {
t.Errorf("Failed to restore working directory: %v", err)
}
}()

if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to change to temp dir: %v", err)
}
// Create a temporary directory and change to it
t.Chdir(t.TempDir())

// Create a test session
sess := session.New()
sess.ID = "test-session-id"

// Test 1: Save with custom filename
evalFile, err := Save(sess, "my-custom-eval")
if err != nil {
t.Fatalf("Failed to save eval: %v", err)
}

expectedPath := filepath.Join("evals", "my-custom-eval.json")
if evalFile != expectedPath {
t.Errorf("Expected file path %q, got %q", expectedPath, evalFile)
}

if _, err := os.Stat(evalFile); os.IsNotExist(err) {
t.Errorf("Expected file %q to exist", evalFile)
}
require.NoError(t, err)
require.Equal(t, filepath.Join("evals", "my-custom-eval.json"), evalFile)
require.FileExists(t, evalFile)

// Test 2: Save without filename (should use session ID)
evalFile2, err := Save(sess, "")
if err != nil {
t.Fatalf("Failed to save eval: %v", err)
}

expectedPath2 := filepath.Join("evals", sess.ID+".json")
if evalFile2 != expectedPath2 {
t.Errorf("Expected file path %q, got %q", expectedPath2, evalFile2)
}

if _, err := os.Stat(evalFile2); os.IsNotExist(err) {
t.Errorf("Expected file %q to exist", evalFile2)
}
require.NoError(t, err)
require.Equal(t, filepath.Join("evals", sess.ID+".json"), evalFile2)
require.FileExists(t, evalFile2)

// Test 3: Save with same filename (should add _1 suffix)
evalFile3, err := Save(sess, "my-custom-eval")
if err != nil {
t.Fatalf("Failed to save eval: %v", err)
}

expectedPath3 := filepath.Join("evals", "my-custom-eval_1.json")
if evalFile3 != expectedPath3 {
t.Errorf("Expected file path %q, got %q", expectedPath3, evalFile3)
}

if _, err := os.Stat(evalFile3); os.IsNotExist(err) {
t.Errorf("Expected file %q to exist", evalFile3)
}
require.NoError(t, err)
require.Equal(t, filepath.Join("evals", "my-custom-eval_1.json"), evalFile3)
require.FileExists(t, evalFile3)
}
6 changes: 1 addition & 5 deletions pkg/skills/skills_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,8 @@ func TestBuildSkillsPrompt_Empty(t *testing.T) {
}

func TestLoad_Integration(t *testing.T) {
originalWd, err := os.Getwd()
require.NoError(t, err)
defer func() { _ = os.Chdir(originalWd) }()

tmpDir := t.TempDir()
require.NoError(t, os.Chdir(tmpDir))
t.Chdir(tmpDir)

claudeProjectDir := filepath.Join(tmpDir, ".claude", "skills", "test-skill")
require.NoError(t, os.MkdirAll(claudeProjectDir, 0o755))
Expand Down