feat(unstable/task): add INIT_CWD env var (#16110)

This commit is contained in:
David Sherret 2022-10-15 16:46:28 -04:00 committed by GitHub
parent 8a736d7dc7
commit 872dc9b1df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 37 additions and 2 deletions

View file

@ -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",

View file

@ -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"
}
}

View file

@ -0,0 +1 @@
[WILDCARD]testdata

View file

@ -0,0 +1 @@
HELLO

View file

@ -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

View file

@ -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

View file

@ -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 {