Skip to content

Commit

Permalink
fix: add more test
Browse files Browse the repository at this point in the history
Its-Just-Nans committed Jan 25, 2024

Verified

This commit was signed with the committer’s verified signature.
1 parent 2f9df58 commit 17c32a4
Showing 2 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.sh
Original file line number Diff line number Diff line change
@@ -11,4 +11,6 @@ cp -r target/llvm-cov/html dist/coverage
sed 's|CHANGELOG.md|CHANGELOG.html|g' README.md >README.md.tmp
comrak --gfm -o dist/index.html --width 10 README.md.tmp
comrak --gfm -o dist/CHANGELOG.html --width 10 CHANGELOG.md

# cleanup
rm README.md.tmp
84 changes: 84 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#[cfg(test)]
mod tests {
use std::{os::unix::fs::PermissionsExt, path::PathBuf};

use notox::OptionnalFields;

#[test]
@@ -34,4 +36,86 @@ mod tests {
let res = notox::check_similar(vec![], &mut String::new(), false);
assert_eq!(res, false);
}

fn setup(dir: &String) {
let directory_path = PathBuf::from(&dir);
std::fs::create_dir(&directory_path).unwrap();
std::fs::File::create(directory_path.join("test 1.txt")).unwrap();
std::fs::File::create(directory_path.join("test_2.txt")).unwrap();
let mut second_level = dir.clone();
second_level.push_str(" second");
let second_level = &directory_path.join(&second_level);
std::fs::create_dir(&second_level).unwrap();
std::fs::File::create(second_level.join("test 3.txt")).unwrap();
std::fs::File::create(second_level.join("test_4.txt")).unwrap();
std::fs::File::create(second_level.join("test_5.txt")).unwrap();

// permissions file
let mut perms = std::fs::metadata(&second_level.join("test_5.txt"))
.unwrap()
.permissions();
perms.set_mode(0o000);
std::fs::set_permissions(&second_level.join("test_5.txt"), perms).unwrap();
// permissions firectory
let mut perms = std::fs::metadata(&second_level).unwrap().permissions();
perms.set_mode(0o000);
std::fs::set_permissions(&second_level, perms).unwrap();
}

fn cleanup(dir: &String) {
let directory_path = PathBuf::from(&dir);
std::fs::remove_file(directory_path.join("test_1.txt")).unwrap();
std::fs::remove_file(directory_path.join("test_2.txt")).unwrap();
let mut second_level = dir.clone();
second_level.push_str("_second");
let second_level = &directory_path.join(&second_level);
let mut perms = std::fs::metadata(&second_level).unwrap().permissions();
perms.set_mode(0o777);
std::fs::set_permissions(&second_level, perms).unwrap();
std::fs::remove_file(second_level.join("test 3.txt")).unwrap();
std::fs::remove_file(second_level.join("test_4.txt")).unwrap();

// test 5
let mut perms = std::fs::metadata(&second_level.join("test_5.txt"))
.unwrap()
.permissions();
perms.set_mode(0o777);
std::fs::set_permissions(&second_level.join("test_5.txt"), perms).unwrap();
std::fs::remove_file(second_level.join("test_5.txt")).unwrap();

// other
std::fs::remove_dir(&second_level).unwrap();
std::fs::remove_dir_all(&directory_path).unwrap();
}

#[test]
fn test_parse_args_clean_directory_recursive() {
let dir = "test_folder".to_owned();
setup(&dir);

let vec_args = vec![
"notox".to_owned(),
dir.clone(),
"-j".to_owned(),
"-d".to_owned(),
];
let res = notox::parse_args(vec_args);
let (options, vect) = res.ok().unwrap();
let res_path = notox::notox(&options, vect);

assert_eq!(
options,
OptionnalFields {
dry_run: false,
verbose: false,
json: notox::JsonFields {
json: true,
json_pretty: false,
json_error: false,
},
}
);
assert_eq!(res_path.len(), 5);
cleanup(&dir);
}
}

0 comments on commit 17c32a4

Please sign in to comment.