1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

feature[env]: Add support for --chdir=DIR in env (fixes #1568) (#1569)

This commit is contained in:
Felipe Lema 2020-08-03 11:32:31 -04:00 committed by Roy Ivy III
parent d9be24e354
commit ba6b55f117
2 changed files with 89 additions and 0 deletions

View file

@ -1,4 +1,11 @@
#[cfg(not(windows))]
use std::fs;
use std::path::Path;
extern crate tempfile;
use self::tempfile::tempdir;
use crate::common::util::*;
use std::env;
#[test]
fn test_env_help() {
@ -150,3 +157,64 @@ fn test_fail_null_with_program() {
let out = new_ucmd!().arg("--null").arg("cd").fails().stderr;
assert!(out.contains("cannot specify --null (-0) with command"));
}
#[cfg(not(windows))]
#[test]
fn test_change_directory() {
let scene = TestScenario::new(util_name!());
let temporary_directory = tempdir().unwrap();
let temporary_path = fs::canonicalize(temporary_directory.path()).unwrap();
assert_ne!(env::current_dir().unwrap(), temporary_path);
// command to print out current working directory
let pwd = "pwd";
let out = scene
.ucmd()
.arg("--chdir")
.arg(&temporary_path)
.arg(pwd)
.run()
.stdout;
assert_eq!(out.trim(), temporary_path.as_os_str())
}
// no way to consistently get "current working directory", `cd` doesn't work @ CI
// instead, we test that the unique temporary directory appears somewhere in the printed variables
#[cfg(windows)]
#[test]
fn test_change_directory() {
let scene = TestScenario::new(util_name!());
let temporary_directory = tempdir().unwrap();
let temporary_path = temporary_directory.path();
assert_ne!(env::current_dir().unwrap(), temporary_path);
let out = scene
.ucmd()
.arg("--chdir")
.arg(&temporary_path)
.run()
.stdout;
assert_eq!(
out.lines()
.any(|line| line.ends_with(temporary_path.file_name().unwrap().to_str().unwrap())),
false
);
}
#[test]
fn test_fail_change_directory() {
let scene = TestScenario::new(util_name!());
let some_non_existing_path = "some_nonexistent_path";
assert_eq!(Path::new(some_non_existing_path).is_dir(), false);
let out = scene
.ucmd()
.arg("--chdir")
.arg(some_non_existing_path)
.arg("pwd")
.fails()
.stderr;
assert!(out.contains("env: cannot change directory to "));
}