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

View File

@ -1,6 +1,6 @@
mod helpers;
use helpers::Playground;
use helpers::{Playground, Stub::*};
use std::path::PathBuf;
#[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]
fn filesystem_change_current_directory_to_parent_directory() {
Playground::setup("cd_test_4", |dirs, _| {
Playground::setup("cd_test_5", |dirs, _| {
let actual = nu!(
cwd: dirs.test(),
r#"
@ -70,7 +87,7 @@ fn filesystem_change_current_directory_to_parent_directory() {
#[test]
fn file_system_change_to_home_directory() {
Playground::setup("cd_test_5", |dirs, _| {
Playground::setup("cd_test_6", |dirs, _| {
let actual = nu!(
cwd: dirs.test(),
r#"
@ -85,7 +102,7 @@ fn file_system_change_to_home_directory() {
#[test]
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");
let actual = nu!(
@ -109,4 +126,4 @@ fn filesystem_directory_not_found() {
assert!(actual.contains("dir_that_does_not_exist"));
assert!(actual.contains("directory not found"));
}
}