nushell/crates/nu-command/tests/commands/mktemp.rs

46 lines
1.1 KiB
Rust
Raw Normal View History

Add `mktemp` command (#11005) closes #10845 I've opened this a little prematurely to get some questions answered before I cleanup the code. As I started trying to better understand GNUs `mktemp` I've realized its kind of peculiar and we might want to change its behavior to introduce it to nushell. #### quiet and dry run Does it make sense to keep the `quiet` and `dry_run` flags? I don't think so. The GNU documentation says this about the dry run flag "Using the output of this command to create a new file is inherently unsafe, as there is a window of time between generating the name and using it where another process can create an object by the same name." So yeah why keep it? As far as quiet goes, does it make sense to silence the errors in nushell? #### other confusing flags According to the [gnu docs](https://www.gnu.org/software/coreutils/manual/html_node/mktemp-invocation.html), the `-t` flag is deprecated and the `-p`/ `--tempdir` are the same flag with the only difference being `--tempdir` takes an optional path, Given that, I've broken the `-p` away from `--tempdir`. Now there is one switch `--tmpdir`/`-t` and one named param `--tmpdir-path`/`-p`. GNU mktemp ``` -p DIR, --tmpdir[=DIR] interpret TEMPLATE relative to DIR; if DIR is not specified, use $TMPDIR if set, else /tmp. With this option, TEMPLATE must not be an absolute name; unlike with -t, TEMPLATE may contain slashes, but mktemp creates only the final component -t interpret TEMPLATE as a single file name component, relative to a directory: $TMPDIR, if set; else the directory specified via -p; else /tmp [deprecated] ``` to nushell mktemp ``` -p, --tmpdir-path <Filepath> # named param, must provide a path -t, --tmpdir # a switch ``` Is this a terrible idea? What should I do? --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-11-18 02:30:53 +01:00
use nu_test_support::nu;
use nu_test_support::playground::Playground;
use std::path::PathBuf;
#[test]
fn creates_temp_file() {
Playground::setup("mktemp_test_1", |dirs, _| {
let output = nu!(
cwd: dirs.test(),
"mktemp"
);
let loc = PathBuf::from(output.out.clone());
println!("{:?}", loc);
assert!(loc.exists());
})
}
#[test]
fn creates_temp_file_with_suffix() {
Playground::setup("mktemp_test_2", |dirs, _| {
let output = nu!(
cwd: dirs.test(),
"mktemp --suffix .txt tempfileXXX"
);
let loc = PathBuf::from(output.out.clone());
assert!(loc.exists());
assert!(loc.is_file());
assert!(output.out.ends_with(".txt"));
assert!(output.out.starts_with(dirs.test().to_str().unwrap()));
})
}
#[test]
fn creates_temp_directory() {
Playground::setup("mktemp_test_3", |dirs, _| {
let output = nu!(
cwd: dirs.test(),
"mktemp -d"
);
let loc = PathBuf::from(output.out);
assert!(loc.exists());
assert!(loc.is_dir());
})
}