Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Baseline lsp unit tests #706

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/pyright-internal/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* Configuration for jest tests.
*/

module.exports = {
/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
const config = {
testEnvironment: 'node',
roots: ['<rootDir>/src/tests'],
transform: {
Expand All @@ -19,3 +20,5 @@ module.exports = {
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
setupFiles: ['./src/tests/setupTests.ts'],
};

module.exports = config;
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/baseline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface BaselinedDiagnostic {
range: { startColumn: number; endColumn: number };
}

interface BaselineFile {
export interface BaselineFile {
files: {
[filePath: string]: BaselinedDiagnostic[];
};
Expand Down
68 changes: 68 additions & 0 deletions packages/pyright-internal/src/tests/languageServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import {
runPyrightServer,
waitForDiagnostics,
} from './lsp/languageServerTestUtils';
import { BaselineFile } from '../baseline';
import { DiagnosticRule } from '../common/diagnosticRules';

/** objects from `sendRequest` don't work with assertions and i cant figure out why */
const assertEqual = <T>(actual: T, expected: T) => expect(JSON.parse(JSON.stringify(actual))).toStrictEqual(expected);
Expand Down Expand Up @@ -962,4 +964,70 @@ describe(`Basic language server tests`, () => {
'invalid diagnosticMode: "asdf". valid options are "workspace" or "openFilesOnly"'
);
});
describe('baseline', () => {
test('baselined error not shown', async () => {
const baseline: BaselineFile = {
files: {
'./foo.py': [
{
code: DiagnosticRule.reportAssignmentType,
range: {
startColumn: 11,
endColumn: 13,
},
},
],
},
};
const code = `
// @filename: .basedpyright/baseline.json
//// ${JSON.stringify(baseline)}
////
// @filename: foo.py
//// foo: int = ""
//// asdf
//// [|/*marker*/|]
`;
const settings = [
{
item: {
scopeUri: `file://${normalizeSlashes(DEFAULT_WORKSPACE_ROOT, '/')}`,
section: 'basedpyright.analysis',
},
value: {
diagnosticSeverityOverrides: {
reportAssignmentType: 'error',
reportUndefinedVariable: 'error',
},
},
},
];

const info = await runLanguageServer(
DEFAULT_WORKSPACE_ROOT,
code,
/* callInitialize */ true,
settings,
undefined,
/* supportsBackgroundThread */ false
);

// get the file containing the marker that also contains our task list comments
await openFile(info, 'marker');

// Wait for the diagnostics to publish
const diagnostics = await waitForDiagnostics(info);
const file = diagnostics.find((d) => d.uri.includes('test.py'));
assert(file);

// Make sure the error has a special rule
assert.equal(file.diagnostics[0].code, 'reportUnknownParameterType');

// make sure additional diagnostic severities work
assert.equal(
file.diagnostics.find((diagnostic) => diagnostic.code === 'reportUnusedFunction')?.severity,
DiagnosticSeverity.Hint // TODO: hint? how do we differentiate between unused/unreachable/deprecated?
);
});
});
});
9 changes: 8 additions & 1 deletion packages/pyright-internal/src/tests/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { SemanticTokenItem, SemanticTokensWalker } from '../analyzer/semanticTok
import { TypeInlayHintsItemType, TypeInlayHintsWalker } from '../analyzer/typeInlayHintsWalker';
import { Range } from 'vscode-languageserver-types';
import { ServiceProvider } from '../common/serviceProvider';
import { BaselineDiff, BaselineHandler } from '../baseline';

// This is a bit gross, but it's necessary to allow the fallback typeshed
// directory to be located when running within the jest environment. This
Expand All @@ -48,6 +49,7 @@ export interface FileAnalysisResult {
unusedCodes: Diagnostic[];
unreachableCodes: Diagnostic[];
deprecateds: Diagnostic[];
baselineDiff: BaselineDiff<boolean> | undefined;
}

export function resolveSampleFilePath(fileName: string): string {
Expand Down Expand Up @@ -171,6 +173,7 @@ export function getAnalysisResults(
}

const sourceFiles = fileUris.map((filePath) => program.getSourceFile(filePath));
const baseline = new BaselineHandler(program.fileSystem, program.rootPath);
return sourceFiles.map((sourceFile, index) => {
if (sourceFile) {
const diagnostics = sourceFile.getDiagnostics(configOptions) || [];
Expand All @@ -183,6 +186,9 @@ export function getAnalysisResults(
unusedCodes: diagnostics.filter((diag) => diag.category === DiagnosticCategory.UnusedCode),
unreachableCodes: diagnostics.filter((diag) => diag.category === DiagnosticCategory.UnreachableCode),
deprecateds: diagnostics.filter((diag) => diag.category === DiagnosticCategory.Deprecated),
baselineDiff: baseline.write(false, true, [
{ diagnostics, fileUri: sourceFile.getUri(), version: 1, reason: 'analysis' },
]),
};
return analysisResult;
} else {
Expand All @@ -197,6 +203,7 @@ export function getAnalysisResults(
unusedCodes: [],
unreachableCodes: [],
deprecateds: [],
baselineDiff: undefined,
};
return analysisResult;
}
Expand Down Expand Up @@ -265,7 +272,7 @@ export const validateResultsButBased = (allResults: FileAnalysisResult[], expect
assert.strictEqual(allResults.length, 1);
const result = allResults[0];
for (const [diagnosticType] of entries(result)) {
if (diagnosticType === 'fileUri' || diagnosticType === 'parseResults') {
if (diagnosticType === 'fileUri' || diagnosticType === 'parseResults' || diagnosticType === 'baselineDiff') {
continue;
}
const actualResult = result[diagnosticType].map(
Expand Down
Loading