|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import { expect } from 'chai'; |
| 7 | +import * as path from 'path'; |
| 8 | +import * as sinon from 'sinon'; |
| 9 | +import { Uri, WorkspaceFolder } from 'vscode'; |
| 10 | +import { DynamicPythonDebugConfigurationService } from '../../../extension/debugger/configuration/dynamicdebugConfigurationService'; |
| 11 | +import * as configurationUtils from '../../../extension/debugger/configuration/utils/configuration'; |
| 12 | + |
| 13 | +suite('Debugging - Dynamic Debug Configuration Service', () => { |
| 14 | + const folder: WorkspaceFolder = { uri: Uri.file('/work'), name: 'ws', index: 0 }; |
| 15 | + let service: DynamicPythonDebugConfigurationService; |
| 16 | + let getFastApiPathsStub: sinon.SinonStub; |
| 17 | + |
| 18 | + setup(() => { |
| 19 | + service = new DynamicPythonDebugConfigurationService(); |
| 20 | + sinon.stub(configurationUtils, 'getDjangoPaths').resolves([]); |
| 21 | + sinon.stub(configurationUtils, 'getFlaskPaths').resolves([]); |
| 22 | + getFastApiPathsStub = sinon.stub(configurationUtils, 'getFastApiPaths'); |
| 23 | + }); |
| 24 | + |
| 25 | + teardown(() => { |
| 26 | + sinon.restore(); |
| 27 | + }); |
| 28 | + |
| 29 | + const fastApiProviders = async () => { |
| 30 | + const result = await service.provideDebugConfigurations(folder); |
| 31 | + return (result ?? []).filter((c) => c.name?.includes('FastAPI')); |
| 32 | + }; |
| 33 | + |
| 34 | + test('No FastAPI detected → no FastAPI configs offered', async () => { |
| 35 | + getFastApiPathsStub.resolves([]); |
| 36 | + |
| 37 | + const fastApi = await fastApiProviders(); |
| 38 | + expect(fastApi).to.have.length(0); |
| 39 | + }); |
| 40 | + |
| 41 | + test('Single match at workspace root → project config uses resolved path, file variant uses ${file}', async () => { |
| 42 | + getFastApiPathsStub.resolves([Uri.file('/work/main.py')]); |
| 43 | + |
| 44 | + const fastApi = await fastApiProviders(); |
| 45 | + expect(fastApi).to.have.length(2); |
| 46 | + expect(fastApi[0]).to.include({ name: 'Python Debugger: FastAPI', module: 'fastapi' }); |
| 47 | + expect(fastApi[0].args).to.deep.equal(['run', 'main.py']); |
| 48 | + expect(fastApi[1]).to.include({ name: 'Python Debugger: FastAPI File', module: 'fastapi' }); |
| 49 | + expect(fastApi[1].args).to.deep.equal(['run', '${file}']); |
| 50 | + }); |
| 51 | + |
| 52 | + test('Single match in subdirectory → project config passes path explicitly', async () => { |
| 53 | + getFastApiPathsStub.resolves([Uri.file('/work/backend/app/main.py')]); |
| 54 | + |
| 55 | + const fastApi = await fastApiProviders(); |
| 56 | + expect(fastApi[0].args).to.deep.equal(['run', path.join('backend', 'app', 'main.py')]); |
| 57 | + }); |
| 58 | + |
| 59 | + test('Multiple matches → project config falls back to plain `fastapi run`', async () => { |
| 60 | + getFastApiPathsStub.resolves([Uri.file('/work/svc-a/main.py'), Uri.file('/work/svc-b/main.py')]); |
| 61 | + |
| 62 | + const fastApi = await fastApiProviders(); |
| 63 | + expect(fastApi[0].args).to.deep.equal(['run']); |
| 64 | + }); |
| 65 | +}); |
0 commit comments