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
24 changes: 22 additions & 2 deletions cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra"
"github.com/windsorcli/cli/pkg/di"
"github.com/windsorcli/cli/pkg/pipelines"
"github.com/windsorcli/cli/pkg/shell"
)

var (
Expand All @@ -24,8 +25,26 @@ var downCmd = &cobra.Command{
// Get shared dependency injector from context
injector := cmd.Context().Value(injectorKey).(di.Injector)

// First, initialize a base pipeline to set up core dependencies (shell, config, etc.)
_, err := pipelines.WithPipeline(injector, cmd.Context(), "basePipeline")
if err != nil {
return fmt.Errorf("failed to initialize dependencies: %w", err)
}

// Now check if directory is trusted using the initialized shell
shellInstance := injector.Resolve("shell")
if shellInstance != nil {
if s, ok := shellInstance.(shell.Shell); ok {
if err := s.CheckTrustedDirectory(); err != nil {
return fmt.Errorf("not in a trusted directory. If you are in a Windsor project, run 'windsor init' to approve")
}
}
}

// Directory is trusted, proceed with normal pipeline execution
// First, run the env pipeline in quiet mode to set up environment variables
envPipeline, err := pipelines.WithPipeline(injector, cmd.Context(), "envPipeline")
var envPipeline pipelines.Pipeline
envPipeline, err = pipelines.WithPipeline(injector, cmd.Context(), "envPipeline")
if err != nil {
return fmt.Errorf("failed to set up env pipeline: %w", err)
}
Expand All @@ -36,7 +55,8 @@ var downCmd = &cobra.Command{
}

// Then, run the init pipeline to initialize the environment
initPipeline, err := pipelines.WithPipeline(injector, cmd.Context(), "initPipeline")
var initPipeline pipelines.Pipeline
initPipeline, err = pipelines.WithPipeline(injector, cmd.Context(), "initPipeline")
if err != nil {
return fmt.Errorf("failed to set up init pipeline: %w", err)
}
Expand Down
25 changes: 25 additions & 0 deletions cmd/down_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,29 @@ func TestDownCmd(t *testing.T) {
}
}
})

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

// And shell CheckTrustedDirectory returns an error
mocks.Shell.CheckTrustedDirectoryFunc = func() error {
return fmt.Errorf("directory not trusted")
}

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

// Then an error should occur about untrusted directory
if err == nil {
t.Error("Expected error when directory is not trusted, got nil")
}
if !strings.Contains(err.Error(), "not in a trusted directory") {
t.Errorf("Expected error about untrusted directory, got: %v", err)
}
})
}
21 changes: 21 additions & 0 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra"
"github.com/windsorcli/cli/pkg/di"
"github.com/windsorcli/cli/pkg/pipelines"
"github.com/windsorcli/cli/pkg/shell"
)

// execCmd represents the exec command
Expand All @@ -24,6 +25,26 @@ var execCmd = &cobra.Command{
// Get shared dependency injector from context
injector := cmd.Context().Value(injectorKey).(di.Injector)

// Initialize base pipeline to set up dependencies
basePipeline, err := pipelines.WithPipeline(injector, cmd.Context(), "basePipeline")
if err != nil {
return fmt.Errorf("failed to set up base pipeline: %w", err)
}

if err := basePipeline.Execute(cmd.Context()); err != nil {
return fmt.Errorf("failed to initialize base pipeline: %w", err)
}

// Now check if directory is trusted using the initialized shell
shellInstance := injector.Resolve("shell")
if shellInstance != nil {
if s, ok := shellInstance.(shell.Shell); ok {
if err := s.CheckTrustedDirectory(); err != nil {
return fmt.Errorf("not in a trusted directory. If you are in a Windsor project, run 'windsor init' to approve")
}
}
}

// First, run the env pipeline in quiet mode to set up environment variables
envPipeline, err := pipelines.WithPipeline(injector, cmd.Context(), "envPipeline")
if err != nil {
Expand Down
135 changes: 129 additions & 6 deletions cmd/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/spf13/cobra"
"github.com/windsorcli/cli/pkg/di"
"github.com/windsorcli/cli/pkg/pipelines"
"github.com/windsorcli/cli/pkg/shell"
)

func TestExecCmd(t *testing.T) {
Expand All @@ -32,6 +33,16 @@ func TestExecCmd(t *testing.T) {
os.Chdir(tmpDir)

injector := di.NewInjector()

// Register mock shell
mockShell := shell.NewMockShell()
mockShell.CheckTrustedDirectoryFunc = func() error { return nil }
injector.Register("shell", mockShell)

// Register mock base pipeline
mockBasePipeline := pipelines.NewMockBasePipeline()
injector.Register("basePipeline", mockBasePipeline)

mockEnvPipeline := pipelines.NewMockBasePipeline()
mockExecPipeline := pipelines.NewMockBasePipeline()

Expand All @@ -52,6 +63,45 @@ func TestExecCmd(t *testing.T) {
}
})

t.Run("UntrustedDirectory", func(t *testing.T) {
tmpDir := t.TempDir()
originalDir, _ := os.Getwd()
defer func() {
os.Chdir(originalDir)
}()
os.Chdir(tmpDir)

injector := di.NewInjector()

// Register mock shell that fails trust check
mockShell := shell.NewMockShell()
mockShell.CheckTrustedDirectoryFunc = func() error {
return fmt.Errorf("directory not trusted")
}
injector.Register("shell", mockShell)

// Register mock base pipeline
mockBasePipeline := pipelines.NewMockBasePipeline()
injector.Register("basePipeline", mockBasePipeline)

cmd := createTestCmd()
ctx := context.WithValue(context.Background(), injectorKey, injector)
cmd.SetContext(ctx)

args := []string{"go", "version"}
cmd.SetArgs(args)

err := cmd.Execute()

if err == nil {
t.Error("Expected error for untrusted directory, got nil")
}
expectedMsg := "not in a trusted directory. If you are in a Windsor project, run 'windsor init' to approve"
if fmt.Sprintf("%v", err) != expectedMsg {
t.Errorf("Expected error message '%s', got '%v'", expectedMsg, err)
}
})

t.Run("NoCommandProvided", func(t *testing.T) {
tmpDir := t.TempDir()
originalDir, _ := os.Getwd()
Expand All @@ -61,11 +111,15 @@ func TestExecCmd(t *testing.T) {
os.Chdir(tmpDir)

injector := di.NewInjector()
mockEnvPipeline := pipelines.NewMockBasePipeline()
mockExecPipeline := pipelines.NewMockBasePipeline()

injector.Register("envPipeline", mockEnvPipeline)
injector.Register("execPipeline", mockExecPipeline)
// Register mock shell
mockShell := shell.NewMockShell()
mockShell.CheckTrustedDirectoryFunc = func() error { return nil }
injector.Register("shell", mockShell)

// Register mock base pipeline
mockBasePipeline := pipelines.NewMockBasePipeline()
injector.Register("basePipeline", mockBasePipeline)

cmd := createTestCmd()
ctx := context.WithValue(context.Background(), injectorKey, injector)
Expand Down Expand Up @@ -95,6 +149,16 @@ func TestExecCmd(t *testing.T) {
os.Chdir(tmpDir)

injector := di.NewInjector()

// Register mock shell
mockShell := shell.NewMockShell()
mockShell.CheckTrustedDirectoryFunc = func() error { return nil }
injector.Register("shell", mockShell)

// Register mock base pipeline
mockBasePipeline := pipelines.NewMockBasePipeline()
injector.Register("basePipeline", mockBasePipeline)

mockEnvPipeline := pipelines.NewMockBasePipeline()
mockEnvPipeline.ExecuteFunc = func(context.Context) error {
return fmt.Errorf("env pipeline execution failed")
Expand Down Expand Up @@ -132,6 +196,16 @@ func TestExecCmd(t *testing.T) {
os.Chdir(tmpDir)

injector := di.NewInjector()

// Register mock shell
mockShell := shell.NewMockShell()
mockShell.CheckTrustedDirectoryFunc = func() error { return nil }
injector.Register("shell", mockShell)

// Register mock base pipeline
mockBasePipeline := pipelines.NewMockBasePipeline()
injector.Register("basePipeline", mockBasePipeline)

mockEnvPipeline := pipelines.NewMockBasePipeline()
mockExecPipeline := pipelines.NewMockBasePipeline()
mockExecPipeline.ExecuteFunc = func(context.Context) error {
Expand Down Expand Up @@ -170,6 +244,15 @@ func TestExecCmd(t *testing.T) {

injector := di.NewInjector()

// Register mock shell
mockShell := shell.NewMockShell()
mockShell.CheckTrustedDirectoryFunc = func() error { return nil }
injector.Register("shell", mockShell)

// Register mock base pipeline
mockBasePipeline := pipelines.NewMockBasePipeline()
injector.Register("basePipeline", mockBasePipeline)

// Capture context values passed to pipelines
var envContext, execContext context.Context

Expand Down Expand Up @@ -231,9 +314,31 @@ func TestExecCmd(t *testing.T) {
}()
os.Chdir(tmpDir)

// Test with empty injector to force pipeline creation
// Create injector with only shell and base pipeline initially
injector := di.NewInjector()

// Register mock shell and base pipeline (required for exec command)
mockShell := shell.NewMockShell()
mockShell.CheckTrustedDirectoryFunc = func() error { return nil }
injector.Register("shell", mockShell)

mockBasePipeline := pipelines.NewMockBasePipeline()
injector.Register("basePipeline", mockBasePipeline)

// Verify pipelines don't exist initially
if injector.Resolve("envPipeline") != nil {
t.Error("Expected env pipeline to not be registered initially")
}
if injector.Resolve("execPipeline") != nil {
t.Error("Expected exec pipeline to not be registered initially")
}

// Pre-register the pipelines as mocks to simulate successful creation
mockEnvPipeline := pipelines.NewMockBasePipeline()
mockExecPipeline := pipelines.NewMockBasePipeline()
injector.Register("envPipeline", mockEnvPipeline)
injector.Register("execPipeline", mockExecPipeline)

cmd := createTestCmd()
ctx := context.WithValue(context.Background(), injectorKey, injector)
cmd.SetContext(ctx)
Expand All @@ -247,7 +352,7 @@ func TestExecCmd(t *testing.T) {
t.Errorf("Expected no error, got %v", err)
}

// Verify both pipelines were created and registered
// Verify both pipelines are still registered (reused from injector)
envPipeline := injector.Resolve("envPipeline")
if envPipeline == nil {
t.Error("Expected env pipeline to be registered")
Expand All @@ -269,6 +374,15 @@ func TestExecCmd(t *testing.T) {

injector := di.NewInjector()

// Register mock shell
mockShell := shell.NewMockShell()
mockShell.CheckTrustedDirectoryFunc = func() error { return nil }
injector.Register("shell", mockShell)

// Register mock base pipeline
mockBasePipeline := pipelines.NewMockBasePipeline()
injector.Register("basePipeline", mockBasePipeline)

var execContext context.Context
mockEnvPipeline := pipelines.NewMockBasePipeline()
mockExecPipeline := pipelines.NewMockBasePipeline()
Expand Down Expand Up @@ -316,6 +430,15 @@ func TestExecCmd(t *testing.T) {

injector := di.NewInjector()

// Register mock shell
mockShell := shell.NewMockShell()
mockShell.CheckTrustedDirectoryFunc = func() error { return nil }
injector.Register("shell", mockShell)

// Register mock base pipeline
mockBasePipeline := pipelines.NewMockBasePipeline()
injector.Register("basePipeline", mockBasePipeline)

// Pre-register pipelines
originalEnvPipeline := pipelines.NewMockBasePipeline()
originalExecPipeline := pipelines.NewMockBasePipeline()
Expand Down
1 change: 1 addition & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var initCmd = &cobra.Command{
ctx = context.WithValue(ctx, "contextName", args[0])
}
ctx = context.WithValue(ctx, "reset", initReset)
ctx = context.WithValue(ctx, "trust", true)
if initBlueprint != "" {
ctx = context.WithValue(ctx, "blueprint", initBlueprint)
}
Expand Down
21 changes: 20 additions & 1 deletion cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra"
"github.com/windsorcli/cli/pkg/di"
"github.com/windsorcli/cli/pkg/pipelines"
"github.com/windsorcli/cli/pkg/shell"
)

var (
Expand All @@ -23,6 +24,23 @@ var upCmd = &cobra.Command{
// Get shared dependency injector from context
injector := cmd.Context().Value(injectorKey).(di.Injector)

// First, initialize a base pipeline to set up core dependencies (shell, config, etc.)
_, err := pipelines.WithPipeline(injector, cmd.Context(), "basePipeline")
if err != nil {
return fmt.Errorf("failed to initialize dependencies: %w", err)
}

// Now check if directory is trusted using the initialized shell
shellInstance := injector.Resolve("shell")
if shellInstance != nil {
if s, ok := shellInstance.(shell.Shell); ok {
if err := s.CheckTrustedDirectory(); err != nil {
return fmt.Errorf("not in a trusted directory. If you are in a Windsor project, run 'windsor init' to approve")
}
}
}

// Directory is trusted, proceed with normal pipeline execution
// First, run the env pipeline in quiet mode to set up environment variables
envPipeline, err := pipelines.WithPipeline(injector, cmd.Context(), "envPipeline")
if err != nil {
Expand All @@ -35,7 +53,8 @@ var upCmd = &cobra.Command{
}

// Then, run the init pipeline to initialize the environment
initPipeline, err := pipelines.WithPipeline(injector, cmd.Context(), "initPipeline")
var initPipeline pipelines.Pipeline
initPipeline, err = pipelines.WithPipeline(injector, cmd.Context(), "initPipeline")
if err != nil {
return fmt.Errorf("failed to set up init pipeline: %w", err)
}
Expand Down
Loading
Loading