make path exists work on expanded path (#5886)

* make path exists works with home

* fix test name
This commit is contained in:
WindSoilder 2022-06-26 19:55:55 +08:00 committed by GitHub
parent dc9d939c83
commit c5a69271a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 4 deletions

View file

@ -1,12 +1,14 @@
use std::path::Path; use std::path::{Path, PathBuf};
use nu_engine::CallExt; use nu_engine::{current_dir, CallExt};
use nu_path::expand_path_with;
use nu_protocol::{engine::Command, Example, Signature, Span, SyntaxShape, Value}; use nu_protocol::{engine::Command, Example, Signature, Span, SyntaxShape, Value};
use super::PathSubcommandArguments; use super::PathSubcommandArguments;
struct Arguments { struct Arguments {
columns: Option<Vec<String>>, columns: Option<Vec<String>>,
pwd: PathBuf,
} }
impl PathSubcommandArguments for Arguments { impl PathSubcommandArguments for Arguments {
@ -46,8 +48,8 @@ impl Command for SubCommand {
let head = call.head; let head = call.head;
let args = Arguments { let args = Arguments {
columns: call.get_flag(engine_state, stack, "columns")?, columns: call.get_flag(engine_state, stack, "columns")?,
pwd: current_dir(engine_state, stack)?,
}; };
input.map( input.map(
move |value| super::operate(&exists, &args, value, head), move |value| super::operate(&exists, &args, value, head),
engine_state.ctrlc.clone(), engine_state.ctrlc.clone(),
@ -93,7 +95,8 @@ impl Command for SubCommand {
} }
} }
fn exists(path: &Path, span: Span, _args: &Arguments) -> Value { fn exists(path: &Path, span: Span, args: &Arguments) -> Value {
let path = expand_path_with(path, &args.pwd);
Value::Bool { Value::Bool {
val: path.exists(), val: path.exists(),
span, span,

View file

@ -51,3 +51,9 @@ fn checks_if_double_dot_exists() {
assert_eq!(actual.out, "true"); assert_eq!(actual.out, "true");
}) })
} }
#[test]
fn checks_tilde_relative_path_exists() {
let actual = nu!(cwd: ".", "'~' | path exists");
assert_eq!(actual.out, "true");
}