|
| 1 | +import assert from 'assert'; |
| 2 | +import * as sinon from 'sinon'; |
| 3 | +import { CancellationError, CancellationTokenSource, LogOutputChannel } from 'vscode'; |
| 4 | +import * as childProcessApis from '../../../common/childProcess.apis'; |
| 5 | +import { runUV } from '../../../managers/builtin/helpers'; |
| 6 | +import { createMockLogOutputChannel } from '../../mocks/helper'; |
| 7 | +import { MockChildProcess } from '../../mocks/mockChildProcess'; |
| 8 | + |
| 9 | +suite('Helpers - runUV', () => { |
| 10 | + let mockLog: LogOutputChannel; |
| 11 | + let spawnStub: sinon.SinonStub; |
| 12 | + |
| 13 | + setup(() => { |
| 14 | + mockLog = createMockLogOutputChannel(); |
| 15 | + spawnStub = sinon.stub(childProcessApis, 'spawnProcess'); |
| 16 | + }); |
| 17 | + |
| 18 | + teardown(() => { |
| 19 | + sinon.restore(); |
| 20 | + }); |
| 21 | + |
| 22 | + test('should resolve with stdout when command succeeds', async () => { |
| 23 | + const mockProcess = new MockChildProcess('uv', ['pip', 'list']); |
| 24 | + spawnStub.withArgs('uv', ['pip', 'list']).returns(mockProcess); |
| 25 | + |
| 26 | + const resultPromise = runUV(['pip', 'list'], undefined, mockLog); |
| 27 | + |
| 28 | + setTimeout(() => { |
| 29 | + mockProcess.stdout?.emit('data', Buffer.from('package1==1.0.0\n')); |
| 30 | + mockProcess.stdout?.emit('data', Buffer.from('package2==2.0.0\n')); |
| 31 | + mockProcess.emit('exit', 0, null); |
| 32 | + (mockProcess as unknown as { emit: (event: string) => void }).emit('close'); |
| 33 | + }, 10); |
| 34 | + |
| 35 | + const result = await resultPromise; |
| 36 | + |
| 37 | + assert.strictEqual(result, 'package1==1.0.0\npackage2==2.0.0\n'); |
| 38 | + }); |
| 39 | + |
| 40 | + test('should reject when command exits with non-zero code', async () => { |
| 41 | + const mockProcess = new MockChildProcess('uv', ['pip', 'install', 'nonexistent']); |
| 42 | + spawnStub.withArgs('uv', ['pip', 'install', 'nonexistent']).returns(mockProcess); |
| 43 | + |
| 44 | + const resultPromise = runUV(['pip', 'install', 'nonexistent'], undefined, mockLog); |
| 45 | + |
| 46 | + setTimeout(() => { |
| 47 | + mockProcess.stderr?.emit('data', Buffer.from('error: package not found\n')); |
| 48 | + mockProcess.emit('exit', 1, null); |
| 49 | + }, 10); |
| 50 | + |
| 51 | + await assert.rejects(resultPromise, Error); |
| 52 | + }); |
| 53 | + |
| 54 | + test('should reject when process spawn fails', async () => { |
| 55 | + const mockProcess = new MockChildProcess('uv', ['pip', 'list']); |
| 56 | + spawnStub.withArgs('uv', ['pip', 'list']).returns(mockProcess); |
| 57 | + |
| 58 | + const resultPromise = runUV(['pip', 'list'], undefined, mockLog); |
| 59 | + |
| 60 | + setTimeout(() => { |
| 61 | + mockProcess.emit('error', new Error('spawn uv ENOENT')); |
| 62 | + }, 10); |
| 63 | + |
| 64 | + await assert.rejects(resultPromise, Error); |
| 65 | + }); |
| 66 | + |
| 67 | + test('should handle cancellation token', async () => { |
| 68 | + const mockProcess = new MockChildProcess('uv', ['venv', 'create']); |
| 69 | + spawnStub.withArgs('uv', ['venv', 'create']).returns(mockProcess); |
| 70 | + |
| 71 | + const tokenSource = new CancellationTokenSource(); |
| 72 | + const resultPromise = runUV(['venv', 'create'], undefined, mockLog, tokenSource.token); |
| 73 | + |
| 74 | + setTimeout(() => { |
| 75 | + tokenSource.cancel(); |
| 76 | + }, 10); |
| 77 | + |
| 78 | + await assert.rejects(resultPromise, (err: Error) => { |
| 79 | + assert.ok(err instanceof CancellationError); |
| 80 | + return true; |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + test('should use provided working directory', async () => { |
| 85 | + const mockProcess = new MockChildProcess('uv', ['pip', 'list']); |
| 86 | + const cwd = '/test/directory'; |
| 87 | + spawnStub.withArgs('uv', ['pip', 'list'], { cwd }).returns(mockProcess); |
| 88 | + |
| 89 | + const resultPromise = runUV(['pip', 'list'], cwd, mockLog); |
| 90 | + |
| 91 | + setTimeout(() => { |
| 92 | + mockProcess.stdout?.emit('data', Buffer.from('output\n')); |
| 93 | + mockProcess.emit('exit', 0, null); |
| 94 | + (mockProcess as unknown as { emit: (event: string) => void }).emit('close'); |
| 95 | + }, 10); |
| 96 | + |
| 97 | + await resultPromise; |
| 98 | + |
| 99 | + assert.ok(spawnStub.calledWith('uv', ['pip', 'list'], { cwd })); |
| 100 | + }); |
| 101 | + |
| 102 | + test('should work without logger', async () => { |
| 103 | + const mockProcess = new MockChildProcess('uv', ['--version']); |
| 104 | + spawnStub.withArgs('uv', ['--version']).returns(mockProcess); |
| 105 | + |
| 106 | + const resultPromise = runUV(['--version']); |
| 107 | + |
| 108 | + setTimeout(() => { |
| 109 | + mockProcess.stdout?.emit('data', Buffer.from('uv 0.1.0\n')); |
| 110 | + mockProcess.emit('exit', 0, null); |
| 111 | + (mockProcess as unknown as { emit: (event: string) => void }).emit('close'); |
| 112 | + }, 10); |
| 113 | + |
| 114 | + const result = await resultPromise; |
| 115 | + |
| 116 | + assert.strictEqual(result, 'uv 0.1.0\n'); |
| 117 | + }); |
| 118 | + |
| 119 | + test('should concatenate multiple stdout chunks correctly', async () => { |
| 120 | + const mockProcess = new MockChildProcess('uv', ['pip', 'list']); |
| 121 | + spawnStub.withArgs('uv', ['pip', 'list']).returns(mockProcess); |
| 122 | + |
| 123 | + const resultPromise = runUV(['pip', 'list'], undefined, mockLog); |
| 124 | + |
| 125 | + setTimeout(() => { |
| 126 | + mockProcess.stdout?.emit('data', Buffer.from('line1\n')); |
| 127 | + mockProcess.stdout?.emit('data', Buffer.from('line2\n')); |
| 128 | + mockProcess.stdout?.emit('data', Buffer.from('line3\n')); |
| 129 | + mockProcess.emit('exit', 0, null); |
| 130 | + (mockProcess as unknown as { emit: (event: string) => void }).emit('close'); |
| 131 | + }, 10); |
| 132 | + |
| 133 | + const result = await resultPromise; |
| 134 | + |
| 135 | + assert.strictEqual(result, 'line1\nline2\nline3\n'); |
| 136 | + }); |
| 137 | +}); |
0 commit comments