This repository has no description
1import test from "node:test"
2import assert from "node:assert/strict"
3import fs from "node:fs/promises"
4import os from "node:os"
5import path from "node:path"
6import { cleanOutput, closeBash } from "./shell"
7import { runCommand } from "./tools"
8
9test.afterEach(() => {
10 closeBash()
11})
12
13test("shell disables git pager and terminal prompts", async () => {
14 const output = await runCommand(
15 "printf '%s\\n%s\\n%s\\n%s' \"$GIT_PAGER\" \"$GIT_TERMINAL_PROMPT\" \"$PAGER\" \"$LESS\"",
16 0,
17 5_000,
18 )
19
20 assert.equal(output, "cat\n0\ncat\nFRX")
21})
22
23test("shell recovers when a command leaves terminal echo enabled", async () => {
24 assert.equal(await runCommand("stty echo; echo first", 0, 5_000), "first")
25 assert.equal(await runCommand("echo second", 0, 5_000), "second")
26})
27
28test("cleanOutput collapses terminal redraw controls", () => {
29 assert.equal(cleanOutput("one\rspinner\rdone\n"), "done\n")
30 assert.equal(cleanOutput("abc\b \bd\n"), "abd\n")
31 assert.equal(cleanOutput("\x1b[?25lhidden\x1b[?25h\n"), "hidden\n")
32})
33
34test("sudo commands run as one-off commands with stdin closed", async () => {
35 const dir = await fs.mkdtemp(path.join(os.tmpdir(), "niri-fake-sudo-"))
36 const fakeSudo = path.join(dir, "sudo")
37 await fs.writeFile(
38 fakeSudo,
39 [
40 "#!/bin/sh",
41 "if [ -t 0 ]; then",
42 " echo stdin_tty",
43 "else",
44 " echo stdin_not_tty",
45 "fi",
46 'exec "$@"',
47 "",
48 ].join("\n"),
49 { mode: 0o755 },
50 )
51
52 const oldPath = process.env.PATH
53 process.env.PATH = `${dir}${path.delimiter}${oldPath ?? ""}`
54 try {
55 const output = await runCommand("echo before; sudo printf ok; echo after", 0, 5_000)
56
57 assert.equal(output, "before\nstdin_not_tty\nokafter")
58 } finally {
59 if (oldPath === undefined) {
60 delete process.env.PATH
61 } else {
62 process.env.PATH = oldPath
63 }
64 await fs.rm(dir, { recursive: true, force: true })
65 }
66})