| 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) gpghome |
| 7 |
|
| 8 |
use uutests::at_and_ucmd; |
| 9 |
use uutests::new_ucmd; |
| 10 |
use uutests::util::TestScenario; |
| 11 |
use uutests::util_name; |
| 12 |
|
| 13 |
use uucore::display::Quotable; |
| 14 |
|
| 15 |
#[cfg(not(windows))] |
| 16 |
use std::path::MAIN_SEPARATOR; |
| 17 |
use std::path::PathBuf; |
| 18 |
use tempfile::tempdir; |
| 19 |
|
| 20 |
#[cfg(unix)] |
| 21 |
use std::os::unix::fs::PermissionsExt; |
| 22 |
|
| 23 |
static TEST_TEMPLATE1: &str = "tempXXXXXX"; |
| 24 |
static TEST_TEMPLATE2: &str = "temp"; |
| 25 |
static TEST_TEMPLATE3: &str = "tempX"; |
| 26 |
static TEST_TEMPLATE4: &str = "tempXX"; |
| 27 |
static TEST_TEMPLATE5: &str = "tempXXX"; |
| 28 |
static TEST_TEMPLATE6: &str = "tempXXXlate"; |
| 29 |
static TEST_TEMPLATE7: &str = "XXXtemplate"; |
| 30 |
#[cfg(unix)] |
| 31 |
static TEST_TEMPLATE8: &str = "tempXXXl/ate"; |
| 32 |
#[cfg(windows)] |
| 33 |
static TEST_TEMPLATE8: &str = "tempXXXl\\ate"; |
| 34 |
static TEST_TEMPLATE9: &str = "XXX_XX"; |
| 35 |
|
| 36 |
#[cfg(not(windows))] |
| 37 |
const TMPDIR: &str = "TMPDIR"; |
| 38 |
#[cfg(windows)] |
| 39 |
const TMPDIR: &str = "TMP"; |
| 40 |
|
| 41 |
/// An assertion that uses [`matches_template`] and adds a helpful error message. |
| 42 |
macro_rules! assert_matches_template { |
| 43 |
($template:expr, $s:expr) => {{ |
| 44 |
assert!( |
| 45 |
matches_template($template, $s), |
| 46 |
"\"{}\" != \"{}\"", |
| 47 |
$template, |
| 48 |
$s |
| 49 |
); |
| 50 |
}}; |
| 51 |
} |
| 52 |
|
| 53 |
/// Like [`assert_matches_template`] but for the suffix of a string. |
| 54 |
#[cfg(windows)] |
| 55 |
macro_rules! assert_suffix_matches_template { |
| 56 |
($template:expr, $s:expr) => {{ |
| 57 |
let n = ($s).len(); |
| 58 |
let m = ($template).len(); |
| 59 |
let suffix = &$s[n - m..n]; |
| 60 |
assert!( |
| 61 |
matches_template($template, suffix), |
| 62 |
"\"{}\" does not end with \"{suffix}\"", |
| 63 |
$template, |
| 64 |
); |
| 65 |
}}; |
| 66 |
} |
| 67 |
|
| 68 |
#[test] |
| 69 |
fn test_mktemp_mktemp() { |
| 70 |
let scene = TestScenario::new(util_name!()); |
| 71 |
|
| 72 |
let pathname = scene.fixtures.as_string(); |
| 73 |
|
| 74 |
scene |
| 75 |
.ucmd() |
| 76 |
.env(TMPDIR, &pathname) |
| 77 |
.arg(TEST_TEMPLATE1) |
| 78 |
.succeeds(); |
| 79 |
scene |
| 80 |
.ucmd() |
| 81 |
.env(TMPDIR, &pathname) |
| 82 |
.arg(TEST_TEMPLATE2) |
| 83 |
.fails(); |
| 84 |
scene |
| 85 |
.ucmd() |
| 86 |
.env(TMPDIR, &pathname) |
| 87 |
.arg(TEST_TEMPLATE3) |
| 88 |
.fails(); |
| 89 |
scene |
| 90 |
.ucmd() |
| 91 |
.env(TMPDIR, &pathname) |
| 92 |
.arg(TEST_TEMPLATE4) |
| 93 |
.fails(); |
| 94 |
scene |
| 95 |
.ucmd() |
| 96 |
.env(TMPDIR, &pathname) |
| 97 |
.arg(TEST_TEMPLATE5) |
| 98 |
.succeeds(); |
| 99 |
scene |
| 100 |
.ucmd() |
| 101 |
.env(TMPDIR, &pathname) |
| 102 |
.arg(TEST_TEMPLATE6) |
| 103 |
.succeeds(); |
| 104 |
scene |
| 105 |
.ucmd() |
| 106 |
.env(TMPDIR, &pathname) |
| 107 |
.arg(TEST_TEMPLATE7) |
| 108 |
.succeeds(); |
| 109 |
scene |
| 110 |
.ucmd() |
| 111 |
.env(TMPDIR, &pathname) |
| 112 |
.arg(TEST_TEMPLATE8) |
| 113 |
.fails(); |
| 114 |
scene |
| 115 |
.ucmd() |
| 116 |
.env(TMPDIR, &pathname) |
| 117 |
.arg(TEST_TEMPLATE9) |
| 118 |
.fails(); |
| 119 |
} |
| 120 |
|
| 121 |
#[test] |
| 122 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] |
| 123 |
fn test_mktemp_mktemp_t() { |
| 124 |
let scene = TestScenario::new(util_name!()); |
| 125 |
|
| 126 |
let pathname = scene.fixtures.as_string(); |
| 127 |
|
| 128 |
scene |
| 129 |
.ucmd() |
| 130 |
.env(TMPDIR, &pathname) |
| 131 |
.arg("-t") |
| 132 |
.arg(TEST_TEMPLATE1) |
| 133 |
.succeeds(); |
| 134 |
scene |
| 135 |
.ucmd() |
| 136 |
.env(TMPDIR, &pathname) |
| 137 |
.arg("-t") |
| 138 |
.arg(TEST_TEMPLATE2) |
| 139 |
.fails(); |
| 140 |
scene |
| 141 |
.ucmd() |
| 142 |
.env(TMPDIR, &pathname) |
| 143 |
.arg("-t") |
| 144 |
.arg(TEST_TEMPLATE3) |
| 145 |
.fails(); |
| 146 |
scene |
| 147 |
.ucmd() |
| 148 |
.env(TMPDIR, &pathname) |
| 149 |
.arg("-t") |
| 150 |
.arg(TEST_TEMPLATE4) |
| 151 |
.fails(); |
| 152 |
scene |
| 153 |
.ucmd() |
| 154 |
.env(TMPDIR, &pathname) |
| 155 |
.arg("-t") |
| 156 |
.arg(TEST_TEMPLATE5) |
| 157 |
.succeeds(); |
| 158 |
scene |
| 159 |
.ucmd() |
| 160 |
.env(TMPDIR, &pathname) |
| 161 |
.arg("-t") |
| 162 |
.arg(TEST_TEMPLATE6) |
| 163 |
.succeeds(); |
| 164 |
scene |
| 165 |
.ucmd() |
| 166 |
.env(TMPDIR, &pathname) |
| 167 |
.arg("-t") |
| 168 |
.arg(TEST_TEMPLATE7) |
| 169 |
.succeeds(); |
| 170 |
scene |
| 171 |
.ucmd() |
| 172 |
.env(TMPDIR, &pathname) |
| 173 |
.arg("-t") |
| 174 |
.arg(TEST_TEMPLATE8) |
| 175 |
.fails() |
| 176 |
.no_stdout() |
| 177 |
.stderr_contains("invalid suffix") |
| 178 |
.stderr_contains("contains directory separator"); |
| 179 |
scene |
| 180 |
.ucmd() |
| 181 |
.env(TMPDIR, &pathname) |
| 182 |
.arg("-t") |
| 183 |
.arg(TEST_TEMPLATE9) |
| 184 |
.fails(); |
| 185 |
} |
| 186 |
|
| 187 |
#[test] |
| 188 |
fn test_mktemp_make_temp_dir() { |
| 189 |
let scene = TestScenario::new(util_name!()); |
| 190 |
|
| 191 |
let pathname = scene.fixtures.as_string(); |
| 192 |
|
| 193 |
scene |
| 194 |
.ucmd() |
| 195 |
.env(TMPDIR, &pathname) |
| 196 |
.arg("-d") |
| 197 |
.arg(TEST_TEMPLATE1) |
| 198 |
.succeeds(); |
| 199 |
scene |
| 200 |
.ucmd() |
| 201 |
.env(TMPDIR, &pathname) |
| 202 |
.arg("-d") |
| 203 |
.arg(TEST_TEMPLATE2) |
| 204 |
.fails(); |
| 205 |
scene |
| 206 |
.ucmd() |
| 207 |
.env(TMPDIR, &pathname) |
| 208 |
.arg("-d") |
| 209 |
.arg(TEST_TEMPLATE3) |
| 210 |
.fails(); |
| 211 |
scene |
| 212 |
.ucmd() |
| 213 |
.env(TMPDIR, &pathname) |
| 214 |
.arg("-d") |
| 215 |
.arg(TEST_TEMPLATE4) |
| 216 |
.fails(); |
| 217 |
scene |
| 218 |
.ucmd() |
| 219 |
.env(TMPDIR, &pathname) |
| 220 |
.arg("-d") |
| 221 |
.arg(TEST_TEMPLATE5) |
| 222 |
.succeeds(); |
| 223 |
scene |
| 224 |
.ucmd() |
| 225 |
.env(TMPDIR, &pathname) |
| 226 |
.arg("-d") |
| 227 |
.arg(TEST_TEMPLATE6) |
| 228 |
.succeeds(); |
| 229 |
scene |
| 230 |
.ucmd() |
| 231 |
.env(TMPDIR, &pathname) |
| 232 |
.arg("-d") |
| 233 |
.arg(TEST_TEMPLATE7) |
| 234 |
.succeeds(); |
| 235 |
scene |
| 236 |
.ucmd() |
| 237 |
.env(TMPDIR, &pathname) |
| 238 |
.arg("-d") |
| 239 |
.arg(TEST_TEMPLATE8) |
| 240 |
.fails(); |
| 241 |
scene |
| 242 |
.ucmd() |
| 243 |
.env(TMPDIR, &pathname) |
| 244 |
.arg("-d") |
| 245 |
.arg(TEST_TEMPLATE9) |
| 246 |
.fails(); |
| 247 |
} |
| 248 |
|
| 249 |
#[test] |
| 250 |
fn test_mktemp_dry_run() { |
| 251 |
let scene = TestScenario::new(util_name!()); |
| 252 |
|
| 253 |
let pathname = scene.fixtures.as_string(); |
| 254 |
|
| 255 |
scene |
| 256 |
.ucmd() |
| 257 |
.env(TMPDIR, &pathname) |
| 258 |
.arg("-u") |
| 259 |
.arg(TEST_TEMPLATE1) |
| 260 |
.succeeds(); |
| 261 |
scene |
| 262 |
.ucmd() |
| 263 |
.env(TMPDIR, &pathname) |
| 264 |
.arg("-u") |
| 265 |
.arg(TEST_TEMPLATE2) |
| 266 |
.fails(); |
| 267 |
scene |
| 268 |
.ucmd() |
| 269 |
.env(TMPDIR, &pathname) |
| 270 |
.arg("-u") |
| 271 |
.arg(TEST_TEMPLATE3) |
| 272 |
.fails(); |
| 273 |
scene |
| 274 |
.ucmd() |
| 275 |
.env(TMPDIR, &pathname) |
| 276 |
.arg("-u") |
| 277 |
.arg(TEST_TEMPLATE4) |
| 278 |
.fails(); |
| 279 |
scene |
| 280 |
.ucmd() |
| 281 |
.env(TMPDIR, &pathname) |
| 282 |
.arg("-u") |
| 283 |
.arg(TEST_TEMPLATE5) |
| 284 |
.succeeds(); |
| 285 |
scene |
| 286 |
.ucmd() |
| 287 |
.env(TMPDIR, &pathname) |
| 288 |
.arg("-u") |
| 289 |
.arg(TEST_TEMPLATE6) |
| 290 |
.succeeds(); |
| 291 |
scene |
| 292 |
.ucmd() |
| 293 |
.env(TMPDIR, &pathname) |
| 294 |
.arg("-u") |
| 295 |
.arg(TEST_TEMPLATE7) |
| 296 |
.succeeds(); |
| 297 |
scene |
| 298 |
.ucmd() |
| 299 |
.env(TMPDIR, &pathname) |
| 300 |
.arg("-u") |
| 301 |
.arg(TEST_TEMPLATE8) |
| 302 |
.fails(); |
| 303 |
scene |
| 304 |
.ucmd() |
| 305 |
.env(TMPDIR, &pathname) |
| 306 |
.arg("-u") |
| 307 |
.arg(TEST_TEMPLATE9) |
| 308 |
.fails(); |
| 309 |
} |
| 310 |
|
| 311 |
#[test] |
| 312 |
fn test_mktemp_quiet() { |
| 313 |
let scene = TestScenario::new(util_name!()); |
| 314 |
|
| 315 |
scene |
| 316 |
.ucmd() |
| 317 |
.arg("-p") |
| 318 |
.arg("/definitely/not/exist/I/promise") |
| 319 |
.arg("-q") |
| 320 |
.fails() |
| 321 |
.no_output(); |
| 322 |
scene |
| 323 |
.ucmd() |
| 324 |
.arg("-d") |
| 325 |
.arg("-p") |
| 326 |
.arg("/definitely/not/exist/I/promise") |
| 327 |
.arg("-q") |
| 328 |
.fails() |
| 329 |
.no_output(); |
| 330 |
} |
| 331 |
|
| 332 |
#[test] |
| 333 |
fn test_mktemp_suffix() { |
| 334 |
let scene = TestScenario::new(util_name!()); |
| 335 |
|
| 336 |
let pathname = scene.fixtures.as_string(); |
| 337 |
|
| 338 |
scene |
| 339 |
.ucmd() |
| 340 |
.env(TMPDIR, &pathname) |
| 341 |
.arg("--suffix") |
| 342 |
.arg("suf") |
| 343 |
.arg(TEST_TEMPLATE1) |
| 344 |
.succeeds(); |
| 345 |
scene |
| 346 |
.ucmd() |
| 347 |
.env(TMPDIR, &pathname) |
| 348 |
.arg("--suffix") |
| 349 |
.arg("suf") |
| 350 |
.arg(TEST_TEMPLATE2) |
| 351 |
.fails(); |
| 352 |
scene |
| 353 |
.ucmd() |
| 354 |
.env(TMPDIR, &pathname) |
| 355 |
.arg("--suffix") |
| 356 |
.arg("suf") |
| 357 |
.arg(TEST_TEMPLATE3) |
| 358 |
.fails(); |
| 359 |
scene |
| 360 |
.ucmd() |
| 361 |
.env(TMPDIR, &pathname) |
| 362 |
.arg("--suffix") |
| 363 |
.arg("suf") |
| 364 |
.arg(TEST_TEMPLATE4) |
| 365 |
.fails(); |
| 366 |
scene |
| 367 |
.ucmd() |
| 368 |
.env(TMPDIR, &pathname) |
| 369 |
.arg("--suffix") |
| 370 |
.arg("suf") |
| 371 |
.arg(TEST_TEMPLATE5) |
| 372 |
.succeeds(); |
| 373 |
scene |
| 374 |
.ucmd() |
| 375 |
.env(TMPDIR, &pathname) |
| 376 |
.arg("--suffix") |
| 377 |
.arg("suf") |
| 378 |
.arg(TEST_TEMPLATE6) |
| 379 |
.fails(); |
| 380 |
scene |
| 381 |
.ucmd() |
| 382 |
.env(TMPDIR, &pathname) |
| 383 |
.arg("--suffix") |
| 384 |
.arg("suf") |
| 385 |
.arg(TEST_TEMPLATE7) |
| 386 |
.fails(); |
| 387 |
scene |
| 388 |
.ucmd() |
| 389 |
.env(TMPDIR, &pathname) |
| 390 |
.arg("--suffix") |
| 391 |
.arg("suf") |
| 392 |
.arg(TEST_TEMPLATE8) |
| 393 |
.fails(); |
| 394 |
scene |
| 395 |
.ucmd() |
| 396 |
.env(TMPDIR, &pathname) |
| 397 |
.arg("--suffix") |
| 398 |
.arg("suf") |
| 399 |
.arg(TEST_TEMPLATE9) |
| 400 |
.fails(); |
| 401 |
} |
| 402 |
|
| 403 |
#[test] |
| 404 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] |
| 405 |
fn test_mktemp_tmpdir() { |
| 406 |
let scene = TestScenario::new(util_name!()); |
| 407 |
let dir = tempdir().unwrap(); |
| 408 |
let path = dir.path().join(scene.fixtures.as_string()); |
| 409 |
let pathname = path.as_os_str(); |
| 410 |
|
| 411 |
scene |
| 412 |
.ucmd() |
| 413 |
.arg("-p") |
| 414 |
.arg(pathname) |
| 415 |
.arg(TEST_TEMPLATE1) |
| 416 |
.succeeds(); |
| 417 |
scene |
| 418 |
.ucmd() |
| 419 |
.arg("-p") |
| 420 |
.arg(pathname) |
| 421 |
.arg(TEST_TEMPLATE2) |
| 422 |
.fails(); |
| 423 |
scene |
| 424 |
.ucmd() |
| 425 |
.arg("-p") |
| 426 |
.arg(pathname) |
| 427 |
.arg(TEST_TEMPLATE3) |
| 428 |
.fails(); |
| 429 |
scene |
| 430 |
.ucmd() |
| 431 |
.arg("-p") |
| 432 |
.arg(pathname) |
| 433 |
.arg(TEST_TEMPLATE4) |
| 434 |
.fails(); |
| 435 |
scene |
| 436 |
.ucmd() |
| 437 |
.arg("-p") |
| 438 |
.arg(pathname) |
| 439 |
.arg(TEST_TEMPLATE5) |
| 440 |
.succeeds(); |
| 441 |
scene |
| 442 |
.ucmd() |
| 443 |
.arg("-p") |
| 444 |
.arg(pathname) |
| 445 |
.arg(TEST_TEMPLATE6) |
| 446 |
.succeeds(); |
| 447 |
scene |
| 448 |
.ucmd() |
| 449 |
.arg("-p") |
| 450 |
.arg(pathname) |
| 451 |
.arg(TEST_TEMPLATE7) |
| 452 |
.succeeds(); |
| 453 |
scene |
| 454 |
.ucmd() |
| 455 |
.arg("-p") |
| 456 |
.arg(pathname) |
| 457 |
.arg(TEST_TEMPLATE8) |
| 458 |
.fails(); |
| 459 |
scene |
| 460 |
.ucmd() |
| 461 |
.arg("-p") |
| 462 |
.arg(pathname) |
| 463 |
.arg(TEST_TEMPLATE9) |
| 464 |
.fails(); |
| 465 |
} |
| 466 |
|
| 467 |
#[test] |
| 468 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] |
| 469 |
fn test_mktemp_empty_tmpdir() { |
| 470 |
let scene = TestScenario::new(util_name!()); |
| 471 |
let pathname = scene.fixtures.as_string(); |
| 472 |
|
| 473 |
let result = scene |
| 474 |
.ucmd() |
| 475 |
.env(TMPDIR, &pathname) |
| 476 |
.args(&["-p", ""]) |
| 477 |
.succeeds(); |
| 478 |
assert!(result.stdout_str().trim().starts_with(&pathname)); |
| 479 |
|
| 480 |
let result = scene |
| 481 |
.ucmd() |
| 482 |
.env(TMPDIR, &pathname) |
| 483 |
.arg("--tmpdir=") |
| 484 |
.succeeds(); |
| 485 |
assert!(result.stdout_str().trim().starts_with(&pathname)); |
| 486 |
} |
| 487 |
|
| 488 |
#[test] |
| 489 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] |
| 490 |
fn test_mktemp_tmpdir_one_arg() { |
| 491 |
let scene = TestScenario::new(util_name!()); |
| 492 |
|
| 493 |
let result = scene |
| 494 |
.ucmd() |
| 495 |
.arg("--tmpdir") |
| 496 |
.arg("apt-key-gpghome.XXXXXXXXXX") |
| 497 |
.succeeds(); |
| 498 |
result.no_stderr().stdout_contains("apt-key-gpghome."); |
| 499 |
assert!(PathBuf::from(result.stdout_str().trim()).is_file()); |
| 500 |
} |
| 501 |
|
| 502 |
#[test] |
| 503 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] |
| 504 |
fn test_mktemp_directory_tmpdir() { |
| 505 |
let scene = TestScenario::new(util_name!()); |
| 506 |
|
| 507 |
let result = scene |
| 508 |
.ucmd() |
| 509 |
.arg("--directory") |
| 510 |
.arg("--tmpdir") |
| 511 |
.arg("apt-key-gpghome.XXXXXXXXXX") |
| 512 |
.succeeds(); |
| 513 |
result.no_stderr().stdout_contains("apt-key-gpghome."); |
| 514 |
assert!(PathBuf::from(result.stdout_str().trim()).is_dir()); |
| 515 |
} |
| 516 |
|
| 517 |
/// Test for combining `--tmpdir` and a template with a subdirectory. |
| 518 |
#[test] |
| 519 |
fn test_tmpdir_template_has_subdirectory() { |
| 520 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 521 |
at.mkdir("a"); |
| 522 |
#[cfg(not(windows))] |
| 523 |
let (template, joined) = ("a/bXXXX", "./a/bXXXX"); |
| 524 |
#[cfg(windows)] |
| 525 |
let (template, joined) = (r"a\bXXXX", r".\a\bXXXX"); |
| 526 |
let result = ucmd.args(&["--tmpdir=.", template]).succeeds(); |
| 527 |
let filename = result.no_stderr().stdout_str().trim_end(); |
| 528 |
assert_matches_template!(joined, filename); |
| 529 |
assert!(at.file_exists(filename)); |
| 530 |
} |
| 531 |
|
| 532 |
/// Test that an absolute path is disallowed when --tmpdir is provided. |
| 533 |
#[test] |
| 534 |
fn test_tmpdir_absolute_path() { |
| 535 |
#[cfg(windows)] |
| 536 |
let path = r"C:\XXX"; |
| 537 |
#[cfg(not(windows))] |
| 538 |
let path = "/XXX"; |
| 539 |
new_ucmd!() |
| 540 |
.args(&["--tmpdir=a", path]) |
| 541 |
.fails() |
| 542 |
.stderr_only(format!( |
| 543 |
"mktemp: invalid template, '{path}'; with --tmpdir, it may not be absolute\n" |
| 544 |
)); |
| 545 |
} |
| 546 |
|
| 547 |
/// Decide whether a string matches a given template. |
| 548 |
/// |
| 549 |
/// In the template, the character `'X'` is treated as a wildcard, |
| 550 |
/// that is, it matches anything. All other characters in `template` |
| 551 |
/// and `s` must match exactly. |
| 552 |
/// |
| 553 |
/// # Examples |
| 554 |
/// |
| 555 |
/// ```rust,ignore |
| 556 |
/// # These all match. |
| 557 |
/// assert!(matches_template("abc", "abc")); |
| 558 |
/// assert!(matches_template("aXc", "abc")); |
| 559 |
/// assert!(matches_template("XXX", "abc")); |
| 560 |
/// |
| 561 |
/// # None of these match |
| 562 |
/// assert!(matches_template("abc", "abcd")); |
| 563 |
/// assert!(matches_template("abc", "ab")); |
| 564 |
/// assert!(matches_template("aXc", "abd")); |
| 565 |
/// assert!(matches_template("XXX", "abcd")); |
| 566 |
/// ``` |
| 567 |
/// |
| 568 |
fn matches_template(template: &str, s: &str) -> bool { |
| 569 |
if template.len() != s.len() { |
| 570 |
return false; |
| 571 |
} |
| 572 |
for (a, b) in template.chars().zip(s.chars()) { |
| 573 |
if !(a == 'X' || a == b) { |
| 574 |
return false; |
| 575 |
} |
| 576 |
} |
| 577 |
true |
| 578 |
} |
| 579 |
|
| 580 |
/// Test that the file is created in the directory given by the template. |
| 581 |
#[test] |
| 582 |
fn test_respect_template() { |
| 583 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 584 |
let template = "XXX"; |
| 585 |
let result = ucmd.arg(template).succeeds(); |
| 586 |
let filename = result.no_stderr().stdout_str().trim_end(); |
| 587 |
assert_matches_template!(template, filename); |
| 588 |
assert!(at.file_exists(filename)); |
| 589 |
} |
| 590 |
|
| 591 |
/// Test that the file is created in the directory given by the template. |
| 592 |
#[test] |
| 593 |
fn test_respect_template_directory() { |
| 594 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 595 |
at.mkdir("d"); |
| 596 |
#[cfg(not(windows))] |
| 597 |
let template = "d/XXX"; |
| 598 |
#[cfg(windows)] |
| 599 |
let template = r"d\XXX"; |
| 600 |
let result = ucmd.arg(template).succeeds(); |
| 601 |
let filename = result.no_stderr().stdout_str().trim_end(); |
| 602 |
assert_matches_template!(template, filename); |
| 603 |
assert!(at.file_exists(filename)); |
| 604 |
} |
| 605 |
|
| 606 |
#[cfg(unix)] |
| 607 |
#[test] |
| 608 |
#[cfg_attr( |
| 609 |
wasi_runner, |
| 610 |
ignore = "WASI: path_create_directory has no mode parameter and chmod returns ENOSYS, \ |
| 611 |
so directories can't be created with restricted permissions" |
| 612 |
)] |
| 613 |
fn test_directory_permissions() { |
| 614 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 615 |
let result = ucmd.args(&["-d", "XXX"]).succeeds(); |
| 616 |
let dirname = result.no_stderr().stdout_str().trim_end(); |
| 617 |
assert_matches_template!("XXX", dirname); |
| 618 |
let metadata = at.metadata(dirname); |
| 619 |
assert!(metadata.is_dir()); |
| 620 |
assert_eq!(metadata.permissions().mode(), 0o40700); |
| 621 |
} |
| 622 |
|
| 623 |
/// Test that a template with a path separator is invalid. |
| 624 |
#[test] |
| 625 |
fn test_template_path_separator() { |
| 626 |
#[cfg(not(windows))] |
| 627 |
new_ucmd!() |
| 628 |
.args(&["-t", "a/bXXX"]) |
| 629 |
.fails() |
| 630 |
.stderr_only(format!( |
| 631 |
"mktemp: invalid template, {}, contains directory separator\n", |
| 632 |
"a/bXXX".quote() |
| 633 |
)); |
| 634 |
#[cfg(windows)] |
| 635 |
new_ucmd!() |
| 636 |
.args(&["-t", r"a\bXXX"]) |
| 637 |
.fails() |
| 638 |
.stderr_only(format!( |
| 639 |
"mktemp: invalid template, {}, contains directory separator\n", |
| 640 |
r"a\bXXX".quote() |
| 641 |
)); |
| 642 |
} |
| 643 |
|
| 644 |
/// Test that a prefix with a point is valid. |
| 645 |
#[test] |
| 646 |
fn test_prefix_template_separator() { |
| 647 |
new_ucmd!().args(&["-p", ".", "-t", "a.XXXX"]).succeeds(); |
| 648 |
} |
| 649 |
|
| 650 |
#[test] |
| 651 |
fn test_prefix_template_with_path_separator() { |
| 652 |
#[cfg(not(windows))] |
| 653 |
new_ucmd!() |
| 654 |
.args(&["-t", "a/XXX"]) |
| 655 |
.fails() |
| 656 |
.stderr_only(format!( |
| 657 |
"mktemp: invalid template, {}, contains directory separator\n", |
| 658 |
"a/XXX".quote() |
| 659 |
)); |
| 660 |
#[cfg(windows)] |
| 661 |
new_ucmd!() |
| 662 |
.args(&["-t", r"a\XXX"]) |
| 663 |
.fails() |
| 664 |
.stderr_only(format!( |
| 665 |
"mktemp: invalid template, {}, contains directory separator\n", |
| 666 |
r"a\XXX".quote() |
| 667 |
)); |
| 668 |
} |
| 669 |
|
| 670 |
/// Test that a suffix with a path separator is invalid. |
| 671 |
#[test] |
| 672 |
fn test_suffix_path_separator() { |
| 673 |
#[cfg(not(windows))] |
| 674 |
new_ucmd!() |
| 675 |
.arg("aXXX/b") |
| 676 |
.fails() |
| 677 |
.stderr_only("mktemp: invalid suffix '/b', contains directory separator\n"); |
| 678 |
#[cfg(windows)] |
| 679 |
new_ucmd!() |
| 680 |
.arg(r"aXXX\b") |
| 681 |
.fails() |
| 682 |
.stderr_only("mktemp: invalid suffix '\\b', contains directory separator\n"); |
| 683 |
#[cfg(not(windows))] |
| 684 |
new_ucmd!() |
| 685 |
.arg("XXX/..") |
| 686 |
.fails() |
| 687 |
.stderr_only("mktemp: invalid suffix '/..', contains directory separator\n"); |
| 688 |
#[cfg(windows)] |
| 689 |
new_ucmd!() |
| 690 |
.arg(r"XXX\..") |
| 691 |
.fails() |
| 692 |
.stderr_only("mktemp: invalid suffix '\\..', contains directory separator\n"); |
| 693 |
} |
| 694 |
|
| 695 |
#[test] |
| 696 |
fn test_too_few_xs_suffix() { |
| 697 |
new_ucmd!() |
| 698 |
.args(&["--suffix=X", "aXX"]) |
| 699 |
.fails() |
| 700 |
.stderr_only("mktemp: too few X's in template 'aXX'\n"); |
| 701 |
} |
| 702 |
|
| 703 |
#[test] |
| 704 |
fn test_too_few_xs_suffix_directory() { |
| 705 |
new_ucmd!() |
| 706 |
.args(&["-d", "--suffix=X", "aXX"]) |
| 707 |
.fails() |
| 708 |
.stderr_only("mktemp: too few X's in template 'aXX'\n"); |
| 709 |
} |
| 710 |
|
| 711 |
#[test] |
| 712 |
fn test_too_few_xs_quiet() { |
| 713 |
new_ucmd!() |
| 714 |
.args(&["-q", "aXX"]) |
| 715 |
.fails() |
| 716 |
.stderr_only("mktemp: too few X's in template 'aXX'\n"); |
| 717 |
} |
| 718 |
|
| 719 |
#[test] |
| 720 |
fn test_too_many_arguments() { |
| 721 |
new_ucmd!() |
| 722 |
.args(&["-q", "a", "b"]) |
| 723 |
.fails_with_code(1) |
| 724 |
.usage_error("too many templates"); |
| 725 |
} |
| 726 |
|
| 727 |
#[test] |
| 728 |
fn test_two_contiguous_wildcard_blocks() { |
| 729 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 730 |
let template = "XXX_XXX"; |
| 731 |
let result = ucmd.arg(template).succeeds(); |
| 732 |
let filename = result.no_stderr().stdout_str().trim_end(); |
| 733 |
assert_eq!(&filename[..4], "XXX_"); |
| 734 |
assert_matches_template!(template, filename); |
| 735 |
assert!(at.file_exists(filename)); |
| 736 |
} |
| 737 |
|
| 738 |
#[test] |
| 739 |
fn test_three_contiguous_wildcard_blocks() { |
| 740 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 741 |
let template = "XXX_XXX_XXX"; |
| 742 |
let result = ucmd.arg(template).succeeds(); |
| 743 |
let filename = result.no_stderr().stdout_str().trim_end(); |
| 744 |
assert_eq!(&filename[..8], "XXX_XXX_"); |
| 745 |
assert_matches_template!(template, filename); |
| 746 |
assert!(at.file_exists(filename)); |
| 747 |
} |
| 748 |
|
| 749 |
/// Test that template must end in X even if `--suffix` is the empty string. |
| 750 |
#[test] |
| 751 |
fn test_suffix_must_end_in_x() { |
| 752 |
new_ucmd!() |
| 753 |
.args(&["--suffix=", "aXXXb"]) |
| 754 |
.fails() |
| 755 |
.stderr_is("mktemp: with --suffix, template 'aXXXb' must end in X\n"); |
| 756 |
} |
| 757 |
|
| 758 |
#[test] |
| 759 |
fn test_suffix_empty_template() { |
| 760 |
new_ucmd!() |
| 761 |
.args(&["--suffix=aXXXb", ""]) |
| 762 |
.fails() |
| 763 |
.stderr_is("mktemp: with --suffix, template '' must end in X\n"); |
| 764 |
|
| 765 |
new_ucmd!() |
| 766 |
.args(&["-d", "--suffix=aXXXb", ""]) |
| 767 |
.fails() |
| 768 |
.stderr_is("mktemp: with --suffix, template '' must end in X\n"); |
| 769 |
} |
| 770 |
|
| 771 |
#[test] |
| 772 |
fn test_mktemp_with_posixly_correct() { |
| 773 |
let scene = TestScenario::new(util_name!()); |
| 774 |
|
| 775 |
scene |
| 776 |
.ucmd() |
| 777 |
.env("POSIXLY_CORRECT", "1") |
| 778 |
.args(&["aXXXX", "--suffix=b"]) |
| 779 |
.fails() |
| 780 |
.usage_error("too many templates"); |
| 781 |
|
| 782 |
scene |
| 783 |
.ucmd() |
| 784 |
.env("POSIXLY_CORRECT", "1") |
| 785 |
.args(&["--suffix=b", "aXXXX"]) |
| 786 |
.succeeds(); |
| 787 |
} |
| 788 |
|
| 789 |
/// Test that files are created relative to `TMPDIR` environment variable. |
| 790 |
#[test] |
| 791 |
fn test_tmpdir_env_var() { |
| 792 |
// `TMPDIR=. mktemp` |
| 793 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 794 |
let result = ucmd.env(TMPDIR, ".").succeeds(); |
| 795 |
let filename = result.no_stderr().stdout_str().trim_end(); |
| 796 |
#[cfg(not(windows))] |
| 797 |
{ |
| 798 |
let template = format!(".{MAIN_SEPARATOR}tmp.XXXXXXXXXX"); |
| 799 |
assert_matches_template!(&template, filename); |
| 800 |
} |
| 801 |
// On Windows, `env::temp_dir()` seems to give an absolute path |
| 802 |
// regardless of the value of `TMPDIR`; see |
| 803 |
// * https://github.com/uutils/coreutils/pull/3552#issuecomment-1211804981 |
| 804 |
// * https://doc.rust-lang.org/std/env/fn.temp_dir.html |
| 805 |
// * https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppath2w |
| 806 |
#[cfg(windows)] |
| 807 |
assert_suffix_matches_template!("tmp.XXXXXXXXXX", filename); |
| 808 |
assert!(at.file_exists(filename)); |
| 809 |
|
| 810 |
// `TMPDIR=. mktemp --tmpdir` |
| 811 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 812 |
let result = ucmd.env(TMPDIR, ".").arg("--tmpdir").succeeds(); |
| 813 |
let filename = result.no_stderr().stdout_str().trim_end(); |
| 814 |
#[cfg(not(windows))] |
| 815 |
{ |
| 816 |
let template = format!(".{MAIN_SEPARATOR}tmp.XXXXXXXXXX"); |
| 817 |
assert_matches_template!(&template, filename); |
| 818 |
} |
| 819 |
#[cfg(windows)] |
| 820 |
assert_suffix_matches_template!("tmp.XXXXXXXXXX", filename); |
| 821 |
assert!(at.file_exists(filename)); |
| 822 |
|
| 823 |
// `TMPDIR=. mktemp --tmpdir XXX` |
| 824 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 825 |
let result = ucmd.env(TMPDIR, ".").args(&["--tmpdir", "XXX"]).succeeds(); |
| 826 |
let filename = result.no_stderr().stdout_str().trim_end(); |
| 827 |
#[cfg(not(windows))] |
| 828 |
{ |
| 829 |
let template = format!(".{MAIN_SEPARATOR}XXX"); |
| 830 |
assert_matches_template!(&template, filename); |
| 831 |
} |
| 832 |
#[cfg(windows)] |
| 833 |
assert_suffix_matches_template!("XXX", filename); |
| 834 |
assert!(at.file_exists(filename)); |
| 835 |
|
| 836 |
// `TMPDIR=. mktemp XXX` - in this case `TMPDIR` is ignored. |
| 837 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 838 |
let result = ucmd.env(TMPDIR, ".").arg("XXX").succeeds(); |
| 839 |
let filename = result.no_stderr().stdout_str().trim_end(); |
| 840 |
let template = "XXX"; |
| 841 |
assert_matches_template!(template, filename); |
| 842 |
assert!(at.file_exists(filename)); |
| 843 |
} |
| 844 |
|
| 845 |
#[test] |
| 846 |
fn test_nonexistent_tmpdir_env_var() { |
| 847 |
#[cfg(not(windows))] |
| 848 |
new_ucmd!().env(TMPDIR, "no/such/dir").fails().stderr_only("mktemp: failed to create file via template 'no/such/dir/tmp.XXXXXXXXXX': No such file or directory\n"); |
| 849 |
#[cfg(windows)] |
| 850 |
{ |
| 851 |
let result = new_ucmd!().env(TMPDIR, r"no\such\dir").fails(); |
| 852 |
result.no_stdout(); |
| 853 |
let stderr = result.stderr_str(); |
| 854 |
assert!( |
| 855 |
stderr.starts_with("mktemp: failed to create file via template"), |
| 856 |
"{stderr}", |
| 857 |
); |
| 858 |
assert!( |
| 859 |
stderr.ends_with("no\\such\\dir\\tmp.XXXXXXXXXX': No such file or directory\n"), |
| 860 |
"{stderr}", |
| 861 |
); |
| 862 |
} |
| 863 |
|
| 864 |
#[cfg(not(windows))] |
| 865 |
new_ucmd!().env(TMPDIR, "no/such/dir").arg("-d").fails().stderr_only("mktemp: failed to create directory via template 'no/such/dir/tmp.XXXXXXXXXX': No such file or directory\n"); |
| 866 |
#[cfg(windows)] |
| 867 |
{ |
| 868 |
let result = new_ucmd!().env(TMPDIR, r"no\such\dir").arg("-d").fails(); |
| 869 |
result.no_stdout(); |
| 870 |
let stderr = result.stderr_str(); |
| 871 |
assert!( |
| 872 |
stderr.starts_with("mktemp: failed to create directory via template"), |
| 873 |
"{stderr}", |
| 874 |
); |
| 875 |
assert!( |
| 876 |
stderr.ends_with("no\\such\\dir\\tmp.XXXXXXXXXX': No such file or directory\n"), |
| 877 |
"{stderr}", |
| 878 |
); |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
#[test] |
| 883 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] |
| 884 |
fn test_empty_tmpdir_env_var() { |
| 885 |
#[cfg(not(any(windows, target_os = "android")))] |
| 886 |
{ |
| 887 |
let result = new_ucmd!().env(TMPDIR, "").succeeds(); |
| 888 |
assert!(result.stdout_str().starts_with("/tmp")); |
| 889 |
} |
| 890 |
|
| 891 |
#[cfg(any(windows, target_os = "android"))] |
| 892 |
{ |
| 893 |
let result = new_ucmd!().env(TMPDIR, "").fails(); |
| 894 |
result.no_stdout(); |
| 895 |
let stderr = result.stderr_str(); |
| 896 |
assert!( |
| 897 |
stderr.starts_with("mktemp: failed to create file via template"), |
| 898 |
"{stderr}" |
| 899 |
); |
| 900 |
#[cfg(windows)] |
| 901 |
assert!( |
| 902 |
stderr.ends_with("/tmp\\tmp.XXXXXXXXXX': No such file or directory\n"), |
| 903 |
"{stderr}", |
| 904 |
); |
| 905 |
#[cfg(target_os = "android")] |
| 906 |
assert!( |
| 907 |
stderr.ends_with("/tmp/tmp.XXXXXXXXXX': No such file or directory\n"), |
| 908 |
"{stderr}", |
| 909 |
); |
| 910 |
} |
| 911 |
|
| 912 |
#[cfg(not(any(windows, target_os = "android")))] |
| 913 |
{ |
| 914 |
let result = new_ucmd!().env(TMPDIR, "").arg("-d").succeeds(); |
| 915 |
assert!(result.stdout_str().starts_with("/tmp")); |
| 916 |
} |
| 917 |
|
| 918 |
#[cfg(any(windows, target_os = "android"))] |
| 919 |
{ |
| 920 |
let result = new_ucmd!().env(TMPDIR, "").arg("-d").fails(); |
| 921 |
result.no_stdout(); |
| 922 |
let stderr = result.stderr_str(); |
| 923 |
assert!( |
| 924 |
stderr.starts_with("mktemp: failed to create directory via template"), |
| 925 |
"{stderr}" |
| 926 |
); |
| 927 |
#[cfg(windows)] |
| 928 |
assert!( |
| 929 |
stderr.ends_with("/tmp\\tmp.XXXXXXXXXX': No such file or directory\n"), |
| 930 |
"{stderr}", |
| 931 |
); |
| 932 |
#[cfg(target_os = "android")] |
| 933 |
assert!( |
| 934 |
stderr.ends_with("/tmp/tmp.XXXXXXXXXX': No such file or directory\n"), |
| 935 |
"{stderr}", |
| 936 |
); |
| 937 |
} |
| 938 |
} |
| 939 |
|
| 940 |
#[test] |
| 941 |
fn test_nonexistent_dir_prefix() { |
| 942 |
#[cfg(not(windows))] |
| 943 |
new_ucmd!().arg("d/XXX").fails().stderr_only( |
| 944 |
"mktemp: failed to create file via template 'd/XXX': No such file or directory\n", |
| 945 |
); |
| 946 |
#[cfg(windows)] |
| 947 |
{ |
| 948 |
let result = new_ucmd!().arg(r"d\XXX").fails(); |
| 949 |
result.no_stdout(); |
| 950 |
let stderr = result.stderr_str(); |
| 951 |
assert!( |
| 952 |
stderr.starts_with("mktemp: failed to create file via template"), |
| 953 |
"{stderr}", |
| 954 |
); |
| 955 |
assert!( |
| 956 |
stderr.ends_with("d\\XXX': No such file or directory\n"), |
| 957 |
"{stderr}", |
| 958 |
); |
| 959 |
} |
| 960 |
|
| 961 |
#[cfg(not(windows))] |
| 962 |
new_ucmd!().arg("-d").arg("d/XXX").fails().stderr_only( |
| 963 |
"mktemp: failed to create directory via template 'd/XXX': No such file or directory\n", |
| 964 |
); |
| 965 |
#[cfg(windows)] |
| 966 |
{ |
| 967 |
let result = new_ucmd!().arg("-d").arg(r"d\XXX").fails(); |
| 968 |
result.no_stdout(); |
| 969 |
let stderr = result.stderr_str(); |
| 970 |
assert!( |
| 971 |
stderr.starts_with("mktemp: failed to create directory via template"), |
| 972 |
"{stderr}", |
| 973 |
); |
| 974 |
assert!( |
| 975 |
stderr.ends_with("d\\XXX': No such file or directory\n"), |
| 976 |
"{stderr}", |
| 977 |
); |
| 978 |
} |
| 979 |
} |
| 980 |
|
| 981 |
#[test] |
| 982 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] |
| 983 |
fn test_default_missing_value() { |
| 984 |
new_ucmd!().arg("-d").arg("--tmpdir").succeeds(); |
| 985 |
} |
| 986 |
|
| 987 |
#[test] |
| 988 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] |
| 989 |
fn test_default_issue_4821_t_tmpdir() { |
| 990 |
let scene = TestScenario::new(util_name!()); |
| 991 |
let pathname = scene.fixtures.as_string(); |
| 992 |
let result = scene |
| 993 |
.ucmd() |
| 994 |
.env(TMPDIR, &pathname) |
| 995 |
.arg("-t") |
| 996 |
.arg("foo.XXXX") |
| 997 |
.succeeds(); |
| 998 |
let stdout = result.stdout_str(); |
| 999 |
println!("stdout = {stdout}"); |
| 1000 |
assert!(stdout.contains(&pathname)); |
| 1001 |
} |
| 1002 |
|
| 1003 |
#[test] |
| 1004 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] |
| 1005 |
fn test_default_issue_4821_t_tmpdir_p() { |
| 1006 |
let scene = TestScenario::new(util_name!()); |
| 1007 |
let pathname = scene.fixtures.as_string(); |
| 1008 |
let result = scene |
| 1009 |
.ucmd() |
| 1010 |
.arg("-t") |
| 1011 |
.arg("-p") |
| 1012 |
.arg(&pathname) |
| 1013 |
.arg("foo.XXXX") |
| 1014 |
.succeeds(); |
| 1015 |
let stdout = result.stdout_str(); |
| 1016 |
println!("stdout = {stdout}"); |
| 1017 |
assert!(stdout.contains(&pathname)); |
| 1018 |
} |
| 1019 |
|
| 1020 |
#[test] |
| 1021 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] |
| 1022 |
fn test_t_ensure_tmpdir_has_higher_priority_than_p() { |
| 1023 |
let scene = TestScenario::new(util_name!()); |
| 1024 |
let pathname = scene.fixtures.as_string(); |
| 1025 |
let result = scene |
| 1026 |
.ucmd() |
| 1027 |
.env(TMPDIR, &pathname) |
| 1028 |
.arg("-t") |
| 1029 |
.arg("-p") |
| 1030 |
.arg("should_not_attempt_to_write_in_this_nonexisting_dir") |
| 1031 |
.arg("foo.XXXX") |
| 1032 |
.succeeds(); |
| 1033 |
let stdout = result.stdout_str(); |
| 1034 |
println!("stdout = {stdout}"); |
| 1035 |
assert!(stdout.contains(&pathname)); |
| 1036 |
} |
| 1037 |
|
| 1038 |
#[test] |
| 1039 |
fn test_missing_xs_tmpdir_template() { |
| 1040 |
let scene = TestScenario::new(util_name!()); |
| 1041 |
scene |
| 1042 |
.ucmd() |
| 1043 |
.arg("--tmpdir") |
| 1044 |
.arg(TEST_TEMPLATE3) |
| 1045 |
.fails() |
| 1046 |
.no_stdout() |
| 1047 |
.stderr_contains("too few X's in template"); |
| 1048 |
scene |
| 1049 |
.ucmd() |
| 1050 |
.arg("--tmpdir=foobar") |
| 1051 |
.fails() |
| 1052 |
.no_stdout() |
| 1053 |
.stderr_contains("failed to create file via template"); |
| 1054 |
} |
| 1055 |
|
| 1056 |
#[test] |
| 1057 |
fn test_both_tmpdir_flags_present() { |
| 1058 |
let scene = TestScenario::new(util_name!()); |
| 1059 |
|
| 1060 |
#[cfg(not(windows))] |
| 1061 |
let template = format!(".{MAIN_SEPARATOR}foobarXXXX"); |
| 1062 |
|
| 1063 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1064 |
let result = ucmd |
| 1065 |
.env(TMPDIR, ".") |
| 1066 |
.arg("-p") |
| 1067 |
.arg("nonsense") |
| 1068 |
.arg("--tmpdir") |
| 1069 |
.arg("foobarXXXX") |
| 1070 |
.succeeds(); |
| 1071 |
let filename = result.no_stderr().stdout_str().trim_end(); |
| 1072 |
|
| 1073 |
#[cfg(not(windows))] |
| 1074 |
assert_matches_template!(&template, filename); |
| 1075 |
#[cfg(windows)] |
| 1076 |
assert_suffix_matches_template!("foobarXXXX", filename); |
| 1077 |
|
| 1078 |
assert!(at.file_exists(filename)); |
| 1079 |
|
| 1080 |
scene |
| 1081 |
.ucmd() |
| 1082 |
.arg("-p") |
| 1083 |
.arg(".") |
| 1084 |
.arg("--tmpdir=does_not_exist") |
| 1085 |
.fails() |
| 1086 |
.no_stdout() |
| 1087 |
.stderr_contains("failed to create file via template"); |
| 1088 |
|
| 1089 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1090 |
let result = ucmd |
| 1091 |
.arg("--tmpdir") |
| 1092 |
.arg("foobarXXXX") |
| 1093 |
.arg("-p") |
| 1094 |
.arg(".") |
| 1095 |
.succeeds(); |
| 1096 |
let filename = result.no_stderr().stdout_str().trim_end(); |
| 1097 |
|
| 1098 |
#[cfg(not(windows))] |
| 1099 |
assert_matches_template!(&template, filename); |
| 1100 |
#[cfg(windows)] |
| 1101 |
assert_suffix_matches_template!("foobarXXXX", filename); |
| 1102 |
|
| 1103 |
assert!(at.file_exists(filename)); |
| 1104 |
} |
| 1105 |
|
| 1106 |
#[test] |
| 1107 |
fn test_missing_short_tmpdir_flag() { |
| 1108 |
let scene = TestScenario::new(util_name!()); |
| 1109 |
scene |
| 1110 |
.ucmd() |
| 1111 |
.arg("-p") |
| 1112 |
.fails() |
| 1113 |
.no_stdout() |
| 1114 |
.stderr_contains("a value is required for '-p <DIR>' but none was supplied"); |
| 1115 |
} |
| 1116 |
|
| 1117 |
#[test] |
| 1118 |
#[cfg(unix)] |
| 1119 |
#[cfg_attr(wasi_runner, ignore = "WASI: wasmtime rejects non-UTF-8 arguments")] |
| 1120 |
fn test_non_utf8_template() { |
| 1121 |
use std::ffi::OsStr; |
| 1122 |
use std::os::unix::ffi::OsStrExt; |
| 1123 |
|
| 1124 |
let ts = TestScenario::new(util_name!()); |
| 1125 |
|
| 1126 |
// Test that mktemp gracefully handles non-UTF-8 templates with an error instead of panicking |
| 1127 |
let template = OsStr::from_bytes(b"test_\xFF\xFE_XXXXXX"); |
| 1128 |
|
| 1129 |
ts.ucmd().arg(template).fails().stderr_contains("invalid"); |
| 1130 |
} |
| 1131 |
|
| 1132 |
#[test] |
| 1133 |
#[cfg(target_os = "linux")] |
| 1134 |
#[cfg_attr(wasi_runner, ignore = "WASI: wasmtime rejects non-UTF-8 arguments")] |
| 1135 |
fn test_non_utf8_tmpdir_path() { |
| 1136 |
use std::os::unix::ffi::OsStrExt; |
| 1137 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1138 |
|
| 1139 |
// Create a directory with non-UTF8 bytes |
| 1140 |
let dir_name = std::ffi::OsStr::from_bytes(b"test_dir_\xFF\xFE"); |
| 1141 |
std::fs::create_dir(at.plus(dir_name)).unwrap(); |
| 1142 |
|
| 1143 |
// Test that mktemp can handle non-UTF8 directory paths with -p option |
| 1144 |
ucmd.arg("-p").arg(at.plus(dir_name)).succeeds(); |
| 1145 |
} |
| 1146 |
|
| 1147 |
#[test] |
| 1148 |
#[cfg(target_os = "linux")] |
| 1149 |
#[cfg_attr(wasi_runner, ignore = "WASI: wasmtime rejects non-UTF-8 arguments")] |
| 1150 |
fn test_non_utf8_tmpdir_long_option() { |
| 1151 |
use std::os::unix::ffi::OsStrExt; |
| 1152 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1153 |
|
| 1154 |
// Create a directory with non-UTF8 bytes |
| 1155 |
let dir_name = std::ffi::OsStr::from_bytes(b"test_dir_\xFF\xFE"); |
| 1156 |
std::fs::create_dir(at.plus(dir_name)).unwrap(); |
| 1157 |
|
| 1158 |
// Test that mktemp can handle non-UTF8 directory paths with --tmpdir option |
| 1159 |
// Note: Due to test framework limitations with non-UTF8 arguments and --tmpdir= syntax, |
| 1160 |
// we'll test a more limited scenario that still validates non-UTF8 path handling |
| 1161 |
ucmd.arg("-p") |
| 1162 |
.arg(at.plus(dir_name)) |
| 1163 |
.arg("tmpXXXXXX") |
| 1164 |
.succeeds(); |
| 1165 |
} |
| 1166 |
|
| 1167 |
#[test] |
| 1168 |
#[cfg(unix)] |
| 1169 |
#[cfg_attr(wasi_runner, ignore = "WASI: wasmtime rejects non-UTF-8 arguments")] |
| 1170 |
fn test_invalid_utf8_suffix() { |
| 1171 |
use std::os::unix::ffi::OsStrExt; |
| 1172 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1173 |
|
| 1174 |
// Create invalid UTF-8 bytes for suffix |
| 1175 |
// Test mktemp with bad unicode characters |
| 1176 |
let invalid_utf8 = std::ffi::OsStr::from_bytes(b"\xC3|\xED\xBA\xAD"); |
| 1177 |
|
| 1178 |
// Test that mktemp handles invalid UTF-8 in suffix gracefully |
| 1179 |
// It should succeed and create a file with the lossy conversion of the invalid UTF-8 |
| 1180 |
ucmd.arg("-p") |
| 1181 |
.arg(at.as_string()) |
| 1182 |
.arg("--suffix") |
| 1183 |
.arg(invalid_utf8) |
| 1184 |
.arg("tmpXXXXXX") |
| 1185 |
.succeeds(); |
| 1186 |
} |
| 1187 |
|
| 1188 |
#[test] |
| 1189 |
#[cfg(target_os = "linux")] |
| 1190 |
#[cfg_attr(wasi_runner, ignore = "WASI: wasmtime rejects non-UTF-8 arguments")] |
| 1191 |
fn test_non_utf8_tmpdir_directory_creation() { |
| 1192 |
use std::os::unix::ffi::OsStrExt; |
| 1193 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1194 |
|
| 1195 |
// Create a directory with non-UTF8 bytes |
| 1196 |
let dir_name = std::ffi::OsStr::from_bytes(b"test_dir_\xFF\xFE"); |
| 1197 |
std::fs::create_dir(at.plus(dir_name)).unwrap(); |
| 1198 |
|
| 1199 |
// Test directory creation (-d flag) with non-UTF8 directory paths |
| 1200 |
// We can't easily verify the exact output path because of UTF8 conversion issues, |
| 1201 |
// but we can verify the command succeeds |
| 1202 |
ucmd.arg("-d").arg("-p").arg(at.plus(dir_name)).succeeds(); |
| 1203 |
} |
| 1204 |
|
| 1205 |
#[test] |
| 1206 |
#[cfg(unix)] |
| 1207 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] |
| 1208 |
fn test_mktemp_hidden_file_single_dot() { |
| 1209 |
let scene = TestScenario::new(util_name!()); |
| 1210 |
let dir = tempdir().unwrap(); |
| 1211 |
let template_name = ".XXXXXX"; |
| 1212 |
let template = dir.path().join(template_name); |
| 1213 |
|
| 1214 |
let result = scene.ucmd().arg(template.to_str().unwrap()).succeeds(); |
| 1215 |
|
| 1216 |
let path = result.stdout_str().trim(); |
| 1217 |
let filename = std::path::Path::new(path) |
| 1218 |
.file_name() |
| 1219 |
.unwrap() |
| 1220 |
.to_str() |
| 1221 |
.unwrap(); |
| 1222 |
|
| 1223 |
assert!( |
| 1224 |
filename.starts_with('.'), |
| 1225 |
"expected hidden file, got {path}" |
| 1226 |
); |
| 1227 |
assert_eq!( |
| 1228 |
filename.len(), |
| 1229 |
template_name.len(), |
| 1230 |
"expected filename of length {}, got {filename}", |
| 1231 |
template_name.len() |
| 1232 |
); |
| 1233 |
} |
| 1234 |
|
| 1235 |
// When the name cannot be printed the caller never learns it, so the file must |
| 1236 |
// not be left behind for nothing to clean up. |
| 1237 |
#[test] |
| 1238 |
#[cfg(target_os = "linux")] |
| 1239 |
fn test_write_error_removes_the_temporary_file() { |
| 1240 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1241 |
ucmd.arg("keep-none-XXXX") |
| 1242 |
.set_stdout(std::fs::File::create("/dev/full").unwrap()) |
| 1243 |
.fails() |
| 1244 |
.stderr_is("mktemp: write error: No space left on device\n"); |
| 1245 |
|
| 1246 |
let leftovers: Vec<_> = std::fs::read_dir(at.as_string()) |
| 1247 |
.unwrap() |
| 1248 |
.filter_map(Result::ok) |
| 1249 |
.map(|entry| entry.file_name().to_string_lossy().into_owned()) |
| 1250 |
.filter(|name| name.starts_with("keep-none-")) |
| 1251 |
.collect(); |
| 1252 |
assert!(leftovers.is_empty(), "left behind: {leftovers:?}"); |
| 1253 |
} |
| 1254 |
|
| 1255 |
#[test] |
| 1256 |
#[cfg(target_os = "linux")] |
| 1257 |
fn test_write_error_removes_the_temporary_directory() { |
| 1258 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1259 |
ucmd.args(&["-d", "keep-none-XXXX"]) |
| 1260 |
.set_stdout(std::fs::File::create("/dev/full").unwrap()) |
| 1261 |
.fails() |
| 1262 |
.stderr_is("mktemp: write error: No space left on device\n"); |
| 1263 |
|
| 1264 |
let leftovers: Vec<_> = std::fs::read_dir(at.as_string()) |
| 1265 |
.unwrap() |
| 1266 |
.filter_map(Result::ok) |
| 1267 |
.map(|entry| entry.file_name().to_string_lossy().into_owned()) |
| 1268 |
.filter(|name| name.starts_with("keep-none-")) |
| 1269 |
.collect(); |
| 1270 |
assert!(leftovers.is_empty(), "left behind: {leftovers:?}"); |
| 1271 |
} |