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
1 change: 1 addition & 0 deletions internal/shells/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ type BashCommandConfiguration struct {
// Executes a bash command and returns the output or error.
func ExecuteBashCommand(command string, config BashCommandConfiguration) (CommandOutput, error) {
var commandWithStateSaved = []string{
"set -e",
command,
"IE_LAST_COMMAND_EXIT_CODE=\"$?\"",
"env > " + environmentStateFile,
Expand Down
117 changes: 114 additions & 3 deletions internal/shells/bash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ func TestEnvironmentVariableValidationAndFiltering(t *testing.T) {
"_AnotherValidKey": "value2",
}

if len(validEnvMap) != len(expectedValidEnvMap) {
t.Errorf("Expected validEnvMap to have %d keys, got %d", len(expectedValidEnvMap), len(validEnvMap))
}
if len(validEnvMap) != len(expectedValidEnvMap) {
t.Errorf(
"Expected validEnvMap to have %d keys, got %d",
len(expectedValidEnvMap),
len(validEnvMap),
)
}

for key, value := range validEnvMap {
if expectedValue, ok := expectedValidEnvMap[key]; !ok || value != expectedValue {
Expand All @@ -64,3 +68,110 @@ func TestEnvironmentVariableValidationAndFiltering(t *testing.T) {
}
})
}

func TestBashCommandExecution(t *testing.T) {
// Ensures that if a command succeeds, the output is returned.
t.Run("Valid command execution", func(t *testing.T) {
cmd := "printf hello"
result, err := ExecuteBashCommand(
cmd,
BashCommandConfiguration{
EnvironmentVariables: nil,
InheritEnvironment: true,
InteractiveCommand: false,
WriteToHistory: false,
},
)
if err != nil {
t.Errorf("Expected err to be nil, got %v", err)
}
if result.StdOut != "hello" {
t.Errorf("Expected result to be non-empty, got '%s'", result.StdOut)
}
})

// Ensures that if a command fails, an error is returned.
t.Run("Invalid command execution", func(t *testing.T) {
cmd := "not_real_command"
_, err := ExecuteBashCommand(
cmd,
BashCommandConfiguration{
EnvironmentVariables: nil,
InheritEnvironment: true,
InteractiveCommand: false,
WriteToHistory: false,
},
)

if err == nil {
t.Errorf("Expected an error to occur, but the command succeeded.")
}

})

// Test the execution of commands with multiple subcommands.
t.Run("Command with multiple subcommands", func(t *testing.T) {
cmd := "printf hello; printf world"
result, err := ExecuteBashCommand(
cmd,
BashCommandConfiguration{
EnvironmentVariables: nil,
InheritEnvironment: true,
InteractiveCommand: false,
WriteToHistory: false,
},
)
if err != nil {
t.Errorf("Expected err to be nil, got %v", err)
}
if result.StdOut != "helloworld" {
t.Errorf("Expected result to be non-empty, got '%s'", result.StdOut)
}
})

// Ensures that if one of the subcommands fail, the other commands do
// as well.
t.Run("Command with multiple subcommands exits on first error", func(t *testing.T) {
cmd := "printf hello; not_real_command; printf world"
_, err := ExecuteBashCommand(
cmd,
BashCommandConfiguration{
EnvironmentVariables: nil,
InheritEnvironment: true,
InteractiveCommand: false,
WriteToHistory: false,
},
)

if err == nil {
t.Errorf("Expected an error to occur, but the command succeeded.")
}

})

// Ensures that commands can access environment variables passed into
// the configuration.
t.Run("Command with environment variables", func(t *testing.T) {
cmd := "printf $TEST_ENV_VAR"
result, err := ExecuteBashCommand(
cmd,
BashCommandConfiguration{
EnvironmentVariables: map[string]string{
"TEST_ENV_VAR": "hello",
},
InheritEnvironment: true,
InteractiveCommand: false,
WriteToHistory: false,
},
)

if err != nil {
t.Errorf("Expected err to be nil, got %v", err)
}

if result.StdOut != "hello" {
t.Errorf("Expected result to be non-empty, got '%s'", result.StdOut)
}
})

}