diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 57dc5ed..8ec8eb8 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -1,28 +1,27 @@ name: Rust on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] + push: + branches: ["main"] + pull_request: + branches: ["main"] env: - CARGO_TERM_COLOR: always + CARGO_TERM_COLOR: always jobs: - build: + build: + runs-on: ubuntu-latest - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - name: Build - run: cargo build --verbose - - name: Run tests - run: cargo test --verbose - - name: Coverage - run: cargo +stable install cargo-llvm-cov --locked && cargo llvm-cov --html - - name: Upload coverage artifact - uses: actions/upload-artifact@v4.3.0 - with: - path: "target/llvm-cov/html/index.html" + steps: + - uses: actions/checkout@v3 + - name: Build + run: cargo build --verbose + - name: Run tests + run: cargo test --verbose + - name: Coverage + run: cargo +stable install cargo-llvm-cov --locked && cargo llvm-cov --html + - name: Upload coverage artifact + uses: actions/upload-artifact@v4.3.0 + with: + path: "target/llvm-cov/html" diff --git a/tests/tests_print_output.rs b/tests/tests_print_output.rs index c4f3a41..950ad8d 100644 --- a/tests/tests_print_output.rs +++ b/tests/tests_print_output.rs @@ -3,8 +3,17 @@ mod tests { use std::path::PathBuf; #[test] - fn test_parse_args() { + fn test_print_output() { let args = vec![ + notox::OptionnalFields { + dry_run: false, + verbose: true, + json: notox::JsonFields { + json: false, + json_pretty: false, + json_error: false, + }, + }, notox::OptionnalFields { dry_run: true, verbose: true, @@ -57,4 +66,164 @@ mod tests { notox::print_output(options, final_res).unwrap(); } } + + fn setup(name1: &PathBuf, name2: &PathBuf) { + // create + let to_correct = PathBuf::from(&name1); + std::fs::File::create(&to_correct).unwrap(); + + // set read only + let read_only = PathBuf::from(&name2); + std::fs::File::create(&read_only).unwrap(); + let mut perms = std::fs::metadata(&read_only).unwrap().permissions(); + perms.set_readonly(true); + std::fs::set_permissions(&read_only, perms).unwrap(); + } + + fn cleanup(name1: &PathBuf, read_only: &PathBuf) { + // remove + std::fs::remove_file(&name1).unwrap(); + + // remove read only + let mut perms = std::fs::metadata(&read_only).unwrap().permissions(); + perms.set_readonly(false); + std::fs::set_permissions(&read_only, perms).unwrap(); + // remove + std::fs::remove_file(&read_only).unwrap(); + } + + #[test] + fn test_print_output_verbose_dry() { + let options = notox::OptionnalFields { + dry_run: true, + verbose: true, + json: notox::JsonFields { + json: false, + json_pretty: false, + json_error: false, + }, + }; + let to_correct = PathBuf::from("tes t verbose dry.txt"); + let read_only = PathBuf::from("test_verbose_dry.txt"); + setup(&to_correct, &read_only); + + let paths_to_check = vec![ + PathBuf::from("README.md"), + to_correct.clone(), + read_only.clone(), + ]; + let final_res = notox::notox(&options, paths_to_check); + notox::print_output(&options, final_res).unwrap(); + + // cleanup + cleanup(&to_correct, &read_only); + } + + #[test] + fn test_print_output_verbose_real() { + let options = notox::OptionnalFields { + dry_run: false, + verbose: true, + json: notox::JsonFields { + json: false, + json_pretty: false, + json_error: false, + }, + }; + let to_correct = PathBuf::from("tes t verbose.txt"); + let read_only = PathBuf::from("test_verbose.txt"); + setup(&to_correct, &read_only); + + let paths_to_check = vec![ + PathBuf::from("README.md"), + to_correct.clone(), + read_only.clone(), + ]; + let final_res = notox::notox(&options, paths_to_check); + notox::print_output(&options, final_res).unwrap(); + + // cleanup + cleanup(&PathBuf::from("tes_t_verbose.txt"), &read_only); + } + + #[test] + fn test_print_output_json_real() { + let options = notox::OptionnalFields { + dry_run: false, + verbose: false, + json: notox::JsonFields { + json: true, + json_pretty: false, + json_error: false, + }, + }; + let to_correct = PathBuf::from("tes t json.txt"); + let read_only = PathBuf::from("test_json.txt"); + setup(&to_correct, &read_only); + + let paths_to_check = vec![ + PathBuf::from("README.md"), + to_correct.clone(), + read_only.clone(), + ]; + let final_res = notox::notox(&options, paths_to_check); + notox::print_output(&options, final_res).unwrap(); + + // cleanup + cleanup(&PathBuf::from("tes_t_json.txt"), &read_only); + } + + #[test] + fn test_print_output_json_error_real() { + let options = notox::OptionnalFields { + dry_run: false, + verbose: false, + json: notox::JsonFields { + json: true, + json_pretty: false, + json_error: true, + }, + }; + let to_correct = PathBuf::from("tes t json error.txt"); + let read_only = PathBuf::from("test_json_error.txt"); + setup(&to_correct, &read_only); + + let paths_to_check = vec![ + PathBuf::from("README.md"), + to_correct.clone(), + read_only.clone(), + ]; + let final_res = notox::notox(&options, paths_to_check); + notox::print_output(&options, final_res).unwrap(); + + // cleanup + cleanup(&PathBuf::from("tes_t_json_error.txt"), &read_only) + } + + #[test] + fn test_print_output_json_error_dry() { + let options = notox::OptionnalFields { + dry_run: true, + verbose: false, + json: notox::JsonFields { + json: true, + json_pretty: false, + json_error: true, + }, + }; + let to_correct = PathBuf::from("tes t json error dry.txt"); + let read_only = PathBuf::from("test_json_error_dry.txt"); + setup(&to_correct, &read_only); + + let paths_to_check = vec![ + PathBuf::from("README.md"), + to_correct.clone(), + read_only.clone(), + ]; + let final_res = notox::notox(&options, paths_to_check); + notox::print_output(&options, final_res).unwrap(); + + // cleanup + cleanup(&to_correct, &read_only) + } }