wildcard.test.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { test, expect } from "bun:test"
  2. import { Wildcard } from "../../src/util/wildcard"
  3. test("match handles glob tokens", () => {
  4. expect(Wildcard.match("file1.txt", "file?.txt")).toBe(true)
  5. expect(Wildcard.match("file12.txt", "file?.txt")).toBe(false)
  6. expect(Wildcard.match("foo+bar", "foo+bar")).toBe(true)
  7. })
  8. test("all picks the most specific pattern", () => {
  9. const rules = {
  10. "*": "deny",
  11. "git *": "ask",
  12. "git status": "allow",
  13. }
  14. expect(Wildcard.all("git status", rules)).toBe("allow")
  15. expect(Wildcard.all("git log", rules)).toBe("ask")
  16. expect(Wildcard.all("echo hi", rules)).toBe("deny")
  17. })
  18. test("allStructured matches command sequences", () => {
  19. const rules = {
  20. "git *": "ask",
  21. "git status*": "allow",
  22. }
  23. expect(Wildcard.allStructured({ head: "git", tail: ["status", "--short"] }, rules)).toBe("allow")
  24. expect(Wildcard.allStructured({ head: "npm", tail: ["run", "build", "--watch"] }, { "npm run *": "allow" })).toBe(
  25. "allow",
  26. )
  27. expect(Wildcard.allStructured({ head: "ls", tail: ["-la"] }, rules)).toBeUndefined()
  28. })
  29. test("allStructured prioritizes flag-specific patterns", () => {
  30. const rules = {
  31. "find *": "allow",
  32. "find * -delete*": "ask",
  33. "sort*": "allow",
  34. "sort -o *": "ask",
  35. }
  36. expect(Wildcard.allStructured({ head: "find", tail: ["src", "-delete"] }, rules)).toBe("ask")
  37. expect(Wildcard.allStructured({ head: "find", tail: ["src", "-print"] }, rules)).toBe("allow")
  38. expect(Wildcard.allStructured({ head: "sort", tail: ["-o", "out.txt"] }, rules)).toBe("ask")
  39. expect(Wildcard.allStructured({ head: "sort", tail: ["--reverse"] }, rules)).toBe("allow")
  40. })
  41. test("allStructured handles sed flags", () => {
  42. const rules = {
  43. "sed * -i*": "ask",
  44. "sed -n*": "allow",
  45. }
  46. expect(Wildcard.allStructured({ head: "sed", tail: ["-i", "file"] }, rules)).toBe("ask")
  47. expect(Wildcard.allStructured({ head: "sed", tail: ["-i.bak", "file"] }, rules)).toBe("ask")
  48. expect(Wildcard.allStructured({ head: "sed", tail: ["-n", "1p", "file"] }, rules)).toBe("allow")
  49. expect(Wildcard.allStructured({ head: "sed", tail: ["-i", "-n", "/./p", "myfile.txt"] }, rules)).toBe("ask")
  50. })