From 34eabca299b8938bc61760091120bcca9e1a1519 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Tue, 25 Oct 2022 22:29:46 +0200 Subject: [PATCH] pwd: support the env variable 'POSIXLY_CORRECT' Should fix tests/misc/pwd-option.sh --- src/uu/pwd/src/pwd.rs | 5 ++++- tests/by-util/test_pwd.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/uu/pwd/src/pwd.rs b/src/uu/pwd/src/pwd.rs index 732df19f6..b1e2b30ff 100644 --- a/src/uu/pwd/src/pwd.rs +++ b/src/uu/pwd/src/pwd.rs @@ -126,7 +126,10 @@ fn logical_path() -> io::Result { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uu_app().try_get_matches_from(args)?; - let cwd = if matches.get_flag(OPT_LOGICAL) { + // if POSIXLY_CORRECT is set, we want to a logical resolution. + // This produces a different output when doing mkdir -p a/b && ln -s a/b c && cd c && pwd + // We should get c in this case instead of a/b at the end of the path + let cwd = if matches.get_flag(OPT_LOGICAL) || env::var("POSIXLY_CORRECT").is_ok() { logical_path() } else { physical_path() diff --git a/tests/by-util/test_pwd.rs b/tests/by-util/test_pwd.rs index 0ae63c895..0ae0cc909 100644 --- a/tests/by-util/test_pwd.rs +++ b/tests/by-util/test_pwd.rs @@ -90,6 +90,35 @@ fn test_symlinked_default() { env.ucmd.succeeds().stdout_is(env.subdir + "\n"); } +#[test] +fn test_symlinked_default_posix() { + let mut env = symlinked_env(); + env.ucmd + .env("POSIXLY_CORRECT", "1") + .succeeds() + .stdout_is(env.symdir.clone() + "\n"); +} + +#[test] +fn test_symlinked_default_posix_l() { + let mut env = symlinked_env(); + env.ucmd + .env("POSIXLY_CORRECT", "1") + .arg("-L") + .succeeds() + .stdout_is(env.symdir + "\n"); +} + +#[test] +fn test_symlinked_default_posix_p() { + let mut env = symlinked_env(); + env.ucmd + .env("POSIXLY_CORRECT", "1") + .arg("-P") + .succeeds() + .stdout_is(env.symdir + "\n"); +} + #[cfg(not(windows))] pub mod untrustworthy_pwd_var { use std::path::Path;