Move uses of trim_quotes to unescape for filenames (#5398)

* Move uses of trim_quotes to unescape for filenames

* Fix Windows tests
This commit is contained in:
JT 2022-05-02 06:37:20 +12:00 committed by GitHub
parent 80d57d70cd
commit 98ab31e15e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 41 deletions

View File

@ -1,7 +1,7 @@
use crate::completions::{ use crate::completions::{
file_completions::file_path_completion, Completer, CompletionOptions, MatchAlgorithm, SortBy, file_completions::file_path_completion, Completer, CompletionOptions, MatchAlgorithm, SortBy,
}; };
use nu_parser::{trim_quotes, FlatShape}; use nu_parser::{unescape_unquote_string, FlatShape};
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, StateWorkingSet}, engine::{EngineState, StateWorkingSet},
Span, Span,
@ -237,9 +237,10 @@ impl Completer for CommandCompletion {
.map(move |x| { .map(move |x| {
if self.flat_idx == 0 { if self.flat_idx == 0 {
// We're in the command position // We're in the command position
if x.1.starts_with('"') && !matches!(preceding_byte.get(0), Some(b'^')) { if (x.1.starts_with('"') || x.1.starts_with('\'') || x.1.starts_with('`'))
let trimmed = trim_quotes(x.1.as_bytes()); && !matches!(preceding_byte.get(0), Some(b'^'))
let trimmed = String::from_utf8_lossy(trimmed).to_string(); {
let (trimmed, _) = unescape_unquote_string(x.1.as_bytes(), span);
let expanded = nu_path::canonicalize_with(trimmed, &cwd); let expanded = nu_path::canonicalize_with(trimmed, &cwd);
if let Ok(expanded) = expanded { if let Ok(expanded) = expanded {

View File

@ -8,7 +8,7 @@ fn copies_a_file() {
Playground::setup("cp_test_1", |dirs, _| { Playground::setup("cp_test_1", |dirs, _| {
nu!( nu!(
cwd: dirs.root(), cwd: dirs.root(),
"cp \"{}\" cp_test_1/sample.ini", "cp `{}` cp_test_1/sample.ini",
dirs.formats().join("sample.ini") dirs.formats().join("sample.ini")
); );
@ -171,13 +171,13 @@ fn copies_same_file_twice() {
Playground::setup("cp_test_8", |dirs, _| { Playground::setup("cp_test_8", |dirs, _| {
nu!( nu!(
cwd: dirs.root(), cwd: dirs.root(),
"cp \"{}\" cp_test_8/sample.ini", "cp `{}` cp_test_8/sample.ini",
dirs.formats().join("sample.ini") dirs.formats().join("sample.ini")
); );
nu!( nu!(
cwd: dirs.root(), cwd: dirs.root(),
"cp \"{}\" cp_test_8/sample.ini", "cp `{}` cp_test_8/sample.ini",
dirs.formats().join("sample.ini") dirs.formats().join("sample.ini")
); );

View File

@ -23,7 +23,7 @@ use crate::{
parse_internal_call, parse_multispan_value, parse_signature, parse_string, parse_internal_call, parse_multispan_value, parse_signature, parse_string,
parse_var_with_opt_type, trim_quotes, parse_var_with_opt_type, trim_quotes,
}, },
ParseError, unescape_unquote_string, ParseError,
}; };
pub fn parse_def_predecl( pub fn parse_def_predecl(
@ -1312,9 +1312,10 @@ pub fn parse_use(
} else { } else {
// TODO: Do not close over when loading module from file // TODO: Do not close over when loading module from file
// It could be a file // It could be a file
if let Ok(module_filename) =
String::from_utf8(trim_quotes(&import_pattern.head.name).to_vec()) let (module_filename, err) =
{ unescape_unquote_string(&import_pattern.head.name, import_pattern.head.span);
if err.is_none() {
if let Some(module_path) = if let Some(module_path) =
find_in_dirs(&module_filename, working_set, &cwd, LIB_DIRS_ENV) find_in_dirs(&module_filename, working_set, &cwd, LIB_DIRS_ENV)
{ {
@ -1816,8 +1817,8 @@ pub fn parse_source(
// Command and one file name // Command and one file name
if spans.len() >= 2 { if spans.len() >= 2 {
let name_expr = working_set.get_span_contents(spans[1]); let name_expr = working_set.get_span_contents(spans[1]);
let name_expr = trim_quotes(name_expr); let (filename, err) = unescape_unquote_string(name_expr, spans[1]);
if let Ok(filename) = String::from_utf8(name_expr.to_vec()) { if err.is_none() {
if let Some(path) = find_in_dirs(&filename, working_set, &cwd, LIB_DIRS_ENV) { if let Some(path) = find_in_dirs(&filename, working_set, &cwd, LIB_DIRS_ENV) {
if let Ok(contents) = std::fs::read(&path) { if let Ok(contents) = std::fs::read(&path) {
// This will load the defs from the file into the // This will load the defs from the file into the
@ -1967,26 +1968,27 @@ pub fn parse_register(
.map(|expr| { .map(|expr| {
let name_expr = working_set.get_span_contents(expr.span); let name_expr = working_set.get_span_contents(expr.span);
let name_expr = trim_quotes(name_expr); let (name, err) = unescape_unquote_string(name_expr, expr.span);
String::from_utf8(name_expr.to_vec())
.map_err(|_| ParseError::NonUtf8(expr.span)) if let Some(err) = err {
.and_then(|name| { Err(err)
if let Some(p) = find_in_dirs(&name, working_set, &cwd, PLUGIN_DIRS_ENV) { } else {
Ok(p) let path = if let Some(p) = find_in_dirs(&name, working_set, &cwd, PLUGIN_DIRS_ENV)
} else { {
Err(ParseError::RegisteredFileNotFound(name, expr.span)) p
} } else {
}) return Err(ParseError::RegisteredFileNotFound(name, expr.span));
.and_then(|path| { };
if path.exists() & path.is_file() {
Ok(path) if path.exists() & path.is_file() {
} else { Ok(path)
Err(ParseError::RegisteredFileNotFound( } else {
format!("{:?}", path), Err(ParseError::RegisteredFileNotFound(
expr.span, format!("{:?}", path),
)) expr.span,
} ))
}) }
}
}) })
.expect("required positional has being checked") .expect("required positional has being checked")
.and_then(|path| { .and_then(|path| {

View File

@ -1928,10 +1928,10 @@ pub fn parse_directory(
span: Span, span: Span,
) -> (Expression, Option<ParseError>) { ) -> (Expression, Option<ParseError>) {
let bytes = working_set.get_span_contents(span); let bytes = working_set.get_span_contents(span);
let bytes = trim_quotes(bytes); let (token, err) = unescape_unquote_string(bytes, span);
trace!("parsing: directory"); trace!("parsing: directory");
if let Ok(token) = String::from_utf8(bytes.into()) { if err.is_none() {
trace!("-- found {}", token); trace!("-- found {}", token);
( (
Expression { Expression {
@ -1955,10 +1955,10 @@ pub fn parse_filepath(
span: Span, span: Span,
) -> (Expression, Option<ParseError>) { ) -> (Expression, Option<ParseError>) {
let bytes = working_set.get_span_contents(span); let bytes = working_set.get_span_contents(span);
let bytes = trim_quotes(bytes); let (token, err) = unescape_unquote_string(bytes, span);
trace!("parsing: filepath"); trace!("parsing: filepath");
if let Ok(token) = String::from_utf8(bytes.into()) { if err.is_none() {
trace!("-- found {}", token); trace!("-- found {}", token);
( (
Expression { Expression {
@ -2267,12 +2267,11 @@ pub fn parse_glob_pattern(
working_set: &mut StateWorkingSet, working_set: &mut StateWorkingSet,
span: Span, span: Span,
) -> (Expression, Option<ParseError>) { ) -> (Expression, Option<ParseError>) {
let bytes = working_set.get_span_contents(span);
let (token, err) = unescape_unquote_string(bytes, span);
trace!("parsing: glob pattern"); trace!("parsing: glob pattern");
let bytes = working_set.get_span_contents(span); if err.is_none() {
let bytes = trim_quotes(bytes);
if let Ok(token) = String::from_utf8(bytes.into()) {
trace!("-- found {}", token); trace!("-- found {}", token);
( (
Expression { Expression {