aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-09-23 03:10:56 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-09-23 03:10:56 -0700
commit62e22b29959cc984cf9abfa42c30a5b2925841fd (patch)
tree7b5d50fcb484677028451e3083f16d3ef279fc65 /test
parent2346be55fc5c0fb51de986e55f698debc5dff362 (diff)
downloadbun-62e22b29959cc984cf9abfa42c30a5b2925841fd.tar.gz
bun-62e22b29959cc984cf9abfa42c30a5b2925841fd.tar.zst
bun-62e22b29959cc984cf9abfa42c30a5b2925841fd.zip
Implement `Bun.which`
Diffstat (limited to '')
-rw-r--r--test/bun.js/which.test.ts52
1 files changed, 52 insertions, 0 deletions
diff --git a/test/bun.js/which.test.ts b/test/bun.js/which.test.ts
new file mode 100644
index 000000000..7fd4b8400
--- /dev/null
+++ b/test/bun.js/which.test.ts
@@ -0,0 +1,52 @@
+import { test, expect } from "bun:test";
+
+import { which } from "bun";
+
+test("which", () => {
+ writeFixture("/tmp/myscript.sh");
+
+ // Our cwd is not /tmp
+ expect(which("myscript.sh")).toBe(null);
+
+ // "bun" is in our PATH
+ expect(which("bun")?.length > 0).toBe(true);
+
+ expect(
+ // You can override PATH
+ which("myscript.sh", {
+ PATH: "/tmp",
+ })
+ ).toBe("/tmp/myscript.sh");
+
+ expect(
+ which("myscript.sh", {
+ PATH: "/not-tmp",
+ })
+ ).toBe(null);
+
+ expect(
+ // PATH works like the $PATH environment variable, respecting colons
+ which("myscript.sh", {
+ PATH: "/not-tmp:/tmp",
+ })
+ ).toBe("/tmp/myscript.sh");
+
+ expect(
+ // cwd is checked first
+ which("myscript.sh", {
+ cwd: "/tmp",
+ })
+ ).toBe("/tmp/myscript.sh");
+});
+
+function writeFixture(path) {
+ var fs = require("fs");
+ try {
+ fs.unlinkSync(path);
+ } catch (e) {}
+
+ var script_name = path;
+ var script_content = "echo Hello world!";
+ fs.writeFileSync(script_name, script_content);
+ fs.chmodSync(script_name, "755");
+}