Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 2c63888

Browse files
committed
♻️ refactor: replace direct fs and cp calls with sys wrapper
1 parent 6eb6794 commit 2c63888

2 files changed

Lines changed: 15 additions & 16 deletions

File tree

src/shared/utils/ddev-utils.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@
1919
*/
2020

2121
import * as vscode from 'vscode';
22-
import { spawnSync } from 'child_process';
22+
import * as cp from 'child_process';
2323
import * as fs from 'fs';
2424
import * as path from 'path';
2525

26+
/**
27+
* System calls wrapper for testability
28+
*/
29+
export const sys = {
30+
spawnSync: cp.spawnSync,
31+
existsSync: fs.existsSync
32+
};
33+
2634
/**
2735
* DDEV project validation result
2836
*/
@@ -49,7 +57,7 @@ export class DdevUtils {
4957
public static hasDdevProject(workspacePath: string): boolean {
5058
try {
5159
const configPath = path.join(workspacePath, '.ddev', 'config.yaml');
52-
return fs.existsSync(configPath);
60+
return sys.existsSync(configPath);
5361
} catch (error) {
5462
return false;
5563
}
@@ -63,7 +71,7 @@ export class DdevUtils {
6371
*/
6472
public static isDdevRunning(workspacePath: string): boolean {
6573
try {
66-
const result = spawnSync('ddev', ['exec', 'echo', 'test'], {
74+
const result = sys.spawnSync('ddev', ['exec', 'echo', 'test'], {
6775
cwd: workspacePath,
6876
encoding: 'utf-8'
6977
});
@@ -197,7 +205,7 @@ export class DdevUtils {
197205
// We use 'env' to set environment variables inside the container
198206
const args = ['exec', 'env', 'XDEBUG_MODE=off', ...command];
199207

200-
const result = spawnSync('ddev', args, {
208+
const result = sys.spawnSync('ddev', args, {
201209
cwd: workspacePath,
202210
encoding: 'utf-8'
203211
});

src/test/ddev-utils.test.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
import * as assert from 'assert';
2121
import * as sinon from 'sinon';
2222
import { afterEach, beforeEach } from 'mocha';
23-
import * as cp from 'child_process';
24-
import * as fs from 'fs';
25-
import { DdevUtils } from '../shared/utils/ddev-utils';
23+
import { DdevUtils, sys } from '../shared/utils/ddev-utils';
2624

2725
suite('DdevUtils Test Suite', () => {
2826
let sandbox: sinon.SinonSandbox;
@@ -31,15 +29,8 @@ suite('DdevUtils Test Suite', () => {
3129

3230
beforeEach(() => {
3331
sandbox = sinon.createSandbox();
34-
35-
// Try to stub, but handle if it fails (basic check)
36-
try {
37-
spawnSyncStub = sandbox.stub(cp, 'spawnSync');
38-
existsSyncStub = sandbox.stub(fs, 'existsSync');
39-
} catch (e) {
40-
console.error('Failed to stub modules:', e);
41-
throw e;
42-
}
32+
spawnSyncStub = sandbox.stub(sys, 'spawnSync');
33+
existsSyncStub = sandbox.stub(sys, 'existsSync');
4334
});
4435

4536
afterEach(() => {

0 commit comments

Comments
 (0)