summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2022-10-15 16:46:28 -0400
committerGitHub <noreply@github.com>2022-10-15 16:46:28 -0400
commit872dc9b1df1dec5466970f085875e50b9436e967 (patch)
tree7b18912c73f9ec26adcb00f7540cc546ac272182 /cli
parent8a736d7dc79ea1442dd043acd75aa9a8d83efb54 (diff)
feat(unstable/task): add `INIT_CWD` env var (#16110)
Diffstat (limited to 'cli')
-rw-r--r--cli/tests/integration/task_tests.rs17
-rw-r--r--cli/tests/testdata/task/deno.json3
-rw-r--r--cli/tests/testdata/task/task_init_cwd.out1
-rw-r--r--cli/tests/testdata/task/task_init_cwd_already_set.out1
-rw-r--r--cli/tests/testdata/task/task_no_args.out2
-rw-r--r--cli/tests/testdata/task/task_non_existent.out2
-rw-r--r--cli/tools/task.rs13
7 files changed, 37 insertions, 2 deletions
diff --git a/cli/tests/integration/task_tests.rs b/cli/tests/integration/task_tests.rs
index 1c3240f89..8b8998b26 100644
--- a/cli/tests/integration/task_tests.rs
+++ b/cli/tests/integration/task_tests.rs
@@ -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",
diff --git a/cli/tests/testdata/task/deno.json b/cli/tests/testdata/task/deno.json
index 229315a4e..043f49b61 100644
--- a/cli/tests/testdata/task/deno.json
+++ b/cli/tests/testdata/task/deno.json
@@ -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"
}
}
diff --git a/cli/tests/testdata/task/task_init_cwd.out b/cli/tests/testdata/task/task_init_cwd.out
new file mode 100644
index 000000000..95ea8a545
--- /dev/null
+++ b/cli/tests/testdata/task/task_init_cwd.out
@@ -0,0 +1 @@
+[WILDCARD]testdata
diff --git a/cli/tests/testdata/task/task_init_cwd_already_set.out b/cli/tests/testdata/task/task_init_cwd_already_set.out
new file mode 100644
index 000000000..e427984d4
--- /dev/null
+++ b/cli/tests/testdata/task/task_init_cwd_already_set.out
@@ -0,0 +1 @@
+HELLO
diff --git a/cli/tests/testdata/task/task_no_args.out b/cli/tests/testdata/task/task_no_args.out
index d7e509656..2001f269d 100644
--- a/cli/tests/testdata/task/task_no_args.out
+++ b/cli/tests/testdata/task/task_no_args.out
@@ -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
diff --git a/cli/tests/testdata/task/task_non_existent.out b/cli/tests/testdata/task/task_non_existent.out
index 4867d3068..645e96f2b 100644
--- a/cli/tests/testdata/task/task_non_existent.out
+++ b/cli/tests/testdata/task/task_non_existent.out
@@ -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
diff --git a/cli/tools/task.rs b/cli/tools/task.rs
index 88a0f5be0..4b05a37e5 100644
--- a/cli/tools/task.rs
+++ b/cli/tools/task.rs
@@ -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 {