| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
// spell-checker:ignore (words) symdir somefakedir |
| 7 |
|
| 8 |
use std::path::PathBuf; |
| 9 |
|
| 10 |
use uutests::at_and_ucmd; |
| 11 |
use uutests::new_ucmd; |
| 12 |
use uutests::util::UCommand; |
| 13 |
|
| 14 |
#[test] |
| 15 |
fn test_invalid_arg() { |
| 16 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 17 |
} |
| 18 |
|
| 19 |
#[test] |
| 20 |
fn test_default() { |
| 21 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 22 |
ucmd.succeeds().stdout_is(at.root_dir_resolved() + "\n"); |
| 23 |
} |
| 24 |
|
| 25 |
#[test] |
| 26 |
fn test_ignores_non_option_arguments() { |
| 27 |
// GNU pwd ignores non-option operands, warning on stderr but exiting 0. |
| 28 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 29 |
ucmd.arg("will-fail") |
| 30 |
.succeeds() |
| 31 |
.stdout_is(at.root_dir_resolved() + "\n") |
| 32 |
.stderr_is("pwd: ignoring non-option arguments\n"); |
| 33 |
} |
| 34 |
|
| 35 |
#[cfg(unix)] |
| 36 |
#[test] |
| 37 |
fn test_deleted_dir() { |
| 38 |
use std::process::Command; |
| 39 |
use uutests::util::TestScenario; |
| 40 |
use uutests::util_name; |
| 41 |
|
| 42 |
let ts = TestScenario::new(util_name!()); |
| 43 |
let at = &ts.fixtures; |
| 44 |
let output = Command::new("sh") |
| 45 |
.arg("-c") |
| 46 |
.arg(format!( |
| 47 |
"cd '{}'; mkdir foo; cd foo; rmdir ../foo; LANG=C exec '{}' {}", |
| 48 |
at.root_dir_resolved(), |
| 49 |
ts.bin_path.to_str().unwrap(), |
| 50 |
ts.util_name, |
| 51 |
)) |
| 52 |
.output() |
| 53 |
.unwrap(); |
| 54 |
assert!(!output.status.success()); |
| 55 |
assert!(output.stdout.is_empty()); |
| 56 |
assert_eq!( |
| 57 |
String::from_utf8_lossy(&output.stderr), |
| 58 |
"pwd: failed to get current directory: No such file or directory\n" |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
struct Env { |
| 63 |
ucmd: UCommand, |
| 64 |
#[cfg(not(windows))] |
| 65 |
root: String, |
| 66 |
subdir: String, |
| 67 |
symdir: String, |
| 68 |
} |
| 69 |
|
| 70 |
fn symlinked_env() -> Env { |
| 71 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 72 |
at.mkdir("subdir"); |
| 73 |
// Note: on Windows this requires admin permissions |
| 74 |
at.symlink_dir("subdir", "symdir"); |
| 75 |
let root = PathBuf::from(at.root_dir_resolved()); |
| 76 |
ucmd.current_dir(root.join("symdir")); |
| 77 |
#[cfg(not(windows))] |
| 78 |
ucmd.env("PWD", root.join("symdir")); |
| 79 |
Env { |
| 80 |
ucmd, |
| 81 |
#[cfg(not(windows))] |
| 82 |
root: root.to_string_lossy().into_owned(), |
| 83 |
subdir: root.join("subdir").to_string_lossy().into_owned(), |
| 84 |
symdir: root.join("symdir").to_string_lossy().into_owned(), |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
#[test] |
| 89 |
fn test_symlinked_logical() { |
| 90 |
let mut env = symlinked_env(); |
| 91 |
env.ucmd.arg("-L").succeeds().stdout_is(env.symdir + "\n"); |
| 92 |
} |
| 93 |
|
| 94 |
#[test] |
| 95 |
fn test_symlinked_physical() { |
| 96 |
let mut env = symlinked_env(); |
| 97 |
env.ucmd.arg("-P").succeeds().stdout_is(env.subdir + "\n"); |
| 98 |
} |
| 99 |
|
| 100 |
#[test] |
| 101 |
fn test_symlinked_default() { |
| 102 |
let mut env = symlinked_env(); |
| 103 |
env.ucmd.succeeds().stdout_is(env.subdir + "\n"); |
| 104 |
} |
| 105 |
|
| 106 |
#[test] |
| 107 |
fn test_symlinked_default_posix() { |
| 108 |
let mut env = symlinked_env(); |
| 109 |
env.ucmd |
| 110 |
.env("POSIXLY_CORRECT", "1") |
| 111 |
.succeeds() |
| 112 |
.stdout_is(env.symdir.clone() + "\n"); |
| 113 |
} |
| 114 |
|
| 115 |
#[test] |
| 116 |
fn test_symlinked_default_posix_l() { |
| 117 |
let mut env = symlinked_env(); |
| 118 |
env.ucmd |
| 119 |
.env("POSIXLY_CORRECT", "1") |
| 120 |
.arg("-L") |
| 121 |
.succeeds() |
| 122 |
.stdout_is(env.symdir + "\n"); |
| 123 |
} |
| 124 |
|
| 125 |
#[test] |
| 126 |
fn test_symlinked_default_posix_p() { |
| 127 |
let mut env = symlinked_env(); |
| 128 |
env.ucmd |
| 129 |
.env("POSIXLY_CORRECT", "1") |
| 130 |
.arg("-P") |
| 131 |
.succeeds() |
| 132 |
.stdout_is(env.subdir + "\n"); |
| 133 |
} |
| 134 |
|
| 135 |
#[cfg(not(windows))] |
| 136 |
pub mod untrustworthy_pwd_var { |
| 137 |
use std::path::Path; |
| 138 |
|
| 139 |
use super::*; |
| 140 |
|
| 141 |
#[test] |
| 142 |
fn test_nonexistent_logical() { |
| 143 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 144 |
ucmd.arg("-L") |
| 145 |
.env("PWD", "/somefakedir") |
| 146 |
.succeeds() |
| 147 |
.stdout_is(at.root_dir_resolved() + "\n"); |
| 148 |
} |
| 149 |
|
| 150 |
#[test] |
| 151 |
fn test_wrong_logical() { |
| 152 |
let mut env = symlinked_env(); |
| 153 |
env.ucmd |
| 154 |
.arg("-L") |
| 155 |
.env("PWD", env.root) |
| 156 |
.succeeds() |
| 157 |
.stdout_is(env.subdir + "\n"); |
| 158 |
} |
| 159 |
|
| 160 |
#[test] |
| 161 |
fn test_redundant_logical() { |
| 162 |
let mut env = symlinked_env(); |
| 163 |
env.ucmd |
| 164 |
.arg("-L") |
| 165 |
.env("PWD", Path::new(&env.symdir).join(".")) |
| 166 |
.succeeds() |
| 167 |
.stdout_is(env.subdir + "\n"); |
| 168 |
} |
| 169 |
|
| 170 |
#[test] |
| 171 |
fn test_relative_logical() { |
| 172 |
let mut env = symlinked_env(); |
| 173 |
env.ucmd |
| 174 |
.arg("-L") |
| 175 |
.env("PWD", ".") |
| 176 |
.succeeds() |
| 177 |
.stdout_is(env.subdir + "\n"); |
| 178 |
} |
| 179 |
} |