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

Realpath relative options (#3710)

* realpath: introduce relative options, make correct exit codes, make pass
GNU test mist/realpath.sh
This commit is contained in:
Niyaz Nigmatullin 2022-07-12 09:29:20 +03:00 committed by GitHub
parent 6b00aec48e
commit de65d4d649
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 256 additions and 16 deletions

View file

@ -1,5 +1,7 @@
use crate::common::util::*;
#[cfg(windows)]
use regex::Regex;
use std::path::{Path, MAIN_SEPARATOR};
static GIBBERISH: &str = "supercalifragilisticexpialidocious";
@ -263,3 +265,102 @@ fn test_realpath_when_symlink_part_is_missing() {
.stderr_contains("realpath: dir1/foo2: No such file or directory\n")
.stderr_contains("realpath: dir1/foo4: No such file or directory\n");
}
#[test]
fn test_relative_existing_require_directories() {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("dir1");
at.touch("dir1/f");
ucmd.args(&["-e", "--relative-base=.", "--relative-to=dir1/f", "."])
.fails()
.code_is(1)
.stderr_contains("directory");
}
#[test]
fn test_relative_existing_require_directories_2() {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("dir1");
at.touch("dir1/f");
ucmd.args(&["-e", "--relative-base=.", "--relative-to=dir1", "."])
.succeeds()
.stdout_is("..\n");
}
#[test]
fn test_relative_base_not_prefix_of_relative_to() {
let result = new_ucmd!()
.args(&[
"-sm",
"--relative-base=/usr/local",
"--relative-to=/usr",
"/usr",
"/usr/local",
])
.succeeds();
#[cfg(windows)]
result.stdout_matches(&Regex::new(r"^.*:\\usr\n.*:\\usr\\local$").unwrap());
#[cfg(not(windows))]
result.stdout_is("/usr\n/usr/local\n");
}
#[test]
fn test_relative_string_handling() {
let result = new_ucmd!()
.args(&["-m", "--relative-to=prefix", "prefixed/1"])
.succeeds();
#[cfg(not(windows))]
result.stdout_is("../prefixed/1\n");
#[cfg(windows)]
result.stdout_is("..\\prefixed\\1\n");
let result = new_ucmd!()
.args(&["-m", "--relative-to=prefixed", "prefix/1"])
.succeeds();
#[cfg(not(windows))]
result.stdout_is("../prefix/1\n");
#[cfg(windows)]
result.stdout_is("..\\prefix\\1\n");
new_ucmd!()
.args(&["-m", "--relative-to=prefixed", "prefixed/1"])
.succeeds()
.stdout_is("1\n");
}
#[test]
fn test_relative() {
let result = new_ucmd!()
.args(&[
"-sm",
"--relative-base=/usr",
"--relative-to=/usr",
"/tmp",
"/usr",
])
.succeeds();
#[cfg(not(windows))]
result.stdout_is("/tmp\n.\n");
#[cfg(windows)]
result.stdout_matches(&Regex::new(r"^.*:\\tmp\n\.$").unwrap());
new_ucmd!()
.args(&["-sm", "--relative-base=/", "--relative-to=/", "/", "/usr"])
.succeeds()
.stdout_is(".\nusr\n"); // spell-checker:disable-line
let result = new_ucmd!()
.args(&["-sm", "--relative-base=/usr", "/tmp", "/usr"])
.succeeds();
#[cfg(not(windows))]
result.stdout_is("/tmp\n.\n");
#[cfg(windows)]
result.stdout_matches(&Regex::new(r"^.*:\\tmp\n\.$").unwrap());
new_ucmd!()
.args(&["-sm", "--relative-base=/", "/", "/usr"])
.succeeds()
.stdout_is(".\nusr\n"); // spell-checker:disable-line
}