Skip to content

Commit 09949e8

Browse files
committed
fix(scanFile): Refacto scanFile function to use status code
1 parent f2e0a28 commit 09949e8

File tree

2 files changed

+45
-47
lines changed

2 files changed

+45
-47
lines changed

src/lib/ggshield-api.ts

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -172,47 +172,42 @@ export async function scanFile(
172172
filePath,
173173
]);
174174

175-
// Ignore errors concerning usage
176-
// This occurs when the path of the file is invalid (i.e.VSCode sending an event for files not on the file system)
177-
// or when the file is ignored in the .gitguardian.yaml
178-
if (
179-
proc.stderr.includes(
180-
"Error: An ignored file or directory cannot be scanned"
181-
)
182-
) {
183-
updateStatusBarItem(StatusBarStatus.ignoredFile);
184-
return;
185-
}
186-
if (proc.stderr.includes("Usage: ggshield secret scan path")) {
187-
return undefined;
188-
}
189-
let errorMessage = "";
190-
proc.stderr.split("\n").forEach((stderrLine) => {
175+
if (proc.status === 128 || proc.status === 3) {
176+
let errorMessage = "";
177+
proc.stderr.split("\n").forEach((stderrLine) => {
178+
if (
179+
stderrLine.length > 0 &&
180+
!stderrLine.includes("Scanning Path...") // ggshield outputs this info message on stderr, ignore it
181+
) {
182+
errorMessage += stderrLine + "\n";
183+
}
184+
});
185+
if (errorMessage.length > 0) {
186+
window.showErrorMessage(`ggshield: ${errorMessage}`);
187+
return undefined;
188+
}
189+
} else if (proc.status === 2) {
190+
// Ignore errors concerning usage
191+
// This occurs when the path of the file is invalid (i.e.VSCode sending an event for files not on the file system)
192+
// or when the file is ignored in the .gitguardian.yaml
191193
if (
192-
stderrLine.length > 0 &&
193-
!stderrLine.includes("Scanning Path...") // ggshield outputs this info message on stderr, ignore it
194+
proc.stderr.includes(
195+
"Error: An ignored file or directory cannot be scanned"
196+
)
194197
) {
195-
errorMessage += stderrLine + "\n";
198+
updateStatusBarItem(StatusBarStatus.ignoredFile);
199+
return;
196200
}
197-
});
198-
if (errorMessage.length > 0) {
199-
window.showErrorMessage(`ggshield: ${errorMessage}`);
200201
return undefined;
201-
}
202-
203-
const results = JSON.parse(proc.stdout);
204-
if (!results) {
205-
updateStatusBarItem(StatusBarStatus.ready);
202+
} else if (proc.status === 0) {
203+
updateStatusBarItem(StatusBarStatus.noSecretFound);
206204
return;
207-
}
208-
let incidentsDiagnostics: Diagnostic[] = parseGGShieldResults(results);
209-
if (incidentsDiagnostics.length !== 0) {
210-
updateStatusBarItem(StatusBarStatus.secretFound);
211205
} else {
212-
updateStatusBarItem(StatusBarStatus.noSecretFound);
206+
const results = JSON.parse(proc.stdout);
207+
let incidentsDiagnostics: Diagnostic[] = parseGGShieldResults(results);
208+
updateStatusBarItem(StatusBarStatus.secretFound);
209+
diagnosticCollection.set(fileUri, incidentsDiagnostics);
213210
}
214-
215-
diagnosticCollection.set(fileUri, incidentsDiagnostics);
216211
}
217212

218213
export async function loginGGShield(

src/test/suite/lib/ggshield-api.test.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ suite("scanFile", () => {
4646

4747
test("successfully scans a file with incidents", async () => {
4848
runGGShieldCommandMock.returnWith({
49-
status: 0,
49+
status: 1,
5050
stdout: scanResultsWithIncident,
5151
stderr: "",
5252
});
@@ -78,19 +78,22 @@ suite("scanFile", () => {
7878
statusBar.StatusBarStatus.ignoredFile
7979
);
8080
});
81-
82-
test("displays an error message if the scan command fails", async () => {
83-
runGGShieldCommandMock.returnWith({
84-
status: 1,
85-
stdout: "",
86-
stderr: "Error",
81+
82+
const errorCodes = [128, 3];
83+
errorCodes.forEach((code) => {
84+
test(`displays an error message if the scan command fails with error code ${code}`, async () => {
85+
runGGShieldCommandMock.returnWith({
86+
status: code,
87+
stdout: "",
88+
stderr: "Error",
89+
});
90+
91+
await scanFile("test.py", Uri.file("test.py"), {} as GGShieldConfiguration);
92+
93+
// The error message is displayed
94+
assert.strictEqual(errorMessageMock.callCount, 1);
95+
assert.strictEqual(errorMessageMock.lastCall.args[0], "ggshield: Error\n");
8796
});
88-
89-
await scanFile("test.py", Uri.file("test.py"), {} as GGShieldConfiguration);
90-
91-
// The error message is displayed
92-
assert.strictEqual(errorMessageMock.callCount, 1);
93-
assert.strictEqual(errorMessageMock.lastCall.args[0], "ggshield: Error\n");
9497
});
9598

9699
test("ignores the 'ignored file cannot be scanned' error", async () => {

0 commit comments

Comments
 (0)