Skip to content

Commit 9f49e35

Browse files
chore(bottlecap): cross-platform test and code cleanups (#1211)
## Overview Portable test and code cleanups in bottlecap. No behavior change on Linux. - `.gitattributes`: force LF for `*.sh` / `*.bash`. Shell scripts now check out with LF on every platform, so a CRLF-prone checkout (e.g. Windows) still executes cleanly inside Linux Docker containers. - `bottlecap/src/tags/lambda/tags.rs`, the three `tags::lambda::tags::tests::test_resolve_*` tests: - Switch from a hard-coded `/tmp/...` path to `std::env::temp_dir()`. Removes a bootstrap race between tests that were implicitly sharing a filesystem side effect through the same path. - Render paths with `to_string_lossy()` so a non-UTF-8 temp dir does not panic the test. - `test_resolve_runtime` now cleans up the file it created. - `bottlecap/src/metrics/enhanced/statfs.rs`: - `statfs_info` is `fn` on both targets. It's an internal helper; `pub` was only present on the non-Windows branch. - The existing `#[cfg(target_os = "windows")]` stub now uses `io::Error::other(..)` (per `clippy::io_other_error`) and prefixes its unused `path` with `_`. > Origin: these commits were split out of #1193 after review feedback, so the portable cleanups can land independently of the broader Windows-build discussion. ## Testing - `cargo check` on Linux: clean. - `cargo test --lib tags::lambda::tags::`: 8 passed, 0 failed. > *"Turns out 'make the tests not race on Windows' is also 'make the tests not race on Linux'."*, Claude 🤖
1 parent e8bf40c commit 9f49e35

3 files changed

Lines changed: 20 additions & 18 deletions

File tree

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
/LICENSE-3rdparty.csv linguist-generated=true
2+
3+
# Set line endings to LF, even on Windows. Otherwise, execution within Docker fails.
4+
# See https://help.github.com/articles/dealing-with-line-endings/
5+
*.sh text eol=lf
6+
*.bash text eol=lf

bottlecap/src/metrics/enhanced/statfs.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::path::Path;
99
#[allow(clippy::cast_lossless)]
1010
/// Returns the block size, total number of blocks, and number of blocks available for the specified directory path.
1111
///
12-
pub fn statfs_info(path: &str) -> Result<(f64, f64, f64), io::Error> {
12+
fn statfs_info(path: &str) -> Result<(f64, f64, f64), io::Error> {
1313
let stat = statfs(Path::new(path)).map_err(io::Error::other)?;
1414
Ok((
1515
stat.block_size() as f64,
@@ -19,11 +19,8 @@ pub fn statfs_info(path: &str) -> Result<(f64, f64, f64), io::Error> {
1919
}
2020

2121
#[cfg(target_os = "windows")]
22-
fn statfs_info(path: &str) -> Result<(f64, f64, f64), io::Error> {
23-
Err(io::Error::new(
24-
io::ErrorKind::Other,
25-
"Cannot get tmp data on Windows",
26-
))
22+
fn statfs_info(_path: &str) -> Result<(f64, f64, f64), io::Error> {
23+
Err(io::Error::other("Cannot get tmp data on Windows"))
2724
}
2825

2926
pub fn get_tmp_max() -> Result<f64, io::Error> {

bottlecap/src/tags/lambda/tags.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ mod tests {
294294
use std::collections::HashMap;
295295
use std::fs::File;
296296
use std::io::Write;
297-
use std::path::Path;
298297

299298
#[test]
300299
fn test_new_from_config() {
@@ -368,8 +367,8 @@ mod tests {
368367

369368
#[test]
370369
fn test_resolve_runtime() {
371-
let proc_id_folder = Path::new("/tmp/test-bottlecap/proc_root/123");
372-
fs::create_dir_all(proc_id_folder).unwrap();
370+
let proc_id_folder = std::env::temp_dir().join("test-bottlecap/proc_root/123");
371+
fs::create_dir_all(&proc_id_folder).unwrap();
373372
let path = proc_id_folder.join("environ");
374373
let content = "\0NAME =\"AmazonLinux\"\0V=\"2\0AWS_EXECUTION_ENV=\"AWS_Lambda_java123\"\0somethingelse=\"abd\0\"";
375374

@@ -378,32 +377,33 @@ mod tests {
378377

379378
let runtime =
380379
resolve_runtime_from_proc(proc_id_folder.parent().unwrap().to_str().unwrap(), "");
381-
fs::remove_file(path).unwrap();
380+
fs::remove_file(&path).unwrap();
381+
fs::remove_dir_all(std::env::temp_dir().join("test-bottlecap")).unwrap();
382382
assert_eq!(runtime, "java123");
383383
}
384384

385385
#[test]
386386
fn test_resolve_provided_al2() {
387-
let path = "/tmp/test-os-release1";
387+
let path = std::env::temp_dir().join("test-os-release1");
388388
let content = "NAME =\"Amazon Linux\"\nVERSION=\"2\nPRETTY_NAME=\"Amazon Linux 2\"";
389-
let mut file = File::create(path).unwrap();
389+
let mut file = File::create(&path).unwrap();
390390
file.write_all(content.as_bytes()).unwrap();
391391

392-
let runtime = resolve_runtime_from_proc("", path);
393-
fs::remove_file(path).unwrap();
392+
let runtime = resolve_runtime_from_proc("", &path.to_string_lossy());
393+
fs::remove_file(&path).unwrap();
394394
assert_eq!(runtime, "provided.al2");
395395
}
396396

397397
#[test]
398398
fn test_resolve_provided_al2023() {
399-
let path = "/tmp/test-os-release2";
399+
let path = std::env::temp_dir().join("test-os-release2");
400400
let content =
401401
"NAME=\"Amazon Linux\"\nVERSION=\"2\nPRETTY_NAME=\"Amazon Linux 2023.4.20240429\"";
402-
let mut file = File::create(path).unwrap();
402+
let mut file = File::create(&path).unwrap();
403403
file.write_all(content.as_bytes()).unwrap();
404404

405-
let runtime = resolve_runtime_from_proc("", path);
406-
fs::remove_file(path).unwrap();
405+
let runtime = resolve_runtime_from_proc("", &path.to_string_lossy());
406+
fs::remove_file(&path).unwrap();
407407
assert_eq!(runtime, "provided.al2023");
408408
}
409409

0 commit comments

Comments
 (0)