change macro to function in test

tidy up macro

reduce reallocations

macro to function

tidy w/ import

add EOL
This commit is contained in:
Jay 2020-10-08 14:16:55 +01:00 committed by Abin Simon
parent 6018be7073
commit 7c971e7f3a

View file

@ -364,46 +364,41 @@ fn test_version_sort_overwrite_by_sizesort() {
#[cfg(test)] #[cfg(test)]
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
macro_rules! bad_utf8 { fn bad_utf8(tmp: &std::path::Path, pre: &str, suf: &str) -> String {
($tmp:expr, $e:expr) => {{ let mut fname = format!("{}/{}", tmp.display(), pre).into_bytes();
let mut fname = format!($e, $tmp.path().to_str().unwrap()) fname.reserve(2 + suf.len());
.as_bytes() fname.push(0xa7);
.to_vec(); fname.push(0xfd);
fname.push(0xa7); fname.extend(suf.as_bytes());
fname.push(0xfd); unsafe { String::from_utf8_unchecked(fname) }
fname
}};
($tmp:expr, $e:expr, $v:expr) => {{
let mut fname = bad_utf8!($tmp, $e);
fname.append($v.as_bytes().to_vec().as_mut());
fname
}};
} }
#[test] #[test]
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
fn test_bad_utf_8_extension() { fn test_bad_utf_8_extension() {
use std::fs::File;
let tmp = tempdir(); let tmp = tempdir();
let fname = unsafe { String::from_utf8_unchecked(bad_utf8!(tmp, "{}/bad.extension")) }; let fname = bad_utf8(tmp.path(), "bad.extension", "");
std::fs::File::create(fname).expect("failed to create file"); File::create(fname).expect("failed to create file");
cmd() cmd()
.arg(tmp.path()) .arg(tmp.path())
.assert() .assert()
.stdout(predicate::str::is_match("bad.extension\u{fffd}\u{fffd}").unwrap()); .stdout(predicate::str::is_match("bad.extension\u{fffd}\u{fffd}\n$").unwrap());
} }
#[test] #[test]
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
fn test_bad_utf_8_name() { fn test_bad_utf_8_name() {
use std::fs::File;
let tmp = tempdir(); let tmp = tempdir();
let fname = unsafe { String::from_utf8_unchecked(bad_utf8!(tmp, "{}/bad-name", ".ext")) }; let fname = bad_utf8(tmp.path(), "bad-name", ".ext");
std::fs::File::create(fname).expect("failed to create file"); File::create(fname).expect("failed to create file");
cmd() cmd()
.arg(tmp.path()) .arg(tmp.path())
.assert() .assert()
.stdout(predicate::str::is_match("bad-name\u{fffd}\u{fffd}.ext").unwrap()); .stdout(predicate::str::is_match("bad-name\u{fffd}\u{fffd}.ext\n$").unwrap());
} }
fn cmd() -> Command { fn cmd() -> Command {