|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import { describe, test, beforeAll, afterAll, expect } from '@jest/globals'; |
| 8 | +import testAssetWorkspace from './testAssets/testAssetWorkspace'; |
| 9 | +import { AnalysisSetting } from '../../src/lsptoolshost/buildDiagnosticsService'; |
| 10 | +import * as integrationHelpers from './integrationHelpers'; |
| 11 | +import path = require('path'); |
| 12 | +import { getCode, setBackgroundAnalysisScopes, waitForExpectedDiagnostics } from './diagnosticsHelpers'; |
| 13 | +describe(`[${testAssetWorkspace.description}] Test diagnostics`, function () { |
| 14 | + beforeAll(async function () { |
| 15 | + await integrationHelpers.activateCSharpExtension(); |
| 16 | + }); |
| 17 | + |
| 18 | + afterAll(async () => { |
| 19 | + await testAssetWorkspace.cleanupWorkspace(); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('Open document diagnostics', () => { |
| 23 | + let file: vscode.Uri; |
| 24 | + beforeAll(async () => { |
| 25 | + file = await integrationHelpers.openFileInWorkspaceAsync(path.join('src', 'app', 'diagnostics.cs')); |
| 26 | + }); |
| 27 | + |
| 28 | + test('Compiler and analyzer diagnostics reported for open file when set to OpenFiles', async () => { |
| 29 | + await setBackgroundAnalysisScopes({ |
| 30 | + compiler: AnalysisSetting.OpenFiles, |
| 31 | + analyzer: AnalysisSetting.OpenFiles, |
| 32 | + }); |
| 33 | + |
| 34 | + await waitForExpectedFileDiagnostics((diagnostics) => { |
| 35 | + expect(diagnostics).toHaveLength(4); |
| 36 | + |
| 37 | + expect(getCode(diagnostics[0])).toBe('IDE0005'); |
| 38 | + expect(diagnostics[0].message).toBe('Using directive is unnecessary.'); |
| 39 | + expect(diagnostics[0].range).toEqual(new vscode.Range(0, 0, 0, 16)); |
| 40 | + expect(diagnostics[0].severity).toBe(vscode.DiagnosticSeverity.Hint); |
| 41 | + |
| 42 | + expect(getCode(diagnostics[1])).toBe('CA1822'); |
| 43 | + expect(diagnostics[1].message).toBe( |
| 44 | + "Member 'FooBarBar' does not access instance data and can be marked as static" |
| 45 | + ); |
| 46 | + expect(diagnostics[1].range).toEqual(new vscode.Range(6, 20, 6, 29)); |
| 47 | + expect(diagnostics[1].severity).toBe(vscode.DiagnosticSeverity.Hint); |
| 48 | + |
| 49 | + expect(getCode(diagnostics[2])).toBe('CS0219'); |
| 50 | + expect(diagnostics[2].message).toBe("The variable 'notUsed' is assigned but its value is never used"); |
| 51 | + expect(diagnostics[2].range).toEqual(new vscode.Range(8, 16, 8, 23)); |
| 52 | + expect(diagnostics[2].severity).toBe(vscode.DiagnosticSeverity.Warning); |
| 53 | + |
| 54 | + expect(getCode(diagnostics[3])).toBe('IDE0059'); |
| 55 | + expect(diagnostics[3].message).toBe("Unnecessary assignment of a value to 'notUsed'"); |
| 56 | + expect(diagnostics[3].range).toEqual(new vscode.Range(8, 16, 8, 23)); |
| 57 | + expect(diagnostics[3].severity).toBe(vscode.DiagnosticSeverity.Hint); |
| 58 | + }, file); |
| 59 | + }); |
| 60 | + |
| 61 | + test('Compiler diagnostics reported for open file when set to FullSolution', async () => { |
| 62 | + await setBackgroundAnalysisScopes({ |
| 63 | + compiler: AnalysisSetting.FullSolution, |
| 64 | + analyzer: AnalysisSetting.OpenFiles, |
| 65 | + }); |
| 66 | + |
| 67 | + await waitForExpectedFileDiagnostics((diagnostics) => { |
| 68 | + expect(diagnostics).toHaveLength(4); |
| 69 | + |
| 70 | + expect(getCode(diagnostics[2])).toBe('CS0219'); |
| 71 | + expect(diagnostics[2].message).toBe("The variable 'notUsed' is assigned but its value is never used"); |
| 72 | + expect(diagnostics[2].range).toEqual(new vscode.Range(8, 16, 8, 23)); |
| 73 | + expect(diagnostics[2].severity).toBe(vscode.DiagnosticSeverity.Warning); |
| 74 | + }, file); |
| 75 | + }); |
| 76 | + |
| 77 | + test('No compiler diagnostics reported for open file when set to None', async () => { |
| 78 | + await setBackgroundAnalysisScopes({ |
| 79 | + compiler: AnalysisSetting.None, |
| 80 | + analyzer: AnalysisSetting.OpenFiles, |
| 81 | + }); |
| 82 | + |
| 83 | + await integrationHelpers.restartLanguageServer(); |
| 84 | + |
| 85 | + await waitForExpectedFileDiagnostics((diagnostics) => { |
| 86 | + expect(diagnostics).toHaveLength(3); |
| 87 | + |
| 88 | + expect(diagnostics.some((d) => getCode(d).startsWith('CS'))).toBe(false); |
| 89 | + }, file); |
| 90 | + }); |
| 91 | + |
| 92 | + test('Analyzer diagnostics reported for open file when set to FullSolution', async () => { |
| 93 | + await setBackgroundAnalysisScopes({ |
| 94 | + compiler: AnalysisSetting.OpenFiles, |
| 95 | + analyzer: AnalysisSetting.FullSolution, |
| 96 | + }); |
| 97 | + |
| 98 | + await waitForExpectedFileDiagnostics((diagnostics) => { |
| 99 | + expect(diagnostics).toHaveLength(4); |
| 100 | + |
| 101 | + expect(getCode(diagnostics[0])).toBe('IDE0005'); |
| 102 | + expect(diagnostics[0].message).toBe('Using directive is unnecessary.'); |
| 103 | + expect(diagnostics[0].range).toEqual(new vscode.Range(0, 0, 0, 16)); |
| 104 | + expect(diagnostics[0].severity).toBe(vscode.DiagnosticSeverity.Hint); |
| 105 | + |
| 106 | + expect(getCode(diagnostics[1])).toBe('CA1822'); |
| 107 | + expect(diagnostics[1].message).toBe( |
| 108 | + "Member 'FooBarBar' does not access instance data and can be marked as static" |
| 109 | + ); |
| 110 | + expect(diagnostics[1].range).toEqual(new vscode.Range(6, 20, 6, 29)); |
| 111 | + expect(diagnostics[1].severity).toBe(vscode.DiagnosticSeverity.Hint); |
| 112 | + |
| 113 | + expect(getCode(diagnostics[3])).toBe('IDE0059'); |
| 114 | + expect(diagnostics[3].message).toBe("Unnecessary assignment of a value to 'notUsed'"); |
| 115 | + expect(diagnostics[3].range).toEqual(new vscode.Range(8, 16, 8, 23)); |
| 116 | + expect(diagnostics[3].severity).toBe(vscode.DiagnosticSeverity.Hint); |
| 117 | + }, file); |
| 118 | + }); |
| 119 | + |
| 120 | + test('No analyzer diagnostics reported for open file when set to None', async () => { |
| 121 | + await setBackgroundAnalysisScopes({ |
| 122 | + compiler: AnalysisSetting.OpenFiles, |
| 123 | + analyzer: AnalysisSetting.None, |
| 124 | + }); |
| 125 | + |
| 126 | + await waitForExpectedFileDiagnostics((diagnostics) => { |
| 127 | + expect(diagnostics).toHaveLength(1); |
| 128 | + |
| 129 | + expect(diagnostics.some((d) => getCode(d).startsWith('IDE') || getCode(d).startsWith('CA'))).toBe( |
| 130 | + false |
| 131 | + ); |
| 132 | + }, file); |
| 133 | + }); |
| 134 | + }); |
| 135 | +}); |
| 136 | + |
| 137 | +async function waitForExpectedFileDiagnostics( |
| 138 | + assertExpectedDiagnostics: (input: vscode.Diagnostic[]) => void, |
| 139 | + file: vscode.Uri |
| 140 | +): Promise<void> { |
| 141 | + return waitForExpectedDiagnostics((diagnostics) => { |
| 142 | + const fileDiagnostics = diagnostics.find((d) => d[0].toString() === file.toString()); |
| 143 | + expect(fileDiagnostics).toBeDefined(); |
| 144 | + |
| 145 | + return assertExpectedDiagnostics(fileDiagnostics![1]); |
| 146 | + }); |
| 147 | +} |
0 commit comments