|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { BashWrapper } from "../../src/permission/wrapper" |
| 3 | + |
| 4 | +describe("BashWrapper.extract", () => { |
| 5 | + describe("each wrapper type", () => { |
| 6 | + test("nohup", () => { |
| 7 | + expect(BashWrapper.extract(["nohup", "git", "push"])).toEqual([["git", "push"]]) |
| 8 | + }) |
| 9 | + |
| 10 | + test("time", () => { |
| 11 | + expect(BashWrapper.extract(["time", "make", "build"])).toEqual([["make", "build"]]) |
| 12 | + }) |
| 13 | + |
| 14 | + test("timeout", () => { |
| 15 | + expect(BashWrapper.extract(["timeout", "5", "curl", "http://example.com"])).toEqual([ |
| 16 | + ["curl", "http://example.com"], |
| 17 | + ]) |
| 18 | + }) |
| 19 | + |
| 20 | + test("nice", () => { |
| 21 | + expect(BashWrapper.extract(["nice", "-n", "5", "make"])).toEqual([["make"]]) |
| 22 | + }) |
| 23 | + |
| 24 | + test("ionice", () => { |
| 25 | + expect(BashWrapper.extract(["ionice", "-c", "2", "-n", "7", "dd", "if=/dev/zero"])).toEqual([ |
| 26 | + ["dd", "if=/dev/zero"], |
| 27 | + ]) |
| 28 | + }) |
| 29 | + |
| 30 | + test("stdbuf", () => { |
| 31 | + expect(BashWrapper.extract(["stdbuf", "-oL", "tail", "-f", "log.txt"])).toEqual([["tail", "-f", "log.txt"]]) |
| 32 | + }) |
| 33 | + |
| 34 | + test("watch", () => { |
| 35 | + expect(BashWrapper.extract(["watch", "-n", "1", "kubectl", "get", "pods"])).toEqual([ |
| 36 | + ["kubectl", "get", "pods"], |
| 37 | + ]) |
| 38 | + }) |
| 39 | + |
| 40 | + test("sudo", () => { |
| 41 | + expect(BashWrapper.extract(["sudo", "rm", "-rf", "/"])).toEqual([["rm", "-rf", "/"]]) |
| 42 | + }) |
| 43 | + |
| 44 | + test("sudo with flags", () => { |
| 45 | + expect(BashWrapper.extract(["sudo", "-u", "root", "systemctl", "restart", "nginx"])).toEqual([ |
| 46 | + ["systemctl", "restart", "nginx"], |
| 47 | + ]) |
| 48 | + }) |
| 49 | + |
| 50 | + test("env", () => { |
| 51 | + expect(BashWrapper.extract(["env", "node", "server.js"])).toEqual([["node", "server.js"]]) |
| 52 | + }) |
| 53 | + |
| 54 | + test("xargs", () => { |
| 55 | + expect(BashWrapper.extract(["xargs", "-I{}", "gh", "api", "-X", "DELETE"])).toEqual([ |
| 56 | + ["gh", "api", "-X", "DELETE"], |
| 57 | + ]) |
| 58 | + }) |
| 59 | + |
| 60 | + test("xargs with split flag and arg", () => { |
| 61 | + expect(BashWrapper.extract(["xargs", "-I", "{}", "rm"])).toEqual([["rm"]]) |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + describe("flag handling", () => { |
| 66 | + test("combined flag+arg", () => { |
| 67 | + expect(BashWrapper.extract(["xargs", "-I{}", "echo"])).toEqual([["echo"]]) |
| 68 | + }) |
| 69 | + |
| 70 | + test("split flag and arg", () => { |
| 71 | + expect(BashWrapper.extract(["xargs", "-I", "{}", "echo"])).toEqual([["echo"]]) |
| 72 | + }) |
| 73 | + |
| 74 | + test("boolean flag", () => { |
| 75 | + expect(BashWrapper.extract(["sudo", "-n", "ls"])).toEqual([["ls"]]) |
| 76 | + }) |
| 77 | + |
| 78 | + test("long flag with =", () => { |
| 79 | + expect(BashWrapper.extract(["timeout", "--signal=KILL", "5", "sleep", "10"])).toEqual([["sleep", "10"]]) |
| 80 | + }) |
| 81 | + |
| 82 | + test("-- terminator", () => { |
| 83 | + expect(BashWrapper.extract(["sudo", "--", "rm", "-rf", "/tmp/x"])).toEqual([["rm", "-rf", "/tmp/x"]]) |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + describe("env specifics", () => { |
| 88 | + test("VAR=val skipping", () => { |
| 89 | + expect(BashWrapper.extract(["env", "FOO=bar", "BAZ=qux", "node", "app.js"])).toEqual([["node", "app.js"]]) |
| 90 | + }) |
| 91 | + |
| 92 | + test("mixed flags and assignments", () => { |
| 93 | + expect(BashWrapper.extract(["env", "-u", "HOME", "PATH=/usr/bin", "sh", "-c", "echo"])).toEqual([ |
| 94 | + ["sh", "-c", "echo"], |
| 95 | + ]) |
| 96 | + }) |
| 97 | + }) |
| 98 | + |
| 99 | + describe("timeout positional", () => { |
| 100 | + test("duration arg skipped before inner command", () => { |
| 101 | + expect(BashWrapper.extract(["timeout", "30s", "npm", "test"])).toEqual([["npm", "test"]]) |
| 102 | + }) |
| 103 | + |
| 104 | + test("with flags and duration", () => { |
| 105 | + expect(BashWrapper.extract(["timeout", "--signal=KILL", "10", "make"])).toEqual([["make"]]) |
| 106 | + }) |
| 107 | + }) |
| 108 | + |
| 109 | + describe("find -exec", () => { |
| 110 | + test("single -exec", () => { |
| 111 | + expect(BashWrapper.extract(["find", ".", "-name", "*.log", "-exec", "rm", "{}", ";"])).toEqual([["rm"]]) |
| 112 | + }) |
| 113 | + |
| 114 | + test("-execdir", () => { |
| 115 | + expect(BashWrapper.extract(["find", "/tmp", "-execdir", "chmod", "644", "{}", ";"])).toEqual([["chmod", "644"]]) |
| 116 | + }) |
| 117 | + |
| 118 | + test("multiple -exec clauses", () => { |
| 119 | + expect( |
| 120 | + BashWrapper.extract([ |
| 121 | + "find", |
| 122 | + ".", |
| 123 | + "-exec", |
| 124 | + "echo", |
| 125 | + "{}", |
| 126 | + ";", |
| 127 | + "-exec", |
| 128 | + "rm", |
| 129 | + "{}", |
| 130 | + "+", |
| 131 | + ]), |
| 132 | + ).toEqual([["echo"], ["rm"]]) |
| 133 | + }) |
| 134 | + |
| 135 | + test("escaped terminator", () => { |
| 136 | + expect(BashWrapper.extract(["find", ".", "-exec", "cat", "{}", "\\;"])).toEqual([["cat"]]) |
| 137 | + }) |
| 138 | + }) |
| 139 | + |
| 140 | + describe("nested wrappers", () => { |
| 141 | + test("timeout + sudo", () => { |
| 142 | + expect(BashWrapper.extract(["timeout", "5", "sudo", "gh", "api"])).toEqual([["gh", "api"]]) |
| 143 | + }) |
| 144 | + |
| 145 | + test("nohup + nice + make", () => { |
| 146 | + expect(BashWrapper.extract(["nohup", "nice", "-n", "10", "make", "build"])).toEqual([["make", "build"]]) |
| 147 | + }) |
| 148 | + |
| 149 | + test("sudo + env + node", () => { |
| 150 | + expect(BashWrapper.extract(["sudo", "env", "NODE_ENV=prod", "node", "app.js"])).toEqual([["node", "app.js"]]) |
| 151 | + }) |
| 152 | + }) |
| 153 | + |
| 154 | + describe("non-wrappers", () => { |
| 155 | + test("regular command returns empty", () => { |
| 156 | + expect(BashWrapper.extract(["git", "checkout", "main"])).toEqual([]) |
| 157 | + }) |
| 158 | + |
| 159 | + test("unknown command returns empty", () => { |
| 160 | + expect(BashWrapper.extract(["myapp", "--verbose"])).toEqual([]) |
| 161 | + }) |
| 162 | + }) |
| 163 | + |
| 164 | + describe("edge cases", () => { |
| 165 | + test("empty tokens", () => { |
| 166 | + expect(BashWrapper.extract([])).toEqual([]) |
| 167 | + }) |
| 168 | + |
| 169 | + test("wrapper with no args", () => { |
| 170 | + expect(BashWrapper.extract(["nohup"])).toEqual([]) |
| 171 | + }) |
| 172 | + |
| 173 | + test("wrapper with only flags", () => { |
| 174 | + expect(BashWrapper.extract(["sudo", "-n"])).toEqual([]) |
| 175 | + }) |
| 176 | + |
| 177 | + test("timeout with only duration", () => { |
| 178 | + expect(BashWrapper.extract(["timeout", "5"])).toEqual([]) |
| 179 | + }) |
| 180 | + }) |
| 181 | +}) |
0 commit comments