Skip to content

Commit

Permalink
feat: exit with 101 from forc-test if there are failing tests (FuelLa…
Browse files Browse the repository at this point in the history
…bs#4463)

## Description
closes FuelLabs#4462.

With this PR we exit from `forc test` with 101 exit status, if there are
failing unit tests. If `forc test` execution itself fails we exit with
1. This is useful especially in CIs as the return status can be checked
to better understand what is happening.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Sophie Dankel <[email protected]>
  • Loading branch information
kayagokalp and sdankel authored Apr 19, 2023
1 parent 49ec0bc commit ebdbbfe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/book/src/testing/unit-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Each test function is ran as if it were the entry point for a
[script](../sway-program-types/scripts.md). Tests "pass" if they return
successfully, and "fail" if they revert or vice versa while [testing failure](#testing-failure).

If the project has failing tests `forc test` will exit with exit status `101`.

## Building and Running Tests

We can build and execute all tests within a package with the following:
Expand Down
8 changes: 8 additions & 0 deletions forc-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ impl ForcError {
pub fn new(error: anyhow::Error, exit_code: u8) -> Self {
Self { error, exit_code }
}

/// Returns a `ForcError` with provided exit_code.
pub fn exit_code(self, exit_code: u8) -> Self {
Self {
error: self.error,
exit_code,
}
}
}

impl AsRef<anyhow::Error> for ForcError {
Expand Down
6 changes: 4 additions & 2 deletions forc/src/cli/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ansi_term::Colour;
use clap::Parser;
use forc_pkg as pkg;
use forc_test::{TestRunnerCount, TestedPackage};
use forc_util::{forc_result_bail, format_log_receipts, ForcResult};
use forc_util::{forc_result_bail, format_log_receipts, ForcError, ForcResult};
use tracing::info;

/// Run the Sway unit tests for the current project.
Expand Down Expand Up @@ -86,7 +86,9 @@ pub(crate) fn exec(cmd: Command) -> ForcResult<()> {
if all_tests_passed {
Ok(())
} else {
Err("Some tests failed.".into())
let forc_error: ForcError = "Some tests failed.".into();
const FAILING_UNIT_TESTS_EXIT_CODE: u8 = 101;
Err(forc_error.exit_code(FAILING_UNIT_TESTS_EXIT_CODE))
}
}

Expand Down

0 comments on commit ebdbbfe

Please sign in to comment.