Add decimals to int when using into string --decimals (#6085)

* Add decimals to int when using `into string --decimals`

* Add tests for `into string` when converting int with `--decimals`

* Apply formatting

* Merge `into_str` test files

* Comment out unused code and add TODOs

* Use decimal separator depending on system locale

* Add test helper to run closure in different locale

* Add tests for int-to-string conversion using different locales

* Add utils function to get system locale

* Add panic message when locking mutex fails

* Catch and resume panic later to prevent Mutex poisoning when test fails

* Move test to `nu-test-support` to keep `nu-utils` free of `nu-*` dependencies

See https://github.com/nushell/nushell/pull/6085#issuecomment-1193131694

* Rename test support fn `with_fake_locale` to `with_locale_override`

* Move `get_system_locale()` to `locale` module

* Allow overriding locale with special env variable (when not in release)

* Use special env var to override locale during testing

* Allow callback to return a value in `with_locale_override()`

* Allow multiple options in `nu!` macro

* Allow to set locale as `nu!` macro option

* Use new `locale` option of `nu!` macro instead of `with_locale_override`

Using the `locale` options does not lock the `LOCALE_OVERRIDE_MUTEX`
mutex in `nu-test-support::locale_override` but instead calls the `nu`
command directly with the `NU_LOCALE_OVERRIDE` environment variable.
This allows for parallel test excecution.

* Fix: Add option identifier for `cwd` in usage of `nu!` macro

* Rely on `Display` trait for formatting `nu!` macro command

- Removed the `DisplayPath` trait
- Implement `Display` for `AbsolutePath`, `RelativePath` and
  `AbsoluteFile`

* Default to locale `en_US.UTF-8` for tests when using `nu!` macro

* Add doc comment to `nu!` macro

* Format code using `cargo fmt --all`

* Pass function directly instead of wrapping the call in a closure

https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure

* Pass function to `or_else()` instead of calling it inside `or()`

https://rust-lang.github.io/rust-clippy/master/index.html#or_fun_call

* Fix: Add option identifier for `cwd` in usage of `nu!` macro
This commit is contained in:
Björn Richter
2022-08-13 04:13:50 +02:00
committed by GitHub
parent ccebdd7a7f
commit cb18dd5200
21 changed files with 390 additions and 122 deletions

View File

@ -31,7 +31,7 @@ fn filesystem_change_from_current_directory_using_absolute_path() {
cd "{}"
echo (pwd)
"#,
dirs.formats()
dirs.formats().display()
);
assert_eq!(PathBuf::from(actual.out), dirs.formats());
@ -52,7 +52,7 @@ fn filesystem_switch_back_to_previous_working_directory() {
cd -
echo (pwd)
"#,
dirs.test()
dirs.test().display()
);
assert_eq!(PathBuf::from(actual.out), dirs.test().join("odin"));
@ -132,7 +132,7 @@ fn filesystem_change_current_directory_to_parent_directory_after_delete_cwd() {
cd ..
echo (pwd)
"#,
dirs.test()
dirs.test().display()
);
let actual = actual.out.split(',').nth(1).unwrap();

View File

@ -10,7 +10,7 @@ fn copies_a_file() {
nu!(
cwd: dirs.root(),
"cp `{}` cp_test_1/sample.ini",
dirs.formats().join("sample.ini")
dirs.formats().join("sample.ini").display()
);
assert!(dirs.test().join("sample.ini").exists());
@ -37,7 +37,7 @@ fn error_if_attempting_to_copy_a_directory_to_another_directory() {
Playground::setup("cp_test_3", |dirs, _| {
let actual = nu!(
cwd: dirs.formats(),
"cp ../formats {}", dirs.test()
"cp ../formats {}", dirs.test().display()
);
assert!(actual.err.contains("../formats"));
@ -128,7 +128,7 @@ fn copies_using_path_with_wildcard() {
Playground::setup("cp_test_6", |dirs, _| {
nu!(
cwd: dirs.formats(),
"cp -r ../formats/* {}", dirs.test()
"cp -r ../formats/* {}", dirs.test().display()
);
assert!(files_exist_at(
@ -150,7 +150,7 @@ fn copies_using_a_glob() {
Playground::setup("cp_test_7", |dirs, _| {
nu!(
cwd: dirs.formats(),
"cp -r * {}", dirs.test()
"cp -r * {}", dirs.test().display()
);
assert!(files_exist_at(
@ -173,13 +173,13 @@ fn copies_same_file_twice() {
nu!(
cwd: dirs.root(),
"cp `{}` cp_test_8/sample.ini",
dirs.formats().join("sample.ini")
dirs.formats().join("sample.ini").display()
);
nu!(
cwd: dirs.root(),
"cp `{}` cp_test_8/sample.ini",
dirs.formats().join("sample.ini")
dirs.formats().join("sample.ini").display()
);
assert!(dirs.test().join("sample.ini").exists());

View File

@ -531,7 +531,7 @@ fn list_directory_contains_invalid_utf8() {
std::fs::create_dir_all(&path).expect("failed to create directory");
let actual = nu!(cwd, "ls");
let actual = nu!(cwd: cwd, "ls");
assert!(actual.out.contains("warning: get non-utf8 filename"));
assert!(actual.err.contains("No matches found for"));

View File

@ -384,7 +384,7 @@ fn mv_directory_with_same_name() {
let cwd = sandbox.cwd().join("testdir");
let actual = nu!(
cwd,
cwd: cwd,
r#"
mv testdir ..
"#

View File

@ -1,4 +1,4 @@
use nu_test_support::fs::{AbsolutePath, DisplayPath, Stub::FileWithContent};
use nu_test_support::fs::{AbsolutePath, Stub::FileWithContent};
use nu_test_support::nu;
use nu_test_support::pipeline;
use nu_test_support::playground::Playground;
@ -18,7 +18,7 @@ fn sources_also_files_under_custom_lib_dirs_path() {
lib_dirs = ["{}"]
skip_welcome_message = true
"#,
library_path.display_path()
library_path
),
)]);

View File

@ -183,3 +183,87 @@ fn from_error() {
assert_eq!(actual.out, "nu::shell::name_not_found");
}
#[test]
fn int_into_string() {
let actual = nu!(
cwd: ".", pipeline(
r#"
10 | into string
"#
));
assert_eq!(actual.out, "10");
}
#[test]
fn int_into_string_decimals_0() {
let actual = nu!(
locale: "en_US.UTF-8",
pipeline(
r#"
10 | into string --decimals 0
"#
)
);
assert_eq!(actual.out, "10");
}
#[test]
fn int_into_string_decimals_1() {
let actual = nu!(
locale: "en_US.UTF-8",
pipeline(
r#"
10 | into string --decimals 1
"#
)
);
assert_eq!(actual.out, "10.0");
}
#[test]
fn int_into_string_decimals_10() {
let actual = nu!(
locale: "en_US.UTF-8",
pipeline(
r#"
10 | into string --decimals 10
"#
)
);
assert_eq!(actual.out, "10.0000000000");
}
#[test]
fn int_into_string_decimals_respects_system_locale_de() {
// Set locale to `de_DE`, which uses `,` (comma) as decimal separator
let actual = nu!(
locale: "de_DE.UTF-8",
pipeline(
r#"
10 | into string --decimals 1
"#
)
);
assert_eq!(actual.out, "10,0");
}
#[test]
fn int_into_string_decimals_respects_system_locale_en() {
// Set locale to `en_US`, which uses `.` (period) as decimal separator
let actual = nu!(
locale: "en_US.UTF-8",
pipeline(
r#"
10 | into string --decimals 1
"#
)
);
assert_eq!(actual.out, "10.0");
}

View File

@ -1,4 +1,4 @@
use nu_test_support::fs::{AbsolutePath, DisplayPath, Stub::FileWithContent};
use nu_test_support::fs::{AbsolutePath, Stub::FileWithContent};
use nu_test_support::nu;
use nu_test_support::pipeline;
use nu_test_support::playground::Playground;
@ -9,7 +9,7 @@ fn use_module_file_within_block() {
let file = AbsolutePath::new(dirs.test().join("spam.nu"));
nu.with_files(vec![FileWithContent(
&file.display_path(),
&file.to_string(),
r#"
export def foo [] {
echo "hello world"
@ -39,7 +39,7 @@ fn use_keeps_doc_comments() {
let file = AbsolutePath::new(dirs.test().join("spam.nu"));
nu.with_files(vec![FileWithContent(
&file.display_path(),
&file.to_string(),
r#"
# this is my foo command
export def foo [