2021-05-30 05:10:54 +00:00
|
|
|
// spell-checker:ignore (words) gpghome
|
|
|
|
|
2020-05-25 17:05:26 +00:00
|
|
|
use crate::common::util::*;
|
2020-06-09 09:30:19 +00:00
|
|
|
|
2021-02-10 23:03:23 +00:00
|
|
|
use std::path::PathBuf;
|
2021-01-19 07:15:53 +00:00
|
|
|
use tempfile::tempdir;
|
2016-01-03 10:10:47 +00:00
|
|
|
|
2021-05-29 12:32:35 +00:00
|
|
|
static TEST_TEMPLATE1: &str = "tempXXXXXX";
|
|
|
|
static TEST_TEMPLATE2: &str = "temp";
|
|
|
|
static TEST_TEMPLATE3: &str = "tempX";
|
|
|
|
static TEST_TEMPLATE4: &str = "tempXX";
|
|
|
|
static TEST_TEMPLATE5: &str = "tempXXX";
|
2021-05-31 03:55:28 +00:00
|
|
|
static TEST_TEMPLATE6: &str = "tempXXXlate"; // spell-checker:disable-line
|
|
|
|
static TEST_TEMPLATE7: &str = "XXXtemplate"; // spell-checker:disable-line
|
2016-01-03 10:10:47 +00:00
|
|
|
#[cfg(unix)]
|
2021-05-29 12:32:35 +00:00
|
|
|
static TEST_TEMPLATE8: &str = "tempXXXl/ate";
|
2016-01-03 10:10:47 +00:00
|
|
|
#[cfg(windows)]
|
2021-05-29 12:32:35 +00:00
|
|
|
static TEST_TEMPLATE8: &str = "tempXXXl\\ate";
|
2016-01-03 10:10:47 +00:00
|
|
|
|
2021-06-22 15:36:56 +00:00
|
|
|
#[cfg(not(windows))]
|
2021-05-29 12:32:35 +00:00
|
|
|
const TMPDIR: &str = "TMPDIR";
|
2021-06-22 15:36:56 +00:00
|
|
|
#[cfg(windows)]
|
|
|
|
const TMPDIR: &str = "TMP";
|
2016-01-03 10:10:47 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mktemp_mktemp() {
|
2016-08-23 11:52:43 +00:00
|
|
|
let scene = TestScenario::new(util_name!());
|
2016-01-03 10:10:47 +00:00
|
|
|
|
2016-07-29 21:26:32 +00:00
|
|
|
let pathname = scene.fixtures.as_string();
|
2016-01-03 10:10:47 +00:00
|
|
|
|
2020-04-13 18:36:03 +00:00
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg(TEST_TEMPLATE1)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg(TEST_TEMPLATE2)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg(TEST_TEMPLATE3)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg(TEST_TEMPLATE4)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg(TEST_TEMPLATE5)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg(TEST_TEMPLATE6)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg(TEST_TEMPLATE7)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg(TEST_TEMPLATE8)
|
|
|
|
.fails();
|
2016-01-03 10:10:47 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 08:53:47 +00:00
|
|
|
#[test]
|
|
|
|
fn test_mktemp_mktemp_t() {
|
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
|
|
|
|
let pathname = scene.fixtures.as_string();
|
|
|
|
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-t")
|
|
|
|
.arg(TEST_TEMPLATE1)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-t")
|
|
|
|
.arg(TEST_TEMPLATE2)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-t")
|
|
|
|
.arg(TEST_TEMPLATE3)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-t")
|
|
|
|
.arg(TEST_TEMPLATE4)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-t")
|
|
|
|
.arg(TEST_TEMPLATE5)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-t")
|
|
|
|
.arg(TEST_TEMPLATE6)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-t")
|
|
|
|
.arg(TEST_TEMPLATE7)
|
|
|
|
.succeeds();
|
2021-04-17 12:14:49 +00:00
|
|
|
scene
|
2021-01-02 08:53:47 +00:00
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-t")
|
|
|
|
.arg(TEST_TEMPLATE8)
|
2021-04-17 12:14:49 +00:00
|
|
|
.fails()
|
|
|
|
.no_stdout()
|
2021-06-28 11:45:15 +00:00
|
|
|
.stderr_contains("invalid suffix")
|
|
|
|
.stderr_contains("contains directory separator");
|
2021-01-02 08:53:47 +00:00
|
|
|
}
|
|
|
|
|
2016-06-18 09:44:07 +00:00
|
|
|
#[test]
|
|
|
|
fn test_mktemp_make_temp_dir() {
|
2016-08-23 11:52:43 +00:00
|
|
|
let scene = TestScenario::new(util_name!());
|
2016-06-18 09:44:07 +00:00
|
|
|
|
2016-07-29 21:26:32 +00:00
|
|
|
let pathname = scene.fixtures.as_string();
|
2016-06-18 09:44:07 +00:00
|
|
|
|
2020-04-13 18:36:03 +00:00
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-d")
|
|
|
|
.arg(TEST_TEMPLATE1)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-d")
|
|
|
|
.arg(TEST_TEMPLATE2)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-d")
|
|
|
|
.arg(TEST_TEMPLATE3)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-d")
|
|
|
|
.arg(TEST_TEMPLATE4)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-d")
|
|
|
|
.arg(TEST_TEMPLATE5)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-d")
|
|
|
|
.arg(TEST_TEMPLATE6)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-d")
|
|
|
|
.arg(TEST_TEMPLATE7)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-d")
|
|
|
|
.arg(TEST_TEMPLATE8)
|
|
|
|
.fails();
|
2016-06-18 09:44:07 +00:00
|
|
|
}
|
2016-01-03 17:11:15 +00:00
|
|
|
|
2016-01-03 10:10:47 +00:00
|
|
|
#[test]
|
|
|
|
fn test_mktemp_dry_run() {
|
2016-08-23 11:52:43 +00:00
|
|
|
let scene = TestScenario::new(util_name!());
|
2016-01-03 10:10:47 +00:00
|
|
|
|
2016-07-29 21:26:32 +00:00
|
|
|
let pathname = scene.fixtures.as_string();
|
2016-01-03 10:10:47 +00:00
|
|
|
|
2020-04-13 18:36:03 +00:00
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-u")
|
|
|
|
.arg(TEST_TEMPLATE1)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-u")
|
|
|
|
.arg(TEST_TEMPLATE2)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-u")
|
|
|
|
.arg(TEST_TEMPLATE3)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-u")
|
|
|
|
.arg(TEST_TEMPLATE4)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-u")
|
|
|
|
.arg(TEST_TEMPLATE5)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-u")
|
|
|
|
.arg(TEST_TEMPLATE6)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-u")
|
|
|
|
.arg(TEST_TEMPLATE7)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("-u")
|
|
|
|
.arg(TEST_TEMPLATE8)
|
|
|
|
.fails();
|
2016-01-03 10:10:47 +00:00
|
|
|
}
|
|
|
|
|
2016-06-18 09:44:07 +00:00
|
|
|
#[test]
|
|
|
|
fn test_mktemp_quiet() {
|
2016-08-23 11:52:43 +00:00
|
|
|
let scene = TestScenario::new(util_name!());
|
2016-06-18 09:44:07 +00:00
|
|
|
|
2020-04-13 18:36:03 +00:00
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.arg("-p")
|
|
|
|
.arg("/definitely/not/exist/I/promise")
|
|
|
|
.arg("-q")
|
|
|
|
.fails()
|
|
|
|
.no_stdout()
|
|
|
|
.no_stderr();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.arg("-d")
|
|
|
|
.arg("-p")
|
|
|
|
.arg("/definitely/not/exist/I/promise")
|
|
|
|
.arg("-q")
|
|
|
|
.fails()
|
|
|
|
.no_stdout()
|
|
|
|
.no_stderr();
|
2016-06-18 09:44:07 +00:00
|
|
|
}
|
2016-01-03 10:10:47 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mktemp_suffix() {
|
2016-08-23 11:52:43 +00:00
|
|
|
let scene = TestScenario::new(util_name!());
|
2016-01-03 10:10:47 +00:00
|
|
|
|
2016-07-29 21:26:32 +00:00
|
|
|
let pathname = scene.fixtures.as_string();
|
2016-01-03 17:11:15 +00:00
|
|
|
|
2020-04-13 18:36:03 +00:00
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("--suffix")
|
|
|
|
.arg("suf")
|
|
|
|
.arg(TEST_TEMPLATE1)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("--suffix")
|
|
|
|
.arg("suf")
|
|
|
|
.arg(TEST_TEMPLATE2)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("--suffix")
|
|
|
|
.arg("suf")
|
|
|
|
.arg(TEST_TEMPLATE3)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("--suffix")
|
|
|
|
.arg("suf")
|
|
|
|
.arg(TEST_TEMPLATE4)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("--suffix")
|
|
|
|
.arg("suf")
|
|
|
|
.arg(TEST_TEMPLATE5)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("--suffix")
|
|
|
|
.arg("suf")
|
|
|
|
.arg(TEST_TEMPLATE6)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("--suffix")
|
|
|
|
.arg("suf")
|
|
|
|
.arg(TEST_TEMPLATE7)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.env(TMPDIR, &pathname)
|
|
|
|
.arg("--suffix")
|
|
|
|
.arg("suf")
|
|
|
|
.arg(TEST_TEMPLATE8)
|
|
|
|
.fails();
|
2016-01-03 10:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mktemp_tmpdir() {
|
2016-08-23 11:52:43 +00:00
|
|
|
let scene = TestScenario::new(util_name!());
|
2020-06-09 09:30:19 +00:00
|
|
|
let dir = tempdir().unwrap();
|
|
|
|
let path = dir.path().join(scene.fixtures.as_string());
|
|
|
|
let pathname = path.as_os_str();
|
2016-08-13 21:59:21 +00:00
|
|
|
|
2020-04-13 18:36:03 +00:00
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.arg("-p")
|
|
|
|
.arg(pathname)
|
|
|
|
.arg(TEST_TEMPLATE1)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.arg("-p")
|
|
|
|
.arg(pathname)
|
|
|
|
.arg(TEST_TEMPLATE2)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.arg("-p")
|
|
|
|
.arg(pathname)
|
|
|
|
.arg(TEST_TEMPLATE3)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.arg("-p")
|
|
|
|
.arg(pathname)
|
|
|
|
.arg(TEST_TEMPLATE4)
|
|
|
|
.fails();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.arg("-p")
|
|
|
|
.arg(pathname)
|
|
|
|
.arg(TEST_TEMPLATE5)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.arg("-p")
|
|
|
|
.arg(pathname)
|
|
|
|
.arg(TEST_TEMPLATE6)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.arg("-p")
|
|
|
|
.arg(pathname)
|
|
|
|
.arg(TEST_TEMPLATE7)
|
|
|
|
.succeeds();
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.arg("-p")
|
|
|
|
.arg(pathname)
|
|
|
|
.arg(TEST_TEMPLATE8)
|
|
|
|
.fails();
|
2016-01-03 10:10:47 +00:00
|
|
|
}
|
2021-02-10 23:03:23 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mktemp_tmpdir_one_arg() {
|
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
|
|
|
|
let result = scene
|
2021-06-21 19:13:40 +00:00
|
|
|
.ucmd_keepenv()
|
2021-02-10 23:03:23 +00:00
|
|
|
.arg("--tmpdir")
|
|
|
|
.arg("apt-key-gpghome.XXXXXXXXXX")
|
|
|
|
.succeeds();
|
2021-04-17 23:33:52 +00:00
|
|
|
result.no_stderr().stdout_contains("apt-key-gpghome.");
|
2021-04-17 12:14:49 +00:00
|
|
|
assert!(PathBuf::from(result.stdout_str().trim()).is_file());
|
2021-02-10 23:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mktemp_directory_tmpdir() {
|
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
|
|
|
|
let result = scene
|
2021-06-21 19:13:40 +00:00
|
|
|
.ucmd_keepenv()
|
2021-02-10 23:03:23 +00:00
|
|
|
.arg("--directory")
|
|
|
|
.arg("--tmpdir")
|
|
|
|
.arg("apt-key-gpghome.XXXXXXXXXX")
|
|
|
|
.succeeds();
|
2021-04-17 12:14:49 +00:00
|
|
|
result.no_stderr().stdout_contains("apt-key-gpghome.");
|
|
|
|
assert!(PathBuf::from(result.stdout_str().trim()).is_dir());
|
2021-02-10 23:03:23 +00:00
|
|
|
}
|
2022-05-01 16:03:02 +00:00
|
|
|
|
|
|
|
/// Decide whether a string matches a given template.
|
|
|
|
///
|
|
|
|
/// In the template, the character `'X'` is treated as a wildcard,
|
|
|
|
/// that is, it matches anything. All other characters in `template`
|
|
|
|
/// and `s` must match exactly.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// # These all match.
|
|
|
|
/// assert!(matches_template("abc", "abc"));
|
|
|
|
/// assert!(matches_template("aXc", "abc"));
|
|
|
|
/// assert!(matches_template("XXX", "abc"));
|
|
|
|
///
|
|
|
|
/// # None of these match
|
|
|
|
/// assert!(matches_template("abc", "abcd"));
|
|
|
|
/// assert!(matches_template("abc", "ab"));
|
|
|
|
/// assert!(matches_template("aXc", "abd"));
|
|
|
|
/// assert!(matches_template("XXX", "abcd"));
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
fn matches_template(template: &str, s: &str) -> bool {
|
|
|
|
if template.len() != s.len() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (a, b) in template.chars().zip(s.chars()) {
|
|
|
|
if !(a == 'X' || a == b) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An assertion that uses [`matches_template`] and adds a helpful error message.
|
|
|
|
macro_rules! assert_matches_template {
|
|
|
|
($template:expr, $s:expr) => {{
|
|
|
|
assert!(
|
|
|
|
matches_template($template, $s),
|
|
|
|
"\"{}\" != \"{}\"",
|
|
|
|
$template,
|
|
|
|
$s
|
|
|
|
);
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test that the file is created in the directory given by the template.
|
|
|
|
#[test]
|
|
|
|
fn test_respect_template() {
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
let template = "XXX";
|
|
|
|
let result = ucmd.arg(template).succeeds();
|
|
|
|
let filename = result.no_stderr().stdout_str().trim_end();
|
|
|
|
assert_matches_template!(template, filename);
|
|
|
|
assert!(at.file_exists(filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test that the file is created in the directory given by the template.
|
|
|
|
#[test]
|
|
|
|
fn test_respect_template_directory() {
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
at.mkdir("d");
|
|
|
|
let template = "d/XXX";
|
|
|
|
let result = ucmd.arg(template).succeeds();
|
|
|
|
let filename = result.no_stderr().stdout_str().trim_end();
|
|
|
|
assert_matches_template!(template, filename);
|
|
|
|
assert!(at.file_exists(filename));
|
|
|
|
}
|