ExpectExit is called once the command completed and before PostRun function in order to check the exit code returned. This function is always required by RunCommand and can call additional test functions processing the command result like ExpectOutput, ExpectError.
(code int, resultOps ...ApptainerCmdResultOp)
| 416 | // test functions processing the command result like ExpectOutput, |
| 417 | // ExpectError. |
| 418 | func ExpectExit(code int, resultOps ...ApptainerCmdResultOp) ApptainerCmdOp { |
| 419 | return func(s *apptainerCmd) { |
| 420 | if s.resultFn == nil { |
| 421 | s.resultFn = ExpectExit(code, resultOps...) |
| 422 | return |
| 423 | } |
| 424 | |
| 425 | r := s.result |
| 426 | t := s.t |
| 427 | |
| 428 | t.Helper() |
| 429 | |
| 430 | if t.Failed() { |
| 431 | return |
| 432 | } |
| 433 | |
| 434 | cause := errors.Cause(s.waitErr) |
| 435 | switch x := cause.(type) { |
| 436 | case *exec.ExitError: |
| 437 | if status, ok := x.Sys().(syscall.WaitStatus); ok { |
| 438 | exitCode := status.ExitStatus() |
| 439 | if status.Signaled() { |
| 440 | s := status.Signal() |
| 441 | exitCode = 128 + int(s) |
| 442 | } |
| 443 | if code != exitCode { |
| 444 | t.Logf("\n%q output:\n%s%s\n", r.FullCmd, string(r.Stderr), string(r.Stdout)) |
| 445 | t.Errorf("got %d as exit code and was expecting %d: %+v", exitCode, code, s.waitErr) |
| 446 | return |
| 447 | } |
| 448 | } |
| 449 | default: |
| 450 | if s.waitErr != nil { |
| 451 | t.Errorf("command execution of %q failed: %+v", r.FullCmd, s.waitErr) |
| 452 | return |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | if code == 0 && s.waitErr != nil { |
| 457 | t.Logf("\n%q output:\n%s%s\n", r.FullCmd, string(r.Stderr), string(r.Stdout)) |
| 458 | t.Errorf("unexpected failure while executing %q", r.FullCmd) |
| 459 | return |
| 460 | } else if code != 0 && s.waitErr == nil { |
| 461 | t.Logf("\n%q output:\n%s%s\n", r.FullCmd, string(r.Stderr), string(r.Stdout)) |
| 462 | t.Errorf("unexpected success while executing %q", r.FullCmd) |
| 463 | return |
| 464 | } |
| 465 | |
| 466 | for _, op := range resultOps { |
| 467 | if op != nil { |
| 468 | op(t, r) |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | // RunApptainer executes an Apptainer command within a test execution |
| 475 | // context. |
no test coverage detected