From 855e8c070d9a461ab4b4142a82d02db8d674b898 Mon Sep 17 00:00:00 2001 From: Himanshu Singh <154805527+himanshu-zetta@users.noreply.github.com> Date: Thu, 31 Jul 2025 13:15:41 +0530 Subject: [PATCH] fix: improve child_process stubbing --- src/analyzer.ts | 19 +++++++++++++++---- test/analyzer.test.ts | 10 ++++++---- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/analyzer.ts b/src/analyzer.ts index f79541a..6b82d7e 100644 --- a/src/analyzer.ts +++ b/src/analyzer.ts @@ -1,12 +1,21 @@ /* eslint-disable indent */ import * as vscode from 'vscode'; -import { spawn, execFile } from 'child_process'; +import childProcess = require('child_process'); import { promisify } from 'util'; import { AnalysisData, AnalysisType, WebviewMessage } from './types'; import { WebviewContent } from './webview-content'; import { DEFAULT_PROMPT } from './constants'; -const execAsync = promisify(execFile); +function execAsync( + file: string, + args: readonly string[], + options?: childProcess.ExecFileOptions +): Promise<{ stdout: string; stderr: string }> { + return promisify(childProcess.execFile)(file, args, options) as Promise<{ + stdout: string; + stderr: string; + }>; +} export class GitDiffAnalyzer { private panel: vscode.WebviewPanel | undefined; @@ -23,7 +32,7 @@ export class GitDiffAnalyzer { this.isAnalyzing = true; - vscode.window.withProgress( + await vscode.window.withProgress( { location: vscode.ProgressLocation.Notification, title: 'Analyzing changes with Claude...', @@ -150,7 +159,9 @@ export class GitDiffAnalyzer { } return new Promise((resolve, reject) => { - const claudeProcess = spawn('claude', ['-p', '-'], { shell: false }); + const claudeProcess = childProcess.spawn('claude', ['-p', '-'], { + shell: false + }); let stdoutData = ''; let stderrData = ''; diff --git a/test/analyzer.test.ts b/test/analyzer.test.ts index 518440b..c82b36d 100644 --- a/test/analyzer.test.ts +++ b/test/analyzer.test.ts @@ -1,13 +1,13 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import * as sinon from 'sinon'; import { expect } from 'chai'; -import { GitDiffAnalyzer } from '../src/analyzer'; +let GitDiffAnalyzer: any; import { mockVscode, createMockContext, createMockWebviewPanel, resetMocks } from './helpers/mock-vscode'; import { mockChildProcess, waitForAsync, mockAnalysisResponse } from './helpers/test-utils'; -import * as childProcess from 'child_process'; +import childProcess = require('child_process'); describe('GitDiffAnalyzer', () => { - let analyzer: GitDiffAnalyzer; + let analyzer: any; let context: any; let sandbox: sinon.SinonSandbox; let mockCP: ReturnType; @@ -15,7 +15,6 @@ describe('GitDiffAnalyzer', () => { beforeEach(() => { sandbox = sinon.createSandbox(); context = createMockContext(); - analyzer = new GitDiffAnalyzer(context); // Setup mocks mockCP = mockChildProcess(); sandbox.stub(childProcess, 'execFile').callsFake(mockCP.execFileStub as any); @@ -33,6 +32,9 @@ describe('GitDiffAnalyzer', () => { mockVscode.window.withProgress.callsFake(async (_options: any, task: any) => { return task({ report: sinon.stub() }, { isCancellationRequested: false }); }); + + ({ GitDiffAnalyzer } = require('../src/analyzer')); + analyzer = new GitDiffAnalyzer(context); }); afterEach(() => {