coreutils/tests/by-util/test_pwd.rs
Joining7943 fdf0f96a01 tests/util: Restructure UCommand to run in shell per default. Introduce constant TESTS_BINARY.
Summary of changes in tests/util:

* Introduce global constant TESTS_BINARY holding the path to `coreutils`
* Implement running an arbitrary command in a sh or cmd shell per default.
* Implement std::fmt::Display for UCommand.
* Change usages of UCommand::util_name from &Option<OsStr> to Option<OsStr>
* `UCommand::new_from_tmp`: Use OsStr directly instead of TempDir -> String -> OsStr
* Collect arguments in `UCommand::args` field
* Build environment variables in `UCommand` itself instead of `std::process::Command`
* Move building of std::process:Command from fields in UCommand to own method `build`
* Remove assertions of UCommand::has_run in arg, args and env.

Summary of changes in tests/by-util:

* Remove usages of UCommand::raw. Fix tests to use UCommand::to_string.
* test_test: Adjust test_invalid_utf8_integer_compare to use `UCommand::args`
* test_chmod: run_single_test
* test_pwd: symlinked_env
* test_cp:
    Fix the usage of &Option<OsStr> in `test_src_base_dot`
    Refactor test_src_base_dot to not use UCommand::new but ts.ucmd() instead
2023-02-17 17:39:33 +01:00

166 lines
3.8 KiB
Rust

// spell-checker:ignore (words) symdir somefakedir
use std::path::PathBuf;
use crate::common::util::*;
#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}
#[test]
fn test_default() {
let (at, mut ucmd) = at_and_ucmd!();
ucmd.succeeds().stdout_is(at.root_dir_resolved() + "\n");
}
#[test]
fn test_failed() {
let (_at, mut ucmd) = at_and_ucmd!();
ucmd.arg("will-fail").fails();
}
#[cfg(unix)]
#[test]
fn test_deleted_dir() {
use std::process::Command;
let ts = TestScenario::new(util_name!());
let at = ts.fixtures.clone();
let output = Command::new("sh")
.arg("-c")
.arg(format!(
"cd '{}'; mkdir foo; cd foo; rmdir ../foo; exec '{}' {}",
at.root_dir_resolved(),
ts.bin_path.to_str().unwrap(),
ts.util_name,
))
.output()
.unwrap();
assert!(!output.status.success());
assert!(output.stdout.is_empty());
assert_eq!(
output.stderr,
b"pwd: failed to get current directory: No such file or directory\n"
);
}
struct Env {
ucmd: UCommand,
#[cfg(not(windows))]
root: String,
subdir: String,
symdir: String,
}
fn symlinked_env() -> Env {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("subdir");
// Note: on Windows this requires admin permissions
at.symlink_dir("subdir", "symdir");
let root = PathBuf::from(at.root_dir_resolved());
ucmd.current_dir(root.join("symdir"));
#[cfg(not(windows))]
ucmd.env("PWD", root.join("symdir"));
Env {
ucmd,
#[cfg(not(windows))]
root: root.to_string_lossy().into_owned(),
subdir: root.join("subdir").to_string_lossy().into_owned(),
symdir: root.join("symdir").to_string_lossy().into_owned(),
}
}
#[test]
fn test_symlinked_logical() {
let mut env = symlinked_env();
env.ucmd.arg("-L").succeeds().stdout_is(env.symdir + "\n");
}
#[test]
fn test_symlinked_physical() {
let mut env = symlinked_env();
env.ucmd.arg("-P").succeeds().stdout_is(env.subdir + "\n");
}
#[test]
fn test_symlinked_default() {
let mut env = symlinked_env();
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;
use super::*;
#[test]
fn test_nonexistent_logical() {
let (at, mut ucmd) = at_and_ucmd!();
ucmd.arg("-L")
.env("PWD", "/somefakedir")
.succeeds()
.stdout_is(at.root_dir_resolved() + "\n");
}
#[test]
fn test_wrong_logical() {
let mut env = symlinked_env();
env.ucmd
.arg("-L")
.env("PWD", env.root)
.succeeds()
.stdout_is(env.subdir + "\n");
}
#[test]
fn test_redundant_logical() {
let mut env = symlinked_env();
env.ucmd
.arg("-L")
.env("PWD", Path::new(&env.symdir).join("."))
.succeeds()
.stdout_is(env.subdir + "\n");
}
#[test]
fn test_relative_logical() {
let mut env = symlinked_env();
env.ucmd
.arg("-L")
.env("PWD", ".")
.succeeds()
.stdout_is(env.subdir + "\n");
}
}