2022-11-26 19:19:02 +01:00
|
|
|
use chrono::{DateTime, Local};
|
ls, rm, cp, open, touch, mkdir: Don't expand tilde if input path is quoted string or a variable. (#12232)
# Description
Fixes: #11887
Fixes: #11626
This pr unify the tilde expand behavior over several filesystem relative
commands. It follows the same rule with glob expansion:
| command | result |
| ----------- | ------ |
| ls ~/aaa | expand tilde
| ls "~/aaa" | don't expand tilde
| let f = "~/aaa"; ls $f | don't expand tilde, if you want to: use `ls
($f \| path expand)`
| let f: glob = "~/aaa"; ls $f | expand tilde, they don't expand on
`mkdir`, `touch` comamnd.
Actually I'm not sure for 4th item, currently it's expanding is just
because it followes the same rule with glob expansion.
### About the change
It changes `expand_path_with` to accept a new argument called
`expand_tilde`, if it's true, expand it, if not, just keep it as `~`
itself.
# User-Facing Changes
After this change, `ls "~/aaa"` won't expand tilde.
# Tests + Formatting
Done
2024-03-25 03:08:38 +01:00
|
|
|
use nu_test_support::fs::{files_exist_at, Stub};
|
2020-02-18 21:54:32 +01:00
|
|
|
use nu_test_support::nu;
|
no deref in touch (#14214)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Adds --no-deref flag to `touch`. Nice and backwards compatible, and I
get to touch symlinks. I still don't get to set their dates directly,
but maybe that'll come with utouch.
Some sadness in the implementation, since `set_symlink_file_times`
doesn't take Option values and we call it twice with the old "read"
values from reference (or now, if missing). This shouldn't be a big
concern since `touch` already did two calls if you set both mtime and
atime. Also, `--no-deref` applies both to the reference file, and to the
target file. No splitting them up, because that's silly.
Can always bikeshed. I nicked `--no-deref` from the uutils flag, and
made the short flag `-d` because it obviously can't be `-h`. I thought
of `-S` like in `glob`, for the "negative/filter out" uppercase short
letters. Ultimately I don't think it matters much.
Should fix #14212 since it's not really tied to uutils, besides the
comment about setting a `datetime` value directly.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
New flag.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the
tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
Maybe.
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-11-03 05:56:05 +01:00
|
|
|
use nu_test_support::playground::{Dirs, Playground};
|
2020-02-18 21:54:32 +01:00
|
|
|
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
// Use 1 instead of 0 because 0 has a special meaning in Windows
|
|
|
|
const TIME_ONE: filetime::FileTime = filetime::FileTime::from_unix_time(1, 0);
|
|
|
|
|
2020-02-18 21:54:32 +01:00
|
|
|
#[test]
|
2020-03-01 18:19:09 +01:00
|
|
|
fn creates_a_file_when_it_doesnt_exist() {
|
|
|
|
Playground::setup("create_test_1", |dirs, _sandbox| {
|
2020-02-18 21:54:32 +01:00
|
|
|
nu!(
|
2020-03-01 18:19:09 +01:00
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch i_will_be_created.txt"
|
2020-02-18 21:54:32 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
let path = dirs.test().join("i_will_be_created.txt");
|
2020-08-22 02:08:30 +02:00
|
|
|
assert!(path.exists());
|
|
|
|
})
|
|
|
|
}
|
2020-02-18 21:54:32 +01:00
|
|
|
|
2020-08-22 02:08:30 +02:00
|
|
|
#[test]
|
|
|
|
fn creates_two_files() {
|
|
|
|
Playground::setup("create_test_2", |dirs, _sandbox| {
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch a b"
|
|
|
|
);
|
|
|
|
|
|
|
|
let path = dirs.test().join("a");
|
2020-02-18 21:54:32 +01:00
|
|
|
assert!(path.exists());
|
2020-08-22 02:08:30 +02:00
|
|
|
|
|
|
|
let path2 = dirs.test().join("b");
|
|
|
|
assert!(path2.exists());
|
2020-02-18 21:54:32 +01:00
|
|
|
})
|
|
|
|
}
|
2022-04-07 13:44:05 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_modified_time_of_file_to_today() {
|
|
|
|
Playground::setup("change_time_test_9", |dirs, sandbox| {
|
2024-05-04 02:53:15 +02:00
|
|
|
sandbox.with_files(&[Stub::EmptyFile("file.txt")]);
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
let path = dirs.test().join("file.txt");
|
|
|
|
|
|
|
|
// Set file.txt's times to the past before the test to make sure `touch` actually changes the mtime to today
|
|
|
|
filetime::set_file_times(&path, TIME_ONE, TIME_ONE).unwrap();
|
2022-04-07 13:44:05 +02:00
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch -m file.txt"
|
|
|
|
);
|
|
|
|
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
let metadata = path.metadata().unwrap();
|
2022-04-07 13:44:05 +02:00
|
|
|
|
|
|
|
// Check only the date since the time may not match exactly
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
let today = Local::now().date_naive();
|
|
|
|
let mtime_day = DateTime::<Local>::from(metadata.modified().unwrap()).date_naive();
|
|
|
|
|
|
|
|
assert_eq!(today, mtime_day);
|
2022-04-07 13:44:05 +02:00
|
|
|
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
// Check that atime remains unchanged
|
|
|
|
assert_eq!(
|
|
|
|
TIME_ONE,
|
|
|
|
filetime::FileTime::from_system_time(metadata.accessed().unwrap())
|
|
|
|
);
|
2022-04-07 13:44:05 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_access_time_of_file_to_today() {
|
|
|
|
Playground::setup("change_time_test_18", |dirs, sandbox| {
|
2024-05-04 02:53:15 +02:00
|
|
|
sandbox.with_files(&[Stub::EmptyFile("file.txt")]);
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
let path = dirs.test().join("file.txt");
|
|
|
|
|
|
|
|
// Set file.txt's times to the past before the test to make sure `touch` actually changes the atime to today
|
|
|
|
filetime::set_file_times(&path, TIME_ONE, TIME_ONE).unwrap();
|
2022-04-07 13:44:05 +02:00
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch -a file.txt"
|
|
|
|
);
|
|
|
|
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
let metadata = path.metadata().unwrap();
|
2022-04-07 13:44:05 +02:00
|
|
|
|
|
|
|
// Check only the date since the time may not match exactly
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
let today = Local::now().date_naive();
|
|
|
|
let atime_day = DateTime::<Local>::from(metadata.accessed().unwrap()).date_naive();
|
2022-04-07 13:44:05 +02:00
|
|
|
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
assert_eq!(today, atime_day);
|
|
|
|
|
|
|
|
// Check that mtime remains unchanged
|
|
|
|
assert_eq!(
|
|
|
|
TIME_ONE,
|
|
|
|
filetime::FileTime::from_system_time(metadata.modified().unwrap())
|
|
|
|
);
|
2022-04-07 13:44:05 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_modified_and_access_time_of_file_to_today() {
|
|
|
|
Playground::setup("change_time_test_27", |dirs, sandbox| {
|
2024-05-04 02:53:15 +02:00
|
|
|
sandbox.with_files(&[Stub::EmptyFile("file.txt")]);
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
let path = dirs.test().join("file.txt");
|
|
|
|
|
|
|
|
filetime::set_file_times(&path, TIME_ONE, TIME_ONE).unwrap();
|
2022-04-07 13:44:05 +02:00
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch -a -m file.txt"
|
|
|
|
);
|
|
|
|
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
let metadata = path.metadata().unwrap();
|
2022-04-07 13:44:05 +02:00
|
|
|
|
|
|
|
// Check only the date since the time may not match exactly
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
let today = Local::now().date_naive();
|
|
|
|
let mtime_day = DateTime::<Local>::from(metadata.modified().unwrap()).date_naive();
|
|
|
|
let atime_day = DateTime::<Local>::from(metadata.accessed().unwrap()).date_naive();
|
2022-04-07 13:44:05 +02:00
|
|
|
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
assert_eq!(today, mtime_day);
|
|
|
|
assert_eq!(today, atime_day);
|
2022-04-07 13:44:05 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_create_file_if_it_not_exists() {
|
|
|
|
Playground::setup("change_time_test_28", |dirs, _sandbox| {
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
let outcome = nu!(
|
2022-04-07 13:44:05 +02:00
|
|
|
cwd: dirs.test(),
|
2023-07-21 17:32:37 +02:00
|
|
|
"touch -c file.txt"
|
2022-04-07 13:44:05 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
let path = dirs.test().join("file.txt");
|
|
|
|
|
|
|
|
assert!(!path.exists());
|
|
|
|
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
// If --no-create is improperly handled `touch` may error when trying to change the times of a nonexistent file
|
|
|
|
assert!(outcome.status.success())
|
2022-04-07 13:44:05 +02:00
|
|
|
})
|
|
|
|
}
|
2023-04-05 19:22:56 +02:00
|
|
|
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
#[test]
|
|
|
|
fn change_file_times_if_exists_with_no_create() {
|
|
|
|
Playground::setup(
|
|
|
|
"change_file_times_if_exists_with_no_create",
|
|
|
|
|dirs, sandbox| {
|
2024-05-04 02:53:15 +02:00
|
|
|
sandbox.with_files(&[Stub::EmptyFile("file.txt")]);
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
let path = dirs.test().join("file.txt");
|
|
|
|
|
|
|
|
filetime::set_file_times(&path, TIME_ONE, TIME_ONE).unwrap();
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch -c file.txt"
|
|
|
|
);
|
|
|
|
|
|
|
|
let metadata = path.metadata().unwrap();
|
|
|
|
|
|
|
|
// Check only the date since the time may not match exactly
|
|
|
|
let today = Local::now().date_naive();
|
|
|
|
let mtime_day = DateTime::<Local>::from(metadata.modified().unwrap()).date_naive();
|
|
|
|
let atime_day = DateTime::<Local>::from(metadata.accessed().unwrap()).date_naive();
|
|
|
|
|
|
|
|
assert_eq!(today, mtime_day);
|
|
|
|
assert_eq!(today, atime_day);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-04-05 19:22:56 +02:00
|
|
|
#[test]
|
|
|
|
fn creates_file_three_dots() {
|
|
|
|
Playground::setup("create_test_1", |dirs, _sandbox| {
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch file..."
|
|
|
|
);
|
|
|
|
|
|
|
|
let path = dirs.test().join("file...");
|
|
|
|
assert!(path.exists());
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn creates_file_four_dots() {
|
|
|
|
Playground::setup("create_test_1", |dirs, _sandbox| {
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch file...."
|
|
|
|
);
|
|
|
|
|
|
|
|
let path = dirs.test().join("file....");
|
|
|
|
assert!(path.exists());
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn creates_file_four_dots_quotation_marks() {
|
|
|
|
Playground::setup("create_test_1", |dirs, _sandbox| {
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch 'file....'"
|
|
|
|
);
|
|
|
|
|
|
|
|
let path = dirs.test().join("file....");
|
|
|
|
assert!(path.exists());
|
|
|
|
})
|
|
|
|
}
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_file_times_to_reference_file() {
|
|
|
|
Playground::setup("change_dir_times_to_reference_dir", |dirs, sandbox| {
|
2024-05-04 02:53:15 +02:00
|
|
|
sandbox.with_files(&[
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
Stub::EmptyFile("reference_file"),
|
|
|
|
Stub::EmptyFile("target_file"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let reference = dirs.test().join("reference_file");
|
|
|
|
let target = dirs.test().join("target_file");
|
|
|
|
|
|
|
|
// Change the times for reference
|
|
|
|
filetime::set_file_times(
|
|
|
|
&reference,
|
|
|
|
filetime::FileTime::from_unix_time(1337, 0),
|
|
|
|
TIME_ONE,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// target should have today's date since it was just created, but reference should be different
|
|
|
|
assert_ne!(
|
|
|
|
reference.metadata().unwrap().accessed().unwrap(),
|
|
|
|
target.metadata().unwrap().accessed().unwrap()
|
|
|
|
);
|
|
|
|
assert_ne!(
|
|
|
|
reference.metadata().unwrap().modified().unwrap(),
|
|
|
|
target.metadata().unwrap().modified().unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch -r reference_file target_file"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
reference.metadata().unwrap().accessed().unwrap(),
|
|
|
|
target.metadata().unwrap().accessed().unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
reference.metadata().unwrap().modified().unwrap(),
|
|
|
|
target.metadata().unwrap().modified().unwrap()
|
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_file_mtime_to_reference() {
|
|
|
|
Playground::setup("change_file_mtime_to_reference", |dirs, sandbox| {
|
2024-05-04 02:53:15 +02:00
|
|
|
sandbox.with_files(&[
|
Fix `touch` to allow changing timestamps on directories, remake from #11760 (#12005)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Based off of #11760 to be mergable without conflicts.
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Fix for #11757.
The main issue in #11757 is I tried to copy the timestamp from one
directory to another only to realize that did not work whereas the
coreutils `^touch` had no problems. I thought `--reference` just did not
work, but apparently the whole `touch` command could not work on
directories because
`OpenOptions::new().write(true).create(true).open(&item)` tries to
create `touch`'s target in advance and then modify its timestamps. But
if the target is a directory that already exists then this would fail
even though the crate used for working with timestamps, `filetime`,
already works on directories.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
I don't believe this should change any existing valid behaviors. It just
changes a non-working behavior.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
~~I only could not run `cargo test` because I get compilation errors on
the latest main branch~~
All tests pass with `cargo test --features=sqlite`
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-03-01 14:23:03 +01:00
|
|
|
Stub::EmptyFile("reference_file"),
|
|
|
|
Stub::EmptyFile("target_file"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let reference = dirs.test().join("reference_file");
|
|
|
|
let target = dirs.test().join("target_file");
|
|
|
|
|
|
|
|
// Change the times for reference
|
|
|
|
filetime::set_file_times(
|
|
|
|
&reference,
|
|
|
|
TIME_ONE,
|
|
|
|
filetime::FileTime::from_unix_time(1337, 0),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// target should have today's date since it was just created, but reference should be different
|
|
|
|
assert_ne!(
|
|
|
|
reference.metadata().unwrap().accessed().unwrap(),
|
|
|
|
target.metadata().unwrap().accessed().unwrap()
|
|
|
|
);
|
|
|
|
assert_ne!(
|
|
|
|
reference.metadata().unwrap().modified().unwrap(),
|
|
|
|
target.metadata().unwrap().modified().unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Save target's current atime to make sure it is preserved
|
|
|
|
let target_original_atime = target.metadata().unwrap().accessed().unwrap();
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch -mr reference_file target_file"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
reference.metadata().unwrap().modified().unwrap(),
|
|
|
|
target.metadata().unwrap().modified().unwrap()
|
|
|
|
);
|
|
|
|
assert_ne!(
|
|
|
|
reference.metadata().unwrap().accessed().unwrap(),
|
|
|
|
target.metadata().unwrap().accessed().unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
target_original_atime,
|
|
|
|
target.metadata().unwrap().accessed().unwrap()
|
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_modified_time_of_dir_to_today() {
|
|
|
|
Playground::setup("change_dir_mtime", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("test_dir");
|
|
|
|
let path = dirs.test().join("test_dir");
|
|
|
|
|
|
|
|
filetime::set_file_mtime(&path, TIME_ONE).unwrap();
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch -m test_dir"
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check only the date since the time may not match exactly
|
|
|
|
let today = Local::now().date_naive();
|
|
|
|
let mtime_day =
|
|
|
|
DateTime::<Local>::from(path.metadata().unwrap().modified().unwrap()).date_naive();
|
|
|
|
|
|
|
|
assert_eq!(today, mtime_day);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_access_time_of_dir_to_today() {
|
|
|
|
Playground::setup("change_dir_atime", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("test_dir");
|
|
|
|
let path = dirs.test().join("test_dir");
|
|
|
|
|
|
|
|
filetime::set_file_atime(&path, TIME_ONE).unwrap();
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch -a test_dir"
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check only the date since the time may not match exactly
|
|
|
|
let today = Local::now().date_naive();
|
|
|
|
let atime_day =
|
|
|
|
DateTime::<Local>::from(path.metadata().unwrap().accessed().unwrap()).date_naive();
|
|
|
|
|
|
|
|
assert_eq!(today, atime_day);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_modified_and_access_time_of_dir_to_today() {
|
|
|
|
Playground::setup("change_dir_times", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("test_dir");
|
|
|
|
let path = dirs.test().join("test_dir");
|
|
|
|
|
|
|
|
filetime::set_file_times(&path, TIME_ONE, TIME_ONE).unwrap();
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch -a -m test_dir"
|
|
|
|
);
|
|
|
|
|
|
|
|
let metadata = path.metadata().unwrap();
|
|
|
|
|
|
|
|
// Check only the date since the time may not match exactly
|
|
|
|
let today = Local::now().date_naive();
|
|
|
|
let mtime_day = DateTime::<Local>::from(metadata.modified().unwrap()).date_naive();
|
|
|
|
let atime_day = DateTime::<Local>::from(metadata.accessed().unwrap()).date_naive();
|
|
|
|
|
|
|
|
assert_eq!(today, mtime_day);
|
|
|
|
assert_eq!(today, atime_day);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_dir_three_dots_times() {
|
|
|
|
Playground::setup("change_dir_three_dots_times", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("test_dir...");
|
|
|
|
let path = dirs.test().join("test_dir...");
|
|
|
|
|
|
|
|
filetime::set_file_times(&path, TIME_ONE, TIME_ONE).unwrap();
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch test_dir..."
|
|
|
|
);
|
|
|
|
|
|
|
|
let metadata = path.metadata().unwrap();
|
|
|
|
|
|
|
|
// Check only the date since the time may not match exactly
|
|
|
|
let today = Local::now().date_naive();
|
|
|
|
let mtime_day = DateTime::<Local>::from(metadata.modified().unwrap()).date_naive();
|
|
|
|
let atime_day = DateTime::<Local>::from(metadata.accessed().unwrap()).date_naive();
|
|
|
|
|
|
|
|
assert_eq!(today, mtime_day);
|
|
|
|
assert_eq!(today, atime_day);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_dir_times_to_reference_dir() {
|
|
|
|
Playground::setup("change_dir_times_to_reference_dir", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("reference_dir");
|
|
|
|
sandbox.mkdir("target_dir");
|
|
|
|
|
|
|
|
let reference = dirs.test().join("reference_dir");
|
|
|
|
let target = dirs.test().join("target_dir");
|
|
|
|
|
|
|
|
// Change the times for reference
|
|
|
|
filetime::set_file_times(
|
|
|
|
&reference,
|
|
|
|
filetime::FileTime::from_unix_time(1337, 0),
|
|
|
|
TIME_ONE,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// target should have today's date since it was just created, but reference should be different
|
|
|
|
assert_ne!(
|
|
|
|
reference.metadata().unwrap().accessed().unwrap(),
|
|
|
|
target.metadata().unwrap().accessed().unwrap()
|
|
|
|
);
|
|
|
|
assert_ne!(
|
|
|
|
reference.metadata().unwrap().modified().unwrap(),
|
|
|
|
target.metadata().unwrap().modified().unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch -r reference_dir target_dir"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
reference.metadata().unwrap().accessed().unwrap(),
|
|
|
|
target.metadata().unwrap().accessed().unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
reference.metadata().unwrap().modified().unwrap(),
|
|
|
|
target.metadata().unwrap().modified().unwrap()
|
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_dir_atime_to_reference() {
|
|
|
|
Playground::setup("change_dir_atime_to_reference", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("reference_dir");
|
|
|
|
sandbox.mkdir("target_dir");
|
|
|
|
|
|
|
|
let reference = dirs.test().join("reference_dir");
|
|
|
|
let target = dirs.test().join("target_dir");
|
|
|
|
|
|
|
|
// Change the times for reference
|
|
|
|
filetime::set_file_times(
|
|
|
|
&reference,
|
|
|
|
filetime::FileTime::from_unix_time(1337, 0),
|
|
|
|
TIME_ONE,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// target should have today's date since it was just created, but reference should be different
|
|
|
|
assert_ne!(
|
|
|
|
reference.metadata().unwrap().accessed().unwrap(),
|
|
|
|
target.metadata().unwrap().accessed().unwrap()
|
|
|
|
);
|
|
|
|
assert_ne!(
|
|
|
|
reference.metadata().unwrap().modified().unwrap(),
|
|
|
|
target.metadata().unwrap().modified().unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Save target's current mtime to make sure it is preserved
|
|
|
|
let target_original_mtime = target.metadata().unwrap().modified().unwrap();
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"touch -ar reference_dir target_dir"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
reference.metadata().unwrap().accessed().unwrap(),
|
|
|
|
target.metadata().unwrap().accessed().unwrap()
|
|
|
|
);
|
|
|
|
assert_ne!(
|
|
|
|
reference.metadata().unwrap().modified().unwrap(),
|
|
|
|
target.metadata().unwrap().modified().unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
target_original_mtime,
|
|
|
|
target.metadata().unwrap().modified().unwrap()
|
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
ls, rm, cp, open, touch, mkdir: Don't expand tilde if input path is quoted string or a variable. (#12232)
# Description
Fixes: #11887
Fixes: #11626
This pr unify the tilde expand behavior over several filesystem relative
commands. It follows the same rule with glob expansion:
| command | result |
| ----------- | ------ |
| ls ~/aaa | expand tilde
| ls "~/aaa" | don't expand tilde
| let f = "~/aaa"; ls $f | don't expand tilde, if you want to: use `ls
($f \| path expand)`
| let f: glob = "~/aaa"; ls $f | expand tilde, they don't expand on
`mkdir`, `touch` comamnd.
Actually I'm not sure for 4th item, currently it's expanding is just
because it followes the same rule with glob expansion.
### About the change
It changes `expand_path_with` to accept a new argument called
`expand_tilde`, if it's true, expand it, if not, just keep it as `~`
itself.
# User-Facing Changes
After this change, `ls "~/aaa"` won't expand tilde.
# Tests + Formatting
Done
2024-03-25 03:08:38 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_a_file_with_tilde() {
|
|
|
|
Playground::setup("touch with tilde", |dirs, _| {
|
|
|
|
let actual = nu!(cwd: dirs.test(), "touch '~tilde'");
|
|
|
|
assert!(actual.err.is_empty());
|
2024-08-03 10:09:13 +02:00
|
|
|
assert!(files_exist_at(&["~tilde"], dirs.test()));
|
ls, rm, cp, open, touch, mkdir: Don't expand tilde if input path is quoted string or a variable. (#12232)
# Description
Fixes: #11887
Fixes: #11626
This pr unify the tilde expand behavior over several filesystem relative
commands. It follows the same rule with glob expansion:
| command | result |
| ----------- | ------ |
| ls ~/aaa | expand tilde
| ls "~/aaa" | don't expand tilde
| let f = "~/aaa"; ls $f | don't expand tilde, if you want to: use `ls
($f \| path expand)`
| let f: glob = "~/aaa"; ls $f | expand tilde, they don't expand on
`mkdir`, `touch` comamnd.
Actually I'm not sure for 4th item, currently it's expanding is just
because it followes the same rule with glob expansion.
### About the change
It changes `expand_path_with` to accept a new argument called
`expand_tilde`, if it's true, expand it, if not, just keep it as `~`
itself.
# User-Facing Changes
After this change, `ls "~/aaa"` won't expand tilde.
# Tests + Formatting
Done
2024-03-25 03:08:38 +01:00
|
|
|
|
|
|
|
// pass variable
|
|
|
|
let actual = nu!(cwd: dirs.test(), "let f = '~tilde2'; touch $f");
|
|
|
|
assert!(actual.err.is_empty());
|
2024-08-03 10:09:13 +02:00
|
|
|
assert!(files_exist_at(&["~tilde2"], dirs.test()));
|
ls, rm, cp, open, touch, mkdir: Don't expand tilde if input path is quoted string or a variable. (#12232)
# Description
Fixes: #11887
Fixes: #11626
This pr unify the tilde expand behavior over several filesystem relative
commands. It follows the same rule with glob expansion:
| command | result |
| ----------- | ------ |
| ls ~/aaa | expand tilde
| ls "~/aaa" | don't expand tilde
| let f = "~/aaa"; ls $f | don't expand tilde, if you want to: use `ls
($f \| path expand)`
| let f: glob = "~/aaa"; ls $f | expand tilde, they don't expand on
`mkdir`, `touch` comamnd.
Actually I'm not sure for 4th item, currently it's expanding is just
because it followes the same rule with glob expansion.
### About the change
It changes `expand_path_with` to accept a new argument called
`expand_tilde`, if it's true, expand it, if not, just keep it as `~`
itself.
# User-Facing Changes
After this change, `ls "~/aaa"` won't expand tilde.
# Tests + Formatting
Done
2024-03-25 03:08:38 +01:00
|
|
|
})
|
|
|
|
}
|
2024-04-04 14:23:10 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn respects_cwd() {
|
|
|
|
Playground::setup("touch_respects_cwd", |dirs, _sandbox| {
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"mkdir 'dir'; cd 'dir'; touch 'i_will_be_created.txt'"
|
|
|
|
);
|
|
|
|
|
|
|
|
let path = dirs.test().join("dir/i_will_be_created.txt");
|
|
|
|
assert!(path.exists());
|
|
|
|
})
|
|
|
|
}
|
2024-05-26 19:24:00 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reference_respects_cwd() {
|
|
|
|
Playground::setup("touch_reference_respects_cwd", |dirs, _sandbox| {
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"mkdir 'dir'; cd 'dir'; touch 'ref.txt'; touch --reference 'ref.txt' 'foo.txt'"
|
|
|
|
);
|
|
|
|
|
|
|
|
let path = dirs.test().join("dir/foo.txt");
|
|
|
|
assert!(path.exists());
|
|
|
|
})
|
|
|
|
}
|
no deref in touch (#14214)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Adds --no-deref flag to `touch`. Nice and backwards compatible, and I
get to touch symlinks. I still don't get to set their dates directly,
but maybe that'll come with utouch.
Some sadness in the implementation, since `set_symlink_file_times`
doesn't take Option values and we call it twice with the old "read"
values from reference (or now, if missing). This shouldn't be a big
concern since `touch` already did two calls if you set both mtime and
atime. Also, `--no-deref` applies both to the reference file, and to the
target file. No splitting them up, because that's silly.
Can always bikeshed. I nicked `--no-deref` from the uutils flag, and
made the short flag `-d` because it obviously can't be `-h`. I thought
of `-S` like in `glob`, for the "negative/filter out" uppercase short
letters. Ultimately I don't think it matters much.
Should fix #14212 since it's not really tied to uutils, besides the
comment about setting a `datetime` value directly.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
New flag.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the
tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
Maybe.
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-11-03 05:56:05 +01:00
|
|
|
|
|
|
|
fn setup_symlink_fs(dirs: &Dirs, sandbox: &mut Playground<'_>) {
|
|
|
|
sandbox.mkdir("d");
|
|
|
|
sandbox.with_files(&[Stub::EmptyFile("f"), Stub::EmptyFile("d/f")]);
|
|
|
|
sandbox.symlink("f", "fs");
|
|
|
|
sandbox.symlink("d", "ds");
|
|
|
|
sandbox.symlink("d/f", "fds");
|
|
|
|
|
|
|
|
// sandbox.symlink does not handle symlinks to missing files well. It panics
|
|
|
|
// But they are useful, and they should be tested.
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
std::os::unix::fs::symlink(dirs.test().join("m"), dirs.test().join("fms")).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
{
|
|
|
|
std::os::windows::fs::symlink_file(dirs.test().join("m"), dirs.test().join("fms")).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the file times to a known "old" value for comparison
|
|
|
|
filetime::set_symlink_file_times(dirs.test().join("f"), TIME_ONE, TIME_ONE).unwrap();
|
|
|
|
filetime::set_symlink_file_times(dirs.test().join("d"), TIME_ONE, TIME_ONE).unwrap();
|
|
|
|
filetime::set_symlink_file_times(dirs.test().join("d/f"), TIME_ONE, TIME_ONE).unwrap();
|
|
|
|
filetime::set_symlink_file_times(dirs.test().join("ds"), TIME_ONE, TIME_ONE).unwrap();
|
|
|
|
filetime::set_symlink_file_times(dirs.test().join("fs"), TIME_ONE, TIME_ONE).unwrap();
|
|
|
|
filetime::set_symlink_file_times(dirs.test().join("fds"), TIME_ONE, TIME_ONE).unwrap();
|
|
|
|
filetime::set_symlink_file_times(dirs.test().join("fms"), TIME_ONE, TIME_ONE).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_times(path: &nu_path::AbsolutePath) -> (filetime::FileTime, filetime::FileTime) {
|
|
|
|
let metadata = path.symlink_metadata().unwrap();
|
|
|
|
|
|
|
|
(
|
|
|
|
filetime::FileTime::from_system_time(metadata.accessed().unwrap()),
|
|
|
|
filetime::FileTime::from_system_time(metadata.modified().unwrap()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn follow_symlinks() {
|
|
|
|
Playground::setup("touch_follows_symlinks", |dirs, sandbox| {
|
|
|
|
setup_symlink_fs(&dirs, sandbox);
|
|
|
|
|
|
|
|
let missing = dirs.test().join("m");
|
|
|
|
assert!(!missing.exists());
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"
|
|
|
|
touch fds
|
|
|
|
touch ds
|
|
|
|
touch fs
|
|
|
|
touch fms
|
|
|
|
"
|
|
|
|
);
|
|
|
|
|
|
|
|
// We created the missing symlink target
|
|
|
|
assert!(missing.exists());
|
|
|
|
|
|
|
|
// The timestamps for files and directories were changed from TIME_ONE
|
|
|
|
let file_times = get_times(&dirs.test().join("f"));
|
|
|
|
let dir_times = get_times(&dirs.test().join("d"));
|
|
|
|
let dir_file_times = get_times(&dirs.test().join("d/f"));
|
|
|
|
|
|
|
|
assert_ne!(file_times, (TIME_ONE, TIME_ONE));
|
|
|
|
assert_ne!(dir_times, (TIME_ONE, TIME_ONE));
|
|
|
|
assert_ne!(dir_file_times, (TIME_ONE, TIME_ONE));
|
|
|
|
|
|
|
|
// For symlinks, they remain (mostly) the same
|
|
|
|
// We can't test accessed times, since to reach the target file, the symlink must be accessed!
|
|
|
|
let file_symlink_times = get_times(&dirs.test().join("fs"));
|
|
|
|
let dir_symlink_times = get_times(&dirs.test().join("ds"));
|
|
|
|
let dir_file_symlink_times = get_times(&dirs.test().join("fds"));
|
|
|
|
let file_missing_symlink_times = get_times(&dirs.test().join("fms"));
|
|
|
|
|
|
|
|
assert_eq!(file_symlink_times.1, TIME_ONE);
|
|
|
|
assert_eq!(dir_symlink_times.1, TIME_ONE);
|
|
|
|
assert_eq!(dir_file_symlink_times.1, TIME_ONE);
|
|
|
|
assert_eq!(file_missing_symlink_times.1, TIME_ONE);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_follow_symlinks() {
|
|
|
|
Playground::setup("touch_touches_symlinks", |dirs, sandbox| {
|
|
|
|
setup_symlink_fs(&dirs, sandbox);
|
|
|
|
|
|
|
|
let missing = dirs.test().join("m");
|
|
|
|
assert!(!missing.exists());
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"
|
|
|
|
touch fds -s
|
|
|
|
touch ds -s
|
|
|
|
touch fs -s
|
|
|
|
touch fms -s
|
|
|
|
"
|
|
|
|
);
|
|
|
|
|
|
|
|
// We did not create the missing symlink target
|
|
|
|
assert!(!missing.exists());
|
|
|
|
|
|
|
|
// The timestamps for files and directories remain the same
|
|
|
|
let file_times = get_times(&dirs.test().join("f"));
|
|
|
|
let dir_times = get_times(&dirs.test().join("d"));
|
|
|
|
let dir_file_times = get_times(&dirs.test().join("d/f"));
|
|
|
|
|
|
|
|
assert_eq!(file_times, (TIME_ONE, TIME_ONE));
|
|
|
|
assert_eq!(dir_times, (TIME_ONE, TIME_ONE));
|
|
|
|
assert_eq!(dir_file_times, (TIME_ONE, TIME_ONE));
|
|
|
|
|
|
|
|
// For symlinks, everything changed. (except their targets, and paths, and personality)
|
|
|
|
let file_symlink_times = get_times(&dirs.test().join("fs"));
|
|
|
|
let dir_symlink_times = get_times(&dirs.test().join("ds"));
|
|
|
|
let dir_file_symlink_times = get_times(&dirs.test().join("fds"));
|
|
|
|
let file_missing_symlink_times = get_times(&dirs.test().join("fms"));
|
|
|
|
|
|
|
|
assert_ne!(file_symlink_times, (TIME_ONE, TIME_ONE));
|
|
|
|
assert_ne!(dir_symlink_times, (TIME_ONE, TIME_ONE));
|
|
|
|
assert_ne!(dir_file_symlink_times, (TIME_ONE, TIME_ONE));
|
|
|
|
assert_ne!(file_missing_symlink_times, (TIME_ONE, TIME_ONE));
|
|
|
|
})
|
|
|
|
}
|