2020-05-25 17:05:26 +00:00
|
|
|
|
use crate::common::util::*;
|
2016-05-22 07:46:54 +00:00
|
|
|
|
use std::io::{Seek, SeekFrom, Write};
|
2015-11-16 05:25:01 +00:00
|
|
|
|
|
2021-05-30 05:10:54 +00:00
|
|
|
|
static FILE1: &str = "truncate_test_1";
|
|
|
|
|
static FILE2: &str = "truncate_test_2";
|
2015-11-16 05:25:01 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_increase_file_size() {
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let expected = 5 * 1024;
|
2016-08-23 11:52:43 +00:00
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
let mut file = at.make_file(FILE1);
|
|
|
|
|
ucmd.args(&["-s", "+5K", FILE1]).succeeds();
|
2015-11-16 05:25:01 +00:00
|
|
|
|
|
|
|
|
|
file.seek(SeekFrom::End(0)).unwrap();
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let actual = file.seek(SeekFrom::Current(0)).unwrap();
|
2021-04-27 03:14:11 +00:00
|
|
|
|
assert!(
|
|
|
|
|
expected == actual,
|
|
|
|
|
"expected '{}' got '{}'",
|
|
|
|
|
expected,
|
|
|
|
|
actual
|
|
|
|
|
);
|
2015-11-16 05:25:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 22:33:24 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_increase_file_size_kb() {
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let expected = 5 * 1000;
|
2020-10-22 22:33:24 +00:00
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
let mut file = at.make_file(FILE1);
|
|
|
|
|
ucmd.args(&["-s", "+5KB", FILE1]).succeeds();
|
2020-10-22 22:33:24 +00:00
|
|
|
|
|
|
|
|
|
file.seek(SeekFrom::End(0)).unwrap();
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let actual = file.seek(SeekFrom::Current(0)).unwrap();
|
2021-04-27 03:14:11 +00:00
|
|
|
|
assert!(
|
|
|
|
|
expected == actual,
|
|
|
|
|
"expected '{}' got '{}'",
|
|
|
|
|
expected,
|
|
|
|
|
actual
|
|
|
|
|
);
|
2020-10-22 22:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_reference() {
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let expected = 5 * 1000;
|
2020-10-22 22:33:24 +00:00
|
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
|
let at = &scene.fixtures;
|
2021-05-30 05:10:54 +00:00
|
|
|
|
let mut file = at.make_file(FILE2);
|
2020-10-22 22:33:24 +00:00
|
|
|
|
|
2021-05-30 05:10:54 +00:00
|
|
|
|
scene.ucmd().arg("-s").arg("+5KB").arg(FILE1).run();
|
2020-10-22 22:33:24 +00:00
|
|
|
|
|
2021-05-31 03:55:28 +00:00
|
|
|
|
scene.ucmd().arg("--reference").arg(FILE1).arg(FILE2).run();
|
2020-10-22 22:33:24 +00:00
|
|
|
|
|
|
|
|
|
file.seek(SeekFrom::End(0)).unwrap();
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let actual = file.seek(SeekFrom::Current(0)).unwrap();
|
2021-04-27 03:14:11 +00:00
|
|
|
|
assert!(
|
|
|
|
|
expected == actual,
|
|
|
|
|
"expected '{}' got '{}'",
|
|
|
|
|
expected,
|
|
|
|
|
actual
|
|
|
|
|
);
|
2020-10-22 22:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-16 05:25:01 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_decrease_file_size() {
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let expected = 6;
|
2016-08-23 11:52:43 +00:00
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
let mut file = at.make_file(FILE2);
|
2015-11-16 05:25:01 +00:00
|
|
|
|
file.write_all(b"1234567890").unwrap();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
ucmd.args(&["--size=-4", FILE2]).succeeds();
|
2015-11-16 05:25:01 +00:00
|
|
|
|
file.seek(SeekFrom::End(0)).unwrap();
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let actual = file.seek(SeekFrom::Current(0)).unwrap();
|
2021-04-27 03:14:11 +00:00
|
|
|
|
assert!(
|
|
|
|
|
expected == actual,
|
|
|
|
|
"expected '{}' got '{}'",
|
|
|
|
|
expected,
|
|
|
|
|
actual
|
|
|
|
|
);
|
2015-11-16 05:25:01 +00:00
|
|
|
|
}
|
2020-10-22 22:33:24 +00:00
|
|
|
|
|
2021-04-23 15:36:46 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_space_in_size() {
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let expected = 4;
|
2021-04-23 15:36:46 +00:00
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
let mut file = at.make_file(FILE2);
|
2021-04-23 15:36:46 +00:00
|
|
|
|
file.write_all(b"1234567890").unwrap();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
ucmd.args(&["--size", " 4", FILE2]).succeeds();
|
2021-04-23 15:36:46 +00:00
|
|
|
|
file.seek(SeekFrom::End(0)).unwrap();
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let actual = file.seek(SeekFrom::Current(0)).unwrap();
|
2021-04-27 03:14:11 +00:00
|
|
|
|
assert!(
|
|
|
|
|
expected == actual,
|
|
|
|
|
"expected '{}' got '{}'",
|
|
|
|
|
expected,
|
|
|
|
|
actual
|
|
|
|
|
);
|
2021-04-23 15:36:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 22:33:24 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_failed() {
|
|
|
|
|
new_ucmd!().fails();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_failed_2() {
|
|
|
|
|
let (_at, mut ucmd) = at_and_ucmd!();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
ucmd.args(&[FILE1]).fails();
|
2020-10-22 22:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_failed_incorrect_arg() {
|
|
|
|
|
let (_at, mut ucmd) = at_and_ucmd!();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
ucmd.args(&["-s", "+5A", FILE1]).fails();
|
2020-10-22 22:33:24 +00:00
|
|
|
|
}
|
2021-04-24 14:25:14 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_at_most_shrinks() {
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let expected = 4;
|
2021-04-24 14:25:14 +00:00
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
let mut file = at.make_file(FILE2);
|
2021-04-24 14:25:14 +00:00
|
|
|
|
file.write_all(b"1234567890").unwrap();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
ucmd.args(&["--size", "<4", FILE2]).succeeds();
|
2021-04-24 14:25:14 +00:00
|
|
|
|
file.seek(SeekFrom::End(0)).unwrap();
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let actual = file.seek(SeekFrom::Current(0)).unwrap();
|
2021-04-27 03:14:11 +00:00
|
|
|
|
assert!(
|
|
|
|
|
expected == actual,
|
|
|
|
|
"expected '{}' got '{}'",
|
|
|
|
|
expected,
|
|
|
|
|
actual
|
|
|
|
|
);
|
2021-04-24 14:25:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_at_most_no_change() {
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let expected = 10;
|
2021-04-24 14:25:14 +00:00
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
let mut file = at.make_file(FILE2);
|
2021-04-24 14:25:14 +00:00
|
|
|
|
file.write_all(b"1234567890").unwrap();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
ucmd.args(&["--size", "<40", FILE2]).succeeds();
|
2021-04-24 14:25:14 +00:00
|
|
|
|
file.seek(SeekFrom::End(0)).unwrap();
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let actual = file.seek(SeekFrom::Current(0)).unwrap();
|
2021-04-27 03:14:11 +00:00
|
|
|
|
assert!(
|
|
|
|
|
expected == actual,
|
|
|
|
|
"expected '{}' got '{}'",
|
|
|
|
|
expected,
|
|
|
|
|
actual
|
|
|
|
|
);
|
2021-04-24 14:25:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_at_least_grows() {
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let expected = 15;
|
2021-04-24 14:25:14 +00:00
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
let mut file = at.make_file(FILE2);
|
2021-04-24 14:25:14 +00:00
|
|
|
|
file.write_all(b"1234567890").unwrap();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
ucmd.args(&["--size", ">15", FILE2]).succeeds();
|
2021-04-24 14:25:14 +00:00
|
|
|
|
file.seek(SeekFrom::End(0)).unwrap();
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let actual = file.seek(SeekFrom::Current(0)).unwrap();
|
2021-04-27 03:14:11 +00:00
|
|
|
|
assert!(
|
|
|
|
|
expected == actual,
|
|
|
|
|
"expected '{}' got '{}'",
|
|
|
|
|
expected,
|
|
|
|
|
actual
|
|
|
|
|
);
|
2021-04-24 14:25:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_at_least_no_change() {
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let expected = 10;
|
2021-04-24 14:25:14 +00:00
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
let mut file = at.make_file(FILE2);
|
2021-04-24 14:25:14 +00:00
|
|
|
|
file.write_all(b"1234567890").unwrap();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
ucmd.args(&["--size", ">4", FILE2]).succeeds();
|
2021-04-24 14:25:14 +00:00
|
|
|
|
file.seek(SeekFrom::End(0)).unwrap();
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let actual = file.seek(SeekFrom::Current(0)).unwrap();
|
2021-04-27 03:14:11 +00:00
|
|
|
|
assert!(
|
|
|
|
|
expected == actual,
|
|
|
|
|
"expected '{}' got '{}'",
|
|
|
|
|
expected,
|
|
|
|
|
actual
|
|
|
|
|
);
|
2021-04-24 14:25:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_round_down() {
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let expected = 8;
|
2021-04-24 14:25:14 +00:00
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
let mut file = at.make_file(FILE2);
|
2021-04-24 14:25:14 +00:00
|
|
|
|
file.write_all(b"1234567890").unwrap();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
ucmd.args(&["--size", "/4", FILE2]).succeeds();
|
2021-04-24 14:25:14 +00:00
|
|
|
|
file.seek(SeekFrom::End(0)).unwrap();
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let actual = file.seek(SeekFrom::Current(0)).unwrap();
|
2021-04-27 03:14:11 +00:00
|
|
|
|
assert!(
|
|
|
|
|
expected == actual,
|
|
|
|
|
"expected '{}' got '{}'",
|
|
|
|
|
expected,
|
|
|
|
|
actual
|
|
|
|
|
);
|
2021-04-24 14:25:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_round_up() {
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let expected = 12;
|
2021-04-24 14:25:14 +00:00
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
let mut file = at.make_file(FILE2);
|
2021-04-24 14:25:14 +00:00
|
|
|
|
file.write_all(b"1234567890").unwrap();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
ucmd.args(&["--size", "%4", FILE2]).succeeds();
|
2021-04-24 14:25:14 +00:00
|
|
|
|
file.seek(SeekFrom::End(0)).unwrap();
|
2021-04-25 12:46:57 +00:00
|
|
|
|
let actual = file.seek(SeekFrom::Current(0)).unwrap();
|
2021-04-27 03:14:11 +00:00
|
|
|
|
assert!(
|
|
|
|
|
expected == actual,
|
|
|
|
|
"expected '{}' got '{}'",
|
|
|
|
|
expected,
|
|
|
|
|
actual
|
|
|
|
|
);
|
2021-04-24 14:25:14 +00:00
|
|
|
|
}
|
2021-04-26 17:39:32 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_size_and_reference() {
|
|
|
|
|
let expected = 15;
|
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
let mut file1 = at.make_file(FILE1);
|
|
|
|
|
let mut file2 = at.make_file(FILE2);
|
2021-04-26 17:39:32 +00:00
|
|
|
|
file1.write_all(b"1234567890").unwrap();
|
2021-05-30 05:10:54 +00:00
|
|
|
|
ucmd.args(&["--reference", FILE1, "--size", "+5", FILE2])
|
2021-05-01 11:12:10 +00:00
|
|
|
|
.succeeds();
|
2021-04-26 17:39:32 +00:00
|
|
|
|
file2.seek(SeekFrom::End(0)).unwrap();
|
|
|
|
|
let actual = file2.seek(SeekFrom::Current(0)).unwrap();
|
2021-05-01 11:12:10 +00:00
|
|
|
|
assert!(
|
|
|
|
|
expected == actual,
|
|
|
|
|
"expected '{}' got '{}'",
|
|
|
|
|
expected,
|
|
|
|
|
actual
|
|
|
|
|
);
|
2021-04-26 17:39:32 +00:00
|
|
|
|
}
|
2021-05-20 02:23:28 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_numbers() {
|
|
|
|
|
// TODO For compatibility with GNU, `truncate -s 0X` should cause
|
|
|
|
|
// the same error as `truncate -s 0X file`, but currently it returns
|
|
|
|
|
// a different error.
|
2021-05-21 00:57:28 +00:00
|
|
|
|
new_ucmd!()
|
|
|
|
|
.args(&["-s", "0X", "file"])
|
|
|
|
|
.fails()
|
|
|
|
|
.stderr_contains("Invalid number: ‘0X’");
|
|
|
|
|
new_ucmd!()
|
|
|
|
|
.args(&["-s", "0XB", "file"])
|
|
|
|
|
.fails()
|
|
|
|
|
.stderr_contains("Invalid number: ‘0XB’");
|
|
|
|
|
new_ucmd!()
|
|
|
|
|
.args(&["-s", "0B", "file"])
|
|
|
|
|
.fails()
|
|
|
|
|
.stderr_contains("Invalid number: ‘0B’");
|
2021-05-20 02:23:28 +00:00
|
|
|
|
}
|
2021-05-21 00:55:11 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_reference_file_not_found() {
|
|
|
|
|
new_ucmd!()
|
|
|
|
|
.args(&["-r", "a", "b"])
|
|
|
|
|
.fails()
|
|
|
|
|
.stderr_contains("cannot stat 'a': No such file or directory");
|
|
|
|
|
}
|
2021-05-21 22:20:05 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_reference_with_size_file_not_found() {
|
|
|
|
|
new_ucmd!()
|
|
|
|
|
.args(&["-r", "a", "-s", "+1", "b"])
|
|
|
|
|
.fails()
|
|
|
|
|
.stderr_contains("cannot stat 'a': No such file or directory");
|
|
|
|
|
}
|