Filesystem cd refactor/cleanup.

This commit is contained in:
Andrés N. Robalino 2019-09-08 04:55:49 -05:00
parent 207f9ece5a
commit 77c2e4200e
2 changed files with 39 additions and 42 deletions

View file

@ -185,29 +185,20 @@ impl Shell for FilesystemShell {
}, },
Some(v) => { Some(v) => {
let target = v.as_path()?; let target = v.as_path()?;
let path = PathBuf::from(self.path());
match dunce::canonicalize(path.join(&target).as_path()) {
Ok(p) => p,
Err(_) => {
let error = Err(ShellError::labeled_error(
"Can not change to directory",
"directory not found",
v.span().clone(),
));
if let Some(t) = target.to_str() { if PathBuf::from("-") == target {
if t == "-" { PathBuf::from(&self.last_path)
match dunce::canonicalize(PathBuf::from(self.last_path.clone()).as_path()) { } else {
Ok(p) => p, let path = PathBuf::from(self.path());
Err(_) => {
return error; match dunce::canonicalize(path.join(&target)) {
} Ok(p) => p,
} Err(_) => {
} else { return Err(ShellError::labeled_error(
return error; "Can not change to directory",
} "directory not found",
} else { v.span().clone(),
return error; ))
} }
} }
} }
@ -215,23 +206,12 @@ impl Shell for FilesystemShell {
}; };
let mut stream = VecDeque::new(); let mut stream = VecDeque::new();
match std::env::set_current_dir(&path) {
Ok(_) => {} stream.push_back(
Err(_) => { ReturnSuccess::change_cwd(
if let Some(directory) = args.nth(0) {
return Err(ShellError::labeled_error(
"Can not change to directory",
"directory not found",
directory.span(),
));
} else {
return Err(ShellError::string("Can not change to directory"));
}
}
}
stream.push_back(ReturnSuccess::change_cwd(
path.to_string_lossy().to_string(), path.to_string_lossy().to_string(),
)); ));
Ok(stream.into()) Ok(stream.into())
} }

View file

@ -1,6 +1,6 @@
mod helpers; mod helpers;
use helpers::Playground; use helpers::{Playground, Stub::*};
use std::path::PathBuf; use std::path::PathBuf;
#[test] #[test]
@ -53,9 +53,26 @@ fn filesystem_switch_back_to_previous_working_directory() {
}) })
} }
#[test]
fn filesytem_change_from_current_directory_using_relative_path_and_dash() {
Playground::setup("cd_test_4", |dirs, sandbox| {
sandbox.within("odin").mkdir("-"); //
let actual = nu!(
cwd: dirs.test(),
r#"
cd odin/-
pwd | echo $it
"#
);
assert_eq!(PathBuf::from(actual), dirs.test().join("odin").join("-"));
})
}
#[test] #[test]
fn filesystem_change_current_directory_to_parent_directory() { fn filesystem_change_current_directory_to_parent_directory() {
Playground::setup("cd_test_4", |dirs, _| { Playground::setup("cd_test_5", |dirs, _| {
let actual = nu!( let actual = nu!(
cwd: dirs.test(), cwd: dirs.test(),
r#" r#"
@ -70,7 +87,7 @@ fn filesystem_change_current_directory_to_parent_directory() {
#[test] #[test]
fn file_system_change_to_home_directory() { fn file_system_change_to_home_directory() {
Playground::setup("cd_test_5", |dirs, _| { Playground::setup("cd_test_6", |dirs, _| {
let actual = nu!( let actual = nu!(
cwd: dirs.test(), cwd: dirs.test(),
r#" r#"
@ -85,7 +102,7 @@ fn file_system_change_to_home_directory() {
#[test] #[test]
fn filesystem_change_to_a_directory_containing_spaces() { fn filesystem_change_to_a_directory_containing_spaces() {
Playground::setup("cd_test_6", |dirs, sandbox| { Playground::setup("cd_test_7", |dirs, sandbox| {
sandbox.mkdir("robalino turner katz"); sandbox.mkdir("robalino turner katz");
let actual = nu!( let actual = nu!(
@ -109,4 +126,4 @@ fn filesystem_directory_not_found() {
assert!(actual.contains("dir_that_does_not_exist")); assert!(actual.contains("dir_that_does_not_exist"));
assert!(actual.contains("directory not found")); assert!(actual.contains("directory not found"));
} }