Span fixes during duration conversion (#9143)

Description: Fix of #8945.


# 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.
-->

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# 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 -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` 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
> ```
-->

# 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.
-->

---------

Co-authored-by: jpaldino <jpaldino@zaloni.com>
This commit is contained in:
tesla232 2023-05-12 12:57:50 -04:00 committed by GitHub
parent a3bf2bff49
commit 8584aa79a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -179,7 +179,10 @@ fn into_duration(
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let span = match input.span() {
Some(t) => t,
None => call.head,
};
let convert_to_unit: Option<Spanned<String>> = call.get_flag(engine_state, stack, "convert")?;
let column_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
let config = engine_state.get_config();
@ -188,14 +191,14 @@ fn into_duration(
input.map(
move |v| {
if column_paths.is_empty() {
action(&v, &convert_to_unit, float_precision, head)
action(&v, &convert_to_unit, float_precision, span)
} else {
let mut ret = v;
for path in &column_paths {
let d = convert_to_unit.clone();
let r = ret.update_cell_path(
&path.members,
Box::new(move |old| action(old, &d, float_precision, head)),
Box::new(move |old| action(old, &d, float_precision, span)),
);
if let Err(error) = r {
return Value::Error {

View File

@ -238,3 +238,13 @@ fn call_command_with_non_ascii_argument() {
assert_eq!(actual.err.len(), 0);
}
#[test]
fn parse_long_duration() {
let actual = nu!(cwd: "tests/parsing/samples",
r#"
"78.797877879789789sec" | into duration
"#);
assert_eq!(actual.out, "1min 18sec 797ms");
}