Skip to content

Commit 8a586c6

Browse files
test: Move a test that was in wrong folder and was using main() instead of #[test] as entry point (#540)
* test: Move a test that was in wrong folder and was using `main()` instead of `#[test]` as entry point * cargo fmt * Update tests/absolute_paths.rs Co-authored-by: amazon-q-developer[bot] <208079219+amazon-q-developer[bot]@users.noreply.github.com> Signed-off-by: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> * Update tests/absolute_paths.rs Co-authored-by: amazon-q-developer[bot] <208079219+amazon-q-developer[bot]@users.noreply.github.com> Signed-off-by: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> * Apply Gemini suggestions * Remove incorrect assertions * style: cargo fmt --all * Fix: test was failing when deflate was write-only * cargo fmt --all --------- Signed-off-by: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Co-authored-by: amazon-q-developer[bot] <208079219+amazon-q-developer[bot]@users.noreply.github.com>
1 parent cfbb476 commit 8a586c6

File tree

2 files changed

+78
-72
lines changed

2 files changed

+78
-72
lines changed

test_fix.rs

Lines changed: 0 additions & 72 deletions
This file was deleted.

tests/absolute_paths.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use std::io::Write;
2+
use zip::result::ZipResult;
3+
use zip::write::SimpleFileOptions;
4+
use zip::{cfg_if_expr, ZipArchive, ZipWriter};
5+
6+
#[test]
7+
fn test_absolute_paths() -> ZipResult<()> {
8+
// Create a ZIP file with absolute paths
9+
let buf = Vec::new();
10+
let mut writer = ZipWriter::new(std::io::Cursor::new(buf));
11+
let options = cfg_if_expr!(
12+
SimpleFileOptions:
13+
#[cfg(all(feature = "deflate-zopfli", not(feature = "deflate-flate2")))] => {
14+
SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored)
15+
},
16+
_ => SimpleFileOptions::default()
17+
);
18+
19+
// Create entries with absolute paths
20+
writer.add_directory("/_/", options)?;
21+
writer.start_file("/_/test.txt", options)?;
22+
writer.write_all(b"Hello, World!")?;
23+
writer.start_file("/_/subdir/nested.txt", options)?;
24+
writer.write_all(b"Nested file content")?;
25+
26+
let zip_data = writer.finish()?.into_inner();
27+
28+
// Try to read the ZIP file
29+
let mut archive = ZipArchive::new(std::io::Cursor::new(zip_data))?;
30+
31+
println!("ZIP file created with {} entries", archive.len());
32+
33+
// Test individual file access
34+
assert_eq!(archive.len(), 3); // directory + 2 files
35+
36+
for i in 0..archive.len() {
37+
let file = archive.by_index(i)?;
38+
39+
// Verify that enclosed_name properly handles the paths
40+
let enclosed_name = file
41+
.enclosed_name()
42+
.expect("enclosed_name should not be None for valid paths");
43+
assert!(
44+
!enclosed_name.is_absolute(),
45+
"enclosed_name for '{}' should be relative, but was: {:?}",
46+
file.name(),
47+
enclosed_name
48+
);
49+
}
50+
51+
// Try to extract the ZIP file
52+
let temp_dir = tempfile::TempDir::new()?;
53+
54+
archive.extract(temp_dir.path())?;
55+
56+
let base_path = temp_dir.path();
57+
assert!(base_path.join("_").is_dir());
58+
assert!(base_path.join("_/subdir").is_dir());
59+
60+
let test_file_path = base_path.join("_/test.txt");
61+
assert!(test_file_path.is_file());
62+
assert_eq!(std::fs::read_to_string(test_file_path)?, "Hello, World!");
63+
64+
let nested_file_path = base_path.join("_/subdir/nested.txt");
65+
assert!(nested_file_path.is_file());
66+
assert_eq!(
67+
std::fs::read_to_string(nested_file_path)?,
68+
"Nested file content"
69+
);
70+
71+
// Verify extraction results with assertions
72+
let extracted_files: Vec<_> = std::fs::read_dir(temp_dir.path())?.collect();
73+
assert!(
74+
!extracted_files.is_empty(),
75+
"Should have extracted at least one file"
76+
);
77+
Ok(())
78+
}

0 commit comments

Comments
 (0)