2021-05-30 05:10:54 +00:00
|
|
|
// spell-checker:ignore (words) bamf chdir
|
|
|
|
|
2020-08-03 15:32:31 +00:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
use std::fs;
|
|
|
|
|
2020-05-25 17:05:26 +00:00
|
|
|
use crate::common::util::*;
|
2020-08-03 15:32:31 +00:00
|
|
|
use std::env;
|
2021-01-19 07:15:53 +00:00
|
|
|
use std::path::Path;
|
|
|
|
use tempfile::tempdir;
|
2015-11-16 05:25:01 +00:00
|
|
|
|
2017-10-28 14:32:50 +00:00
|
|
|
#[test]
|
|
|
|
fn test_env_help() {
|
2021-04-05 20:03:43 +00:00
|
|
|
new_ucmd!()
|
2020-04-13 18:36:03 +00:00
|
|
|
.arg("--help")
|
|
|
|
.succeeds()
|
|
|
|
.no_stderr()
|
2021-04-05 20:03:43 +00:00
|
|
|
.stdout_contains("OPTIONS:");
|
2017-10-28 14:32:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_env_version() {
|
2021-04-05 20:03:43 +00:00
|
|
|
new_ucmd!()
|
2020-04-13 18:36:03 +00:00
|
|
|
.arg("--version")
|
|
|
|
.succeeds()
|
|
|
|
.no_stderr()
|
2021-04-05 20:03:43 +00:00
|
|
|
.stdout_contains(util_name!());
|
2017-10-28 14:32:50 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 08:22:13 +00:00
|
|
|
#[test]
|
|
|
|
fn test_echo() {
|
2021-04-17 09:22:49 +00:00
|
|
|
let result = new_ucmd!().arg("echo").arg("FOO-bar").succeeds();
|
2018-11-19 08:22:13 +00:00
|
|
|
|
2021-04-05 20:03:43 +00:00
|
|
|
assert_eq!(result.stdout_str().trim(), "FOO-bar");
|
2018-11-19 08:22:13 +00:00
|
|
|
}
|
|
|
|
|
2019-04-28 15:19:14 +00:00
|
|
|
#[test]
|
|
|
|
fn test_file_option() {
|
2021-04-17 09:22:49 +00:00
|
|
|
let out = new_ucmd!()
|
|
|
|
.arg("-f")
|
|
|
|
.arg("vars.conf.txt")
|
|
|
|
.run()
|
|
|
|
.stdout_move_str();
|
2020-04-13 18:36:03 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
out.lines()
|
|
|
|
.filter(|&line| line == "FOO=bar" || line == "BAR=bamf this")
|
|
|
|
.count(),
|
|
|
|
2
|
|
|
|
);
|
2019-04-28 15:19:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_combined_file_set() {
|
|
|
|
let out = new_ucmd!()
|
2020-04-13 18:36:03 +00:00
|
|
|
.arg("-f")
|
|
|
|
.arg("vars.conf.txt")
|
2019-04-28 15:19:14 +00:00
|
|
|
.arg("FOO=bar.alt")
|
2020-04-13 18:36:03 +00:00
|
|
|
.run()
|
2021-04-05 20:03:43 +00:00
|
|
|
.stdout_move_str();
|
2019-04-28 15:19:14 +00:00
|
|
|
|
|
|
|
assert_eq!(out.lines().filter(|&line| line == "FOO=bar.alt").count(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_combined_file_set_unset() {
|
|
|
|
let out = new_ucmd!()
|
2020-04-13 18:36:03 +00:00
|
|
|
.arg("-u")
|
|
|
|
.arg("BAR")
|
|
|
|
.arg("-f")
|
|
|
|
.arg("vars.conf.txt")
|
2019-04-28 15:19:14 +00:00
|
|
|
.arg("FOO=bar.alt")
|
2021-04-05 20:03:43 +00:00
|
|
|
.succeeds()
|
|
|
|
.stdout_move_str();
|
2020-04-13 18:36:03 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
out.lines()
|
|
|
|
.filter(|&line| line == "FOO=bar.alt" || line.starts_with("BAR="))
|
|
|
|
.count(),
|
|
|
|
1
|
|
|
|
);
|
2019-04-28 15:19:14 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 05:25:01 +00:00
|
|
|
#[test]
|
|
|
|
fn test_single_name_value_pair() {
|
2021-04-05 20:03:43 +00:00
|
|
|
let out = new_ucmd!().arg("FOO=bar").run();
|
2015-11-16 05:25:01 +00:00
|
|
|
|
2021-04-05 20:03:43 +00:00
|
|
|
assert!(out.stdout_str().lines().any(|line| line == "FOO=bar"));
|
2015-11-16 05:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multiple_name_value_pairs() {
|
2021-04-05 20:03:43 +00:00
|
|
|
let out = new_ucmd!().arg("FOO=bar").arg("ABC=xyz").run();
|
2020-04-13 18:36:03 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2021-04-17 09:22:49 +00:00
|
|
|
out.stdout_str()
|
|
|
|
.lines()
|
2020-04-13 18:36:03 +00:00
|
|
|
.filter(|&line| line == "FOO=bar" || line == "ABC=xyz")
|
|
|
|
.count(),
|
|
|
|
2
|
|
|
|
);
|
2015-11-16 05:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ignore_environment() {
|
2016-08-23 11:52:43 +00:00
|
|
|
let scene = TestScenario::new(util_name!());
|
2015-11-16 05:25:01 +00:00
|
|
|
|
2021-04-05 20:03:43 +00:00
|
|
|
scene.ucmd().arg("-i").run().no_stdout();
|
|
|
|
scene.ucmd().arg("-").run().no_stdout();
|
2015-11-16 05:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_null_delimiter() {
|
2016-08-23 11:52:43 +00:00
|
|
|
let out = new_ucmd!()
|
2020-04-13 18:36:03 +00:00
|
|
|
.arg("-i")
|
|
|
|
.arg("--null")
|
|
|
|
.arg("FOO=bar")
|
|
|
|
.arg("ABC=xyz")
|
2021-04-05 20:03:43 +00:00
|
|
|
.succeeds()
|
|
|
|
.stdout_move_str();
|
2020-04-13 18:36:03 +00:00
|
|
|
|
|
|
|
let mut vars: Vec<_> = out.split('\0').collect();
|
2015-12-22 11:44:05 +00:00
|
|
|
assert_eq!(vars.len(), 3);
|
2021-05-29 12:32:35 +00:00
|
|
|
vars.sort_unstable();
|
2015-12-22 11:44:05 +00:00
|
|
|
assert_eq!(vars[0], "");
|
|
|
|
assert_eq!(vars[1], "ABC=xyz");
|
|
|
|
assert_eq!(vars[2], "FOO=bar");
|
2015-11-16 05:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unset_variable() {
|
|
|
|
// This test depends on the HOME variable being pre-defined by the
|
|
|
|
// default shell
|
2016-08-23 11:52:43 +00:00
|
|
|
let out = TestScenario::new(util_name!())
|
2020-04-13 18:36:03 +00:00
|
|
|
.ucmd_keepenv()
|
|
|
|
.arg("-u")
|
|
|
|
.arg("HOME")
|
2021-04-05 20:03:43 +00:00
|
|
|
.succeeds()
|
|
|
|
.stdout_move_str();
|
2015-11-16 05:25:01 +00:00
|
|
|
|
2021-05-29 12:32:35 +00:00
|
|
|
assert!(!out.lines().any(|line| line.starts_with("HOME=")));
|
2015-11-16 05:25:01 +00:00
|
|
|
}
|
2019-04-28 09:12:37 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fail_null_with_program() {
|
2021-04-22 20:37:44 +00:00
|
|
|
new_ucmd!()
|
|
|
|
.arg("--null")
|
|
|
|
.arg("cd")
|
|
|
|
.fails()
|
|
|
|
.stderr_contains("cannot specify --null (-0) with command");
|
2019-04-28 09:12:37 +00:00
|
|
|
}
|
2020-08-03 15:32:31 +00:00
|
|
|
|
|
|
|
#[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)
|
2021-04-05 20:03:43 +00:00
|
|
|
.succeeds()
|
|
|
|
.stdout_move_str();
|
2020-08-03 15:32:31 +00:00
|
|
|
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)
|
2021-04-05 20:03:43 +00:00
|
|
|
.succeeds()
|
|
|
|
.stdout_move_str();
|
2021-06-04 19:28:53 +00:00
|
|
|
|
|
|
|
assert!(!out
|
|
|
|
.lines()
|
|
|
|
.any(|line| line.ends_with(temporary_path.file_name().unwrap().to_str().unwrap())));
|
2020-08-03 15:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fail_change_directory() {
|
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
let some_non_existing_path = "some_nonexistent_path";
|
2021-05-29 12:32:35 +00:00
|
|
|
assert!(!Path::new(some_non_existing_path).is_dir());
|
2020-08-03 15:32:31 +00:00
|
|
|
|
|
|
|
let out = scene
|
|
|
|
.ucmd()
|
|
|
|
.arg("--chdir")
|
|
|
|
.arg(some_non_existing_path)
|
|
|
|
.arg("pwd")
|
|
|
|
.fails()
|
2021-04-05 20:03:43 +00:00
|
|
|
.stderr_move_str();
|
2020-08-03 15:32:31 +00:00
|
|
|
assert!(out.contains("env: cannot change directory to "));
|
|
|
|
}
|