1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 20:47:46 +00:00

Merge pull request #6108 from BenWiederhake/dev-truncate-reference-create

truncate: correctly handle file (non-)creation
This commit is contained in:
Sylvestre Ledru 2024-03-23 18:46:16 +01:00 committed by GitHub
commit ff1ecf6242
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 85 additions and 45 deletions

View file

@ -43,9 +43,6 @@ fn test_reference() {
let mut file = at.make_file(FILE2);
// manpage: "A FILE argument that does not exist is created."
// TODO: 'truncate' does not create the file in this case,
// but should because '--no-create' wasn't specified.
at.touch(FILE1); // TODO: remove this when 'no-create' is fixed
scene.ucmd().arg("-s").arg("+5KB").arg(FILE1).succeeds();
scene
@ -240,10 +237,9 @@ fn test_reference_with_size_file_not_found() {
#[test]
fn test_truncate_bytes_size() {
// TODO: this should succeed without error, uncomment when '--no-create' is fixed
// new_ucmd!()
// .args(&["--no-create", "--size", "K", "file"])
// .succeeds();
new_ucmd!()
.args(&["--no-create", "--size", "K", "file"])
.succeeds();
new_ucmd!()
.args(&["--size", "1024R", "file"])
.fails()
@ -270,9 +266,39 @@ fn test_new_file() {
assert_eq!(at.read_bytes(filename), vec![b'\0'; 8]);
}
/// Test that truncating a non-existent file creates that file, even in reference-mode.
#[test]
fn test_new_file_reference() {
let (at, mut ucmd) = at_and_ucmd!();
let mut old_file = at.make_file(FILE1);
old_file.write_all(b"1234567890").unwrap();
let filename = "new_file_that_does_not_exist_yet";
ucmd.args(&["-r", FILE1, filename])
.succeeds()
.no_stdout()
.no_stderr();
assert!(at.file_exists(filename));
assert_eq!(at.read_bytes(filename), vec![b'\0'; 10]);
}
/// Test that truncating a non-existent file creates that file, even in size-and-reference-mode.
#[test]
fn test_new_file_size_and_reference() {
let (at, mut ucmd) = at_and_ucmd!();
let mut old_file = at.make_file(FILE1);
old_file.write_all(b"1234567890").unwrap();
let filename = "new_file_that_does_not_exist_yet";
ucmd.args(&["-s", "+3", "-r", FILE1, filename])
.succeeds()
.no_stdout()
.no_stderr();
assert!(at.file_exists(filename));
assert_eq!(at.read_bytes(filename), vec![b'\0'; 13]);
}
/// Test for not creating a non-existent file.
#[test]
fn test_new_file_no_create() {
fn test_new_file_no_create_size_only() {
let (at, mut ucmd) = at_and_ucmd!();
let filename = "new_file_that_does_not_exist_yet";
ucmd.args(&["-s", "8", "-c", filename])
@ -282,6 +308,34 @@ fn test_new_file_no_create() {
assert!(!at.file_exists(filename));
}
/// Test for not creating a non-existent file.
#[test]
fn test_new_file_no_create_reference_only() {
let (at, mut ucmd) = at_and_ucmd!();
let mut old_file = at.make_file(FILE1);
old_file.write_all(b"1234567890").unwrap();
let filename = "new_file_that_does_not_exist_yet";
ucmd.args(&["-r", FILE1, "-c", filename])
.succeeds()
.no_stdout()
.no_stderr();
assert!(!at.file_exists(filename));
}
/// Test for not creating a non-existent file.
#[test]
fn test_new_file_no_create_size_and_reference() {
let (at, mut ucmd) = at_and_ucmd!();
let mut old_file = at.make_file(FILE1);
old_file.write_all(b"1234567890").unwrap();
let filename = "new_file_that_does_not_exist_yet";
ucmd.args(&["-r", FILE1, "-s", "+8", "-c", filename])
.succeeds()
.no_stdout()
.no_stderr();
assert!(!at.file_exists(filename));
}
#[test]
fn test_division_by_zero_size_only() {
new_ucmd!()