|
| 1 | +import * as assert from 'assert'; |
| 2 | +import { activate } from '../utils'; |
| 3 | + |
| 4 | +import * as vscode from 'vscode'; |
| 5 | +import { adaExtState } from '../../src/extension'; |
| 6 | +import { CMD_RELOAD_PROJECT } from '../../src/commands'; |
| 7 | +import { readFileSync, writeFileSync } from 'fs'; |
| 8 | +import { integer } from 'vscode-languageclient'; |
| 9 | + |
| 10 | +suite('Status Bar Test Suite', function () { |
| 11 | + // Make sure the extension is activated |
| 12 | + this.beforeAll(async () => { |
| 13 | + await activate(); |
| 14 | + }); |
| 15 | + |
| 16 | + /** |
| 17 | + * This function checks diagnostics for the given URI, making sure we have |
| 18 | + * the expected count for the given severity. |
| 19 | + */ |
| 20 | + async function checkDiagnosticsAndStatusBar( |
| 21 | + prjUri: vscode.Uri, |
| 22 | + nbDiags: integer, |
| 23 | + severity: vscode.DiagnosticSeverity, |
| 24 | + ) { |
| 25 | + // Get the diagnostics with the expected severity |
| 26 | + const diagnostics = new Promise<vscode.Diagnostic[]>((resolve) => { |
| 27 | + const disposable = vscode.languages.onDidChangeDiagnostics( |
| 28 | + (e: vscode.DiagnosticChangeEvent) => { |
| 29 | + if (e.uris.some((uri) => uri.path == prjUri.path)) { |
| 30 | + const diags = vscode.languages.getDiagnostics(prjUri); |
| 31 | + if (diags.some((diag) => diag.severity == severity)) { |
| 32 | + disposable.dispose(); |
| 33 | + resolve(diags); |
| 34 | + } |
| 35 | + } |
| 36 | + }, |
| 37 | + ); |
| 38 | + }); |
| 39 | + const alsDiagnostics = await diagnostics; |
| 40 | + |
| 41 | + // Check that we have the diagnostics we expect |
| 42 | + assert.strictEqual( |
| 43 | + alsDiagnostics.length, |
| 44 | + nbDiags, |
| 45 | + `Wrong number of project-related diagnostics with ${severity} severity. |
| 46 | +Actual diagnostics are: |
| 47 | +${JSON.stringify(alsDiagnostics)}`, |
| 48 | + ); |
| 49 | + |
| 50 | + // Check that the status bar colors have been updated accordingly |
| 51 | + const expectedBgColor = |
| 52 | + severity == vscode.DiagnosticSeverity.Error |
| 53 | + ? new vscode.ThemeColor('statusBarItem.errorBackground') |
| 54 | + : severity == vscode.DiagnosticSeverity.Warning |
| 55 | + ? new vscode.ThemeColor('statusBarItem.warningBackground') |
| 56 | + : undefined; |
| 57 | + const expectedFgColor = |
| 58 | + severity == vscode.DiagnosticSeverity.Error |
| 59 | + ? new vscode.ThemeColor('statusBarItem.errorForeground') |
| 60 | + : severity == vscode.DiagnosticSeverity.Warning |
| 61 | + ? new vscode.ThemeColor('statusBarItem.warningForeground') |
| 62 | + : undefined; |
| 63 | + |
| 64 | + assert.deepEqual( |
| 65 | + adaExtState.statusBar.backgroundColor, |
| 66 | + expectedBgColor, |
| 67 | + `Status bar foreground has wrong color for ${severity}`, |
| 68 | + ); |
| 69 | + assert.deepEqual( |
| 70 | + adaExtState.statusBar.color, |
| 71 | + expectedFgColor, |
| 72 | + `Status bar foreground has wrong color for ${severity}`, |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + test('Status Bar - Project loaded successfully', () => { |
| 77 | + if (vscode.workspace.workspaceFolders !== undefined) { |
| 78 | + // Gather all the diagnostics from the interesting ALS diagnostics' sources. |
| 79 | + // For the status bar we are interested only in project-related diagnostics. |
| 80 | + const alsDiagnostics: vscode.Diagnostic[] = vscode.languages |
| 81 | + .getDiagnostics() |
| 82 | + .flatMap(([, diagnostics]) => diagnostics) |
| 83 | + .filter((diag) => ['ada.project', 'ada.alire'].includes(diag.source ?? '')); |
| 84 | + |
| 85 | + // Check that we don't have any project-related |
| 86 | + assert.equal( |
| 87 | + alsDiagnostics.length, |
| 88 | + 0, |
| 89 | + 'We should not have project-related diagnostics', |
| 90 | + ); |
| 91 | + |
| 92 | + // Check the status bar colors: they should be transparent |
| 93 | + assert.strictEqual( |
| 94 | + adaExtState.statusBar.backgroundColor, |
| 95 | + undefined, |
| 96 | + 'Status bar background color should be transparent', |
| 97 | + ); |
| 98 | + assert.strictEqual( |
| 99 | + adaExtState.statusBar.color, |
| 100 | + undefined, |
| 101 | + 'Status bar foreground color should be transparent', |
| 102 | + ); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + test('Status Bar - Project loaded with warnings', async () => { |
| 107 | + if (vscode.workspace.workspaceFolders !== undefined) { |
| 108 | + // Get the workspace root folder and project URI |
| 109 | + const folder = vscode.workspace.workspaceFolders[0].uri; |
| 110 | + const prjUri = vscode.Uri.joinPath(folder, 'workspace.gpr'); |
| 111 | + const contentBefore = readFileSync(prjUri.fsPath, 'utf-8'); |
| 112 | + |
| 113 | + try { |
| 114 | + // Modify the .gpr file so that we get warnings after reloading it |
| 115 | + const newContent = contentBefore.replace( |
| 116 | + 'for Languages use ("ada");', |
| 117 | + 'for Languages use ("ada", "c");', |
| 118 | + ); |
| 119 | + writeFileSync(prjUri.fsPath, newContent, 'utf-8'); |
| 120 | + |
| 121 | + // Reload the project |
| 122 | + await vscode.commands.executeCommand(CMD_RELOAD_PROJECT); |
| 123 | + |
| 124 | + await checkDiagnosticsAndStatusBar(prjUri, 1, vscode.DiagnosticSeverity.Warning); |
| 125 | + } finally { |
| 126 | + // Restore the old .gpr file content |
| 127 | + writeFileSync(prjUri.fsPath, contentBefore); |
| 128 | + } |
| 129 | + } |
| 130 | + }); |
| 131 | + |
| 132 | + test('Status Bar - Project loaded with errors', async () => { |
| 133 | + if (vscode.workspace.workspaceFolders !== undefined) { |
| 134 | + // Get the workspace root folder and project URI |
| 135 | + const folder = vscode.workspace.workspaceFolders[0].uri; |
| 136 | + const prjUri = vscode.Uri.joinPath(folder, 'workspace.gpr'); |
| 137 | + const contentBefore = readFileSync(prjUri.fsPath, 'utf-8'); |
| 138 | + |
| 139 | + try { |
| 140 | + // Modify the .gpr file so that we get warnings after reloading it |
| 141 | + const newContent = contentBefore.replace( |
| 142 | + 'for Languages use ("ada");', |
| 143 | + 'for Unknown_Attriute use "unknown";', |
| 144 | + ); |
| 145 | + writeFileSync(prjUri.fsPath, newContent, 'utf-8'); |
| 146 | + |
| 147 | + // Reload the project |
| 148 | + await vscode.commands.executeCommand(CMD_RELOAD_PROJECT); |
| 149 | + |
| 150 | + await checkDiagnosticsAndStatusBar(prjUri, 1, vscode.DiagnosticSeverity.Error); |
| 151 | + } finally { |
| 152 | + // Restore the old .gpr file content |
| 153 | + writeFileSync(prjUri.fsPath, contentBefore); |
| 154 | + } |
| 155 | + } |
| 156 | + }); |
| 157 | +}); |
0 commit comments