Skip to content

Commit e4e902c

Browse files
committed
♻️ Extract create_test_archive and get_archive_entry_names into shared test utils
Move duplicated test helpers from stdio/archive_inclusion.rs into utils/archive.rs so they can be reused across test modules.
1 parent e4a381d commit e4e902c

2 files changed

Lines changed: 39 additions & 33 deletions

File tree

cli/tests/cli/stdio/archive_inclusion.rs

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,11 @@
1-
use crate::utils::{archive::for_each_entry, setup};
1+
use crate::utils::{
2+
archive::{create_test_archive, get_archive_entry_names},
3+
setup,
4+
};
25
use assert_cmd::cargo::cargo_bin_cmd;
3-
use pna::{Archive, EntryBuilder, WriteOptions};
46
use std::collections::HashSet;
57
use std::fs;
6-
use std::io::Write;
7-
use std::path::{Path, PathBuf};
8-
9-
fn create_test_archive(path: &Path, entries: &[(&str, &str)]) {
10-
if let Some(parent) = path.parent() {
11-
fs::create_dir_all(parent).unwrap();
12-
}
13-
let file = fs::File::create(path).unwrap();
14-
let mut writer = Archive::write_header(file).unwrap();
15-
for (name, contents) in entries {
16-
writer
17-
.add_entry({
18-
let mut builder =
19-
EntryBuilder::new_file((*name).into(), WriteOptions::builder().build())
20-
.unwrap();
21-
builder.write_all(contents.as_bytes()).unwrap();
22-
builder.build().unwrap()
23-
})
24-
.unwrap();
25-
}
26-
writer.finalize().unwrap();
27-
}
28-
29-
fn get_archive_entry_names(path: &Path) -> Vec<String> {
30-
let mut names = Vec::new();
31-
for_each_entry(path, |entry| {
32-
names.push(entry.header().path().to_string());
33-
})
34-
.unwrap();
35-
names
36-
}
8+
use std::path::PathBuf;
379

3810
/// Test basic @archive inclusion: create a new archive including entries from an existing archive.
3911
#[test]

cli/tests/cli/utils/archive.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,37 @@ pub fn read_symlink_target(entry: &pna::NormalEntry) -> String {
152152
.unwrap();
153153
String::from_utf8(target).unwrap()
154154
}
155+
156+
/// Creates a simple archive with named text entries.
157+
pub fn create_test_archive(path: impl AsRef<Path>, entries: &[(&str, &str)]) {
158+
let path = path.as_ref();
159+
if let Some(parent) = path.parent() {
160+
std::fs::create_dir_all(parent).unwrap();
161+
}
162+
let file = File::create(path).unwrap();
163+
let mut writer = pna::Archive::write_header(file).unwrap();
164+
for (name, contents) in entries {
165+
writer
166+
.add_entry({
167+
let mut builder = pna::EntryBuilder::new_file(
168+
(*name).into(),
169+
pna::WriteOptions::builder().build(),
170+
)
171+
.unwrap();
172+
builder.write_all(contents.as_bytes()).unwrap();
173+
builder.build().unwrap()
174+
})
175+
.unwrap();
176+
}
177+
writer.finalize().unwrap();
178+
}
179+
180+
/// Collects all entry names from an archive.
181+
pub fn get_archive_entry_names(path: impl AsRef<Path>) -> Vec<String> {
182+
let mut names = Vec::new();
183+
for_each_entry(path, |entry| {
184+
names.push(entry.header().path().to_string());
185+
})
186+
.unwrap();
187+
names
188+
}

0 commit comments

Comments
 (0)