|
| 1 | +/// <reference types="mocha" /> |
| 2 | + |
| 3 | +import * as assert from 'assert'; |
| 4 | +import * as fs from 'fs'; |
| 5 | +import * as os from 'os'; |
| 6 | +import * as path from 'path'; |
| 7 | +import * as sinon from 'sinon'; |
| 8 | +import * as vscode from 'vscode'; |
| 9 | +import { AspireEditorCommandProvider } from '../editor/AspireEditorCommandProvider'; |
| 10 | + |
| 11 | +function createEditor(filePath: string): vscode.TextEditor { |
| 12 | + return { |
| 13 | + document: { |
| 14 | + uri: vscode.Uri.file(filePath), |
| 15 | + fileName: filePath, |
| 16 | + languageId: 'csharp' |
| 17 | + } as vscode.TextDocument |
| 18 | + } as vscode.TextEditor; |
| 19 | +} |
| 20 | + |
| 21 | +suite('AspireEditorCommandProvider', () => { |
| 22 | + let tempDir: string; |
| 23 | + let activeEditor: vscode.TextEditor | undefined; |
| 24 | + let activeEditorStub: sinon.SinonStub; |
| 25 | + let workspaceFoldersStub: sinon.SinonStub; |
| 26 | + let getWorkspaceFolderStub: sinon.SinonStub; |
| 27 | + let onDidChangeWorkspaceFoldersStub: sinon.SinonStub; |
| 28 | + let onDidChangeActiveTextEditorStub: sinon.SinonStub; |
| 29 | + let executeCommandStub: sinon.SinonStub; |
| 30 | + |
| 31 | + setup(() => { |
| 32 | + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'aspire-editor-command-provider-')); |
| 33 | + activeEditor = undefined; |
| 34 | + |
| 35 | + activeEditorStub = sinon.stub(vscode.window, 'activeTextEditor').get(() => activeEditor); |
| 36 | + workspaceFoldersStub = sinon.stub(vscode.workspace, 'workspaceFolders').value(undefined); |
| 37 | + getWorkspaceFolderStub = sinon.stub(vscode.workspace, 'getWorkspaceFolder').callsFake((uri: vscode.Uri) => { |
| 38 | + if (uri.fsPath.startsWith(tempDir)) { |
| 39 | + return { uri: vscode.Uri.file(tempDir), name: 'test', index: 0 }; |
| 40 | + } |
| 41 | + |
| 42 | + return undefined; |
| 43 | + }); |
| 44 | + onDidChangeWorkspaceFoldersStub = sinon.stub(vscode.workspace, 'onDidChangeWorkspaceFolders').returns({ dispose: () => { } } as vscode.Disposable); |
| 45 | + onDidChangeActiveTextEditorStub = sinon.stub(vscode.window, 'onDidChangeActiveTextEditor').returns({ dispose: () => { } } as vscode.Disposable); |
| 46 | + executeCommandStub = sinon.stub(vscode.commands, 'executeCommand').resolves(undefined); |
| 47 | + }); |
| 48 | + |
| 49 | + teardown(() => { |
| 50 | + executeCommandStub.restore(); |
| 51 | + onDidChangeActiveTextEditorStub.restore(); |
| 52 | + onDidChangeWorkspaceFoldersStub.restore(); |
| 53 | + getWorkspaceFolderStub.restore(); |
| 54 | + workspaceFoldersStub.restore(); |
| 55 | + activeEditorStub.restore(); |
| 56 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 57 | + }); |
| 58 | + |
| 59 | + test('returns containing project file when active editor is SDK-style AppHost Program.cs', async () => { |
| 60 | + const appHostDirectory = path.join(tempDir, 'AppHost'); |
| 61 | + fs.mkdirSync(appHostDirectory); |
| 62 | + |
| 63 | + const programPath = path.join(appHostDirectory, 'Program.cs'); |
| 64 | + const projectPath = path.join(appHostDirectory, 'AppHost.csproj'); |
| 65 | + fs.writeFileSync(programPath, 'var builder = DistributedApplication.CreateBuilder(args);\nbuilder.Build().Run();'); |
| 66 | + fs.writeFileSync(projectPath, '<Project Sdk="Microsoft.NET.Sdk" />'); |
| 67 | + activeEditor = createEditor(programPath); |
| 68 | + |
| 69 | + const provider = new AspireEditorCommandProvider(); |
| 70 | + try { |
| 71 | + assert.strictEqual(await provider.getAppHostPath(), projectPath); |
| 72 | + } |
| 73 | + finally { |
| 74 | + provider.dispose(); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + test('returns source file when active editor is single-file apphost.cs', async () => { |
| 79 | + const appHostPath = path.join(tempDir, 'apphost.cs'); |
| 80 | + fs.writeFileSync(appHostPath, '#:sdk Aspire.AppHost.Sdk\nvar builder = DistributedApplication.CreateBuilder(args);'); |
| 81 | + activeEditor = createEditor(appHostPath); |
| 82 | + |
| 83 | + const provider = new AspireEditorCommandProvider(); |
| 84 | + try { |
| 85 | + assert.strictEqual(await provider.getAppHostPath(), appHostPath); |
| 86 | + } |
| 87 | + finally { |
| 88 | + provider.dispose(); |
| 89 | + } |
| 90 | + }); |
| 91 | +}); |
0 commit comments