mirror of
https://github.com/nushell/nushell.git
synced 2024-12-25 00:19:39 +01:00
Run exitscripts in original dir (#2352)
* Modify testcase * Run exitscript in the folder it was specified * Update documentation * Add comment * Borrow instead of clone * Does this just... work on windows? * fmt * as_str * Collapse if by order of clippy * Support windows * fmt * refactor tests * fmt * This time it will work on windows FOR SURE * Remove debug prints * Comment * Refactor tests * fmt * fix spelling * update comment
This commit is contained in:
parent
f6ff6ab6e4
commit
57101d5022
@ -55,7 +55,7 @@ impl WholeStreamCommand for Autoenv {
|
|||||||
The file can contain several optional sections:
|
The file can contain several optional sections:
|
||||||
env: environment variables to set when visiting the directory. The variables are unset after leaving the directory and any overwritten values are restored.
|
env: environment variables to set when visiting the directory. The variables are unset after leaving the directory and any overwritten values are restored.
|
||||||
scriptvars: environment variables that should be set to the return value of a script. After they have been set, they behave in the same way as variables set in the env section.
|
scriptvars: environment variables that should be set to the return value of a script. After they have been set, they behave in the same way as variables set in the env section.
|
||||||
scripts: scripts to run when entering the directory or leaving it. Note that exitscripts are not run in the directory they are declared in."#
|
scripts: scripts to run when entering the directory or leaving it."#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
|
@ -112,7 +112,7 @@ impl DirectorySpecificEnvironment {
|
|||||||
|
|
||||||
if let Some(es) = nu_env_doc.entryscripts {
|
if let Some(es) = nu_env_doc.entryscripts {
|
||||||
for s in es {
|
for s in es {
|
||||||
run(s.as_str())?;
|
run(s.as_str(), None)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,7 +147,7 @@ impl DirectorySpecificEnvironment {
|
|||||||
new_exitscripts.insert(dir, scripts);
|
new_exitscripts.insert(dir, scripts);
|
||||||
} else {
|
} else {
|
||||||
for s in scripts {
|
for s in scripts {
|
||||||
run(s.as_str())?;
|
run(s.as_str(), Some(&dir))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -215,9 +215,23 @@ impl DirectorySpecificEnvironment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(cmd: &str) -> Result<(), ShellError> {
|
fn run(cmd: &str, dir: Option<&PathBuf>) -> Result<(), ShellError> {
|
||||||
if cfg!(target_os = "windows") {
|
if cfg!(target_os = "windows") {
|
||||||
|
if let Some(dir) = dir {
|
||||||
|
let command = format!("cd {} & {}", dir.to_string_lossy(), cmd);
|
||||||
|
Command::new("cmd")
|
||||||
|
.args(&["/C", command.as_str()])
|
||||||
|
.output()?
|
||||||
|
} else {
|
||||||
Command::new("cmd").args(&["/C", cmd]).output()?
|
Command::new("cmd").args(&["/C", cmd]).output()?
|
||||||
|
}
|
||||||
|
} else if let Some(dir) = dir {
|
||||||
|
// FIXME: When nu scripting is added, cding like might not be a good idea. If nu decides to execute entryscripts when entering the dir this way, it will cause troubles.
|
||||||
|
// For now only standard shell scripts are used, so this is an issue for the future.
|
||||||
|
Command::new("sh")
|
||||||
|
.arg("-c")
|
||||||
|
.arg(format!("cd {:?}; {}", dir, cmd))
|
||||||
|
.output()?
|
||||||
} else {
|
} else {
|
||||||
Command::new("sh").arg("-c").arg(&cmd).output()?
|
Command::new("sh").arg("-c").arg(&cmd).output()?
|
||||||
};
|
};
|
||||||
|
@ -48,9 +48,8 @@ fn autoenv() {
|
|||||||
sandbox.mkdir("bizz/buzz");
|
sandbox.mkdir("bizz/buzz");
|
||||||
sandbox.mkdir("foob");
|
sandbox.mkdir("foob");
|
||||||
|
|
||||||
let scriptfile = if cfg!(target_os = "windows") {
|
// Windows uses a different command to create an empty file so we need to have different content on windows.
|
||||||
FileWithContent(
|
let full_nu_env = if cfg!(target_os = "windows") {
|
||||||
".nu-env",
|
|
||||||
r#"[env]
|
r#"[env]
|
||||||
testkey = "testvalue"
|
testkey = "testvalue"
|
||||||
|
|
||||||
@ -59,11 +58,8 @@ fn autoenv() {
|
|||||||
|
|
||||||
[scripts]
|
[scripts]
|
||||||
entryscripts = ["echo nul > hello.txt"]
|
entryscripts = ["echo nul > hello.txt"]
|
||||||
exitscripts = ["echo nul > bye.txt"]"#,
|
exitscripts = ["echo nul > bye.txt"]"#
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
FileWithContent(
|
|
||||||
".nu-env",
|
|
||||||
r#"[env]
|
r#"[env]
|
||||||
testkey = "testvalue"
|
testkey = "testvalue"
|
||||||
|
|
||||||
@ -72,12 +68,11 @@ fn autoenv() {
|
|||||||
|
|
||||||
[scripts]
|
[scripts]
|
||||||
entryscripts = ["touch hello.txt"]
|
entryscripts = ["touch hello.txt"]
|
||||||
exitscripts = ["touch bye.txt"]"#,
|
exitscripts = ["touch bye.txt"]"#
|
||||||
)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
sandbox.with_files(vec![
|
sandbox.with_files(vec![
|
||||||
scriptfile,
|
FileWithContent(".nu-env", full_nu_env),
|
||||||
FileWithContent(
|
FileWithContent(
|
||||||
"foo/.nu-env",
|
"foo/.nu-env",
|
||||||
r#"[env]
|
r#"[env]
|
||||||
@ -89,14 +84,28 @@ fn autoenv() {
|
|||||||
r#"[env]
|
r#"[env]
|
||||||
overwrite_me = "set_in_bar""#,
|
overwrite_me = "set_in_bar""#,
|
||||||
),
|
),
|
||||||
FileWithContent(
|
FileWithContent("bizz/.nu-env", full_nu_env),
|
||||||
"bizz/.nu-env",
|
|
||||||
r#"[scripts]
|
|
||||||
entryscripts = ["touch hello.txt"]
|
|
||||||
exitscripts = ["touch bye.txt"]"#,
|
|
||||||
),
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
//Make sure basic keys are set
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: dirs.test(),
|
||||||
|
r#"autoenv trust
|
||||||
|
echo $nu.env.testkey"#
|
||||||
|
);
|
||||||
|
assert!(actual.out.ends_with("testvalue"));
|
||||||
|
|
||||||
|
// Make sure exitscripts are run in the directory they were specified.
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: dirs.test(),
|
||||||
|
r#"autoenv trust
|
||||||
|
cd ..
|
||||||
|
cd autoenv_test
|
||||||
|
ls
|
||||||
|
ls | where name == "bye.txt" | get name"#
|
||||||
|
);
|
||||||
|
assert!(actual.out.contains("bye.txt"));
|
||||||
|
|
||||||
// Make sure entry scripts are run
|
// Make sure entry scripts are run
|
||||||
let actual = nu!(
|
let actual = nu!(
|
||||||
cwd: dirs.test(),
|
cwd: dirs.test(),
|
||||||
@ -138,14 +147,6 @@ fn autoenv() {
|
|||||||
);
|
);
|
||||||
assert!(!actual.out.ends_with("hello.txt"));
|
assert!(!actual.out.ends_with("hello.txt"));
|
||||||
|
|
||||||
//Make sure basic keys are set
|
|
||||||
let actual = nu!(
|
|
||||||
cwd: dirs.test(),
|
|
||||||
r#"autoenv trust
|
|
||||||
echo $nu.env.testkey"#
|
|
||||||
);
|
|
||||||
assert!(actual.out.ends_with("testvalue"));
|
|
||||||
|
|
||||||
//Backing out of the directory should unset the keys
|
//Backing out of the directory should unset the keys
|
||||||
let actual = nu!(
|
let actual = nu!(
|
||||||
cwd: dirs.test(),
|
cwd: dirs.test(),
|
||||||
@ -189,14 +190,6 @@ fn autoenv() {
|
|||||||
);
|
);
|
||||||
assert!(actual.out.contains("hello.txt"));
|
assert!(actual.out.contains("hello.txt"));
|
||||||
|
|
||||||
// Make sure exit scripts are run
|
|
||||||
let actual = nu!(
|
|
||||||
cwd: dirs.test(),
|
|
||||||
r#"cd ..
|
|
||||||
ls | where name == "bye.txt" | get name"#
|
|
||||||
);
|
|
||||||
assert!(actual.out.contains("bye.txt"));
|
|
||||||
|
|
||||||
//Variables set in parent directories should be set even if you directly cd to a subdir
|
//Variables set in parent directories should be set even if you directly cd to a subdir
|
||||||
let actual = nu!(
|
let actual = nu!(
|
||||||
cwd: dirs.test(),
|
cwd: dirs.test(),
|
||||||
|
Loading…
Reference in New Issue
Block a user