refactor(rust): allow unwrap in tests; restyle expect messages (#4423) Follows up on post-merge review feedback on #4407 (https://github.com/bazel-contrib/rules_python/pull/4407#discussion_r4500292802): tests should be free to panic, so unwrap() and expect() should not be lint errors there. To do so, drop the global `-Dclippy::unwrap_used` flag and add a `.clippy.toml` with the `allow-*-in-tests` settings. Production crates keep enforcing `deny(clippy::expect_used, clippy::unwrap_used)` themselves via crate-level attributes. The global flag has to go because clippy's `allow-*-in-tests` only recognizes `#[test]` functions and `#[cfg(test)]` items. Helper functions in integration-test crates match neither, so a global `unwrap_used` would still flag them. Relying on crate-level `deny` attributes scopes the lint to production code only. Test `expect()` messages now follow std's recommended style: they state why the value should be `Ok` rather than naming the failed action. Also records these conventions in `.agents/rules/rust.md`.
diff --git a/.agents/rules/rust.md b/.agents/rules/rust.md index 939086b..adaa035 100644 --- a/.agents/rules/rust.md +++ b/.agents/rules/rust.md
@@ -65,3 +65,38 @@ * **Formatting**: Format Rust sources with `bazel run @rules_rust//:rustfmt` and verify with `--aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect` `--output_groups=+rustfmt_checks`. + +## Errors, Panics & Linting +* **Return errors, don't panic**: Production crates (`crates/*/src/*.rs`) + carry `#![deny(clippy::expect_used, clippy::unwrap_used)]`; propagate + with `?` and attach context so every failure names the path, line, or + entry it concerns. Reach for `const` values (e.g. + `zip::DateTime::DEFAULT`) instead of `.expect()` on infallible + constructors. +* **Choose the error type by audience**: For CLI tools and other code that + owns the error-handling context (the `shrubbery` subcommand crates), use + `anyhow` (`bail!`, `.context()`, `.with_context(|| ...)`) and print the + chain with `{err:#}` in `run_cli`. For library code consumed by other + crates, define a typed error (`thiserror` enum implementing + `std::error::Error` with `#[from]`/`#[source]`) so callers can match on + variants and errors compose across crate boundaries; such a type still + converts into `anyhow::Error` for free at the CLI layer. +* **Tests may panic**: `unwrap()`, `expect()`, `panic!`, and indexing are + allowed in test code (`.clippy.toml` sets `allow-*-in-tests`). When using + `expect()`, write the message in std's recommended style, stating why the + value *should* be `Ok`/`Some` (`"manifest should be writable"`, + `"output zip should be a valid archive"`), not the action that failed + (`"write manifest"`). +* **Lints run on every build**: `.bazelrc` applies the clippy and rustfmt + aspects by default (`--config=rust-lint`, `-Dwarnings`), so a plain + `bazel build` fails on lint or formatting problems. Do not add + `-Dclippy::unwrap_used`/`expect_used` globally: clippy's + `allow-*-in-tests` settings only recognize `#[test]` functions and + `#[cfg(test)]` items, so helper functions in integration-test crates + (`tests/*/*_test.rs`) would be flagged. Code under `#[cfg(unix)]` must + leave no `unused_mut`/`useless_vec` behind on Windows; rebind instead of + conditionally `push`ing. +* **WORKSPACE-mode stub**: Any new `@rules_rust//...` flag or aspect + referenced from `.bazelrc` must also exist in + `tests/modules/rules_rust_stub/`, which WORKSPACE CI jobs use in place of + rules_rust.
diff --git a/.bazelrc b/.bazelrc index ed25451..e85d8bf 100644 --- a/.bazelrc +++ b/.bazelrc
@@ -30,9 +30,12 @@ build:rust-lint --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect build:rust-lint --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect build:rust-lint --output_groups=+clippy_checks,+rustfmt_checks -# `unwrap()` is forbidden everywhere; the non-test crates additionally deny -# `expect()` via a crate-level attribute. -build:rust-lint --@rules_rust//rust/settings:clippy_flags=-Dwarnings,-Dclippy::unwrap_used +# Non-test crates deny `unwrap()`/`expect()` via a crate-level attribute +# (errors must be returned, not panicked). Tests may panic freely; see +# .clippy.toml. `-Dwarnings` makes any clippy warning fail the build; the toml +# only configures how lints behave, it cannot set lint levels. +build:rust-lint --@rules_rust//rust/settings:clippy_flags=-Dwarnings +build:rust-lint --@rules_rust//rust/settings:clippy.toml=//:.clippy.toml # Ensure ongoing compatibility with this flag. common --incompatible_disallow_struct_provider_syntax
diff --git a/.clippy.toml b/.clippy.toml new file mode 100644 index 0000000..9d1db37 --- /dev/null +++ b/.clippy.toml
@@ -0,0 +1,12 @@ +# Clippy configuration, applied via the `rust-lint` config in .bazelrc. +# +# Tests may panic freely. Production crates deny unwrap/expect themselves via +# `#![deny(clippy::expect_used, clippy::unwrap_used)]`. Note these settings only +# recognize `#[test]` functions and `#[cfg(test)]` items, not helper functions +# in integration-test crates, which is why unwrap/expect lints are not enabled +# globally. +allow-dbg-in-tests = true +allow-expect-in-tests = true +allow-indexing-slicing-in-tests = true +allow-panic-in-tests = true +allow-unwrap-in-tests = true
diff --git a/BUILD.bazel b/BUILD.bazel index 71cdb82..9f57081 100644 --- a/BUILD.bazel +++ b/BUILD.bazel
@@ -54,6 +54,7 @@ licenses(["notice"]) exports_files([ + ".clippy.toml", "LICENSE", "version.bzl", ])
diff --git a/tests/exe_zip_maker/exe_zip_maker_test.rs b/tests/exe_zip_maker/exe_zip_maker_test.rs index 1ba26dc..5f891dd 100644 --- a/tests/exe_zip_maker/exe_zip_maker_test.rs +++ b/tests/exe_zip_maker/exe_zip_maker_test.rs
@@ -36,11 +36,11 @@ #[test] fn test_compute_file_sha256_hex() { let temp_dir = env::temp_dir().join(format!("sha256_test_{}", std::process::id())); - fs::create_dir_all(&temp_dir).expect("create temp dir"); + fs::create_dir_all(&temp_dir).expect("temp dir should be creatable"); let file_path = temp_dir.join("sample.txt"); - fs::write(&file_path, b"hello world\n").expect("write file_path"); - let hash = compute_file_sha256_hex(&file_path).expect("hash file"); + fs::write(&file_path, b"hello world\n").expect("file_path should be writable"); + let hash = compute_file_sha256_hex(&file_path).expect("file should be hashable"); assert_eq!( hash, "a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447" @@ -52,26 +52,26 @@ #[test] fn test_create_exe_zip_successful() { let temp_dir = env::temp_dir().join(format!("create_exe_zip_test_{}", std::process::id())); - fs::create_dir_all(&temp_dir).expect("create temp dir"); + fs::create_dir_all(&temp_dir).expect("temp dir should be creatable"); let preamble_path = temp_dir.join("preamble.sh"); let zip_path = temp_dir.join("data.zip"); let output_path = temp_dir.join("output.exe"); let zip_content = b"PK\x03\x04dummyzipcontent"; - fs::write(&zip_path, zip_content).expect("write zip_path"); + fs::write(&zip_path, zip_content).expect("zip_path should be writable"); let preamble_text = b"#!/bin/bash\nEXPECTED_HASH='%ZIP_HASH%'\n# ... logic ...\n"; - fs::write(&preamble_path, preamble_text).expect("write preamble_path"); + fs::write(&preamble_path, preamble_text).expect("preamble_path should be writable"); - create_exe_zip(&preamble_path, &zip_path, &output_path).expect("create_exe_zip"); + create_exe_zip(&preamble_path, &zip_path, &output_path).expect("create_exe_zip should succeed"); assert!(output_path.exists()); #[cfg(unix)] { use std::os::unix::fs::PermissionsExt; - let st = fs::metadata(&output_path).expect("stat output"); + let st = fs::metadata(&output_path).expect("output should exist"); assert_ne!( st.permissions().mode() & 0o100, 0, @@ -79,7 +79,7 @@ ); } - let content = fs::read(&output_path).expect("read output"); + let content = fs::read(&output_path).expect("output should be readable"); let expected_hash = "65e39989ca91c49484998aa3f0429f6943c029609bfd2f3c18c77bf9ded72c59"; let expected_preamble = replace_bytes(preamble_text, PLACEHOLDER, expected_hash.as_bytes()); @@ -93,21 +93,21 @@ #[test] fn test_create_exe_zip_multiple_placeholders() { let temp_dir = env::temp_dir().join(format!("create_exe_zip_multi_{}", std::process::id())); - fs::create_dir_all(&temp_dir).expect("create temp dir"); + fs::create_dir_all(&temp_dir).expect("temp dir should be creatable"); let preamble_path = temp_dir.join("preamble.sh"); let zip_path = temp_dir.join("data.zip"); let output_path = temp_dir.join("output.exe"); let zip_content = b"PK\x03\x04dummyzipcontent"; - fs::write(&zip_path, zip_content).expect("write zip_path"); + fs::write(&zip_path, zip_content).expect("zip_path should be writable"); let preamble_text = b"# First: %ZIP_HASH%\n# Second: %ZIP_HASH%\n"; - fs::write(&preamble_path, preamble_text).expect("write preamble_path"); + fs::write(&preamble_path, preamble_text).expect("preamble_path should be writable"); - create_exe_zip(&preamble_path, &zip_path, &output_path).expect("create_exe_zip"); + create_exe_zip(&preamble_path, &zip_path, &output_path).expect("create_exe_zip should succeed"); - let content = fs::read(&output_path).expect("read output"); + let content = fs::read(&output_path).expect("output should be readable"); let expected_hash = "65e39989ca91c49484998aa3f0429f6943c029609bfd2f3c18c77bf9ded72c59"; let expected_preamble = replace_bytes(preamble_text, PLACEHOLDER, expected_hash.as_bytes()); @@ -120,16 +120,16 @@ #[test] fn test_create_exe_zip_creates_parent_dir() { let temp_dir = env::temp_dir().join(format!("create_exe_zip_parent_{}", std::process::id())); - fs::create_dir_all(&temp_dir).expect("create temp dir"); + fs::create_dir_all(&temp_dir).expect("temp dir should be creatable"); let preamble_path = temp_dir.join("preamble.sh"); let zip_path = temp_dir.join("data.zip"); let output_path = temp_dir.join("nested").join("sub").join("output.exe"); - fs::write(&zip_path, b"content").expect("write zip_path"); - fs::write(&preamble_path, b"preamble").expect("write preamble_path"); + fs::write(&zip_path, b"content").expect("zip_path should be writable"); + fs::write(&preamble_path, b"preamble").expect("preamble_path should be writable"); - create_exe_zip(&preamble_path, &zip_path, &output_path).expect("create_exe_zip"); + create_exe_zip(&preamble_path, &zip_path, &output_path).expect("create_exe_zip should succeed"); assert!(output_path.exists()); let _ = fs::remove_dir_all(&temp_dir); @@ -138,17 +138,17 @@ #[test] fn test_create_exe_zip_missing_files() { let temp_dir = env::temp_dir().join(format!("create_exe_zip_err_{}", std::process::id())); - fs::create_dir_all(&temp_dir).expect("create temp dir"); + fs::create_dir_all(&temp_dir).expect("temp dir should be creatable"); let missing_preamble = temp_dir.join("nonexistent_preamble.sh"); let zip_path = temp_dir.join("data.zip"); let output_path = temp_dir.join("output.exe"); - fs::write(&zip_path, b"dummy").expect("write zip_path"); + fs::write(&zip_path, b"dummy").expect("zip_path should be writable"); assert!(create_exe_zip(&missing_preamble, &zip_path, &output_path).is_err()); let preamble_path = temp_dir.join("preamble.sh"); - fs::write(&preamble_path, b"preamble").expect("write preamble_path"); + fs::write(&preamble_path, b"preamble").expect("preamble_path should be writable"); let missing_zip = temp_dir.join("nonexistent_data.zip"); assert!(create_exe_zip(&preamble_path, &missing_zip, &output_path).is_err()); @@ -172,12 +172,13 @@ assert!(parse_args(["--unknown", "b", "c"]).is_err()); let temp_dir = env::temp_dir().join(format!("exe_zip_cli_{}", std::process::id())); - fs::create_dir_all(&temp_dir).expect("create temp dir"); + fs::create_dir_all(&temp_dir).expect("temp dir should be creatable"); let preamble_path = temp_dir.join("preamble.sh"); let zip_path = temp_dir.join("data.zip"); let output_path = temp_dir.join("output.exe"); - fs::write(&preamble_path, b"#!/bin/sh\n# %ZIP_HASH%\n").expect("write preamble_path"); - fs::write(&zip_path, b"PK\x03\x04dummy").expect("write zip_path"); + fs::write(&preamble_path, b"#!/bin/sh\n# %ZIP_HASH%\n") + .expect("preamble_path should be writable"); + fs::write(&zip_path, b"PK\x03\x04dummy").expect("zip_path should be writable"); assert_eq!(run_cli(["missing_args"]), 1); assert_eq!(
diff --git a/tests/modules/rules_rust_stub/rust/settings/BUILD.bazel b/tests/modules/rules_rust_stub/rust/settings/BUILD.bazel index 91ce64c..85ebd1b 100644 --- a/tests/modules/rules_rust_stub/rust/settings/BUILD.bazel +++ b/tests/modules/rules_rust_stub/rust/settings/BUILD.bazel
@@ -2,9 +2,17 @@ package(default_visibility = ["//visibility:public"]) -# The root .bazelrc sets this flag on every build (see the `rust-lint` config); -# it is unused in WORKSPACE mode because the stub has no Rust targets. +# The root .bazelrc sets these flags on every build (see the `rust-lint` +# config); they are unused in WORKSPACE mode because the stub has no Rust +# targets. string_list_flag( name = "clippy_flags", build_setting_default = [], ) + +label_flag( + name = "clippy.toml", + build_setting_default = ":empty_clippy_toml", +) + +filegroup(name = "empty_clippy_toml")
diff --git a/tests/shrubbery/shrubbery_test.rs b/tests/shrubbery/shrubbery_test.rs index 7a03035..2519b35 100644 --- a/tests/shrubbery/shrubbery_test.rs +++ b/tests/shrubbery/shrubbery_test.rs
@@ -11,7 +11,7 @@ .unwrap_or_else(env::temp_dir); let dir = base.join(format!("shrubbery_{}_{}", prefix, std::process::id())); let _ = fs::remove_dir_all(&dir); - fs::create_dir_all(&dir).expect("create temp dir"); + fs::create_dir_all(&dir).expect("temp dir should be creatable"); dir } @@ -43,20 +43,20 @@ let zip_in = dir.join("in.zip"); let out_pyz = dir.join("out.pyz"); - fs::write(&preamble, b"#!/bin/sh\n").expect("write preamble"); + fs::write(&preamble, b"#!/bin/sh\n").expect("preamble should be writable"); fs::write( &zip_in, b"PK\x05\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", ) - .expect("write zip_in"); + .expect("zip_in should be writable"); assert_eq!( run_cli([ "shrubbery", "exe_zip_maker", - preamble.to_str().expect("preamble path is UTF-8"), - zip_in.to_str().expect("zip_in path is UTF-8"), - out_pyz.to_str().expect("out_pyz path is UTF-8"), + preamble.to_str().expect("preamble path should be UTF-8"), + zip_in.to_str().expect("zip_in path should be UTF-8"), + out_pyz.to_str().expect("out_pyz path should be UTF-8"), ]), 0 ); @@ -65,12 +65,14 @@ #[test] fn test_cli_binary_subprocess() { - let resolved = PathBuf::from(env::var_os("SHRUBBERY_BIN").expect("SHRUBBERY_BIN must be set")); + let resolved = PathBuf::from( + env::var_os("SHRUBBERY_BIN").expect("SHRUBBERY_BIN should be set by the test runner"), + ); let help_out = Command::new(&resolved) .arg("--help") .output() - .expect("run shrubbery --help"); + .expect("shrubbery --help should run"); assert_eq!(help_out.status.code(), Some(0)); let stdout = String::from_utf8_lossy(&help_out.stdout); for sub in SUBCOMMANDS { @@ -80,7 +82,7 @@ let bad_out = Command::new(&resolved) .arg("not_a_subcommand") .output() - .expect("run shrubbery with bad subcommand"); + .expect("shrubbery should run with a bad subcommand"); assert_eq!(bad_out.status.code(), Some(1)); assert!( String::from_utf8_lossy(&bad_out.stderr).contains("unknown subcommand"),
diff --git a/tests/zipper/zipper_test.rs b/tests/zipper/zipper_test.rs index 37fdbce..c779cb6 100644 --- a/tests/zipper/zipper_test.rs +++ b/tests/zipper/zipper_test.rs
@@ -12,14 +12,16 @@ /// Reads all entries from a ZIP file on disk using `zip::ZipArchive` (which verifies CRC-32). fn read_zip_entries(zip_path: &Path) -> Vec<ParsedZipEntry> { - let file = File::open(zip_path).expect("open output zip"); - let mut archive = ZipArchive::new(file).expect("parse output zip"); + let file = File::open(zip_path).expect("output zip should open"); + let mut archive = ZipArchive::new(file).expect("output zip should be a valid archive"); let mut entries = Vec::with_capacity(archive.len()); for i in 0..archive.len() { - let mut entry = archive.by_index(i).expect("read zip entry"); + let mut entry = archive.by_index(i).expect("zip entry should be readable"); let mut data = Vec::with_capacity(entry.size() as usize); - entry.read_to_end(&mut data).expect("read zip entry data"); + entry + .read_to_end(&mut data) + .expect("zip entry data should be readable"); assert_eq!( data.len() as u64, entry.size(), @@ -61,12 +63,12 @@ let output_zip = tmp.path().join("output.zip"); let file1_path = tmp.path().join("file1.txt"); - fs::write(&file1_path, b"content1").expect("write file1_path"); + fs::write(&file1_path, b"content1").expect("file1_path should be writable"); #[cfg(unix)] let symlink_path = { let p = tmp.path().join("symlink_source"); - std::os::unix::fs::symlink("target.txt", &p).expect("create symlink_source"); + std::os::unix::fs::symlink("target.txt", &p).expect("symlink_source should be creatable"); p }; @@ -82,10 +84,10 @@ #[cfg(unix)] let manifest = format!("{manifest}\nrf-symlink|1|link1|{}", symlink_path.display()); - fs::write(&manifest_path, manifest).expect("write manifest"); + fs::write(&manifest_path, manifest).expect("manifest should be writable"); let opts = default_options(&manifest_path, &output_zip); - create_zip(&opts).expect("create_zip"); + create_zip(&opts).expect("create_zip should succeed"); let entries = read_zip_entries(&output_zip); let by_name: HashMap<String, ParsedZipEntry> = @@ -105,7 +107,7 @@ assert_eq!( by_name["runfiles/my_ws/empty_file"] .unix_mode - .expect("entry has unix mode") + .expect("entry should have a unix mode") & 0o777, 0o644 ); @@ -123,10 +125,11 @@ let manifest_path = tmp.path().join("manifest.txt"); let output_zip = tmp.path().join("output.zip"); - fs::write(&manifest_path, "symlink|path/to/link|target/path\n").expect("write manifest_path"); + fs::write(&manifest_path, "symlink|path/to/link|target/path\n") + .expect("manifest_path should be writable"); let opts = default_options(&manifest_path, &output_zip); - create_zip(&opts).expect("create_zip"); + create_zip(&opts).expect("create_zip should succeed"); let entries = read_zip_entries(&output_zip); assert_eq!(entries.len(), 1); @@ -143,18 +146,19 @@ let output_zip = tmp.path().join("output.zip"); let target_path = tmp.path().join("python3.14"); - fs::write(&target_path, b"python").expect("write target_path"); + fs::write(&target_path, b"python").expect("target_path should be writable"); let symlink_path = tmp.path().join("python"); - std::os::unix::fs::symlink("python3.14", &symlink_path).expect("create symlink symlink_path"); + std::os::unix::fs::symlink("python3.14", &symlink_path) + .expect("symlink_path symlink should be creatable"); fs::write( &manifest_path, format!("rf-file|0|bin/python|{}\n", symlink_path.display()), ) - .expect("write manifest_path"); + .expect("manifest_path should be writable"); let opts = default_options(&manifest_path, &output_zip); - create_zip(&opts).expect("create_zip"); + create_zip(&opts).expect("create_zip should succeed"); let entries = read_zip_entries(&output_zip); assert_eq!(entries.len(), 1); @@ -171,21 +175,21 @@ let output_zip = tmp.path().join("output.zip"); let source_dir = tmp.path().join("source"); - fs::create_dir_all(&source_dir).expect("create source_dir"); + fs::create_dir_all(&source_dir).expect("source_dir should be creatable"); let target_path = source_dir.join("python3.14"); - fs::write(&target_path, b"python").expect("write target_path"); + fs::write(&target_path, b"python").expect("target_path should be writable"); let source_symlink = source_dir.join("python"); std::os::unix::fs::symlink("python3.14", &source_symlink) - .expect("create symlink source_symlink"); + .expect("source_symlink symlink should be creatable"); let sandbox_dir = tmp.path().join("sandbox"); - fs::create_dir_all(&sandbox_dir).expect("create sandbox_dir"); + fs::create_dir_all(&sandbox_dir).expect("sandbox_dir should be creatable"); let sandbox_symlink = sandbox_dir.join("python"); std::os::unix::fs::symlink(&source_symlink, &sandbox_symlink) - .expect("create symlink sandbox_symlink"); + .expect("sandbox_symlink symlink should be creatable"); let sandbox_regular = sandbox_dir.join("python3.14"); std::os::unix::fs::symlink(&target_path, &sandbox_regular) - .expect("create symlink sandbox_regular"); + .expect("sandbox_regular symlink should be creatable"); fs::write( &manifest_path, @@ -195,10 +199,10 @@ sandbox_regular.display() ), ) - .expect("write manifest_path"); + .expect("manifest_path should be writable"); let opts = default_options(&manifest_path, &output_zip); - create_zip(&opts).expect("create_zip"); + create_zip(&opts).expect("create_zip should succeed"); let entries = read_zip_entries(&output_zip); let by_name: HashMap<String, ParsedZipEntry> = @@ -219,28 +223,30 @@ let output_zip = tmp.path().join("output.zip"); let library_dir = tmp.path().join("external"); - fs::create_dir_all(&library_dir).expect("create library_dir"); + fs::create_dir_all(&library_dir).expect("library_dir should be creatable"); let library = library_dir.join("libfoo.so"); - fs::write(&library, b"library").expect("write library"); + fs::write(&library, b"library").expect("library should be writable"); let solib_dir = tmp.path().join("solib"); - fs::create_dir_all(&solib_dir).expect("create solib_dir"); + fs::create_dir_all(&solib_dir).expect("solib_dir should be creatable"); let solib_link = solib_dir.join("libfoo.so"); - std::os::unix::fs::symlink(&library, &solib_link).expect("create symlink solib_link"); + std::os::unix::fs::symlink(&library, &solib_link) + .expect("solib_link symlink should be creatable"); let sandbox_dir = tmp.path().join("sandbox"); - fs::create_dir_all(&sandbox_dir).expect("create sandbox_dir"); + fs::create_dir_all(&sandbox_dir).expect("sandbox_dir should be creatable"); let sandbox_link = sandbox_dir.join("libfoo.so"); - std::os::unix::fs::symlink(&solib_link, &sandbox_link).expect("create symlink sandbox_link"); + std::os::unix::fs::symlink(&solib_link, &sandbox_link) + .expect("sandbox_link symlink should be creatable"); fs::write( &manifest_path, format!("rf-file|0|_solib/libfoo.so|{}\n", sandbox_link.display()), ) - .expect("write manifest_path"); + .expect("manifest_path should be writable"); let opts = default_options(&manifest_path, &output_zip); - create_zip(&opts).expect("create_zip"); + create_zip(&opts).expect("create_zip should succeed"); let entries = read_zip_entries(&output_zip); assert_eq!(entries.len(), 1); @@ -258,10 +264,10 @@ let broken_link = tmp.path().join("dangling_link"); std::os::unix::fs::symlink("nonexistent_target", &broken_link) - .expect("create symlink broken_link"); + .expect("broken_link symlink should be creatable"); let existing_file = tmp.path().join("real.txt"); - fs::write(&existing_file, b"real content").expect("write existing_file"); + fs::write(&existing_file, b"real content").expect("existing_file should be writable"); fs::write( &manifest_path, @@ -271,10 +277,10 @@ existing_file.display() ), ) - .expect("write manifest_path"); + .expect("manifest_path should be writable"); let opts = default_options(&manifest_path, &output_zip); - create_zip(&opts).expect("create_zip"); + create_zip(&opts).expect("create_zip should succeed"); let entries = read_zip_entries(&output_zip); let by_name: HashMap<String, ParsedZipEntry> = @@ -296,7 +302,7 @@ let output_zip = tmp.path().join("output.zip"); let file1_path = tmp.path().join("file1.txt"); - fs::write(&file1_path, b"content1").expect("write file1_path"); + fs::write(&file1_path, b"content1").expect("file1_path should be writable"); fs::write( &manifest_path, @@ -305,11 +311,11 @@ file1_path.display() ), ) - .expect("write manifest_path"); + .expect("manifest_path should be writable"); let mut opts = default_options(&manifest_path, &output_zip); opts.platform_pathsep = "\\".to_string(); - create_zip(&opts).expect("create_zip"); + create_zip(&opts).expect("create_zip should succeed"); let entries = read_zip_entries(&output_zip); let by_name: HashMap<String, ParsedZipEntry> = @@ -327,7 +333,7 @@ let output_zip = tmp.path().join("output.zip"); let file1_path = tmp.path().join("file1.txt"); - fs::write(&file1_path, b"content1").expect("write file1_path"); + fs::write(&file1_path, b"content1").expect("file1_path should be writable"); fs::write( &manifest_path, @@ -336,10 +342,10 @@ file1_path.display() ), ) - .expect("write manifest_path"); + .expect("manifest_path should be writable"); let opts = default_options(&manifest_path, &output_zip); - create_zip(&opts).expect("create_zip"); + create_zip(&opts).expect("create_zip should succeed"); let entries = read_zip_entries(&output_zip); assert_eq!(entries.len(), 1); @@ -355,36 +361,39 @@ let output_zip = tmp.path().join("output.zip"); let file1_path = tmp.path().join("file1.sh"); - fs::write(&file1_path, b"#!/bin/sh\necho hi\n").expect("write file1_path"); + fs::write(&file1_path, b"#!/bin/sh\necho hi\n").expect("file1_path should be writable"); #[cfg(unix)] { use std::os::unix::fs::PermissionsExt; let mut perms = fs::metadata(&file1_path) - .expect("stat file1_path") + .expect("file1_path should exist") .permissions(); perms.set_mode(0o755); - fs::set_permissions(&file1_path, perms).expect("chmod file1_path"); + fs::set_permissions(&file1_path, perms).expect("file1_path permissions should be settable"); } fs::write( &manifest_path, format!("regular|0|file1.sh|{}\n", file1_path.display()), ) - .expect("write manifest_path"); + .expect("manifest_path should be writable"); let opts = default_options(&manifest_path, &output_zip); - create_zip(&opts).expect("create_zip"); + create_zip(&opts).expect("create_zip should succeed"); let entries = read_zip_entries(&output_zip); assert_eq!(entries.len(), 1); assert_eq!( entries[0].last_modified, - Some(DateTime::from_date_and_time(1980, 1, 1, 0, 0, 0).expect("valid timestamp")) + Some( + DateTime::from_date_and_time(1980, 1, 1, 0, 0, 0) + .expect("1980-01-01 should be a valid zip timestamp") + ) ); assert!(entries[0].unix_mode.is_some()); #[cfg(unix)] assert_eq!( - entries[0].unix_mode.expect("entry has unix mode") & 0o777, + entries[0].unix_mode.expect("entry should have a unix mode") & 0o777, 0o755 ); } @@ -396,7 +405,7 @@ let output_zip = tmp.path().join("output.zip"); let file1_path = tmp.path().join("file1.txt"); - fs::write(&file1_path, b"content1").expect("write file1_path"); + fs::write(&file1_path, b"content1").expect("file1_path should be writable"); fs::write( &manifest_path, @@ -405,10 +414,10 @@ file1_path.display() ), ) - .expect("write manifest_path"); + .expect("manifest_path should be writable"); let opts = default_options(&manifest_path, &output_zip); - create_zip(&opts).expect("create_zip"); + create_zip(&opts).expect("create_zip should succeed"); let entries = read_zip_entries(&output_zip); let by_name: HashMap<String, ParsedZipEntry> = @@ -426,7 +435,7 @@ let output_zip = tmp.path().join("output.zip"); let file1_path = tmp.path().join("file1.txt"); - fs::write(&file1_path, b"content1").expect("write file1_path"); + fs::write(&file1_path, b"content1").expect("file1_path should be writable"); fs::write( &manifest_path, @@ -435,11 +444,11 @@ file1_path.display() ), ) - .expect("write manifest_path"); + .expect("manifest_path should be writable"); let mut opts = default_options(&manifest_path, &output_zip); opts.legacy_external_runfiles = true; - create_zip(&opts).expect("create_zip"); + create_zip(&opts).expect("create_zip should succeed"); let entries = read_zip_entries(&output_zip); let by_name: HashMap<String, ParsedZipEntry> = @@ -457,11 +466,11 @@ let output_zip = tmp.path().join("output.zip"); let file1 = tmp.path().join("file1"); - fs::write(&file1, b"1").expect("write file1"); + fs::write(&file1, b"1").expect("file1 should be writable"); let file2 = tmp.path().join("file2"); - fs::write(&file2, b"2").expect("write file2"); + fs::write(&file2, b"2").expect("file2 should be writable"); let file3 = tmp.path().join("file3"); - fs::write(&file3, b"3").expect("write file3"); + fs::write(&file3, b"3").expect("file3 should be writable"); fs::write( &manifest_path, @@ -475,10 +484,10 @@ ] .join("\n"), ) - .expect("write manifest"); + .expect("manifest should be writable"); let opts = default_options(&manifest_path, &output_zip); - create_zip(&opts).expect("create_zip"); + create_zip(&opts).expect("create_zip should succeed"); let names: Vec<String> = read_zip_entries(&output_zip) .into_iter() @@ -506,7 +515,7 @@ let file1 = tmp.path().join("repeated.txt"); let content = "hello world! ".repeat(500); - fs::write(&file1, content.as_bytes()).expect("write file1"); + fs::write(&file1, content.as_bytes()).expect("file1 should be writable"); fs::write( &manifest_path, @@ -515,11 +524,11 @@ file1.display() ), ) - .expect("write manifest_path"); + .expect("manifest_path should be writable"); let mut opts = default_options(&manifest_path, &output_zip); opts.compress_level = 6; - create_zip(&opts).expect("create_zip"); + create_zip(&opts).expect("create_zip should succeed"); let entries = read_zip_entries(&output_zip); let by_name: HashMap<String, ParsedZipEntry> = @@ -571,7 +580,7 @@ "--runfiles-dir=custom_rf", "--target-platform-pathsep=\\", ]) - .expect("all flags parse"), + .expect("all flags should parse"), ); assert_eq!(opts.manifest_path, PathBuf::from("manifest.txt")); @@ -591,28 +600,34 @@ "m.txt", "o.zip", ]) - .expect("space-separated flags parse"), + .expect("space-separated flag values should parse"), ); assert_eq!(opts2.compress_level, 0); assert!(!opts2.legacy_external_runfiles); - assert_eq!(parse_args(["--help"]).expect("--help parses"), Cli::Help); - assert_eq!(parse_args(["a", "b", "-h"]).expect("-h parses"), Cli::Help); + assert_eq!( + parse_args(["--help"]).expect("--help should parse"), + Cli::Help + ); + assert_eq!( + parse_args(["a", "b", "-h"]).expect("-h should parse"), + Cli::Help + ); let err = parse_args(["only_one_arg"]) - .expect_err("one positional must fail") + .expect_err("a single positional should be rejected") .to_string(); assert!(err.contains("expected 2 positional"), "{err}"); let err = parse_args(["a", "b", "--legacy-external-runfiles=2"]) - .expect_err("bad bool flag must fail") + .expect_err("a non-0/1 bool flag should be rejected") .to_string(); assert!(err.contains("expected 0 or 1"), "{err}"); let err = parse_args(["a", "b", "--compression=10"]) - .expect_err("compression 10 must fail") + .expect_err("--compression=10 should be rejected") .to_string(); assert!(err.contains("expected 0-9"), "{err}"); let err = parse_args(["a", "b", "--unknown-flag"]) - .expect_err("unknown flag must fail") + .expect_err("an unknown flag should be rejected") .to_string(); assert!(err.contains("unknown-flag"), "{err}"); } @@ -625,7 +640,7 @@ let output_zip = tmp.path().join("output.zip"); let target_file = tmp.path().join("target_file.txt"); - fs::write(&target_file, b"target content").expect("write target_file"); + fs::write(&target_file, b"target content").expect("target_file should be writable"); fs::write( &manifest_path, @@ -637,52 +652,52 @@ ] .join("\n"), ) - .expect("write manifest"); + .expect("manifest should be writable"); let opts = default_options(&manifest_path, &output_zip); - create_zip(&opts).expect("create_zip"); + create_zip(&opts).expect("create_zip should succeed"); let extract_dir = tmp.path().join("extract"); - fs::create_dir_all(&extract_dir).expect("create extract_dir"); + fs::create_dir_all(&extract_dir).expect("extract_dir should be creatable"); for entry in read_zip_entries(&output_zip) { let extract_path = extract_dir.join(&entry.name); if let Some(parent) = extract_path.parent() { - fs::create_dir_all(parent).expect("create parent dir"); + fs::create_dir_all(parent).expect("parent dir should be creatable"); } if entry.is_symlink { std::os::unix::fs::symlink(entry.text(), &extract_path) - .expect("create symlink extract_path"); + .expect("extract_path symlink should be creatable"); } else { - fs::write(&extract_path, &entry.data).expect("write extract_path"); + fs::write(&extract_path, &entry.data).expect("extract_path should be writable"); } } let link_path = extract_dir.join("runfiles/my_ws/path/to/link"); assert!(fs::symlink_metadata(&link_path) - .expect("lstat link_path") + .expect("link_path should exist") .file_type() .is_symlink()); assert_eq!( - fs::read_link(&link_path).expect("readlink link_path"), + fs::read_link(&link_path).expect("link_path should be a symlink"), PathBuf::from("../../target/path") ); assert_eq!( - fs::read_to_string(&link_path).expect("read link_path"), + fs::read_to_string(&link_path).expect("link_path should be readable"), "target content" ); let link2_path = extract_dir.join("runfiles/my_ws/same_dir_link"); assert!(fs::symlink_metadata(&link2_path) - .expect("lstat link2_path") + .expect("link2_path should exist") .file_type() .is_symlink()); assert_eq!( - fs::read_link(&link2_path).expect("readlink link2_path"), + fs::read_link(&link2_path).expect("link2_path should be a symlink"), PathBuf::from("same_dir_target") ); assert_eq!( - fs::read_to_string(&link2_path).expect("read link2_path"), + fs::read_to_string(&link2_path).expect("link2_path should be readable"), "target content" ); } @@ -693,12 +708,12 @@ let manifest_path = tmp.path().join("manifest.txt"); let output_zip = tmp.path().join("output.zip"); - fs::write(&manifest_path, "unknown_type|foo|bar\n").expect("write manifest_path"); + fs::write(&manifest_path, "unknown_type|foo|bar\n").expect("manifest_path should be writable"); let opts = default_options(&manifest_path, &output_zip); let err = format!( "{:#}", - create_zip(&opts).expect_err("malformed manifest must fail") + create_zip(&opts).expect_err("a malformed manifest should be rejected") ); assert!(err.contains("manifest line 1: unknown entry type"), "{err}"); } @@ -710,17 +725,17 @@ let output_zip = tmp.path().join("output.zip"); let file1_path = tmp.path().join("file1.txt"); - fs::write(&file1_path, b"content1").expect("write file1_path"); + fs::write(&file1_path, b"content1").expect("file1_path should be writable"); fs::write( &manifest_path, format!("rf-file|5|foo|{}\n", file1_path.display()), ) - .expect("write manifest_path"); + .expect("manifest_path should be writable"); let opts = default_options(&manifest_path, &output_zip); let err = format!( "{:#}", - create_zip(&opts).expect_err("bad is_symlink must fail") + create_zip(&opts).expect_err("an invalid is_symlink value should be rejected") ); assert!( err.contains("manifest line 1: invalid is_symlink value"), @@ -733,13 +748,13 @@ let tmp = TempDir::new("compress_level_range"); let manifest_path = tmp.path().join("manifest.txt"); let output_zip = tmp.path().join("output.zip"); - fs::write(&manifest_path, "rf-empty|empty\n").expect("write manifest_path"); + fs::write(&manifest_path, "rf-empty|empty\n").expect("manifest_path should be writable"); let mut opts = default_options(&manifest_path, &output_zip); opts.compress_level = 10; let err = format!( "{:#}", - create_zip(&opts).expect_err("compress_level 10 must fail") + create_zip(&opts).expect_err("compress_level 10 should be rejected") ); assert!(err.contains("expected 0-9"), "{err}"); assert!(!output_zip.exists()); @@ -759,7 +774,7 @@ id )); let _ = fs::remove_dir_all(&path); - fs::create_dir_all(&path).expect("create temp dir"); + fs::create_dir_all(&path).expect("temp dir should be creatable"); Self { path } } @@ -789,6 +804,6 @@ impl ParsedZipEntry { fn text(&self) -> &str { - std::str::from_utf8(&self.data).expect("entry data is UTF-8") + std::str::from_utf8(&self.data).expect("entry data should be UTF-8") } }