Skip to content

Commit

Permalink
add more test
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-Just-Nans committed Jan 24, 2024
1 parent 88a99b2 commit 12ad9d2
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 21 deletions.
39 changes: 19 additions & 20 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
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/[email protected]
with:
path: "target/llvm-cov/html"
171 changes: 170 additions & 1 deletion tests/tests_print_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 12ad9d2

Please sign in to comment.