Skip to content

Commit

Permalink
fix exit code error (bruin-data#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
y-bruin authored Dec 4, 2024
1 parent b79e577 commit 045fedb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
47 changes: 30 additions & 17 deletions integration-tests/integration-test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {

expectExitCode("validate happy-path", 0)
expectExitCode("run --use-uv happy-path", 0)
expectExitCode("run happy-path", 0)
// expectExitCode("run happy-path", 0)
expectJSONOutput("internal parse-pipeline happy-path", "happy-path/expectations/pipeline.yml.json")
expectJSONOutput(
"internal parse-asset happy-path/assets/asset.py",
Expand All @@ -46,6 +46,7 @@ func main() {

expectOutputIncludes(
"internal parse-asset faulty-pipeline/assets/error.sql",
1,
[]string{"error creating asset from file", "unmarshal errors"},
)

Expand All @@ -56,6 +57,7 @@ func main() {

expectOutputIncludes(
"run malformed/assets/malformed.sql",
1,
[]string{"Parser Error: syntax error at or near \"S_ELECT_\"", "Failed assets 1"},
)

Expand All @@ -79,11 +81,13 @@ func main() {
)
}

func expectOutputIncludes(command string, contains []string) {
output, err := runCommand(command)
func expectOutputIncludes(command string, code int, contains []string) {
output, exitCode, err := runCommandWithExitCode(command)
if err != nil {
fmt.Println("Failed:", err)
os.Exit(1)
if exitCode != code {
fmt.Println("Failed:", err)
os.Exit(1)
}
}

for _, c := range contains {
Expand Down Expand Up @@ -137,19 +141,12 @@ func expectJSONOutput(command string, jsonFilePath string) {
}

func expectExitCode(command string, code int) {
output, err := runCommand(command)
output, exitCode, err := runCommandWithExitCode(command)
if err != nil {
// Try to get the exit code
if errors.As(err, exec.ExitError{}) {
exitError := err.(*exec.ExitError) //nolint: errorlint
// Get the status code
if status, ok := exitError.Sys().(syscall.WaitStatus); ok {
if status.ExitStatus() != code {
fmt.Println(strconv.Itoa(code) + " Was expected but got:" + strconv.Itoa(status.ExitStatus()))
fmt.Println(output)
os.Exit(1)
}
}
if exitCode != code {
fmt.Println(strconv.Itoa(code) + " Was expected but got:" + strconv.Itoa(exitCode))
fmt.Println(output)
os.Exit(1)
}
// Handle other errors
fmt.Printf("Error running command: %v\n", err)
Expand All @@ -170,3 +167,19 @@ func runCommand(command string) (string, error) {

return string(output), err
}

func runCommandWithExitCode(command string) (string, int, error) {
output, err := runCommand(command)
if err != nil {
// Try to get the exit code
var exitError *exec.ExitError
if errors.As(err, &exitError) {
// Get the status code
if status, ok := exitError.Sys().(syscall.WaitStatus); ok {
return output, status.ExitStatus(), nil
}
}
return output, 1, err
}
return output, 0, nil
}
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,9 @@ func main() {
},
}

_ = app.Run(os.Args)
err := app.Run(os.Args)

if err != nil {
cli.HandleExitCoder(err)
}
}

0 comments on commit 045fedb

Please sign in to comment.