mirror of
https://github.com/nushell/nushell.git
synced 2024-12-22 15:13:01 +01:00
Filesystem cd refactor/cleanup.
This commit is contained in:
parent
207f9ece5a
commit
77c2e4200e
@ -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())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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"));
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user