Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Use t.Chdir in CLI tests
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
  • Loading branch information
Copilot and pelikhan authored Oct 9, 2026
commit 3bb6eabb82d8360a5b9291dce8f8379a320cc0eb
18 changes: 2 additions & 16 deletions pkg/cli/add_no_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,9 @@ import (

// TestAddCommandRequiresArguments verifies that the add command requires at least one argument
func TestAddCommandRequiresArguments(t *testing.T) {
// Save current directory
originalDir, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get current directory: %v", err)
}
defer func() {
// Restore original directory
if err := os.Chdir(originalDir); err != nil {
t.Logf("Warning: Failed to restore directory: %v", err)
}
}()

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

// Initialize git repo
if err := os.MkdirAll(".git", 0755); err != nil {
Expand All @@ -48,7 +34,7 @@ func TestAddCommandRequiresArguments(t *testing.T) {
cmd.SetArgs([]string{})

// Execute and expect an error
err = cmd.Execute()
err := cmd.Execute()
if err == nil {
t.Error("Expected error when calling add without arguments, got nil")
}
Expand Down
64 changes: 6 additions & 58 deletions pkg/cli/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ func TestCompileWorkflowsPurgeFlag(t *testing.T) {
os.MkdirAll(workflowsDir, 0755)

// Change to temp directory to simulate being in a git repo
originalDir, _ := os.Getwd()
defer os.Chdir(originalDir)
os.Chdir(tempDir)
t.Chdir(tempDir)

// Create .git directory to make it look like a git repo
os.MkdirAll(".git", 0755)
Expand Down Expand Up @@ -346,9 +344,7 @@ func TestAllCommandsExist(t *testing.T) {
os.MkdirAll(workflowsDir, 0755)

// Change to temp directory
originalDir, _ := os.Getwd()
defer os.Chdir(originalDir)
os.Chdir(tempDir)
t.Chdir(tempDir)

// Create a minimal test workflow to avoid "no workflows found" error
minimalWorkflow := `---
Expand Down Expand Up @@ -414,19 +410,7 @@ func TestCreateWorkflowMarkdownFile(t *testing.T) {
defer os.RemoveAll(tempDir)

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

if err := os.Chdir(tempDir); err != nil {
t.Fatalf("Failed to change to temp directory: %v", err)
}
t.Chdir(tempDir)

tests := []struct {
name string
Expand Down Expand Up @@ -640,19 +624,7 @@ This workflow uses an include.
}

// Change to the temporary directory to simulate the git root
oldDir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer func() {
if err := os.Chdir(oldDir); err != nil {
t.Logf("Warning: Failed to restore working directory: %v", err)
}
}()

if err := os.Chdir(tmpDir); err != nil {
t.Fatal(err)
}
t.Chdir(tmpDir)

// Run cleanup
err = cleanupOrphanedIncludes(true)
Expand Down Expand Up @@ -764,19 +736,7 @@ This workflow also uses common include.
}

// Change to the temporary directory
oldDir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer func() {
if err := os.Chdir(oldDir); err != nil {
t.Logf("Warning: Failed to restore working directory: %v", err)
}
}()

if err := os.Chdir(tmpDir); err != nil {
t.Fatal(err)
}
t.Chdir(tmpDir)

// Test Case 1: Remove workflow2 - should orphan shared/tools.md but not shared/common.md
// Use relative paths like the real RemoveWorkflows function does
Expand Down Expand Up @@ -872,19 +832,7 @@ This workflow uses an include.
}

// Change to the temporary directory
oldDir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer func() {
if err := os.Chdir(oldDir); err != nil {
t.Logf("Warning: Failed to restore working directory: %v", err)
}
}()

if err := os.Chdir(tmpDir); err != nil {
t.Fatal(err)
}
t.Chdir(tmpDir)

// Test 1: Verify include file exists before removal
if _, err := os.Stat(filepath.Join(sharedDir, "common.md")); os.IsNotExist(err) {
Expand Down
14 changes: 2 additions & 12 deletions pkg/cli/forecast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,13 +543,8 @@ func TestRenderForecastTable_ZeroMonteCarloRangeRendersDash(t *testing.T) {
}

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

originalDownload := forecastDownloadRunArtifacts
originalAnalyze := forecastAnalyzeTokenUsage
Expand All @@ -576,13 +571,8 @@ func TestLoadCachedRunAIC_UsageArtifactFirst(t *testing.T) {
}

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

originalDownload := forecastDownloadRunArtifacts
originalAnalyze := forecastAnalyzeTokenUsage
Expand Down
Loading
Loading