use assert_cmd::{Command, cargo_bin};
use assert_fs::TempDir;
use assert_fs::prelude::*;
use predicates::prelude::*;
#[test]
fn fmt() {
let temp_dir = TempDir::new().unwrap();
let input_file = temp_dir.child("rule.yar");
input_file.write_str("rule test { condition: true }").unwrap();
Command::new(cargo_bin!("yr"))
.arg("fmt")
.arg(input_file.path())
.assert()
.code(1);
Command::new(cargo_bin!("yr"))
.arg("fmt")
.arg(input_file.path())
.assert()
.code(0); }
#[test]
fn fmt_check_shows_filenames() {
let temp_dir = TempDir::new().unwrap();
let input_file = temp_dir.child("rule.yar");
input_file.write_str("rule test { condition: true }").unwrap();
Command::new(cargo_bin!("yr"))
.arg("fmt")
.arg("--check")
.arg(input_file.path())
.assert()
.stderr(predicate::str::contains("rule.yar"))
.code(1);
}
#[test]
fn utf8_error() {
let temp_dir = TempDir::new().unwrap();
let input_file = temp_dir.child("rule.yar");
input_file.write_binary(&[0xff, 0xff]).unwrap();
Command::new(cargo_bin!("yr"))
.arg("fmt")
.arg(input_file.path())
.assert()
.stderr("error: invalid UTF-8 at [0..1]\n")
.code(1);
}
#[test]
fn fmt_directory() {
let temp_dir = TempDir::new().unwrap();
let subdir = temp_dir.child("subdir");
subdir.create_dir_all().unwrap();
let file1 = temp_dir.child("rule1.yar");
let file2 = subdir.child("rule2.yar");
file1.write_str("rule test1 { condition: true }").unwrap();
file2.write_str("rule test2 { condition: true }").unwrap();
Command::new(cargo_bin!("yr"))
.arg("fmt")
.arg(temp_dir.path())
.assert()
.code(1);
Command::new(cargo_bin!("yr"))
.arg("fmt")
.arg(temp_dir.path())
.assert()
.code(0);
Command::new(cargo_bin!("yr"))
.arg("fmt")
.arg("-r")
.arg(temp_dir.path())
.assert()
.code(1);
Command::new(cargo_bin!("yr"))
.arg("fmt")
.arg("-r")
.arg(temp_dir.path())
.assert()
.code(0);
}