mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
feat(unstable/task): add INIT_CWD
env var (#16110)
This commit is contained in:
parent
8a736d7dc7
commit
872dc9b1df
7 changed files with 37 additions and 2 deletions
|
@ -19,6 +19,23 @@ itest!(task_cwd {
|
|||
exit_code: 0,
|
||||
});
|
||||
|
||||
itest!(task_init_cwd {
|
||||
args: "task -q --config task/deno.json --cwd .. echo_init_cwd",
|
||||
output: "task/task_init_cwd.out",
|
||||
envs: vec![("NO_COLOR".to_string(), "1".to_string())],
|
||||
exit_code: 0,
|
||||
});
|
||||
|
||||
itest!(task_init_cwd_already_set {
|
||||
args: "task -q --config task/deno.json echo_init_cwd",
|
||||
output: "task/task_init_cwd_already_set.out",
|
||||
envs: vec![
|
||||
("NO_COLOR".to_string(), "1".to_string()),
|
||||
("INIT_CWD".to_string(), "HELLO".to_string())
|
||||
],
|
||||
exit_code: 0,
|
||||
});
|
||||
|
||||
itest!(task_cwd_resolves_config_from_specified_dir {
|
||||
args: "task -q --cwd task",
|
||||
output: "task/task_no_args.out",
|
||||
|
|
3
cli/tests/testdata/task/deno.json
vendored
3
cli/tests/testdata/task/deno.json
vendored
|
@ -6,6 +6,7 @@
|
|||
"strings": "deno run main.ts && deno eval \"console.log(\\\"test\\\")\"",
|
||||
"piped": "echo 12345 | (deno eval 'const b = new Uint8Array(1);Deno.stdin.readSync(b);console.log(b)' && deno eval 'const b = new Uint8Array(1);Deno.stdin.readSync(b);console.log(b)')",
|
||||
"exit_code_5": "echo $(echo 10 ; exit 2) && exit 5",
|
||||
"echo_cwd": "echo $(pwd)"
|
||||
"echo_cwd": "echo $(pwd)",
|
||||
"echo_init_cwd": "echo $INIT_CWD"
|
||||
}
|
||||
}
|
||||
|
|
1
cli/tests/testdata/task/task_init_cwd.out
vendored
Normal file
1
cli/tests/testdata/task/task_init_cwd.out
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
[WILDCARD]testdata
|
1
cli/tests/testdata/task/task_init_cwd_already_set.out
vendored
Normal file
1
cli/tests/testdata/task/task_init_cwd_already_set.out
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
HELLO
|
2
cli/tests/testdata/task/task_no_args.out
vendored
2
cli/tests/testdata/task/task_no_args.out
vendored
|
@ -7,6 +7,8 @@ Available tasks:
|
|||
echo 1
|
||||
- echo_cwd
|
||||
echo $(pwd)
|
||||
- echo_init_cwd
|
||||
echo $INIT_CWD
|
||||
- exit_code_5
|
||||
echo $(echo 10 ; exit 2) && exit 5
|
||||
- piped
|
||||
|
|
|
@ -9,6 +9,8 @@ Available tasks:
|
|||
echo 1
|
||||
- echo_cwd
|
||||
echo $(pwd)
|
||||
- echo_init_cwd
|
||||
echo $INIT_CWD
|
||||
- exit_code_5
|
||||
echo $(echo 10 ; exit 2) && exit 5
|
||||
- piped
|
||||
|
|
|
@ -70,7 +70,18 @@ pub async fn execute_script(
|
|||
);
|
||||
let seq_list = deno_task_shell::parser::parse(script)
|
||||
.with_context(|| format!("Error parsing script '{}'.", task_name))?;
|
||||
let env_vars = std::env::vars().collect::<HashMap<String, String>>();
|
||||
|
||||
// get the starting env vars (the PWD env var will be set by deno_task_shell)
|
||||
let mut env_vars = std::env::vars().collect::<HashMap<String, String>>();
|
||||
const INIT_CWD_NAME: &str = "INIT_CWD";
|
||||
if !env_vars.contains_key(INIT_CWD_NAME) {
|
||||
if let Ok(cwd) = std::env::current_dir() {
|
||||
// if not set, set an INIT_CWD env var that has the cwd
|
||||
env_vars
|
||||
.insert(INIT_CWD_NAME.to_string(), cwd.to_string_lossy().to_string());
|
||||
}
|
||||
}
|
||||
|
||||
let exit_code = deno_task_shell::execute(seq_list, env_vars, &cwd).await;
|
||||
Ok(exit_code)
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue