Skip to content

Commit 951ceda

Browse files
authored
Create python file with random names for testing refactoring (#966)
Fixes #964
1 parent a44c255 commit 951ceda

File tree

4 files changed

+37
-39
lines changed

4 files changed

+37
-39
lines changed

src/test/common/process/proc.observable.test.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { expect, use } from 'chai';
55
import * as chaiAsPromised from 'chai-as-promised';
66
import { CancellationTokenSource } from 'vscode';
77
import { PythonSettings } from '../../../client/common/configSettings';
8+
import { createDeferred } from '../../../client/common/helpers';
89
import { BufferDecoder } from '../../../client/common/process/decoder';
910
import { ProcessService } from '../../../client/common/process/proc';
1011
import { initialize } from './../../initialize';
@@ -85,17 +86,27 @@ suite('ProcessService', () => {
8586
const cancellationToken = new CancellationTokenSource();
8687
const result = procService.execObservable(pythonPath, ['-c', pythonCode.join(';')], { token: cancellationToken.token });
8788

89+
const def = createDeferred();
90+
def.promise.then(done).catch(done);
8891
expect(result).not.to.be.an('undefined', 'result is undefined');
8992
result.out.subscribe(output => {
9093
const value = output.out.trim();
9194
if (value === '1') {
9295
cancellationToken.cancel();
9396
} else {
94-
done('Output received when we shouldn\'t have.');
97+
if (!def.completed) {
98+
def.reject('Output received when we shouldn\'t have.');
99+
}
95100
}
96101
}, done, () => {
97-
const errorMsg = cancellationToken.token.isCancellationRequested ? undefined : 'Program terminated even before cancelling it.';
98-
done(errorMsg);
102+
if (def.completed) {
103+
return;
104+
}
105+
if (cancellationToken.token.isCancellationRequested) {
106+
def.resolve();
107+
} else {
108+
def.reject('Program terminated even before cancelling it.');
109+
}
99110
});
100111
});
101112

src/test/initialize.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,6 @@ export async function initializeTest(): Promise<any> {
4343
PythonSettings.dispose();
4444
}
4545

46-
export async function wait(timeoutMilliseconds: number) {
47-
return new Promise(resolve => {
48-
// tslint:disable-next-line:no-string-based-set-timeout
49-
setTimeout(resolve, timeoutMilliseconds);
50-
});
51-
}
52-
5346
export async function closeActiveWindows(): Promise<void> {
5447
return new Promise<void>((resolve, reject) => vscode.commands.executeCommand('workbench.action.closeAllEditors')
5548
// tslint:disable-next-line:no-unnecessary-callback-wrapper

src/test/refactor/extension.refactor.extract.method.test.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// tslint:disable:interface-name no-any max-func-body-length estrict-plus-operands
1+
// tslint:disable:interface-name no-any max-func-body-length estrict-plus-operands no-empty
22

33
import * as assert from 'assert';
44
import * as fs from 'fs-extra';
@@ -10,12 +10,12 @@ import { getTextEditsFromPatch } from '../../client/common/editor';
1010
import { extractMethod } from '../../client/providers/simpleRefactorProvider';
1111
import { RefactorProxy } from '../../client/refactor/proxy';
1212
import { UnitTestIocContainer } from '../unittests/serviceRegistry';
13-
import { closeActiveWindows, initialize, initializeTest, wait } from './../initialize';
13+
import { closeActiveWindows, initialize, initializeTest } from './../initialize';
1414
import { MockOutputChannel } from './../mockClasses';
1515

1616
const EXTENSION_DIR = path.join(__dirname, '..', '..', '..');
1717
const refactorSourceFile = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'refactoring', 'standAlone', 'refactor.py');
18-
const refactorTargetFile = path.join(__dirname, '..', '..', '..', 'out', 'test', 'pythonFiles', 'refactoring', 'standAlone', 'refactor.py');
18+
const refactorTargetFileDir = path.join(__dirname, '..', '..', '..', 'out', 'test', 'pythonFiles', 'refactoring', 'standAlone');
1919

2020
interface RenameResponse {
2121
results: [{ diff: string }];
@@ -25,31 +25,27 @@ suite('Method Extraction', () => {
2525
// Hack hac hack
2626
const oldExecuteCommand = vscode.commands.executeCommand;
2727
const options: vscode.TextEditorOptions = { cursorStyle: vscode.TextEditorCursorStyle.Line, insertSpaces: true, lineNumbers: vscode.TextEditorLineNumbersStyle.Off, tabSize: 4 };
28-
28+
let refactorTargetFile = '';
2929
let ioc: UnitTestIocContainer;
30-
suiteSetup(async () => {
31-
fs.copySync(refactorSourceFile, refactorTargetFile, { overwrite: true });
32-
await initialize();
33-
initializeDI();
34-
});
30+
suiteSetup(initialize);
3531
suiteTeardown(() => {
3632
vscode.commands.executeCommand = oldExecuteCommand;
3733
return closeActiveWindows();
3834
});
3935
setup(async () => {
40-
if (fs.existsSync(refactorTargetFile)) {
41-
await wait(500);
42-
fs.unlinkSync(refactorTargetFile);
43-
}
36+
initializeDI();
37+
refactorTargetFile = path.join(refactorTargetFileDir, `refactor${new Date().getTime()}.py`);
4438
fs.copySync(refactorSourceFile, refactorTargetFile, { overwrite: true });
4539
await initializeTest();
4640
(<any>vscode).commands.executeCommand = (cmd) => Promise.resolve();
4741
});
48-
teardown(() => {
42+
teardown(async () => {
4943
vscode.commands.executeCommand = oldExecuteCommand;
50-
return closeActiveWindows();
44+
try {
45+
await fs.unlink(refactorTargetFile);
46+
} catch { }
47+
await closeActiveWindows();
5148
});
52-
5349
function initializeDI() {
5450
ioc = new UnitTestIocContainer();
5551
ioc.registerCommonTypes();

src/test/refactor/extension.refactor.extract.var.test.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// tslint:disable:interface-name no-any max-func-body-length estrict-plus-operands
1+
// tslint:disable:interface-name no-any max-func-body-length estrict-plus-operands no-empty
22

33
import * as assert from 'assert';
44
import * as fs from 'fs-extra';
@@ -10,12 +10,12 @@ import { getTextEditsFromPatch } from '../../client/common/editor';
1010
import { extractVariable } from '../../client/providers/simpleRefactorProvider';
1111
import { RefactorProxy } from '../../client/refactor/proxy';
1212
import { UnitTestIocContainer } from '../unittests/serviceRegistry';
13-
import { closeActiveWindows, initialize, initializeTest, IS_CI_SERVER, wait } from './../initialize';
13+
import { closeActiveWindows, initialize, initializeTest, IS_CI_SERVER } from './../initialize';
1414
import { MockOutputChannel } from './../mockClasses';
1515

1616
const EXTENSION_DIR = path.join(__dirname, '..', '..', '..');
1717
const refactorSourceFile = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'refactoring', 'standAlone', 'refactor.py');
18-
const refactorTargetFile = path.join(__dirname, '..', '..', '..', 'out', 'test', 'pythonFiles', 'refactoring', 'standAlone', 'refactor.py');
18+
const refactorTargetFileDir = path.join(__dirname, '..', '..', '..', 'out', 'test', 'pythonFiles', 'refactoring', 'standAlone');
1919

2020
interface RenameResponse {
2121
results: [{ diff: string }];
@@ -25,28 +25,26 @@ suite('Variable Extraction', () => {
2525
// Hack hac hack
2626
const oldExecuteCommand = vscode.commands.executeCommand;
2727
const options: vscode.TextEditorOptions = { cursorStyle: vscode.TextEditorCursorStyle.Line, insertSpaces: true, lineNumbers: vscode.TextEditorLineNumbersStyle.Off, tabSize: 4 };
28+
let refactorTargetFile = '';
2829
let ioc: UnitTestIocContainer;
29-
suiteSetup(async () => {
30-
fs.copySync(refactorSourceFile, refactorTargetFile, { overwrite: true });
31-
await initialize();
32-
});
30+
suiteSetup(initialize);
3331
suiteTeardown(() => {
3432
vscode.commands.executeCommand = oldExecuteCommand;
3533
return closeActiveWindows();
3634
});
3735
setup(async () => {
3836
initializeDI();
39-
if (fs.existsSync(refactorTargetFile)) {
40-
await wait(500);
41-
fs.unlinkSync(refactorTargetFile);
42-
}
37+
refactorTargetFile = path.join(refactorTargetFileDir, `refactor${new Date().getTime()}.py`);
4338
fs.copySync(refactorSourceFile, refactorTargetFile, { overwrite: true });
4439
await initializeTest();
4540
(<any>vscode).commands.executeCommand = (cmd) => Promise.resolve();
4641
});
47-
teardown(() => {
42+
teardown(async () => {
4843
vscode.commands.executeCommand = oldExecuteCommand;
49-
return closeActiveWindows();
44+
try {
45+
await fs.unlink(refactorTargetFile);
46+
} catch { }
47+
await closeActiveWindows();
5048
});
5149

5250
function initializeDI() {

0 commit comments

Comments
 (0)