Make the same file error more likely to appear (#12601)

# Description
When saving to a file we currently try to check if the data source in
the pipeline metadata is the same as the file we are saving to. If so,
we create an error, since reading and writing to a file at the same time
is currently not supported/handled gracefully. However, there are still
a few instances where this error is not properly triggered, and so this
PR attempts to reduce these cases. Inspired by #12599.

# Tests + Formatting
Added a few tests.

# After Submitting
Some commands still do not properly preserve metadata (e.g., `str trim`)
and so prevent us from detecting this error.
This commit is contained in:
Ian Manske
2024-04-22 01:12:13 +00:00
committed by GitHub
parent a60381a932
commit 83720a9f30
3 changed files with 104 additions and 39 deletions

View File

@ -329,6 +329,26 @@ fn save_file_correct_relative_path() {
#[test]
fn save_same_file_with_extension() {
Playground::setup("save_test_16", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
"
echo 'world'
| save --raw hello.md;
open --raw hello.md
| save --raw --force hello.md
"
)
);
assert!(actual
.err
.contains("pipeline input and output are the same file"));
})
}
#[test]
fn save_same_file_with_extension_pipeline() {
Playground::setup("save_test_17", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
"
@ -343,13 +363,33 @@ fn save_same_file_with_extension() {
assert!(actual
.err
.contains("pipeline input and output are same file"));
.contains("pipeline input and output are the same file"));
})
}
#[test]
fn save_same_file_without_extension() {
Playground::setup("save_test_17", |dirs, _sandbox| {
Playground::setup("save_test_18", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
"
echo 'world'
| save hello;
open hello
| save --force hello
"
)
);
assert!(actual
.err
.contains("pipeline input and output are the same file"));
})
}
#[test]
fn save_same_file_without_extension_pipeline() {
Playground::setup("save_test_19", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
"
@ -364,6 +404,6 @@ fn save_same_file_without_extension() {
assert!(actual
.err
.contains("pipeline input and output are same file"));
.contains("pipeline input and output are the same file"));
})
}