closure.test.ts 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. import { describe, expect, test } from "bun:test"
  2. import { ShellScan } from "../src/index.js"
  3. const opaque = ["$COMMAND hidden", "$(printf command) hidden", 'printf "unterminated'] as const
  4. const contexts = [
  5. (source: string) => source,
  6. (source: string) => `${source}; printf visible`,
  7. (source: string) => `printf visible; ${source}`,
  8. (source: string) => `${source} && printf visible`,
  9. (source: string) => `printf visible || ${source}`,
  10. (source: string) => `printf "$(${source})"`,
  11. (source: string) => `X=$(${source}) printf visible`,
  12. (source: string) => `printf visible >$(${source})`,
  13. ] as const
  14. describe("ShellScan recursive structural opacity", () => {
  15. for (const seed of opaque) {
  16. for (const outer of contexts) {
  17. for (const inner of contexts.slice(0, 5)) {
  18. const source = outer(inner(seed))
  19. test(source, () => expect(ShellScan.scan(source).kind).toBe("opaque"))
  20. }
  21. }
  22. }
  23. })
  24. describe("ShellScan quote suppression", () => {
  25. test.each([...opaque])("single quotes suppress active syntax: %s", (source) => {
  26. expect(ShellScan.scan(`printf '%s' '${source.replaceAll("'", "")}'`).kind).toBe("scanned")
  27. })
  28. })