| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
// spell-checker:ignore (words) RFILE |
| 7 |
|
| 8 |
use std::io::{Seek, SeekFrom, Write}; |
| 9 |
#[cfg(unix)] |
| 10 |
use std::os::unix::fs::MetadataExt; |
| 11 |
use uutests::util::TestScenario; |
| 12 |
use uutests::{at_and_ucmd, new_ucmd, util_name}; |
| 13 |
|
| 14 |
static FILE1: &str = "truncate_test_1"; |
| 15 |
static FILE2: &str = "truncate_test_2"; |
| 16 |
|
| 17 |
#[test] |
| 18 |
fn test_increase_file_size() { |
| 19 |
let expected = 5 * 1024; |
| 20 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 21 |
let mut file = at.make_file(FILE1); |
| 22 |
ucmd.args(&["-s", "+5K", FILE1]).succeeds(); |
| 23 |
|
| 24 |
file.seek(SeekFrom::End(0)).unwrap(); |
| 25 |
let actual = file.stream_position().unwrap(); |
| 26 |
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'"); |
| 27 |
} |
| 28 |
|
| 29 |
#[test] |
| 30 |
fn test_increase_file_size_kb() { |
| 31 |
let expected = 5 * 1000; |
| 32 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 33 |
let mut file = at.make_file(FILE1); |
| 34 |
ucmd.args(&["-s", "+5KB", FILE1]).succeeds(); |
| 35 |
|
| 36 |
file.seek(SeekFrom::End(0)).unwrap(); |
| 37 |
let actual = file.stream_position().unwrap(); |
| 38 |
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'"); |
| 39 |
} |
| 40 |
|
| 41 |
#[test] |
| 42 |
fn test_size_above_i64_max_is_rejected_without_creating_file() { |
| 43 |
// A size above i64::MAX is rejected up front and the file is not created. |
| 44 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 45 |
ucmd.args(&["-s", "9223372036854775808", "new-file"]) |
| 46 |
.fails_with_code(1) |
| 47 |
.stderr_contains("Value too large for defined data type"); |
| 48 |
assert!(!at.file_exists("new-file")); |
| 49 |
} |
| 50 |
|
| 51 |
#[test] |
| 52 |
fn test_reference() { |
| 53 |
let expected = 5 * 1000; |
| 54 |
let scene = TestScenario::new(util_name!()); |
| 55 |
let at = &scene.fixtures; |
| 56 |
let mut file = at.make_file(FILE2); |
| 57 |
|
| 58 |
// manpage: "A FILE argument that does not exist is created." |
| 59 |
scene.ucmd().arg("-s").arg("+5KB").arg(FILE1).succeeds(); |
| 60 |
|
| 61 |
scene |
| 62 |
.ucmd() |
| 63 |
.arg("--reference") |
| 64 |
.arg(FILE1) |
| 65 |
.arg(FILE2) |
| 66 |
.succeeds(); |
| 67 |
|
| 68 |
file.seek(SeekFrom::End(0)).unwrap(); |
| 69 |
let actual = file.stream_position().unwrap(); |
| 70 |
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'"); |
| 71 |
} |
| 72 |
|
| 73 |
#[test] |
| 74 |
#[cfg(target_os = "linux")] |
| 75 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")] |
| 76 |
fn test_reference_non_utf8_path() { |
| 77 |
use std::os::unix::ffi::OsStrExt; |
| 78 |
|
| 79 |
let expected = 5 * 1000; |
| 80 |
let scene = TestScenario::new(util_name!()); |
| 81 |
let at = &scene.fixtures; |
| 82 |
let mut file = at.make_file(FILE2); |
| 83 |
|
| 84 |
let reference = std::ffi::OsStr::from_bytes(b"test_\xFF\xFE.txt"); |
| 85 |
scene.ucmd().arg("-s").arg("+5KB").arg(reference).succeeds(); |
| 86 |
|
| 87 |
scene |
| 88 |
.ucmd() |
| 89 |
.arg("--reference") |
| 90 |
.arg(reference) |
| 91 |
.arg(FILE2) |
| 92 |
.succeeds(); |
| 93 |
|
| 94 |
file.seek(SeekFrom::End(0)).unwrap(); |
| 95 |
let actual = file.stream_position().unwrap(); |
| 96 |
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'"); |
| 97 |
} |
| 98 |
|
| 99 |
#[test] |
| 100 |
fn test_decrease_file_size() { |
| 101 |
let expected = 6; |
| 102 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 103 |
let mut file = at.make_file(FILE2); |
| 104 |
file.write_all(b"1234567890").unwrap(); |
| 105 |
ucmd.args(&["--size=-4", FILE2]).succeeds(); |
| 106 |
file.seek(SeekFrom::End(0)).unwrap(); |
| 107 |
let actual = file.stream_position().unwrap(); |
| 108 |
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'"); |
| 109 |
} |
| 110 |
|
| 111 |
#[test] |
| 112 |
fn test_space_in_size() { |
| 113 |
let expected = 4; |
| 114 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 115 |
let mut file = at.make_file(FILE2); |
| 116 |
file.write_all(b"1234567890").unwrap(); |
| 117 |
ucmd.args(&["--size", " 4", FILE2]).succeeds(); |
| 118 |
file.seek(SeekFrom::End(0)).unwrap(); |
| 119 |
let actual = file.stream_position().unwrap(); |
| 120 |
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'"); |
| 121 |
} |
| 122 |
|
| 123 |
#[test] |
| 124 |
fn test_failed() { |
| 125 |
new_ucmd!().fails(); |
| 126 |
} |
| 127 |
|
| 128 |
#[test] |
| 129 |
fn test_failed_incorrect_arg() { |
| 130 |
let (_at, mut ucmd) = at_and_ucmd!(); |
| 131 |
ucmd.args(&["-s", "+5A", FILE1]).fails(); |
| 132 |
} |
| 133 |
|
| 134 |
#[test] |
| 135 |
fn test_at_most_shrinks() { |
| 136 |
let expected = 4; |
| 137 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 138 |
let mut file = at.make_file(FILE2); |
| 139 |
file.write_all(b"1234567890").unwrap(); |
| 140 |
ucmd.args(&["--size", "<4", FILE2]).succeeds(); |
| 141 |
file.seek(SeekFrom::End(0)).unwrap(); |
| 142 |
let actual = file.stream_position().unwrap(); |
| 143 |
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'"); |
| 144 |
} |
| 145 |
|
| 146 |
#[test] |
| 147 |
fn test_at_most_no_change() { |
| 148 |
let expected = 10; |
| 149 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 150 |
let mut file = at.make_file(FILE2); |
| 151 |
file.write_all(b"1234567890").unwrap(); |
| 152 |
ucmd.args(&["--size", "<40", FILE2]).succeeds(); |
| 153 |
file.seek(SeekFrom::End(0)).unwrap(); |
| 154 |
let actual = file.stream_position().unwrap(); |
| 155 |
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'"); |
| 156 |
} |
| 157 |
|
| 158 |
#[test] |
| 159 |
fn test_at_least_grows() { |
| 160 |
let expected = 15; |
| 161 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 162 |
let mut file = at.make_file(FILE2); |
| 163 |
file.write_all(b"1234567890").unwrap(); |
| 164 |
ucmd.args(&["--size", ">15", FILE2]).succeeds(); |
| 165 |
file.seek(SeekFrom::End(0)).unwrap(); |
| 166 |
let actual = file.stream_position().unwrap(); |
| 167 |
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'"); |
| 168 |
} |
| 169 |
|
| 170 |
#[test] |
| 171 |
fn test_at_least_no_change() { |
| 172 |
let expected = 10; |
| 173 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 174 |
let mut file = at.make_file(FILE2); |
| 175 |
file.write_all(b"1234567890").unwrap(); |
| 176 |
ucmd.args(&["--size", ">4", FILE2]).succeeds(); |
| 177 |
file.seek(SeekFrom::End(0)).unwrap(); |
| 178 |
let actual = file.stream_position().unwrap(); |
| 179 |
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'"); |
| 180 |
} |
| 181 |
|
| 182 |
#[test] |
| 183 |
fn test_round_down() { |
| 184 |
let expected = 8; |
| 185 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 186 |
let mut file = at.make_file(FILE2); |
| 187 |
file.write_all(b"1234567890").unwrap(); |
| 188 |
ucmd.args(&["--size", "/4", FILE2]).succeeds(); |
| 189 |
file.seek(SeekFrom::End(0)).unwrap(); |
| 190 |
let actual = file.stream_position().unwrap(); |
| 191 |
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'"); |
| 192 |
} |
| 193 |
|
| 194 |
#[test] |
| 195 |
fn test_round_up() { |
| 196 |
let expected = 12; |
| 197 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 198 |
let mut file = at.make_file(FILE2); |
| 199 |
file.write_all(b"1234567890").unwrap(); |
| 200 |
ucmd.args(&["--size", "%4", FILE2]).succeeds(); |
| 201 |
file.seek(SeekFrom::End(0)).unwrap(); |
| 202 |
let actual = file.stream_position().unwrap(); |
| 203 |
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'"); |
| 204 |
} |
| 205 |
|
| 206 |
#[test] |
| 207 |
fn test_round_up_file_smaller_than_size() { |
| 208 |
let expected = 4096; |
| 209 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 210 |
let mut file = at.make_file(FILE2); |
| 211 |
file.write_all(b"1234567890").unwrap(); |
| 212 |
ucmd.args(&["--size", "%4K", FILE2]).succeeds(); |
| 213 |
file.seek(SeekFrom::End(0)).unwrap(); |
| 214 |
let actual = file.stream_position().unwrap(); |
| 215 |
assert_eq!(expected, actual); |
| 216 |
} |
| 217 |
|
| 218 |
#[test] |
| 219 |
fn test_round_up_unaligned() { |
| 220 |
let expected = 16; |
| 221 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 222 |
let mut file = at.make_file(FILE2); |
| 223 |
file.write_all(b"1234567890123").unwrap(); |
| 224 |
ucmd.args(&["--size", "%8", FILE2]).succeeds(); |
| 225 |
file.seek(SeekFrom::End(0)).unwrap(); |
| 226 |
let actual = file.stream_position().unwrap(); |
| 227 |
assert_eq!(expected, actual); |
| 228 |
} |
| 229 |
|
| 230 |
#[test] |
| 231 |
fn test_round_up_already_aligned() { |
| 232 |
let expected = 8; |
| 233 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 234 |
let mut file = at.make_file(FILE2); |
| 235 |
file.write_all(b"12345678").unwrap(); |
| 236 |
ucmd.args(&["--size", "%4", FILE2]).succeeds(); |
| 237 |
file.seek(SeekFrom::End(0)).unwrap(); |
| 238 |
let actual = file.stream_position().unwrap(); |
| 239 |
assert_eq!(expected, actual); |
| 240 |
} |
| 241 |
|
| 242 |
#[test] |
| 243 |
fn test_size_and_reference() { |
| 244 |
let expected = 15; |
| 245 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 246 |
let mut file1 = at.make_file(FILE1); |
| 247 |
let mut file2 = at.make_file(FILE2); |
| 248 |
file1.write_all(b"1234567890").unwrap(); |
| 249 |
ucmd.args(&["--reference", FILE1, "--size", "+5", FILE2]) |
| 250 |
.succeeds(); |
| 251 |
file2.seek(SeekFrom::End(0)).unwrap(); |
| 252 |
let actual = file2.stream_position().unwrap(); |
| 253 |
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'"); |
| 254 |
} |
| 255 |
|
| 256 |
#[test] |
| 257 |
fn test_error_filename_only() { |
| 258 |
// truncate: you must specify either '--size' or '--reference' |
| 259 |
new_ucmd!() |
| 260 |
.args(&["file"]) |
| 261 |
.fails_with_code(1) |
| 262 |
.stderr_contains("error: the following required arguments were not provided:"); |
| 263 |
} |
| 264 |
|
| 265 |
#[test] |
| 266 |
fn test_invalid_option() { |
| 267 |
// truncate: cli parsing error returns 1 |
| 268 |
new_ucmd!() |
| 269 |
.args(&["--this-arg-does-not-exist"]) |
| 270 |
.fails_with_code(1); |
| 271 |
} |
| 272 |
|
| 273 |
#[test] |
| 274 |
fn test_invalid_numbers() { |
| 275 |
new_ucmd!() |
| 276 |
.args(&["-s", "0X", "file"]) |
| 277 |
.fails() |
| 278 |
.stderr_contains("Invalid number: '0X'"); |
| 279 |
new_ucmd!() |
| 280 |
.args(&["-s", "0XB", "file"]) |
| 281 |
.fails() |
| 282 |
.stderr_contains("Invalid number: '0XB'"); |
| 283 |
new_ucmd!() |
| 284 |
.args(&["-s", "0B", "file"]) |
| 285 |
.fails() |
| 286 |
.stderr_contains("Invalid number: '0B'"); |
| 287 |
new_ucmd!() |
| 288 |
.args(&["-s", "1b", "file"]) |
| 289 |
.fails() |
| 290 |
.stderr_contains("Invalid number: '1b'"); |
| 291 |
} |
| 292 |
|
| 293 |
#[test] |
| 294 |
fn test_reference_file_not_found() { |
| 295 |
new_ucmd!() |
| 296 |
.args(&["-r", "a", "b"]) |
| 297 |
.fails() |
| 298 |
.stderr_contains("cannot stat 'a': No such file or directory"); |
| 299 |
} |
| 300 |
|
| 301 |
#[test] |
| 302 |
fn test_reference_with_size_file_not_found() { |
| 303 |
new_ucmd!() |
| 304 |
.args(&["-r", "a", "-s", "+1", "b"]) |
| 305 |
.fails() |
| 306 |
.stderr_contains("cannot stat 'a': No such file or directory"); |
| 307 |
} |
| 308 |
|
| 309 |
#[test] |
| 310 |
fn test_truncate_bytes_size() { |
| 311 |
new_ucmd!() |
| 312 |
.args(&["--no-create", "--size", "K", "file"]) |
| 313 |
.succeeds(); |
| 314 |
new_ucmd!() |
| 315 |
.args(&["--size", "1024R", "file"]) |
| 316 |
.fails_with_code(1) |
| 317 |
.stderr_only("truncate: Invalid number: '1024R': Value too large for defined data type\n"); |
| 318 |
new_ucmd!() |
| 319 |
.args(&["--size", "1Y", "file"]) |
| 320 |
.fails_with_code(1) |
| 321 |
.stderr_only("truncate: Invalid number: '1Y': Value too large for defined data type\n"); |
| 322 |
} |
| 323 |
|
| 324 |
#[test] |
| 325 |
fn test_relative_size_overflow_preserves_file() { |
| 326 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 327 |
at.write(FILE1, "x"); |
| 328 |
|
| 329 |
ucmd.args(&["--size=+18446744073709551615", FILE1]) |
| 330 |
.fails_with_code(1) |
| 331 |
.stderr_contains("Value too large for defined data type"); |
| 332 |
|
| 333 |
assert_eq!(at.read(FILE1), "x"); |
| 334 |
} |
| 335 |
|
| 336 |
#[cfg(unix)] |
| 337 |
#[test] |
| 338 |
#[cfg_attr(wasi_runner, ignore = "WASI: no block size support")] |
| 339 |
fn test_io_blocks_uses_file_block_size() { |
| 340 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 341 |
at.write(FILE1, "x"); |
| 342 |
let block_size = at.metadata(FILE1).blksize(); |
| 343 |
|
| 344 |
ucmd.args(&["--io-blocks", "--size=1", FILE1]) |
| 345 |
.succeeds() |
| 346 |
.no_output(); |
| 347 |
|
| 348 |
assert_eq!(at.metadata(FILE1).len(), block_size); |
| 349 |
} |
| 350 |
|
| 351 |
#[cfg(unix)] |
| 352 |
#[test] |
| 353 |
#[cfg_attr(wasi_runner, ignore = "WASI: no block size support")] |
| 354 |
fn test_io_blocks_uses_parent_block_size_for_new_file() { |
| 355 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 356 |
let block_size = at.metadata(".").blksize(); |
| 357 |
|
| 358 |
ucmd.args(&["--io-blocks", "--size=2", FILE1]) |
| 359 |
.succeeds() |
| 360 |
.no_output(); |
| 361 |
|
| 362 |
assert_eq!(at.metadata(FILE1).len(), 2 * block_size); |
| 363 |
} |
| 364 |
|
| 365 |
/// Test that truncating a non-existent file creates that file. |
| 366 |
#[test] |
| 367 |
fn test_new_file() { |
| 368 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 369 |
let filename = "new_file_that_does_not_exist_yet"; |
| 370 |
ucmd.args(&["-s", "8", filename]).succeeds().no_output(); |
| 371 |
assert!(at.file_exists(filename)); |
| 372 |
assert_eq!(at.read_bytes(filename), vec![b'\0'; 8]); |
| 373 |
} |
| 374 |
|
| 375 |
/// Test that truncating a non-existent file creates that file, even in reference-mode. |
| 376 |
#[test] |
| 377 |
fn test_new_file_reference() { |
| 378 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 379 |
let mut old_file = at.make_file(FILE1); |
| 380 |
old_file.write_all(b"1234567890").unwrap(); |
| 381 |
let filename = "new_file_that_does_not_exist_yet"; |
| 382 |
ucmd.args(&["-r", FILE1, filename]).succeeds().no_output(); |
| 383 |
assert!(at.file_exists(filename)); |
| 384 |
assert_eq!(at.read_bytes(filename), vec![b'\0'; 10]); |
| 385 |
} |
| 386 |
|
| 387 |
/// Test that truncating a non-existent file creates that file, even in size-and-reference-mode. |
| 388 |
#[test] |
| 389 |
fn test_new_file_size_and_reference() { |
| 390 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 391 |
let mut old_file = at.make_file(FILE1); |
| 392 |
old_file.write_all(b"1234567890").unwrap(); |
| 393 |
let filename = "new_file_that_does_not_exist_yet"; |
| 394 |
ucmd.args(&["-s", "+3", "-r", FILE1, filename]) |
| 395 |
.succeeds() |
| 396 |
.no_output(); |
| 397 |
assert!(at.file_exists(filename)); |
| 398 |
assert_eq!(at.read_bytes(filename), vec![b'\0'; 13]); |
| 399 |
} |
| 400 |
|
| 401 |
/// Test for not creating a non-existent file. |
| 402 |
#[test] |
| 403 |
fn test_new_file_no_create_size_only() { |
| 404 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 405 |
let filename = "new_file_that_does_not_exist_yet"; |
| 406 |
ucmd.args(&["-s", "8", "-c", filename]) |
| 407 |
.succeeds() |
| 408 |
.no_output(); |
| 409 |
assert!(!at.file_exists(filename)); |
| 410 |
} |
| 411 |
|
| 412 |
/// Test for not creating a non-existent file. |
| 413 |
#[test] |
| 414 |
fn test_new_file_no_create_reference_only() { |
| 415 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 416 |
let mut old_file = at.make_file(FILE1); |
| 417 |
old_file.write_all(b"1234567890").unwrap(); |
| 418 |
let filename = "new_file_that_does_not_exist_yet"; |
| 419 |
ucmd.args(&["-r", FILE1, "-c", filename]) |
| 420 |
.succeeds() |
| 421 |
.no_output(); |
| 422 |
assert!(!at.file_exists(filename)); |
| 423 |
} |
| 424 |
|
| 425 |
/// Test for not creating a non-existent file. |
| 426 |
#[test] |
| 427 |
fn test_new_file_no_create_size_and_reference() { |
| 428 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 429 |
let mut old_file = at.make_file(FILE1); |
| 430 |
old_file.write_all(b"1234567890").unwrap(); |
| 431 |
let filename = "new_file_that_does_not_exist_yet"; |
| 432 |
ucmd.args(&["-r", FILE1, "-s", "+8", "-c", filename]) |
| 433 |
.succeeds() |
| 434 |
.no_output(); |
| 435 |
assert!(!at.file_exists(filename)); |
| 436 |
} |
| 437 |
|
| 438 |
#[test] |
| 439 |
fn test_division_by_zero_size_only() { |
| 440 |
new_ucmd!() |
| 441 |
.args(&["-s", "/0", "file"]) |
| 442 |
.fails() |
| 443 |
.no_stdout() |
| 444 |
.stderr_contains("division by zero"); |
| 445 |
new_ucmd!() |
| 446 |
.args(&["-s", "%0", "file"]) |
| 447 |
.fails() |
| 448 |
.no_stdout() |
| 449 |
.stderr_contains("division by zero"); |
| 450 |
} |
| 451 |
|
| 452 |
#[test] |
| 453 |
fn test_division_by_zero_reference_and_size() { |
| 454 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 455 |
at.make_file(FILE1); |
| 456 |
ucmd.args(&["-r", FILE1, "-s", "/0", "file"]) |
| 457 |
.fails() |
| 458 |
.no_stdout() |
| 459 |
.stderr_contains("division by zero"); |
| 460 |
|
| 461 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 462 |
at.make_file(FILE1); |
| 463 |
ucmd.args(&["-r", FILE1, "-s", "%0", "file"]) |
| 464 |
.fails() |
| 465 |
.no_stdout() |
| 466 |
.stderr_contains("division by zero"); |
| 467 |
} |
| 468 |
|
| 469 |
#[test] |
| 470 |
fn test_no_such_dir() { |
| 471 |
new_ucmd!() |
| 472 |
.args(&["-s", "0", "a/b"]) |
| 473 |
.fails() |
| 474 |
.no_stdout() |
| 475 |
.stderr_contains("cannot open 'a/b' for writing: No such file or directory"); |
| 476 |
} |
| 477 |
|
| 478 |
/// Test that truncate processes every file even if one fails. |
| 479 |
#[test] |
| 480 |
fn test_continue_after_error() { |
| 481 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 482 |
at.mkdir("dir"); |
| 483 |
ucmd.args(&["-s", "0", "a", "dir", "b"]) |
| 484 |
.fails() |
| 485 |
.no_stdout() |
| 486 |
.stderr_contains("dir"); |
| 487 |
assert!(at.file_exists("a")); |
| 488 |
assert!(at.file_exists("b")); |
| 489 |
} |
| 490 |
|
| 491 |
/// Test that truncate with a relative size less than 0 is not an error. |
| 492 |
#[test] |
| 493 |
fn test_underflow_relative_size() { |
| 494 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 495 |
ucmd.args(&["-s-1", FILE1]).succeeds().no_output(); |
| 496 |
assert!(at.file_exists(FILE1)); |
| 497 |
assert!(at.read_bytes(FILE1).is_empty()); |
| 498 |
} |
| 499 |
|
| 500 |
#[test] |
| 501 |
fn test_negative_size_with_space() { |
| 502 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 503 |
ucmd.args(&["-s", "-1", FILE1]).succeeds().no_output(); |
| 504 |
assert!(at.file_exists(FILE1)); |
| 505 |
assert!(at.read_bytes(FILE1).is_empty()); |
| 506 |
} |
| 507 |
|
| 508 |
#[test] |
| 509 |
#[cfg(target_os = "linux")] |
| 510 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")] |
| 511 |
fn test_truncate_non_utf8_paths() { |
| 512 |
use std::os::unix::ffi::OsStrExt; |
| 513 |
let ts = TestScenario::new(util_name!()); |
| 514 |
let at = &ts.fixtures; |
| 515 |
let file_name = std::ffi::OsStr::from_bytes(b"test_\xFF\xFE.txt"); |
| 516 |
at.write(&file_name.to_string_lossy(), "test content"); |
| 517 |
|
| 518 |
// Test that truncate can handle non-UTF-8 filenames |
| 519 |
ts.ucmd().arg("-s").arg("10").arg(file_name).succeeds(); |
| 520 |
} |
| 521 |
|
| 522 |
#[test] |
| 523 |
fn test_empty_size() { |
| 524 |
new_ucmd!() |
| 525 |
.args(&["-s", "", "asd"]) |
| 526 |
.fails() |
| 527 |
.stderr_is("truncate: Invalid number: ''\n"); |
| 528 |
} |
| 529 |
|
| 530 |
#[test] |
| 531 |
fn test_sign_as_a_size() { |
| 532 |
new_ucmd!() |
| 533 |
.args(&["-s", "+", "asd"]) |
| 534 |
.fails() |
| 535 |
.stderr_is("truncate: Invalid number: '+'\n"); |
| 536 |
} |
| 537 |
|
| 538 |
#[test] |
| 539 |
fn test_repeated_size_takes_the_last() { |
| 540 |
// GNU lets a later -s override an earlier one rather than erroring. |
| 541 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 542 |
at.make_file("repeated"); |
| 543 |
ucmd.args(&["-s", "1", "-s", "2", "repeated"]).succeeds(); |
| 544 |
assert_eq!(at.metadata("repeated").len(), 2); |
| 545 |
} |
| 546 |
|
| 547 |
#[cfg(unix)] |
| 548 |
#[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))] |
| 549 |
mod diagnostics { |
| 550 |
use super::*; |
| 551 |
|
| 552 |
#[test] |
| 553 |
fn test_snippet_counts_the_mode_character_before_the_size() { |
| 554 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 555 |
at.touch("probe"); |
| 556 |
|
| 557 |
let result = ucmd |
| 558 |
.terminal_sim_stderr() |
| 559 |
.args(&["--size=+2Zx", "probe"]) |
| 560 |
.fails_with_code(1); |
| 561 |
let stderr = result.stderr_as_displayed(); |
| 562 |
|
| 563 |
// The `+` is part of the operand but not of the size, so the caret has |
| 564 |
// to count it back in to land on the unit. |
| 565 |
assert!(stderr.contains("truncate:1:19"), "{stderr}"); |
| 566 |
assert!(stderr.contains("not a known unit"), "{stderr}"); |
| 567 |
} |
| 568 |
|
| 569 |
#[test] |
| 570 |
fn test_plain_message_when_stderr_is_a_pipe() { |
| 571 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 572 |
at.touch("probe"); |
| 573 |
|
| 574 |
ucmd.args(&["-s", "10fb", "probe"]) |
| 575 |
.fails_with_code(1) |
| 576 |
.stderr_contains("Invalid number: '10fb'"); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
#[cfg(not(windows))] |
| 581 |
#[cfg(not(wasi_runner))] // WASI: no FIFO/mkfifo support |
| 582 |
mod fifo { |
| 583 |
use super::*; |
| 584 |
|
| 585 |
#[test] |
| 586 |
fn test_fifo_error_size_only() { |
| 587 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 588 |
at.mkfifo("fifo"); |
| 589 |
ucmd.args(&["-s", "0", "fifo"]) |
| 590 |
.fails() |
| 591 |
.no_stdout() |
| 592 |
.stderr_contains("cannot open 'fifo' for writing: No such device or address"); |
| 593 |
} |
| 594 |
|
| 595 |
#[test] |
| 596 |
fn test_fifo_error_reference_file_only() { |
| 597 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 598 |
at.mkfifo("fifo"); |
| 599 |
at.make_file("reference_file"); |
| 600 |
ucmd.args(&["-r", "reference_file", "fifo"]) |
| 601 |
.fails() |
| 602 |
.no_stdout() |
| 603 |
.stderr_contains("cannot open 'fifo' for writing: No such device or address"); |
| 604 |
} |
| 605 |
|
| 606 |
#[test] |
| 607 |
fn test_fifo_error_reference_and_size() { |
| 608 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 609 |
at.mkfifo("fifo"); |
| 610 |
at.make_file("reference_file"); |
| 611 |
ucmd.args(&["-r", "reference_file", "-s", "+0", "fifo"]) |
| 612 |
.fails() |
| 613 |
.no_stdout() |
| 614 |
.stderr_contains("cannot open 'fifo' for writing: No such device or address"); |
| 615 |
} |
| 616 |
} |