Making open case-insensitive to file extensions (#10451)

# Description

Closes #10441 

Uses `String::to_lowercase()` when the file's extension `ext` is parsed
to allow `from_decl(format!("from {ext}"))` to return the desired output
regardless of extension case.

It doesn't work with sqlite files since those are handled earlier in the
parsing but I think is good- since there's no standard file extension
used by sqlite so a user will likely want case sensitivity in that case.

This also has the (possibly undesired) effect of making `open`
completely case insensitive, e.g. `open foo.JSON` will work on a file
named `foo.json` and vice versa. This is good on Windows as it treats
`foo.json` and `foo.JSON` as the same file, but may not be the desired
behaviour on Unix.

If this behaviour is undesired I assume it would be fixed with a
`#[cfg(not(unix))]` attribute on the `to_lowercase()` operation but that
produces slightly "uglier" code that I didn't wish to submit unless
necessary. 

old behaviour:

![image](https://github.com/nushell/nushell/assets/79598494/261df577-e377-44ac-bef3-f6384bceaeb5)

new behaviour: 

![image](https://github.com/nushell/nushell/assets/79598494/04271740-a46f-4613-a3a6-1e220ef7f829)


# User-Facing Changes

`open` will now present a table when `open`-ing files with captitalized
extensions rather than the file's raw data

# Tests + Formatting

new test: `parses_file_with_uppercase_extension` which tests the desired
behaviour

---------

Co-authored-by: Stefan Holderbach <sholderbach@users.noreply.github.com>
This commit is contained in:
poketch 2023-09-29 11:20:59 -04:00 committed by GitHub
parent d6b9153ac5
commit 16453b6986
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 2 deletions

View File

@ -171,12 +171,11 @@ impl Command for Open {
metadata: None, metadata: None,
trim_end_newline: false, trim_end_newline: false,
}; };
let ext = if raw { let ext = if raw {
None None
} else { } else {
path.extension() path.extension()
.map(|name| name.to_string_lossy().to_string()) .map(|name| name.to_string_lossy().to_string().to_lowercase())
}; };
if let Some(ext) = ext { if let Some(ext) = ext {

View File

@ -1,8 +1,39 @@
use nu_test_support::fs::Stub::EmptyFile; use nu_test_support::fs::Stub::EmptyFile;
use nu_test_support::fs::Stub::FileWithContent;
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed; use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::playground::Playground; use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline}; use nu_test_support::{nu, pipeline};
#[test]
fn parses_file_with_uppercase_extension() {
Playground::setup("open_test_uppercase_extension", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"nu.zion.JSON",
r#"{
"glossary": {
"GlossDiv": {
"GlossList": {
"GlossEntry": {
"ID": "SGML"
}
}
}
}
}"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open nu.zion.JSON
| get glossary.GlossDiv.GlossList.GlossEntry.ID
"#
));
assert_eq!(actual.out, "SGML");
})
}
#[test] #[test]
fn parses_csv() { fn parses_csv() {
Playground::setup("open_test_1", |dirs, sandbox| { Playground::setup("open_test_1", |dirs, sandbox| {