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

package_buildpack() and package_crate_buildpack() contain asserts! even though they return Results #709

Closed
edmorley opened this issue Nov 2, 2023 · 0 comments · Fixed by #720
Assignees

Comments

@edmorley
Copy link
Member

edmorley commented Nov 2, 2023

Currently these functions mix and match assertions and returning Result:

pub(crate) fn package_crate_buildpack(
cargo_profile: CargoProfile,
target_triple: impl AsRef<str>,
cargo_manifest_dir: &Path,
target_buildpack_dir: &Path,
) -> Result<PathBuf, PackageBuildpackError> {
let buildpack_toml = cargo_manifest_dir.join("buildpack.toml");
assert!(
buildpack_toml.exists(),
"Could not package directory as buildpack! No `buildpack.toml` file exists at {}",
cargo_manifest_dir.display()
);

assert!(
root_node.is_some(),
"Could not package directory as buildpack! No buildpack with id `{buildpack_id}` exists in the workspace at {}",
workspace_root_path.display()
);

These were introduced as part of #666, and are part of the reason that the regression in #704 was able to sneak in (since it means the tests circumvent the top level error handling that was broken in #666).

They should be converted to additional error types and returned as an error instead.

There's also a lint we can use to prevent this in the future:
https://rust-lang.github.io/rust-clippy/master/index.html#/panic_in_result_fn
(in addition to panics, it detects assertions in result functions)

@edmorley edmorley self-assigned this Nov 2, 2023
edmorley added a commit that referenced this issue Nov 2, 2023
…`panic!` (#711)

The `clippy::unwrap_used` lint prevents usage of `.unwrap()` outside of tests:
https://rust-lang.github.io/rust-clippy/master/index.html#/unwrap_used

The `clippy::panic_in_result_fn` lint prevents usages of `assert!` or
`panic!` in functions that already return a `Result`:
https://rust-lang.github.io/rust-clippy/master/index.html#/panic_in_result_fn

These lints catch cases like #709, #710 and #712.

The lints have not been enabled for the integration test modules (since using
`unwrap()` is fine there), or to the example buildpacks, since it's a lot of boilerplate
to add to an example.

Very soon we will be able to switch to the upcoming `[lints]` table feature, which will
mean the duplication of lint definitions can be avoided entirely:
https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#lints

GUS-W-14413286.
edmorley added a commit that referenced this issue Nov 7, 2023
After #666, incorrect error messages were shown for failures that
occurred during buildpack compilation packaging, since the
handling in `TestRunner::build_internal` used the wrong message
text, plus didn't include the error struct in the `panic!` string.

These have been fixed, plus display representations added to
all of the new error variants.

In addition, the usages of `assert!` and `.unwrap()` in functions that
return `Result` have been replaced with new error enum variants.

The change in output can be seen in the new tests added by #717.

There is one last scenario that doesn't yet have a test - the testing
of the cross-compilation output itself. However, I'll add that separately,
since it's going to be more complex to test / there are a few different
options that we'll need to choose between.

Fixes #704.
Fixes #709.
Fixes #710.
GUS-W-14395170.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant