| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
//! Tests on the `uudoc` binary. |
| 7 |
//! |
| 8 |
//! To run the uudoc |
| 9 |
//! ``` |
| 10 |
//! cargo run --bin uudoc --features uudoc |
| 11 |
//! ``` |
| 12 |
//! |
| 13 |
//! To run the tests |
| 14 |
//! ``` |
| 15 |
//! cargo test --features uudoc |
| 16 |
//! ``` |
| 17 |
|
| 18 |
#![cfg(feature = "uudoc")] |
| 19 |
|
| 20 |
use std::env; |
| 21 |
pub const TESTS_BINARY: &str = env!("CARGO_BIN_EXE_uudoc"); |
| 22 |
|
| 23 |
// Set the environment variable for any tests |
| 24 |
|
| 25 |
// Use the ctor attribute to run this function before any tests |
| 26 |
#[ctor::ctor(unsafe)] |
| 27 |
fn init() { |
| 28 |
unsafe { |
| 29 |
env::set_var("UUTESTS_BINARY_PATH", TESTS_BINARY); |
| 30 |
} |
| 31 |
// Print for debugging |
| 32 |
eprintln!("Setting UUTESTS_BINARY_PATH={TESTS_BINARY}"); |
| 33 |
} |
| 34 |
|
| 35 |
/// Run the `uudoc` command and return the output as a vector of strings. |
| 36 |
/// # Errors |
| 37 |
/// If the command fails to execute or if the output cannot be converted to UTF-8, an `io::Error` is returned. |
| 38 |
fn run_write_doc() -> Vec<String> { |
| 39 |
use std::process::Command; |
| 40 |
use uutests::util::TestScenario; |
| 41 |
|
| 42 |
let scenario = TestScenario::new(""); |
| 43 |
let output = Command::new(&scenario.bin_path).output().unwrap(); |
| 44 |
assert!( |
| 45 |
output.status.success(), |
| 46 |
"uudoc command failed: {}", |
| 47 |
String::from_utf8_lossy(&output.stderr) |
| 48 |
); |
| 49 |
|
| 50 |
String::from_utf8(output.stdout) |
| 51 |
.unwrap() |
| 52 |
.lines() |
| 53 |
.map(String::from) |
| 54 |
.filter(|line| line.starts_with("Wrote")) |
| 55 |
.collect::<Vec<String>>() |
| 56 |
} |
| 57 |
|
| 58 |
fn get_doc_file_from_output(output: &str) -> (String, String) { |
| 59 |
let correct_path_test = output |
| 60 |
.strip_prefix("Wrote to '") |
| 61 |
.unwrap() |
| 62 |
.strip_suffix("'") |
| 63 |
.unwrap() |
| 64 |
.to_string(); |
| 65 |
let content = std::fs::read_to_string(&correct_path_test); |
| 66 |
let content = match content { |
| 67 |
Ok(content) => content, |
| 68 |
Err(e) => { |
| 69 |
panic!( |
| 70 |
"Failed to read file {correct_path_test}: {e} from {:?}", |
| 71 |
env::current_dir(), |
| 72 |
); |
| 73 |
} |
| 74 |
}; |
| 75 |
(correct_path_test, content) |
| 76 |
} |
| 77 |
|
| 78 |
#[test] |
| 79 |
fn uudoc_check_test() { |
| 80 |
let pages = run_write_doc(); |
| 81 |
// assert wrote to the correct file |
| 82 |
let path_test = pages.iter().find(|line| line.contains("test.md")).unwrap(); |
| 83 |
let (correct_path, content) = get_doc_file_from_output(path_test); |
| 84 |
|
| 85 |
// open the file |
| 86 |
assert!( |
| 87 |
content.contains( |
| 88 |
"``` |
| 89 |
test EXPRESSION |
| 90 |
test |
| 91 |
[ EXPRESSION ] |
| 92 |
[ ] |
| 93 |
[ OPTION |
| 94 |
``` |
| 95 |
", |
| 96 |
), |
| 97 |
"{correct_path} does not contains the required text" |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
#[test] |
| 102 |
fn uudoc_check_sums() { |
| 103 |
let pages = run_write_doc(); |
| 104 |
let sums = [ |
| 105 |
"md5sum", |
| 106 |
"sha1sum", |
| 107 |
"sha224sum", |
| 108 |
"sha256sum", |
| 109 |
"sha384sum", |
| 110 |
"sha512sum", |
| 111 |
"b2sum", |
| 112 |
]; |
| 113 |
for one_sum in sums { |
| 114 |
let output_path = pages |
| 115 |
.iter() |
| 116 |
.find(|one_line| one_line.contains(one_sum)) |
| 117 |
.unwrap(); |
| 118 |
let (correct_path, content) = get_doc_file_from_output(output_path); |
| 119 |
let formatted = format!("```\n{one_sum} [OPTIONS] [FILE]...\n```"); |
| 120 |
assert!( |
| 121 |
content.contains(&formatted), |
| 122 |
"Content of {correct_path} does not contain the expected format: {formatted}", |
| 123 |
); |
| 124 |
} |
| 125 |
} |