aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js/which.test.ts
blob: e142e398cfaf37b6a5f04f03dc92d5adcfe4411c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { test, expect } from "bun:test";

import { which } from "bun";
import { chmodSync, mkdirSync, unlinkSync } from "node:fs";

test("which", () => {
  writeFixture("/tmp/myscript.sh");

  // Our cwd is not /tmp
  expect(which("myscript.sh")).toBe(null);

  try {
    mkdirSync("myscript.sh");
    chmodSync("myscript.sh", "755");
  } catch (e) {}

  // directories should not be returned
  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");

  try {
    unlinkSync("myscript.sh");
  } catch (e) {}
});

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");
}