Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
DetachHead committed Jan 25, 2024
1 parent dcae290 commit 3899754
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
11 changes: 9 additions & 2 deletions packages/pyright-internal/src/analyzer/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -943,8 +943,15 @@ export class AnalyzerService {
return this._attemptParseFile(pyprojectPath, (fileContents, attemptCount) => {
try {
const configObj = TOML.parse(fileContents);
if (configObj && configObj.tool && (configObj.tool as TOML.JsonMap).basedpyright) {
return (configObj.tool as TOML.JsonMap).basedpyright as object;
if (configObj && configObj.tool) {
const toml = configObj.tool as TOML.JsonMap;
if (toml.basedpyright && toml.pyright) {
this._console.error(
'Pyproject file cannot have both `pyright` and `basedpyright` sections. pick one'
);
return undefined;
}
return (toml.basedpyright || toml.pyright) as object;
}
} catch (e: any) {
this._console.error(`Pyproject file parse attempt ${attemptCount} error: ${JSON.stringify(e)}`);
Expand Down
22 changes: 19 additions & 3 deletions packages/pyright-internal/src/tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,19 +300,35 @@ test('AutoSearchPathsOnAndExtraPaths', () => {
assert.deepStrictEqual(configOptions.defaultExtraPaths, expectedExtraPaths);
});

test('BasicPyprojectTomlParsing', () => {
const cwd = normalizePath(combinePaths(process.cwd(), 'src/tests/samples/project_with_pyproject_toml'));
const setupPyprojectToml = (projectPath: string) => {
const cwd = normalizePath(combinePaths(process.cwd(), projectPath));
const service = createAnalyzer();
const commandLineOptions = new CommandLineOptions(cwd, /* fromVsCodeExtension */ true);

service.setOptions(commandLineOptions);

const configOptions = service.test_getConfigOptions(commandLineOptions);
return service.test_getConfigOptions(commandLineOptions);
};

test('BasicPyprojectTomlParsing', () => {
const configOptions = setupPyprojectToml('src/tests/samples/project_with_pyproject_toml');
assert.strictEqual(configOptions.defaultPythonVersion!, PythonVersion.V3_9);
assert.strictEqual(configOptions.diagnosticRuleSet.reportMissingImports, 'error');
assert.strictEqual(configOptions.diagnosticRuleSet.reportUnusedClass, 'warning');
});

test('basedPyprojectTomlParsing', () => {
const configOptions = setupPyprojectToml('src/tests/samples/based_project_with_pyproject_toml');
assert.strictEqual(configOptions.defaultPythonVersion!, PythonVersion.V3_9);
assert.strictEqual(configOptions.diagnosticRuleSet.reportMissingImports, 'error');
assert.strictEqual(configOptions.diagnosticRuleSet.reportUnusedClass, 'warning');
});

test('both pyright and basedpyright in pyproject.toml', () => {
const configOptions = setupPyprojectToml('src/tests/samples/project_with_both_config_sections_in_pyproject_toml');
assert.strictEqual(configOptions.defaultPythonVersion!, undefined);
});

test('FindFilesInMemoryOnly', () => {
const cwd = normalizePath(process.cwd());
const service = createAnalyzer();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[tool.basedpyright]
include = ["test*.py"]

reportMissingImports = true
reportUnusedClass = "warning"

pythonVersion = "3.9"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[tool.basedpyright]
pythonVersion = "3.10"

[tool.pyright]
pythonVersion = "3.9"

0 comments on commit 3899754

Please sign in to comment.