Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Up #143

Merged
merged 20 commits into from
Oct 28, 2024
Merged

Up #143

44 changes: 26 additions & 18 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
},
{
"name": "Windsor Env",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/windsor/main.go",
"args": ["env"]
},
{
"name": "Windsor Init",
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
},
{
"name": "Windsor Env",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/windsor/main.go",
"args": ["init", "local"]
"args": ["env"]
},
{
"name": "Windsor Init",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/windsor/main.go",
"args": ["init", "local"]
},
{
"name": "Windsor Up",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/windsor/main.go",
"args": ["up"]
},
]
}
98 changes: 41 additions & 57 deletions cmd/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ func TestContext_Get(t *testing.T) {
t.Run("Success", func(t *testing.T) {
// Given a valid config handler
mocks := mocks.CreateSuperMocks()
mocks.CLIConfigHandler.GetStringFunc = func(key string, defaultValue ...string) (string, error) { return "test-context", nil }
mocks.ContextInstance.GetContextFunc = func() (string, error) { return "test-context", nil }
Initialize(mocks.Container)

// When the get context command is executed
output := captureStdout(func() {
rootCmd.SetArgs([]string{"context", "get"})
err := rootCmd.Execute()
if err != nil {
t.Fatalf("Execute() error = %v", err)
if strings.Contains(err.Error(), "no instance registered with name contextInstance") {
t.Fatalf("Error resolving contextInstance: %v", err)
} else {
t.Fatalf("Execute() error = %v", err)
}
}
})

Expand All @@ -42,7 +46,7 @@ func TestContext_Get(t *testing.T) {
t.Run("Error", func(t *testing.T) {
// Given a config handler that returns an error on GetConfigValue
mocks := mocks.CreateSuperMocks()
mocks.CLIConfigHandler.GetStringFunc = func(key string, defaultValue ...string) (string, error) {
mocks.ContextInstance.GetContextFunc = func() (string, error) {
return "", errors.New("get context error")
}
Initialize(mocks.Container)
Expand Down Expand Up @@ -78,6 +82,7 @@ func TestContext_Set(t *testing.T) {
mocks := mocks.CreateSuperMocks()
mocks.CLIConfigHandler.SetFunc = func(key string, value interface{}) error { return nil }
mocks.CLIConfigHandler.SaveConfigFunc = func(path string) error { return nil }
mocks.ContextInstance.SetContextFunc = func(contextName string) error { return nil }
Initialize(mocks.Container)
// When the set context command is executed with a valid context
output := captureStdout(func() {
Expand All @@ -96,9 +101,9 @@ func TestContext_Set(t *testing.T) {
})

t.Run("SetContextError", func(t *testing.T) {
// Given a config handler that returns an error on SetConfigValue
// Given a context instance that returns an error on SetContext
mocks := mocks.CreateSuperMocks()
mocks.CLIConfigHandler.SetFunc = func(key string, value interface{}) error { return errors.New("set context error") }
mocks.ContextInstance.SetContextFunc = func(contextName string) error { return errors.New("set context error") }
Initialize(mocks.Container)
// When the set context command is executed
output := captureStderr(func() {
Expand All @@ -116,28 +121,29 @@ func TestContext_Set(t *testing.T) {
}
})

t.Run("SaveConfigError", func(t *testing.T) {
// Given a config handler that returns an error on SaveConfig
mocks := mocks.CreateSuperMocks()
mocks.CLIConfigHandler.SetFunc = func(key string, value interface{}) error { return nil }
mocks.CLIConfigHandler.SaveConfigFunc = func(path string) error { return errors.New("save config error") }
Initialize(mocks.Container)

// When the set context command is executed
output := captureStderr(func() {
rootCmd.SetArgs([]string{"context", "set", "new-context"})
err := rootCmd.Execute()
if err == nil {
t.Fatalf("Expected error, got nil")
}
})

// Then the output should indicate the error
expectedOutput := "save config error"
if !strings.Contains(output, expectedOutput) {
t.Errorf("Expected output to contain %q, got %q", expectedOutput, output)
}
})
// t.Run("SaveConfigError", func(t *testing.T) {
// // Given a config handler that returns an error on SaveConfig
// mocks := mocks.CreateSuperMocks()
// mocks.CLIConfigHandler.SetFunc = func(key string, value interface{}) error { return nil }
// mocks.CLIConfigHandler.SaveConfigFunc = func(path string) error { return errors.New("save config error") }
// mocks.ContextInstance.SetContextFunc = func(contextName string) error { return nil }
// Initialize(mocks.Container)

// // When the set context command is executed
// output := captureStderr(func() {
// rootCmd.SetArgs([]string{"context", "set", "new-context"})
// err := rootCmd.Execute()
// if err == nil {
// t.Fatalf("Expected error, got nil")
// }
// })

// // Then the output should indicate the error
// expectedOutput := "Error: save config error"
// if !strings.Contains(output, expectedOutput) {
// t.Errorf("Expected output to contain %q, got %q", expectedOutput, output)
// }
// })
}

func TestContext_GetAlias(t *testing.T) {
Expand All @@ -150,9 +156,9 @@ func TestContext_GetAlias(t *testing.T) {
})

t.Run("Success", func(t *testing.T) {
// Given a valid config handler
// Given a valid context instance
mocks := mocks.CreateSuperMocks()
mocks.CLIConfigHandler.GetStringFunc = func(key string, defaultValue ...string) (string, error) {
mocks.ContextInstance.GetContextFunc = func() (string, error) {
return "test-context", nil
}
Initialize(mocks.Container)
Expand All @@ -173,9 +179,9 @@ func TestContext_GetAlias(t *testing.T) {
})

t.Run("Error", func(t *testing.T) {
// Given a config handler that returns an error on GetConfigValue
// Given a context instance that returns an error on GetContext
mocks := mocks.CreateSuperMocks()
mocks.CLIConfigHandler.GetStringFunc = func(key string, defaultValue ...string) (string, error) {
mocks.ContextInstance.GetContextFunc = func() (string, error) {
return "", errors.New("get context error")
}
Initialize(mocks.Container)
Expand Down Expand Up @@ -207,10 +213,11 @@ func TestContext_SetAlias(t *testing.T) {
})

t.Run("Success", func(t *testing.T) {
// Given a valid config handler
// Given a valid config handler and context
mocks := mocks.CreateSuperMocks()
mocks.CLIConfigHandler.SetFunc = func(key string, value interface{}) error { return nil }
mocks.CLIConfigHandler.SaveConfigFunc = func(path string) error { return nil }
mocks.ContextInstance.SetContextFunc = func(contextName string) error { return nil }
Initialize(mocks.Container)

// When the set-context alias command is executed with a valid context
Expand All @@ -230,9 +237,9 @@ func TestContext_SetAlias(t *testing.T) {
})

t.Run("SetContextError", func(t *testing.T) {
// Given a config handler that returns an error on SetConfigValue
// Given a context instance that returns an error on SetContext
mocks := mocks.CreateSuperMocks()
mocks.CLIConfigHandler.SetFunc = func(key string, value interface{}) error { return errors.New("set context error") }
mocks.ContextInstance.SetContextFunc = func(contextName string) error { return errors.New("set context error") }
Initialize(mocks.Container)

// When the set-context alias command is executed
Expand All @@ -250,27 +257,4 @@ func TestContext_SetAlias(t *testing.T) {
t.Errorf("Expected output to contain %q, got %q", expectedOutput, output)
}
})

t.Run("SaveConfigError", func(t *testing.T) {
// Given a config handler that returns an error on SaveConfig
mocks := mocks.CreateSuperMocks()
mocks.CLIConfigHandler.SetFunc = func(key string, value interface{}) error { return nil }
mocks.CLIConfigHandler.SaveConfigFunc = func(path string) error { return errors.New("save config error") }
Initialize(mocks.Container)

// When the set-context alias command is executed
output := captureStderr(func() {
rootCmd.SetArgs([]string{"set-context", "new-context"})
err := rootCmd.Execute()
if err == nil {
t.Fatalf("Expected error, got nil")
}
})

// Then the output should indicate the error
expectedOutput := "save config error"
if !strings.Contains(output, expectedOutput) {
t.Errorf("Expected output to contain %q, got %q", expectedOutput, output)
}
})
}
91 changes: 54 additions & 37 deletions cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,25 @@ var envCmd = &cobra.Command{
Short: "Output commands to set environment variables",
Long: "Output commands to set environment variables for the application.",
RunE: func(cmd *cobra.Command, args []string) error {
// Resolve all helpers from the DI container
helperInstances, err := container.ResolveAll((*helpers.Helper)(nil))
// Collect environment variables
envVars, err := collectEnvVars()
if err != nil {
if verbose {
return fmt.Errorf("Error resolving helpers: %w", err)
}
return nil
return err
}

// Iterate through all helpers and get environment variables
for _, instance := range helperInstances {
helper := instance.(helpers.Helper)
envVars, err := helper.GetEnvVars()
if err != nil {
if verbose {
return fmt.Errorf("Error getting environment variables: %w", err)
}
return nil
}

// Sort the environment variables by key
keys := make([]string, 0, len(envVars))
for k := range envVars {
keys = append(keys, k)
}
sort.Strings(keys)

// Use the shell to print environment variables
sortedEnvVars := make(map[string]string)
for _, k := range keys {
sortedEnvVars[k] = envVars[k]
}
shellInstance.PrintEnvVars(sortedEnvVars)
// Sort the environment variables by key
keys := make([]string, 0, len(envVars))
for k := range envVars {
keys = append(keys, k)
}
sort.Strings(keys)

// Call PostEnvExec on the helper
if err := helper.PostEnvExec(); err != nil {
if verbose {
return fmt.Errorf("Error executing PostEnvExec: %w", err)
}
return nil
}
// Use the shell to print environment variables
sortedEnvVars := make(map[string]string)
for _, k := range keys {
sortedEnvVars[k] = envVars[k]
}
shellInstance.PrintEnvVars(sortedEnvVars)

return nil
},
Expand All @@ -63,3 +40,43 @@ var envCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(envCmd)
}

// collectEnvVars collects environment variables from all helpers and returns them as a map.
func collectEnvVars() (map[string]string, error) {
// Resolve all helpers from the DI container
helperInstances, err := container.ResolveAll((*helpers.Helper)(nil))
if err != nil {
if verbose {
return nil, fmt.Errorf("Error resolving helpers: %w", err)
}
return nil, nil
}

envVars := make(map[string]string)
// Iterate through all helpers and get environment variables
for _, instance := range helperInstances {
helper := instance.(helpers.Helper)
helperEnvVars, err := helper.GetEnvVars()
if err != nil {
if verbose {
return nil, fmt.Errorf("Error getting environment variables: %w", err)
}
return nil, nil
}
for k, v := range helperEnvVars {
if v != "" {
envVars[k] = v
}
}

// Call PostEnvExec on the helper
if err := helper.PostEnvExec(); err != nil {
if verbose {
return nil, fmt.Errorf("Error executing PostEnvExec: %w", err)
}
return nil, nil
}
}

return envVars, nil
}
23 changes: 14 additions & 9 deletions cmd/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,18 @@ func TestEnvCmd(t *testing.T) {
mocks := mocks.CreateSuperMocks(mockContainer)
Initialize(mocks.Container)

// Capture stderr
var buf bytes.Buffer
rootCmd.SetErr(&buf)

// When the env command is executed with verbose flag
output := captureStderr(func() {
rootCmd.SetArgs([]string{"env", "--verbose"})
err := rootCmd.Execute()
if err == nil {
t.Fatalf("Expected error, got nil")
}
})
rootCmd.SetArgs([]string{"env", "--verbose"})
err := rootCmd.Execute()
if err == nil {
t.Fatalf("Expected error, got nil")
}

output := buf.String()

// Then the output should indicate the error
expectedOutput := "Error resolving helpers: resolve helpers error"
Expand All @@ -85,9 +89,8 @@ func TestEnvCmd(t *testing.T) {
mocks := mocks.CreateSuperMocks(mockContainer)
Initialize(mocks.Container)

// Capture the output
// Capture stderr
var buf bytes.Buffer
rootCmd.SetOut(&buf)
rootCmd.SetErr(&buf)

// When the env command is executed without verbose flag
Expand All @@ -97,6 +100,8 @@ func TestEnvCmd(t *testing.T) {
if err != nil {
t.Fatalf("Expected error nil, got %v", err)
}

// Then there should be no output
if buf.Len() != 0 {
t.Fatalf("Expected no output, got %s", buf.String())
}
Expand Down
Loading
Loading