rpz

Response policy zone (RPZ) file generator.
git clone https://git.philomathiclife.com/repos/rpz
Log | Files | Refs | README

tests.rs (8746B)


      1 use crate::{ArgsErr, E, tests};
      2 use core::convert;
      3 use std::io::Write as _;
      4 use std::process::Stdio;
      5 use std::thread;
      6 #[expect(clippy::too_many_lines, reason = "a lot to test")]
      7 #[test]
      8 #[ignore = "requires I/O"]
      9 fn args() {
     10     tests::verify_files();
     11     assert!(
     12         tests::get_command()
     13             .stderr(Stdio::piped())
     14             .stdin(Stdio::null())
     15             .stdout(Stdio::null())
     16             .output()
     17             .is_ok_and(|output| {
     18                 !output.status.success()
     19                     && output.stderr
     20                         == format!("Error: {:?}\n", E::Args(ArgsErr::NoArgs)).into_bytes()
     21             })
     22     );
     23     assert!(
     24         tests::get_command()
     25             .arg("-f")
     26             .stderr(Stdio::piped())
     27             .stdin(Stdio::null())
     28             .stdout(Stdio::null())
     29             .output()
     30             .is_ok_and(|output| {
     31                 !output.status.success()
     32                     && output.stderr
     33                         == format!("Error: {:?}\n", E::Args(ArgsErr::ConfigPathNotPassed))
     34                             .into_bytes()
     35             })
     36     );
     37     assert!(
     38         tests::get_command()
     39             .arg("-q")
     40             .stderr(Stdio::piped())
     41             .stdin(Stdio::null())
     42             .stdout(Stdio::null())
     43             .output()
     44             .is_ok_and(|output| {
     45                 !output.status.success()
     46                     && output.stderr
     47                         == format!("Error: {:?}\n", E::Args(ArgsErr::ConfigPathNotPassed))
     48                             .into_bytes()
     49             })
     50     );
     51     assert!(
     52         tests::get_command()
     53             .arg("-v")
     54             .stderr(Stdio::piped())
     55             .stdin(Stdio::null())
     56             .stdout(Stdio::null())
     57             .output()
     58             .is_ok_and(|output| {
     59                 !output.status.success()
     60                     && output.stderr
     61                         == format!("Error: {:?}\n", E::Args(ArgsErr::ConfigPathNotPassed))
     62                             .into_bytes()
     63             })
     64     );
     65     assert!(
     66         tests::get_command()
     67             .arg("-fq")
     68             .stderr(Stdio::piped())
     69             .stdin(Stdio::null())
     70             .stdout(Stdio::null())
     71             .output()
     72             .is_ok_and(|output| {
     73                 !output.status.success()
     74                     && output.stderr
     75                         == format!("Error: {:?}\n", E::Args(ArgsErr::ConfigPathNotPassed))
     76                             .into_bytes()
     77             })
     78     );
     79     assert!(
     80         tests::get_command()
     81             .arg("-qf")
     82             .stderr(Stdio::piped())
     83             .stdin(Stdio::null())
     84             .stdout(Stdio::null())
     85             .output()
     86             .is_ok_and(|output| {
     87                 !output.status.success()
     88                     && output.stderr
     89                         == format!("Error: {:?}\n", E::Args(ArgsErr::ConfigPathNotPassed))
     90                             .into_bytes()
     91             })
     92     );
     93     assert!(
     94         tests::get_command()
     95             .arg("-fv")
     96             .stderr(Stdio::piped())
     97             .stdin(Stdio::null())
     98             .stdout(Stdio::null())
     99             .output()
    100             .is_ok_and(|output| {
    101                 !output.status.success()
    102                     && output.stderr
    103                         == format!("Error: {:?}\n", E::Args(ArgsErr::ConfigPathNotPassed))
    104                             .into_bytes()
    105             })
    106     );
    107     assert!(
    108         tests::get_command()
    109             .arg("-vf")
    110             .stderr(Stdio::piped())
    111             .stdin(Stdio::null())
    112             .stdout(Stdio::null())
    113             .output()
    114             .is_ok_and(|output| {
    115                 !output.status.success()
    116                     && output.stderr
    117                         == format!("Error: {:?}\n", E::Args(ArgsErr::ConfigPathNotPassed))
    118                             .into_bytes()
    119             })
    120     );
    121     assert!(
    122         tests::get_command()
    123             .args(["-h", "-V"])
    124             .stderr(Stdio::piped())
    125             .stdin(Stdio::null())
    126             .stdout(Stdio::null())
    127             .output()
    128             .is_ok_and(|output| {
    129                 !output.status.success()
    130                     && output.stderr
    131                         == format!("Error: {:?}\n", E::Args(ArgsErr::MoreThanOneOption))
    132                             .into_bytes()
    133             })
    134     );
    135     assert!(
    136         tests::get_command()
    137             .args(["-h", "-h"])
    138             .stderr(Stdio::piped())
    139             .stdin(Stdio::null())
    140             .stdout(Stdio::null())
    141             .output()
    142             .is_ok_and(|output| {
    143                 !output.status.success()
    144                     && output.stderr
    145                         == format!(
    146                             "Error: {:?}\n",
    147                             E::Args(ArgsErr::DuplicateOption("-h/--help"))
    148                         )
    149                         .into_bytes()
    150             })
    151     );
    152     assert!(
    153         tests::get_command()
    154             .args(["-f", "/home/zack/foo", "-V"])
    155             .stderr(Stdio::piped())
    156             .stdin(Stdio::null())
    157             .stdout(Stdio::null())
    158             .output()
    159             .is_ok_and(|output| {
    160                 !output.status.success()
    161                     && output.stderr
    162                         == format!("Error: {:?}\n", E::Args(ArgsErr::MoreThanOneOption))
    163                             .into_bytes()
    164             })
    165     );
    166     assert!(
    167         tests::get_command()
    168             .args(["-f", "home/zack/foo"])
    169             .stderr(Stdio::piped())
    170             .stdin(Stdio::null())
    171             .stdout(Stdio::null())
    172             .output()
    173             .is_ok_and(|output| {
    174                 !output.status.success()
    175                     && output.stderr
    176                         == format!("Error: {:?}\n", E::Args(ArgsErr::InvalidConfigPath))
    177                             .into_bytes()
    178             })
    179     );
    180     assert!(
    181         tests::get_command()
    182             .args(["-f", "/home/zack/foo/"])
    183             .stderr(Stdio::piped())
    184             .stdin(Stdio::null())
    185             .stdout(Stdio::null())
    186             .output()
    187             .is_ok_and(|output| {
    188                 !output.status.success()
    189                     && output.stderr
    190                         == format!("Error: {:?}\n", E::Args(ArgsErr::InvalidConfigPath))
    191                             .into_bytes()
    192             })
    193     );
    194     assert!(
    195         tests::get_command()
    196             .arg("-foo")
    197             .stderr(Stdio::piped())
    198             .stdin(Stdio::null())
    199             .stdout(Stdio::null())
    200             .output()
    201             .is_ok_and(|output| {
    202                 !output.status.success()
    203                     && output.stderr
    204                         == format!(
    205                             "Error: {:?}\n",
    206                             E::Args(ArgsErr::InvalidOption(String::from("-foo")))
    207                         )
    208                         .into_bytes()
    209             })
    210     );
    211     assert!(
    212         tests::get_command()
    213             .args(["-f", "-"])
    214             .stderr(Stdio::piped())
    215             .stdin(Stdio::null())
    216             .stdout(Stdio::null())
    217             .output()
    218             .is_ok_and(|output| !output.status.success()
    219                 && output.stderr.get(..23) == Some(b"Error: TOML parse error"))
    220     );
    221     assert!(
    222         tests::get_command()
    223             .args(["-f", "-"])
    224             .stderr(Stdio::piped())
    225             .stdin(Stdio::piped())
    226             .stdout(Stdio::null())
    227             .spawn()
    228             .is_ok_and(|mut cmd| {
    229                 cmd.stdin.take().is_some_and(|mut stdin| {
    230                     thread::spawn(move || {
    231                         stdin
    232                             .write_all(b"junk")
    233                             .is_ok_and(|()| stdin.flush().is_ok())
    234                     })
    235                     .join()
    236                     .is_ok_and(convert::identity)
    237                 }) && cmd.wait_with_output().is_ok_and(|output| {
    238                     !output.status.success()
    239                         && output.stderr.get(..23) == Some(b"Error: TOML parse error")
    240                 })
    241             })
    242     );
    243     assert!(
    244         tests::get_command()
    245             .arg("-h")
    246             .stderr(Stdio::null())
    247             .stdin(Stdio::null())
    248             .stdout(Stdio::piped())
    249             .output()
    250             .is_ok_and(|output| output.status.success()
    251                 && output.stdout.get(..crate::HELP.len()) == Some(crate::HELP.as_bytes()))
    252     );
    253     assert!(
    254         tests::get_command()
    255             .arg("-V")
    256             .stderr(Stdio::null())
    257             .stdin(Stdio::null())
    258             .stdout(Stdio::piped())
    259             .output()
    260             .is_ok_and(|output| output.status.success()
    261                 && output.stdout.get(..crate::VERSION.len()) == Some(crate::VERSION.as_bytes()))
    262     );
    263 }