|
| 1 | +// npx vitest run src/__tests__/looksLikeFilePath.spec.ts |
| 2 | + |
| 3 | +import { looksLikeFilePath } from "../utils/looksLikeFilePath.js" |
| 4 | + |
| 5 | +describe("looksLikeFilePath", () => { |
| 6 | + describe("nullish, empty, and whitespace-only input", () => { |
| 7 | + it.each([ |
| 8 | + ["undefined", undefined], |
| 9 | + ["null", null], |
| 10 | + ["empty string", ""], |
| 11 | + ["whitespace only", " \t\n "], |
| 12 | + ])("returns false for %s", (_label, value) => { |
| 13 | + expect(looksLikeFilePath(value)).toBe(false) |
| 14 | + }) |
| 15 | + }) |
| 16 | + |
| 17 | + describe("path-shaped input", () => { |
| 18 | + it.each([ |
| 19 | + ["Windows backslash path", "C:\\Users\\dev\\sa.json"], |
| 20 | + ["Windows forward-slash path", "C:/Users/dev/sa.json"], |
| 21 | + ["Windows drive lowercase", "d:\\creds.json"], |
| 22 | + ["POSIX absolute path", "/home/dev/sa.json"], |
| 23 | + ["POSIX absolute root /tmp", "/tmp/creds.json"], |
| 24 | + ["POSIX home path", "~/sa.json"], |
| 25 | + ["POSIX relative ./", "./sa.json"], |
| 26 | + ["POSIX relative ../", "../secrets/sa.json"], |
| 27 | + ])("returns true for %s", (_label, value) => { |
| 28 | + expect(looksLikeFilePath(value)).toBe(true) |
| 29 | + }) |
| 30 | + |
| 31 | + it("returns true after trimming surrounding whitespace", () => { |
| 32 | + expect(looksLikeFilePath(" /tmp/creds.json ")).toBe(true) |
| 33 | + expect(looksLikeFilePath("\tC:\\sa.json\n")).toBe(true) |
| 34 | + }) |
| 35 | + }) |
| 36 | + |
| 37 | + describe("JSON-shaped or bare-token input", () => { |
| 38 | + it.each([ |
| 39 | + ["JSON object", '{"type":"service_account","client_email":"x@y.z"}'], |
| 40 | + ["JSON array", "[1,2,3]"], |
| 41 | + ["JSON with leading whitespace", ' {"type":"service_account"}'], |
| 42 | + ["bare token", "not-json-and-not-a-path"], |
| 43 | + ["service-account-style email", "sa@project.iam.gserviceaccount.com"], |
| 44 | + ])("returns false for %s", (_label, value) => { |
| 45 | + expect(looksLikeFilePath(value)).toBe(false) |
| 46 | + }) |
| 47 | + }) |
| 48 | +}) |
0 commit comments