|
| 1 | +import { GGShieldConfiguration } from "../../../lib/ggshield-configuration"; |
| 2 | +import * as statusBar from "../../../gitguardian-interface/gitguardian-status-bar"; |
| 3 | +import * as simple from "simple-mock"; |
| 4 | +import { diagnosticCollection, scanFile } from "../../../lib/ggshield-api"; |
| 5 | +import * as runGGShield from "../../../lib/run-ggshield"; |
| 6 | +import path = require("path"); |
| 7 | +import { Uri, window } from "vscode"; |
| 8 | +import assert = require("assert"); |
| 9 | +import { |
| 10 | + scanResultsNoIncident, |
| 11 | + scanResultsWithIncident, |
| 12 | +} from "../../constants"; |
| 13 | + |
| 14 | +suite("scanFile", () => { |
| 15 | + let updateStatusBarMock: simple.Stub<Function>; |
| 16 | + let runGGShieldCommandMock: simple.Stub<Function>; |
| 17 | + |
| 18 | + setup(() => { |
| 19 | + updateStatusBarMock = simple.mock(statusBar, "updateStatusBarItem"); |
| 20 | + runGGShieldCommandMock = simple.mock(runGGShield, "runGGShieldCommand"); |
| 21 | + }); |
| 22 | + |
| 23 | + teardown(() => { |
| 24 | + simple.restore(); |
| 25 | + }); |
| 26 | + |
| 27 | + test("successfully scans a file with no incidents", async () => { |
| 28 | + runGGShieldCommandMock.returnWith({ |
| 29 | + status: 0, |
| 30 | + stdout: scanResultsNoIncident, |
| 31 | + stderr: "", |
| 32 | + }); |
| 33 | + |
| 34 | + await scanFile("test.py", Uri.file("test.py"), {} as GGShieldConfiguration); |
| 35 | + |
| 36 | + // The status bar displays "No Secret Found" |
| 37 | + assert.strictEqual(updateStatusBarMock.callCount, 1); |
| 38 | + assert.strictEqual( |
| 39 | + updateStatusBarMock.lastCall.args[0], |
| 40 | + statusBar.StatusBarStatus.noSecretFound |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + test("successfully scans a file with incidents", async () => { |
| 45 | + runGGShieldCommandMock.returnWith({ |
| 46 | + status: 0, |
| 47 | + stdout: scanResultsWithIncident, |
| 48 | + stderr: "", |
| 49 | + }); |
| 50 | + |
| 51 | + await scanFile("test.py", Uri.file("test.py"), {} as GGShieldConfiguration); |
| 52 | + |
| 53 | + // The status bar displays "Secret Found" |
| 54 | + assert.strictEqual(updateStatusBarMock.callCount, 1); |
| 55 | + assert.strictEqual( |
| 56 | + updateStatusBarMock.lastCall.args[0], |
| 57 | + statusBar.StatusBarStatus.secretFound |
| 58 | + ); |
| 59 | + |
| 60 | + // The diagnostic collection contains the incident |
| 61 | + assert.strictEqual( |
| 62 | + diagnosticCollection.get(Uri.file("test.py"))?.length, |
| 63 | + 1 |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + test("skips the file if it is ignored", async () => { |
| 68 | + const filePath = "out/test.py"; |
| 69 | + await scanFile(filePath, Uri.file(filePath), {} as GGShieldConfiguration); |
| 70 | + |
| 71 | + // The status bar displays "Ignored File" |
| 72 | + assert.strictEqual(updateStatusBarMock.callCount, 1); |
| 73 | + assert.strictEqual( |
| 74 | + updateStatusBarMock.lastCall.args[0], |
| 75 | + statusBar.StatusBarStatus.ignoredFile |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + test("displays an error message if the scan command fails", async () => { |
| 80 | + const errorMessageMock = simple.mock(window, "showErrorMessage"); |
| 81 | + runGGShieldCommandMock.returnWith({ |
| 82 | + status: 1, |
| 83 | + stdout: "", |
| 84 | + stderr: "Error", |
| 85 | + }); |
| 86 | + |
| 87 | + await scanFile("test.py", Uri.file("test.py"), {} as GGShieldConfiguration); |
| 88 | + |
| 89 | + // The error message is displayed |
| 90 | + assert.strictEqual(errorMessageMock.callCount, 1); |
| 91 | + assert.strictEqual(errorMessageMock.lastCall.args[0], "ggshield: Error\n"); |
| 92 | + }); |
| 93 | +}); |
0 commit comments