Skip to content

Commit 77595a1

Browse files
authored
Fix tests related to the onEnter format provider (#3726)
For #3674
1 parent 95f37aa commit 77595a1

6 files changed

Lines changed: 44 additions & 382 deletions

File tree

news/2 Fixes/3674.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed tests related to the `onEnter` format provider.

src/test/activation/languageServer/languageServerPackageService.test.ts

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@
77

88
import { expect } from 'chai';
99
import * as typeMoq from 'typemoq';
10-
import { WorkspaceConfiguration } from 'vscode';
1110
import { LanguageServerPackageStorageContainers } from '../../../client/activation/languageServer/languageServerPackageRepository';
1211
import { LanguageServerPackageService } from '../../../client/activation/languageServer/languageServerPackageService';
13-
import { IHttpClient } from '../../../client/activation/types';
14-
import { IApplicationEnvironment, IWorkspaceService } from '../../../client/common/application/types';
15-
import { HttpClient } from '../../../client/common/net/httpClient';
12+
import { IApplicationEnvironment } from '../../../client/common/application/types';
1613
import { AzureBlobStoreNugetRepository } from '../../../client/common/nuget/azureBlobStoreNugetRepository';
17-
import { NugetRepository } from '../../../client/common/nuget/nugetRepository';
1814
import { NugetService } from '../../../client/common/nuget/nugetService';
1915
import { INugetRepository, INugetService } from '../../../client/common/nuget/types';
2016
import { PlatformService } from '../../../client/common/platform/platformService';
@@ -29,54 +25,6 @@ suite('Language Server Package Service', () => {
2925
setup(() => {
3026
serviceContainer = typeMoq.Mock.ofType<IServiceContainer>();
3127
});
32-
test('Ensure new Major versions of Language Server is accounted for (nuget)', async function () {
33-
const skipped = true;
34-
if (skipped) {
35-
// tslint:disable-next-line:no-suspicious-comment
36-
// TODO: Why was this skipped? See gh-2615.
37-
return this.skip();
38-
}
39-
40-
const workSpaceService = typeMoq.Mock.ofType<IWorkspaceService>();
41-
const config = typeMoq.Mock.ofType<WorkspaceConfiguration>();
42-
config
43-
.setup(c => c.get(typeMoq.It.isValue('proxy'), typeMoq.It.isValue('')))
44-
.returns(() => '')
45-
.verifiable(typeMoq.Times.once());
46-
workSpaceService
47-
.setup(w => w.getConfiguration(typeMoq.It.isValue('http')))
48-
.returns(() => config.object)
49-
.verifiable(typeMoq.Times.once());
50-
serviceContainer.setup(a => a.get(typeMoq.It.isValue(IWorkspaceService))).returns(() => workSpaceService.object);
51-
52-
const nugetService = new NugetService();
53-
serviceContainer.setup(c => c.get(typeMoq.It.isValue(INugetService))).returns(() => nugetService);
54-
const httpClient = new HttpClient(serviceContainer.object);
55-
serviceContainer.setup(c => c.get(typeMoq.It.isValue(IHttpClient))).returns(() => httpClient);
56-
const platformService = new PlatformService();
57-
serviceContainer.setup(c => c.get(typeMoq.It.isValue(IPlatformService))).returns(() => platformService);
58-
const nugetRepo = new NugetRepository(serviceContainer.object);
59-
serviceContainer.setup(c => c.get(typeMoq.It.isValue(INugetRepository))).returns(() => nugetRepo);
60-
const appEnv = typeMoq.Mock.ofType<IApplicationEnvironment>();
61-
const packageJson = { languageServerVersion: '0.1.0' };
62-
appEnv.setup(e => e.packageJson).returns(() => packageJson);
63-
const platform = typeMoq.Mock.ofType<IPlatformService>();
64-
const lsPackageService = new LanguageServerPackageService(serviceContainer.object, appEnv.object, platform.object);
65-
66-
const packageName = lsPackageService.getNugetPackageName();
67-
const packages = await nugetRepo.getPackages(packageName);
68-
69-
const latestReleases = packages
70-
.filter(item => nugetService.isReleaseVersion(item.version))
71-
.sort((a, b) => a.version.compare(b.version));
72-
const latestRelease = latestReleases[latestReleases.length - 1];
73-
74-
config.verifyAll();
75-
workSpaceService.verifyAll();
76-
expect(packages).to.be.length.greaterThan(0, 'No packages returned.');
77-
expect(latestReleases).to.be.length.greaterThan(0, 'No release packages returned.');
78-
expect(latestRelease.version.major).to.be.equal(lsPackageService.maxMajorVersion, 'New Major version of Language server has been released, we need to update it at our end.');
79-
});
8028
test('Ensure new Major versions of Language Server is accounted for (azure blob)', async () => {
8129
const nugetService = new NugetService();
8230
serviceContainer.setup(c => c.get(typeMoq.It.isValue(INugetService))).returns(() => nugetService);

src/test/common/platform/platformService.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33

44
'use strict';
55

6-
import { expect } from 'chai';
6+
import { expect, use } from 'chai';
7+
import * as chaiAsPromised from 'chai-as-promised';
78
import * as os from 'os';
89
import { parse } from 'semver';
910
import { PlatformService } from '../../../client/common/platform/platformService';
1011
import { OSType } from '../../../client/common/utils/platform';
1112

13+
use(chaiAsPromised);
14+
1215
// tslint:disable-next-line:max-func-body-length
1316
suite('PlatformService', () => {
1417
const osType = getOSType();

src/test/debugger/run.test.ts

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55

66
// tslint:disable:no-invalid-this no-require-imports no-require-imports no-var-requires
77

8-
import { expect } from 'chai';
98
import * as path from 'path';
109
import { DebugClient } from 'vscode-debugadapter-testsupport';
11-
import { DebugProtocol } from 'vscode-debugprotocol';
1210
import { EXTENSION_ROOT_DIR } from '../../client/common/constants';
1311
import { noop } from '../../client/common/utils/misc';
1412
import { DebuggerTypeName, PTVSD_PATH } from '../../client/debugger/constants';
@@ -17,8 +15,6 @@ import { PYTHON_PATH, sleep } from '../common';
1715
import { IS_MULTI_ROOT_TEST, TEST_DEBUGGER } from '../initialize';
1816
import { createDebugAdapter } from './utils';
1917

20-
const isProcessRunning = require('is-running') as (number) => boolean;
21-
2218
const debugFilesPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'debugging');
2319
const debuggerType = DebuggerTypeName;
2420
suite('Run without Debugging', () => {
@@ -86,35 +82,4 @@ suite('Run without Debugging', () => {
8682
debugClient.waitForEvent('terminated')
8783
]);
8884
});
89-
test('Should kill python process when ending debug session', async function () {
90-
const skipped = true;
91-
if (skipped) {
92-
// tslint:disable-next-line:no-suspicious-comment
93-
// TODO: Why was this skipped? See gh-2308.
94-
return this.skip();
95-
}
96-
97-
const processIdOutput = new Promise<number>(resolve => {
98-
debugClient.on('output', (event: DebugProtocol.OutputEvent) => {
99-
if (event.event === 'output' && event.body.category === 'stdout') {
100-
resolve(parseInt(event.body.output.trim(), 10));
101-
}
102-
});
103-
});
104-
await Promise.all([
105-
debugClient.configurationSequence(),
106-
debugClient.launch(buildLaunchArgs('sampleWithSleep.py', false)),
107-
debugClient.waitForEvent('initialized'),
108-
processIdOutput
109-
]);
110-
111-
const processId = await processIdOutput;
112-
expect(processId).to.be.greaterThan(0, 'Invalid process id');
113-
114-
await debugClient.stop();
115-
await sleep(1000);
116-
117-
// Confirm the process is dead
118-
expect(isProcessRunning(processId)).to.be.equal(false, 'Python program is still alive');
119-
});
12085
});
Lines changed: 38 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,63 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
13

2-
// Note: This example test is leveraging the Mocha test framework.
3-
// Please refer to their documentation on https://mochajs.org/ for help.
4+
'use strict';
45

5-
import * as assert from 'assert';
6+
import { expect } from 'chai';
67
import * as path from 'path';
7-
import * as vscode from 'vscode';
8+
import { CancellationTokenSource, Position, TextDocument, workspace } from 'vscode';
9+
import { EXTENSION_ROOT_DIR } from '../../client/constants';
10+
import { OnEnterFormatter } from '../../client/typeFormatters/onEnterFormatter';
811
import { closeActiveWindows, initialize } from '../initialize';
912

10-
const formatFilesPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'formatting');
13+
const formatFilesPath = path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'pythonFiles', 'formatting');
1114
const unformattedFile = path.join(formatFilesPath, 'fileToFormatOnEnter.py');
1215

1316
suite('Formatting - OnEnter provider', () => {
14-
let document: vscode.TextDocument;
15-
let editor: vscode.TextEditor;
16-
17-
suiteSetup(initialize);
18-
setup(async () => {
19-
document = await vscode.workspace.openTextDocument(unformattedFile);
20-
editor = await vscode.window.showTextDocument(document);
17+
let document: TextDocument;
18+
let formatter: OnEnterFormatter;
19+
suiteSetup(async () => {
20+
await initialize();
21+
document = await workspace.openTextDocument(unformattedFile);
22+
formatter = new OnEnterFormatter();
2123
});
2224
suiteTeardown(closeActiveWindows);
23-
teardown(closeActiveWindows);
2425

25-
test('Simple statement', async () => {
26-
const text = await formatAtPosition(1, 0);
27-
assert.equal(text, 'x = 1', 'Line was not formatted');
28-
});
26+
test('Simple statement', () => testFormattingAtPosition(1, 0, 'x = 1'));
2927

30-
test('No formatting inside strings', async () => {
31-
let text = await formatAtPosition(2, 0);
32-
assert.equal(text, '"""x=1', 'Text inside string was formatted');
33-
text = await formatAtPosition(3, 0);
34-
assert.equal(text, '"""', 'Text inside string was formatted');
35-
});
28+
test('No formatting inside strings (2)', () => doesNotFormat(2, 0));
3629

37-
test('Whitespace before comment', async () => {
38-
const text = await formatAtPosition(4, 0);
39-
assert.equal(text, ' # comment', 'Whitespace before comment was not preserved');
40-
});
30+
test('No formatting inside strings (3)', () => doesNotFormat(3, 0));
4131

42-
test('No formatting of comment', async () => {
43-
const text = await formatAtPosition(5, 0);
44-
assert.equal(text, '# x=1', 'Text inside comment was formatted');
45-
});
32+
test('Whitespace before comment', () => doesNotFormat(4, 0));
4633

47-
test('Formatting line ending in comment', async () => {
48-
const text = await formatAtPosition(6, 0);
49-
assert.equal(text, 'x + 1 # ', 'Line ending in comment was not formatted');
50-
});
34+
test('No formatting of comment', () => doesNotFormat(5, 0));
5135

52-
test('Formatting line with @', async () => {
53-
const text = await formatAtPosition(7, 0);
54-
assert.equal(text, '@x', 'Line with @ was reformatted');
55-
});
36+
test('Formatting line ending in comment', () => testFormattingAtPosition(6, 0, 'x + 1 # '));
5637

57-
test('Formatting line with @', async () => {
58-
const text = await formatAtPosition(8, 0);
59-
assert.equal(text, 'x.y', 'Line ending with period was reformatted');
60-
});
38+
test('Formatting line with @', () => doesNotFormat(7, 0));
6139

62-
test('Formatting line with unknown neighboring tokens', async () => {
63-
const text = await formatAtPosition(9, 0);
64-
assert.equal(text, 'if x <= 1:', 'Line with unknown neighboring tokens was not formatted');
65-
});
40+
test('Formatting line with @', () => doesNotFormat(8, 0));
6641

67-
test('Formatting line with unknown neighboring tokens', async () => {
68-
const text = await formatAtPosition(10, 0);
69-
assert.equal(text, 'if 1 <= x:', 'Line with unknown neighboring tokens was not formatted');
70-
});
42+
test('Formatting line with unknown neighboring tokens', () => testFormattingAtPosition(9, 0, 'if x <= 1:'));
7143

72-
test('Formatting method definition with arguments', async () => {
73-
const text = await formatAtPosition(11, 0);
74-
assert.equal(text, 'def __init__(self, age=23)', 'Method definition with arguments was not formatted');
75-
});
44+
test('Formatting line with unknown neighboring tokens', () => testFormattingAtPosition(10, 0, 'if 1 <= x:'));
7645

77-
test('Formatting space after open brace', async () => {
78-
const text = await formatAtPosition(12, 0);
79-
assert.equal(text, 'while (1)', 'Space after open brace was not formatted');
80-
});
46+
test('Formatting method definition with arguments', () => testFormattingAtPosition(11, 0, 'def __init__(self, age=23)'));
8147

82-
test('Formatting line ending in string', async () => {
83-
const text = await formatAtPosition(13, 0);
84-
assert.equal(text, 'x + """', 'Line ending in multiline string was not formatted');
85-
});
48+
test('Formatting space after open brace', () => testFormattingAtPosition(12, 0, 'while (1)'));
49+
50+
test('Formatting line ending in string', () => testFormattingAtPosition(13, 0, 'x + """'));
8651

87-
async function formatAtPosition(line: number, character: number): Promise<string> {
88-
const edits = await vscode.commands.executeCommand<vscode.TextEdit[]>('vscode.executeFormatOnTypeProvider',
89-
document.uri, new vscode.Position(line, character), '\n', { insertSpaces: true, tabSize: 2 });
90-
if (edits) {
91-
await editor.edit(builder => edits.forEach(e => builder.replace(e.range, e.newText)));
92-
}
93-
return document.lineAt(line - 1).text;
52+
function testFormattingAtPosition(line: number, character: number, expectedFormattedString?: string): void {
53+
const token = new CancellationTokenSource().token;
54+
const edits = formatter.provideOnTypeFormattingEdits(document, new Position(line, character), '\n', { insertSpaces: true, tabSize: 2 }, token);
55+
expect(edits).to.be.lengthOf(1);
56+
expect(edits[0].newText).to.be.equal(expectedFormattedString);
57+
}
58+
function doesNotFormat(line: number, character: number): void {
59+
const token = new CancellationTokenSource().token;
60+
const edits = formatter.provideOnTypeFormattingEdits(document, new Position(line, character), '\n', { insertSpaces: true, tabSize: 2 }, token);
61+
expect(edits).to.be.lengthOf(0);
9462
}
9563
});

0 commit comments

Comments
 (0)