Skip to content

Commit b6e0130

Browse files
committed
chore: update tests following switch to sinon
1 parent c3b90f5 commit b6e0130

6 files changed

Lines changed: 36 additions & 13 deletions

File tree

src/lib/authentication.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
WebviewView,
88
workspace,
99
} from "vscode";
10-
import { spawn, SpawnOptionsWithoutStdio } from "child_process";
10+
import { SpawnOptionsWithoutStdio } from "child_process";
11+
import { childProcess } from "./child-process";
1112
import * as os from "os";
1213
import {
1314
StatusBarStatus,
@@ -137,7 +138,7 @@ export async function loginGGShield(
137138
}
138139

139140
return new Promise<void>((resolve, reject) => {
140-
const proc = spawn(ggshieldPath, args, options);
141+
const proc = childProcess.spawn(ggshieldPath, args, options);
141142

142143
proc.stdout.on("data", (data) => {
143144
const urlLine = data.toString().match(/https:\/\/[^\s]+/);

src/lib/child-process.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as cp from "child_process";
2+
3+
// Plain object wrapper so tests can stub these with Sinon.
4+
// Node.js 22+ makes built-in module properties non-configurable,
5+
// which prevents sinon.stub(childProcess, "spawnSync") from working.
6+
export const childProcess = {
7+
spawnSync: cp.spawnSync.bind(cp),
8+
spawn: cp.spawn.bind(cp),
9+
};
10+
11+
export type {
12+
ChildProcessWithoutNullStreams,
13+
SpawnOptionsWithoutStdio,
14+
SpawnSyncOptionsWithStringEncoding,
15+
SpawnSyncReturns,
16+
} from "child_process";

src/lib/run-ggshield.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
2-
spawnSync,
3-
SpawnSyncOptionsWithStringEncoding,
4-
SpawnSyncReturns,
5-
} from "child_process";
2+
type SpawnSyncOptionsWithStringEncoding,
3+
type SpawnSyncReturns,
4+
childProcess,
5+
} from "./child-process";
66
import { GGShieldConfiguration } from "./ggshield-configuration";
77
import { workspace } from "vscode";
88

@@ -58,7 +58,11 @@ export function runGGShieldCommand(
5858
};
5959
}
6060

61-
const proc = spawnSync(configuration.ggshieldPath, args, options);
61+
const proc = childProcess.spawnSync(
62+
configuration.ggshieldPath,
63+
args,
64+
options,
65+
);
6266

6367
return proc;
6468
}

src/test/suite/lib/authentication.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { GGShieldConfiguration } from "../../../lib/ggshield-configuration";
22
import * as sinon from "sinon";
33
import * as runGGShield from "../../../lib/run-ggshield";
4-
import * as childProcess from "child_process";
5-
import { ChildProcessWithoutNullStreams } from "child_process";
4+
import {
5+
childProcess,
6+
type ChildProcessWithoutNullStreams,
7+
} from "../../../lib/child-process";
68
import * as statusBar from "../../../gitguardian-interface/gitguardian-status-bar";
79
import assert from "assert";
810
import { EventEmitter } from "events";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as sinon from "sinon";
2-
import * as childProcess from "child_process";
2+
import { childProcess } from "../../../lib/child-process";
33
import * as vscode from "vscode";
44
import * as runGGShield from "../../../lib/run-ggshield";
55
import assert from "assert";

src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as vscode from "vscode";
2-
import { spawnSync } from "child_process";
2+
import { childProcess } from "./lib/child-process";
33
import * as path from "path";
44

55
export function checkGitInstalled(): boolean {
6-
const proc = spawnSync("git", ["--version"]);
6+
const proc = childProcess.spawnSync("git", ["--version"]);
77
const success = proc.status === 0;
88
if (!success) {
99
vscode.window.showErrorMessage(
@@ -15,7 +15,7 @@ export function checkGitInstalled(): boolean {
1515

1616
// Since git is required to use ggshield, we know that it is installed
1717
export function isFileGitignored(filePath: string): boolean {
18-
const proc = spawnSync("git", ["check-ignore", filePath], {
18+
const proc = childProcess.spawnSync("git", ["check-ignore", filePath], {
1919
cwd: path.dirname(filePath),
2020
});
2121
return proc.status === 0;

0 commit comments

Comments
 (0)