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
4 changes: 4 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func runInit(injector di.Injector, contextName string, overwrite bool) error {
return fmt.Errorf("failed to initialize context: %w", err)
}

if err := baseCtx.Shell.AddCurrentDirToTrustedFile(); err != nil {
return fmt.Errorf("failed to add current directory to trusted file: %w", err)
}

configHandler := baseCtx.ConfigHandler

if err := configHandler.Initialize(); err != nil {
Expand Down
58 changes: 58 additions & 0 deletions cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"fmt"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -999,4 +1000,61 @@ func TestInitCmd(t *testing.T) {
t.Errorf("Expected success for invalid set flag format, got error: %v", err)
}
})

t.Run("AddsCurrentDirToTrustedFile", func(t *testing.T) {
// Given a temporary directory with mocked dependencies
mocks := setupInitTest(t)

// And tracking whether AddCurrentDirToTrustedFile is called
var addCurrentDirToTrustedFileCalled bool
mocks.Shell.Shell.AddCurrentDirToTrustedFileFunc = func() error {
addCurrentDirToTrustedFileCalled = true
return nil
}

// When executing the init command
cmd := createTestInitCmd()
ctx := context.WithValue(context.Background(), injectorKey, mocks.Injector)
cmd.SetArgs([]string{})
cmd.SetContext(ctx)
err := cmd.Execute()

// Then no error should occur
if err != nil {
t.Errorf("Expected success, got error: %v", err)
}

// And AddCurrentDirToTrustedFile should have been called
if !addCurrentDirToTrustedFileCalled {
t.Error("Expected AddCurrentDirToTrustedFile to be called, but it was not")
}
})

t.Run("HandlesAddCurrentDirToTrustedFileError", func(t *testing.T) {
// Given a temporary directory with mocked dependencies
mocks := setupInitTest(t)

// And AddCurrentDirToTrustedFile returns an error
expectedError := fmt.Errorf("failed to add current directory to trusted file")
mocks.Shell.Shell.AddCurrentDirToTrustedFileFunc = func() error {
return expectedError
}

// When executing the init command
cmd := createTestInitCmd()
ctx := context.WithValue(context.Background(), injectorKey, mocks.Injector)
cmd.SetArgs([]string{})
cmd.SetContext(ctx)
err := cmd.Execute()

// Then an error should occur
if err == nil {
t.Error("Expected error when AddCurrentDirToTrustedFile fails, got nil")
}

// And the error should contain the expected message
if !strings.Contains(err.Error(), "failed to add current directory to trusted file") {
t.Errorf("Expected error message to contain 'failed to add current directory to trusted file', got: %v", err)
}
})
}
Loading