|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
| 2 | +import fs from "fs"; |
| 3 | +import os from "os"; |
| 4 | +import path from "path"; |
| 5 | +import { BetterleaksScanner } from "../src/scanners/betterleaks.js"; |
| 6 | + |
| 7 | +// parseResults is private; exercise it directly rather than shelling out to the |
| 8 | +// real binary, so these stay hermetic and run without betterleaks installed. |
| 9 | +const scanner = new BetterleaksScanner(); |
| 10 | +const parseResults = (scanner as any).parseResults.bind(scanner); |
| 11 | + |
| 12 | +let tmpDir: string; |
| 13 | +let stderrSpy: ReturnType<typeof vi.spyOn>; |
| 14 | + |
| 15 | +function writeReport(content: string): string { |
| 16 | + const p = path.join(tmpDir, "report.json"); |
| 17 | + fs.writeFileSync(p, content, "utf-8"); |
| 18 | + return p; |
| 19 | +} |
| 20 | + |
| 21 | +beforeEach(() => { |
| 22 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "bl-parse-test-")); |
| 23 | + stderrSpy = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 24 | +}); |
| 25 | + |
| 26 | +afterEach(() => { |
| 27 | + stderrSpy.mockRestore(); |
| 28 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 29 | +}); |
| 30 | + |
| 31 | +describe("BetterleaksScanner.parseResults", () => { |
| 32 | + // #217 — betterleaks >=1.1.2 writes the literal `null` for a clean scan via |
| 33 | + // the `dir`/`git` subcommands. Regression guard: this is an empty result, not |
| 34 | + // a version mismatch, and must not emit warning noise on every clean file. |
| 35 | + it("treats a literal `null` report as an empty result set", () => { |
| 36 | + expect(parseResults(writeReport("null"))).toEqual([]); |
| 37 | + }); |
| 38 | + |
| 39 | + it("does not warn on a `null` report", () => { |
| 40 | + parseResults(writeReport("null")); |
| 41 | + expect(stderrSpy).not.toHaveBeenCalled(); |
| 42 | + }); |
| 43 | + |
| 44 | + it("tolerates trailing whitespace around `null`", () => { |
| 45 | + expect(parseResults(writeReport("null\n"))).toEqual([]); |
| 46 | + expect(stderrSpy).not.toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it("returns an empty result set for an empty report", () => { |
| 50 | + expect(parseResults(writeReport(""))).toEqual([]); |
| 51 | + expect(stderrSpy).not.toHaveBeenCalled(); |
| 52 | + }); |
| 53 | + |
| 54 | + it("returns findings from a normal array report", () => { |
| 55 | + const findings = [{ RuleID: "aws-secret-key", Description: "AWS key", StartLine: 3 }]; |
| 56 | + expect(parseResults(writeReport(JSON.stringify(findings)))).toEqual(findings); |
| 57 | + expect(stderrSpy).not.toHaveBeenCalled(); |
| 58 | + }); |
| 59 | + |
| 60 | + it("returns an empty result set for an empty array report", () => { |
| 61 | + expect(parseResults(writeReport("[]"))).toEqual([]); |
| 62 | + expect(stderrSpy).not.toHaveBeenCalled(); |
| 63 | + }); |
| 64 | + |
| 65 | + // sable-o4k — the stale-binary guard must survive the #217 fix. |
| 66 | + it("still warns about a non-array object report", () => { |
| 67 | + expect(parseResults(writeReport('{"findings": []}'))).toEqual([]); |
| 68 | + expect(stderrSpy).toHaveBeenCalledOnce(); |
| 69 | + expect(String(stderrSpy.mock.calls[0][0])).toContain("possible version mismatch"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("still warns about malformed JSON", () => { |
| 73 | + expect(parseResults(writeReport("{not json"))).toEqual([]); |
| 74 | + expect(stderrSpy).toHaveBeenCalledOnce(); |
| 75 | + expect(String(stderrSpy.mock.calls[0][0])).toContain("Failed to parse"); |
| 76 | + }); |
| 77 | +}); |
0 commit comments