coreutils/tests/by-util/test_basename.rs
Christopher Regali 368e984fac
Change unchecked unwrapping to unwrap_or_default for Args-trait (#1845) (#1852)
* Change unchecked unwrapping to unwrap_or_default for argument parsing (resolving #1845)

* Added unit-testing for the collect_str function on invalid utf8 OsStrs

* Added a warning-message for identification purpose to the collect_str method.

* - Add removal of wrongly encoded empty strings to basename
- Add testing of broken encoding to basename
- Changed UCommand to use collect_str in args method to allow for integration testing of that method
- Change UCommand to use unwarp_or_default in arg method to match the behaviour of collect_str

* Trying out a new pattern for convert_str for getting a feeling of how the API feels with more control

* Adding convenience API for compact calls

* Add new API to everywhere, fix test for basename

* Added unit-testing for the conversion options

* Added unit-testing for the conversion options for windows

* fixed compilation and some merge hiccups

* Remove windows tests in order to make merge request build

* Fix formatting to match rustfmt for the merged file

* Improve documentation of the collect_str method and the unit-tests

* Fix compilation problems with test

Co-authored-by: Christopher Regali <chris.vdop@gmail.com>
Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
2021-04-25 23:28:42 +02:00

102 lines
2.2 KiB
Rust
Raw Blame History

use crate::common::util::*;
use std::ffi::OsStr;
#[test]
fn test_directory() {
new_ucmd!()
.args(&["/root/alpha/beta/gamma/delta/epsilon/omega/"])
.succeeds()
.stdout_only("omega\n");
}
#[test]
fn test_file() {
new_ucmd!()
.args(&["/etc/passwd"])
.succeeds()
.stdout_only("passwd\n");
}
#[test]
fn test_remove_suffix() {
new_ucmd!()
.args(&["/usr/local/bin/reallylongexecutable.exe", ".exe"])
.succeeds()
.stdout_only("reallylongexecutable\n");
}
#[test]
fn test_dont_remove_suffix() {
new_ucmd!()
.args(&["/foo/bar/baz", "baz"])
.succeeds()
.stdout_only("baz\n");
}
#[test]
fn test_multiple_param() {
for multiple_param in vec!["-a", "--multiple"] {
let path = "/foo/bar/baz";
new_ucmd!()
.args(&[multiple_param, path, path])
.succeeds()
.stdout_only("baz\nbaz\n");
}
}
#[test]
fn test_suffix_param() {
for suffix_param in vec!["-s", "--suffix"] {
let path = "/foo/bar/baz.exe";
new_ucmd!()
.args(&[suffix_param, ".exe", path, path])
.succeeds()
.stdout_only("baz\nbaz\n");
}
}
#[test]
fn test_zero_param() {
for zero_param in vec!["-z", "--zero"] {
let path = "/foo/bar/baz";
new_ucmd!()
.args(&[zero_param, "-a", path, path])
.succeeds()
.stdout_only("baz\0baz\0");
}
}
fn expect_error(input: Vec<&str>) {
assert!(new_ucmd!().args(&input).fails().no_stdout().stderr().len() > 0);
}
#[test]
fn test_invalid_option() {
let path = "/foo/bar/baz";
expect_error(vec!["-q", path]);
}
#[test]
fn test_no_args() {
expect_error(vec![]);
}
#[test]
fn test_too_many_args() {
expect_error(vec!["a", "b", "c"]);
}
fn test_invalid_utf8_args(os_str: &OsStr) {
let test_vec = vec![os_str.to_os_string()];
new_ucmd!().args(&test_vec).succeeds().stdout_is("fo<EFBFBD>o\n");
}
#[cfg(any(unix, target_os = "redox"))]
#[test]
fn invalid_utf8_args_unix() {
use std::os::unix::ffi::OsStrExt;
let source = [0x66, 0x6f, 0x80, 0x6f];
let os_str = OsStr::from_bytes(&source[..]);
test_invalid_utf8_args(os_str);
}