mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 12:05:46 +00:00
misc comments and wording changes
This commit is contained in:
parent
253cedcf60
commit
2b9023e180
@ -10,10 +10,11 @@ use rand::{rngs::SmallRng, RngCore, SeedableRng};
|
|||||||
use tempfile::tempdir;
|
use tempfile::tempdir;
|
||||||
use test_strategy::{proptest, Arbitrary};
|
use test_strategy::{proptest, Arbitrary};
|
||||||
|
|
||||||
use crate::utils::{assert_same_directory, create_file_random};
|
use crate::utils::{assert_same_directory, write_random_content};
|
||||||
|
|
||||||
|
// tar and zip extensions
|
||||||
#[derive(Arbitrary, Debug, Display)]
|
#[derive(Arbitrary, Debug, Display)]
|
||||||
#[display(style = "snake_case")]
|
#[display(style = "lowercase")]
|
||||||
enum DirectoryExtension {
|
enum DirectoryExtension {
|
||||||
Tar,
|
Tar,
|
||||||
Tbz,
|
Tbz,
|
||||||
@ -25,8 +26,9 @@ enum DirectoryExtension {
|
|||||||
Zip,
|
Zip,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// extensions of single file compression formats
|
||||||
#[derive(Arbitrary, Debug, Display)]
|
#[derive(Arbitrary, Debug, Display)]
|
||||||
#[display(style = "snake_case")]
|
#[display(style = "lowercase")]
|
||||||
enum FileExtension {
|
enum FileExtension {
|
||||||
Bz,
|
Bz,
|
||||||
Bz2,
|
Bz2,
|
||||||
@ -44,10 +46,12 @@ enum Extension {
|
|||||||
File(FileExtension),
|
File(FileExtension),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// converts a list of extension structs to string
|
||||||
fn merge_extensions(ext: impl ToString, exts: Vec<FileExtension>) -> String {
|
fn merge_extensions(ext: impl ToString, exts: Vec<FileExtension>) -> String {
|
||||||
once(ext.to_string()).chain(exts.into_iter().map(|x| x.to_string())).collect::<Vec<_>>().join(".")
|
once(ext.to_string()).chain(exts.into_iter().map(|x| x.to_string())).collect::<Vec<_>>().join(".")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create random nested directories and files under the specified directory
|
||||||
fn create_random_files(dir: impl Into<PathBuf>, depth: u8, rng: &mut SmallRng) {
|
fn create_random_files(dir: impl Into<PathBuf>, depth: u8, rng: &mut SmallRng) {
|
||||||
if depth == 0 {
|
if depth == 0 {
|
||||||
return;
|
return;
|
||||||
@ -55,15 +59,18 @@ fn create_random_files(dir: impl Into<PathBuf>, depth: u8, rng: &mut SmallRng) {
|
|||||||
|
|
||||||
let dir = &dir.into();
|
let dir = &dir.into();
|
||||||
|
|
||||||
|
// create 0 to 7 random files
|
||||||
for _ in 0..rng.next_u32() % 8 {
|
for _ in 0..rng.next_u32() % 8 {
|
||||||
create_file_random(&mut tempfile::Builder::new().tempfile_in(dir).unwrap().keep().unwrap().0, rng);
|
write_random_content(&mut tempfile::Builder::new().tempfile_in(dir).unwrap().keep().unwrap().0, rng);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create more random files in 0 to 3 new directories
|
||||||
for _ in 0..rng.next_u32() % 4 {
|
for _ in 0..rng.next_u32() % 4 {
|
||||||
create_random_files(&tempfile::tempdir_in(dir).unwrap().into_path(), depth - 1, rng);
|
create_random_files(&tempfile::tempdir_in(dir).unwrap().into_path(), depth - 1, rng);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// compress and decompress a single empty file
|
||||||
#[proptest(cases = 512)]
|
#[proptest(cases = 512)]
|
||||||
fn single_empty_file(ext: Extension, #[any(size_range(0..8).lift())] exts: Vec<FileExtension>) {
|
fn single_empty_file(ext: Extension, #[any(size_range(0..8).lift())] exts: Vec<FileExtension>) {
|
||||||
let dir = tempdir().unwrap();
|
let dir = tempdir().unwrap();
|
||||||
@ -73,12 +80,13 @@ fn single_empty_file(ext: Extension, #[any(size_range(0..8).lift())] exts: Vec<F
|
|||||||
let before_file = &before.join("file");
|
let before_file = &before.join("file");
|
||||||
let archive = &dir.join(format!("file.{}", merge_extensions(ext, exts)));
|
let archive = &dir.join(format!("file.{}", merge_extensions(ext, exts)));
|
||||||
let after = &dir.join("after");
|
let after = &dir.join("after");
|
||||||
create_file_random(&mut fs::File::create(before_file).unwrap(), &mut SmallRng::from_entropy());
|
write_random_content(&mut fs::File::create(before_file).unwrap(), &mut SmallRng::from_entropy());
|
||||||
ouch!("c", before_file, archive);
|
ouch!("c", before_file, archive);
|
||||||
ouch!("d", archive, "-d", after);
|
ouch!("d", archive, "-d", after);
|
||||||
assert_same_directory(before, after, false);
|
assert_same_directory(before, after, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// compress and decompress a single file
|
||||||
#[proptest(cases = 512)]
|
#[proptest(cases = 512)]
|
||||||
fn single_file(ext: Extension, #[any(size_range(0..8).lift())] exts: Vec<FileExtension>) {
|
fn single_file(ext: Extension, #[any(size_range(0..8).lift())] exts: Vec<FileExtension>) {
|
||||||
let dir = tempdir().unwrap();
|
let dir = tempdir().unwrap();
|
||||||
@ -94,6 +102,7 @@ fn single_file(ext: Extension, #[any(size_range(0..8).lift())] exts: Vec<FileExt
|
|||||||
assert_same_directory(before, after, false);
|
assert_same_directory(before, after, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// compress and decompress a directory with random content generated with create_random_files
|
||||||
#[proptest(cases = 512)]
|
#[proptest(cases = 512)]
|
||||||
fn multiple_files(
|
fn multiple_files(
|
||||||
ext: DirectoryExtension,
|
ext: DirectoryExtension,
|
||||||
|
@ -4,7 +4,7 @@ mod utils;
|
|||||||
use rand::{rngs::SmallRng, SeedableRng};
|
use rand::{rngs::SmallRng, SeedableRng};
|
||||||
use tempfile::NamedTempFile;
|
use tempfile::NamedTempFile;
|
||||||
|
|
||||||
use crate::utils::create_file_random;
|
use crate::utils::write_random_content;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
/// Makes sure that the files ouch produces are what they claim to be, checking their
|
/// Makes sure that the files ouch produces are what they claim to be, checking their
|
||||||
@ -14,7 +14,7 @@ fn sanity_check_through_mime() {
|
|||||||
let temp_dir_path = temp_dir.path();
|
let temp_dir_path = temp_dir.path();
|
||||||
|
|
||||||
let test_file = &mut NamedTempFile::new_in(temp_dir_path).expect("to be able to build a temporary file");
|
let test_file = &mut NamedTempFile::new_in(temp_dir_path).expect("to be able to build a temporary file");
|
||||||
create_file_random(test_file, &mut SmallRng::from_entropy());
|
write_random_content(test_file, &mut SmallRng::from_entropy());
|
||||||
|
|
||||||
let formats = [
|
let formats = [
|
||||||
"tar", "zip", "tar.gz", "tgz", "tbz", "tbz2", "txz", "tlz", "tlzma", "tzst", "tar.bz", "tar.bz2", "tar.lzma",
|
"tar", "zip", "tar.gz", "tgz", "tbz", "tbz2", "txz", "tlz", "tlzma", "tzst", "tar.bz", "tar.bz2", "tar.lzma",
|
||||||
|
@ -13,12 +13,15 @@ macro_rules! ouch {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_file_random(file: &mut impl Write, rng: &mut impl RngCore) {
|
// write random content to a file
|
||||||
|
pub fn write_random_content(file: &mut impl Write, rng: &mut impl RngCore) {
|
||||||
let data = &mut Vec::with_capacity((rng.next_u32() % 8192) as usize);
|
let data = &mut Vec::with_capacity((rng.next_u32() % 8192) as usize);
|
||||||
rng.fill_bytes(data);
|
rng.fill_bytes(data);
|
||||||
file.write_all(data).unwrap();
|
file.write_all(data).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check that two directories have the exact same content recursively
|
||||||
|
// checks equility of file types if preserve_permissions is true, ignored on non-unix
|
||||||
pub fn assert_same_directory(x: impl Into<PathBuf>, y: impl Into<PathBuf>, preserve_permissions: bool) {
|
pub fn assert_same_directory(x: impl Into<PathBuf>, y: impl Into<PathBuf>, preserve_permissions: bool) {
|
||||||
fn read_dir(dir: impl Into<PathBuf>) -> impl Iterator<Item = fs::DirEntry> {
|
fn read_dir(dir: impl Into<PathBuf>) -> impl Iterator<Item = fs::DirEntry> {
|
||||||
let mut dir: Vec<_> = fs::read_dir(dir).unwrap().map(|entry| entry.unwrap()).collect();
|
let mut dir: Vec<_> = fs::read_dir(dir).unwrap().map(|entry| entry.unwrap()).collect();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user