Skip to content

Commit

Permalink
test: we should find broken symlink with and without --follow
Browse files Browse the repository at this point in the history
  • Loading branch information
tommilligan authored and sharkdp committed Feb 28, 2020
1 parent 0f2429c commit 3ca0210
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
19 changes: 19 additions & 0 deletions tests/testenv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@ impl TestEnv {
}
}

/// Create a broken symlink at the given path in the temp_dir.
pub fn create_broken_symlink<P: AsRef<Path>>(
&mut self,
link_path: P,
) -> Result<PathBuf, io::Error> {
let root = self.test_root();
let broken_symlink_link = root.join(link_path);
{
let temp_target_dir = TempDir::new("fd-tests-broken-symlink")?;
let broken_symlink_target = temp_target_dir.path().join("broken_symlink_target");
fs::File::create(&broken_symlink_target)?;
#[cfg(unix)]
unix::fs::symlink(&broken_symlink_target, &broken_symlink_link)?;
#[cfg(windows)]
windows::fs::symlink_file(&broken_symlink_target, &broken_symlink_link)?;
}
Ok(broken_symlink_link)
}

/// Get the root directory for the tests.
pub fn test_root(&self) -> PathBuf {
self.temp_dir.path().to_path_buf()
Expand Down
15 changes: 13 additions & 2 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,15 @@ fn test_file_system_boundaries() {
);
}

#[test]
fn test_follow_broken_symlink() {
let mut te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
te.create_broken_symlink("broken_symlink")
.expect("Failed to create broken symlink.");

te.assert_output(&["--follow", "--type", "f", "symlink"], "broken_symlink");
}

/// Null separator (--print0)
#[test]
fn test_print0() {
Expand Down Expand Up @@ -878,7 +887,9 @@ fn test_extension() {
/// Symlink as search directory
#[test]
fn test_symlink_as_root() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
let mut te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
te.create_broken_symlink("broken_symlink")
.expect("Failed to create broken symlink.");

// From: http://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html
// The getcwd() function shall place an absolute pathname of the current working directory in
Expand All @@ -899,6 +910,7 @@ fn test_symlink_as_root() {
&["", parent_parent],
&format!(
"{dir}/a.foo
{dir}/broken_symlink
{dir}/e1 e2
{dir}/one
{dir}/one/b.foo
Expand Down Expand Up @@ -990,7 +1002,6 @@ fn test_symlink_and_full_path_abs_path() {
),
);
}

/// Exclude patterns (--exclude)
#[test]
fn test_excludes() {
Expand Down

0 comments on commit 3ca0210

Please sign in to comment.