Skip to content

Commit

Permalink
Add tests for non-utf8
Browse files Browse the repository at this point in the history
  • Loading branch information
tmccombs authored and sylvestre committed Feb 18, 2023
1 parent 90786d4 commit aaaef33
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 22 deletions.
77 changes: 62 additions & 15 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ use std::os::unix::fs::PermissionsExt;
#[cfg(windows)]
use std::os::windows::fs::symlink_file;
#[cfg(not(windows))]
use std::path::Path;
use std::path::{Path, PathBuf};

#[cfg(any(target_os = "linux", target_os = "android"))]
use filetime::FileTime;
#[cfg(any(target_os = "linux", target_os = "android"))]
use rlimit::Resource;
use std::ffi::OsString;
#[cfg(any(target_os = "linux", target_os = "android"))]
use std::fs as std_fs;
use std::thread::sleep;
Expand Down Expand Up @@ -1225,8 +1226,8 @@ fn test_cp_archive_recursive() {
let file_2 = at.subdir.join(TEST_COPY_TO_FOLDER).join("2");
let file_2_link = at.subdir.join(TEST_COPY_TO_FOLDER).join("2.link");

at.touch(&file_1.to_string_lossy());
at.touch(&file_2.to_string_lossy());
at.touch(&file_1);
at.touch(&file_2);

at.symlink_file("1", &file_1_link.to_string_lossy());
at.symlink_file("2", &file_2_link.to_string_lossy());
Expand All @@ -1252,18 +1253,8 @@ fn test_cp_archive_recursive() {
.run();

println!("ls dest {}", result.stdout_str());
assert!(at.file_exists(
&at.subdir
.join(TEST_COPY_TO_FOLDER_NEW)
.join("1")
.to_string_lossy()
));
assert!(at.file_exists(
&at.subdir
.join(TEST_COPY_TO_FOLDER_NEW)
.join("2")
.to_string_lossy()
));
assert!(at.file_exists(&at.subdir.join(TEST_COPY_TO_FOLDER_NEW).join("1")));
assert!(at.file_exists(&at.subdir.join(TEST_COPY_TO_FOLDER_NEW).join("2")));

assert!(at.is_symlink(
&at.subdir
Expand Down Expand Up @@ -2532,3 +2523,59 @@ fn test_src_base_dot() {
.no_stdout();
assert!(!at.dir_exists("y/x"));
}

#[cfg(unix)]
fn non_utf8_name(suffix: &str) -> OsString {
use std::os::unix::ffi::OsStringExt;
let mut name = OsString::from_vec(vec![0xff, 0xff]);
name.push(suffix);
name
}

#[cfg(windows)]
fn non_utf8_name(suffix: &str) -> OsString {
use std::os::windows::ffi::OsStringExt;
let mut name = OsString::from_wide(&[0xdfff, 0xd400]);
name.push(suffix);
name
}

#[cfg(any(unix, windows))]
#[test]
fn test_non_utf8_src() {
let (at, mut ucmd) = at_and_ucmd!();
let src = non_utf8_name("src");
std::fs::File::create(at.plus(&src)).unwrap();
ucmd.args(&[src, "dest".into()])
.succeeds()
.no_stderr()
.no_stdout();
assert!(at.file_exists("dest"));
}

#[cfg(any(unix, windows))]
#[test]
fn test_non_utf8_dest() {
let (at, mut ucmd) = at_and_ucmd!();
let dest = non_utf8_name("dest");
ucmd.args(&[TEST_HELLO_WORLD_SOURCE.as_ref(), &*dest])
.succeeds()
.no_stderr()
.no_stdout();
assert!(at.file_exists(dest));
}

#[cfg(any(unix, windows))]
#[test]
fn test_non_utf8_target() {
let (at, mut ucmd) = at_and_ucmd!();
let dest = non_utf8_name("dest");
at.mkdir(&dest);
ucmd.args(&["-t".as_ref(), &*dest, TEST_HELLO_WORLD_SOURCE.as_ref()])
.succeeds()
.no_stderr()
.no_stdout();
let mut copied_file = PathBuf::from(dest);
copied_file.push(TEST_HELLO_WORLD_SOURCE);
assert!(at.file_exists(copied_file));
}
2 changes: 1 addition & 1 deletion tests/by-util/test_ln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ fn test_symlink_implicit_target_dir() {
let file = &path.to_string_lossy();

at.mkdir(dir);
at.touch(file);
at.touch(&path);

ucmd.args(&["-s", file]).succeeds().no_stderr();

Expand Down
14 changes: 8 additions & 6 deletions tests/common/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,14 +746,14 @@ impl AtPath {
self.subdir.to_str().unwrap().to_owned()
}

pub fn plus(&self, name: &str) -> PathBuf {
pub fn plus<P: AsRef<Path>>(&self, name: P) -> PathBuf {
let mut pathbuf = self.subdir.clone();
pathbuf.push(name);
pathbuf
}

pub fn plus_as_string(&self, name: &str) -> String {
String::from(self.plus(name).to_str().unwrap())
pub fn plus_as_string<P: AsRef<Path>>(&self, name: P) -> String {
self.plus(name).display().to_string()
}

fn minus(&self, name: &str) -> PathBuf {
Expand Down Expand Up @@ -876,7 +876,8 @@ impl AtPath {
fs::remove_dir(self.plus(dir)).unwrap();
}

pub fn mkdir(&self, dir: &str) {
pub fn mkdir<P: AsRef<Path>>(&self, dir: P) {
let dir = dir.as_ref();
log_info("mkdir", self.plus_as_string(dir));
fs::create_dir(self.plus(dir)).unwrap();
}
Expand All @@ -893,7 +894,8 @@ impl AtPath {
}
}

pub fn touch(&self, file: &str) {
pub fn touch<P: AsRef<Path>>(&self, file: P) {
let file = file.as_ref();
log_info("touch", self.plus_as_string(file));
File::create(self.plus(file)).unwrap();
}
Expand Down Expand Up @@ -1016,7 +1018,7 @@ impl AtPath {
}
}

pub fn file_exists(&self, path: &str) -> bool {
pub fn file_exists<P: AsRef<Path>>(&self, path: P) -> bool {
match fs::metadata(self.plus(path)) {
Ok(m) => m.is_file(),
Err(_) => false,
Expand Down

0 comments on commit aaaef33

Please sign in to comment.