1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-09-16 19:56:17 +00:00

Merge pull request #2735 from thomasqueirozb/printenv_env_compat

env+printenv: use UResult + improve compatibility
This commit is contained in:
Sylvestre Ledru 2021-11-19 21:39:34 +01:00 committed by GitHub
commit 01440734a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 33 deletions

View file

@ -1,7 +1,4 @@
// spell-checker:ignore (words) bamf chdir
#[cfg(not(windows))]
use std::fs;
// spell-checker:ignore (words) bamf chdir rlimit prlimit COMSPEC
use crate::common::util::*;
use std::env;
@ -80,6 +77,20 @@ fn test_combined_file_set_unset() {
);
}
#[test]
fn test_unset_invalid_variables() {
use uucore::display::Quotable;
// Cannot test input with \0 in it, since output will also contain \0. rlimit::prlimit fails
// with this error: Error { kind: InvalidInput, message: "nul byte found in provided data" }
for var in &["", "a=b"] {
new_ucmd!().arg("-u").arg(var).run().stderr_only(format!(
"env: cannot unset {}: Invalid argument",
var.quote()
));
}
}
#[test]
fn test_single_name_value_pair() {
let out = new_ucmd!().arg("FOO=bar").run();
@ -163,7 +174,7 @@ fn test_fail_null_with_program() {
fn test_change_directory() {
let scene = TestScenario::new(util_name!());
let temporary_directory = tempdir().unwrap();
let temporary_path = fs::canonicalize(temporary_directory.path()).unwrap();
let temporary_path = std::fs::canonicalize(temporary_directory.path()).unwrap();
assert_ne!(env::current_dir().unwrap(), temporary_path);
// command to print out current working directory
@ -179,27 +190,36 @@ fn test_change_directory() {
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 temporary_path = temporary_directory.path();
let temporary_path = temporary_path
.strip_prefix(r"\\?\")
.unwrap_or(temporary_path);
let env_cd = env::current_dir().unwrap();
let env_cd = env_cd.strip_prefix(r"\\?\").unwrap_or(&env_cd);
assert_ne!(env_cd, temporary_path);
// COMSPEC is a variable that contains the full path to cmd.exe
let cmd_path = env::var("COMSPEC").unwrap();
// command to print out current working directory
let pwd = [&*cmd_path, "/C", "cd"];
let out = scene
.ucmd()
.arg("--chdir")
.arg(&temporary_path)
.args(&pwd)
.succeeds()
.stdout_move_str();
assert!(!out
.lines()
.any(|line| line.ends_with(temporary_path.file_name().unwrap().to_str().unwrap())));
assert_eq!(out.trim(), temporary_path.as_os_str())
}
#[test]