diff --git a/cmd/init.go b/cmd/init.go index fb3a690bd..6d8feb83b 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -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 { diff --git a/cmd/init_test.go b/cmd/init_test.go index 870d49544..5926b854a 100644 --- a/cmd/init_test.go +++ b/cmd/init_test.go @@ -2,6 +2,7 @@ package cmd import ( "context" + "fmt" "os" "strings" "testing" @@ -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) + } + }) }