Skip to content

Commit

Permalink
test: add test for directory sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Dec 22, 2024
1 parent 9da7f02 commit e8f0c0f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,19 @@ pub fn get_graveyard(graveyard: Option<PathBuf>) -> PathBuf {
env::temp_dir().join(format!("graveyard-{}", user))
}
}

/// Testing module for exposing internal functions to unit tests.
/// This module is only used for testing purposes and should not be used in production code.
pub mod testing {
use super::*;

pub fn testable_should_we_bury_this(
target: &Path,
source: &PathBuf,
metadata: &Metadata,
mode: &impl util::TestingMode,
stream: &mut impl Write,
) -> Result<bool, Error> {
should_we_bury_this(target, source, metadata, mode, stream)
}
}
46 changes: 46 additions & 0 deletions tests/unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,49 @@ fn fail_move_dir() {
assert!(e.to_string().contains("Failed to remove dir"));
}
}

#[rstest]
fn test_directory_size_output() {
let tmpdir = tempdir().unwrap();
let path = PathBuf::from(tmpdir.path());
// Create a directory with some files
let test_dir = path.join("test_dir");
fs::create_dir(&test_dir).unwrap();

// Create a few files with known sizes
fs::write(test_dir.join("file1"), vec![0; 1024]).unwrap(); // 1 KiB
fs::write(test_dir.join("file2"), vec![0; 2048]).unwrap(); // 2 KiB

let mut output = Vec::new();
let mode = TestMode;
// Test the directory size calculation and output
let result = rip2::testing::testable_should_we_bury_this(
&PathBuf::from("test_dir"),
&test_dir,
&fs::metadata(&test_dir).unwrap(),
&mode,
&mut output,
);

assert!(result.is_ok());
let output_str = String::from_utf8(output).unwrap();

// Should actually show the files in the directory
assert!(output_str.contains("test_dir"));
assert!(output_str.contains("file1"));
assert!(output_str.contains("file2"));

let re = regex::Regex::new(r"directory, ([\d.]+ KiB)").unwrap();
let size = re.captures(&output_str).unwrap().get(1).unwrap().as_str();

// The total size should be at least 3 KiB (can be larger due to filesystem overhead)
assert!(size.contains("KiB"));
let numeric_size = size
.split_whitespace()
.next()
.unwrap()
.parse::<f64>()
.unwrap();
assert!(numeric_size >= 3.0);
assert!(numeric_size < 6.0);
}

0 comments on commit e8f0c0f

Please sign in to comment.