diff --git a/news/3 Code Health/1953.md b/news/3 Code Health/1953.md new file mode 100644 index 000000000000..4749e1048d14 --- /dev/null +++ b/news/3 Code Health/1953.md @@ -0,0 +1 @@ +Fix rename refactoring unit tests. diff --git a/src/client/common/extensions.ts b/src/client/common/extensions.ts index 96e329532df9..ea4ec4d1bf3a 100644 --- a/src/client/common/extensions.ts +++ b/src/client/common/extensions.ts @@ -15,7 +15,7 @@ declare interface String { * By default lines are trimmed and empty lines are removed. * @param {SplitLinesOptions=} splitOptions - Options used for splitting the string. */ - splitLines(splitOptions?: { trim: boolean, removeEmptyEntries?: boolean }): string[]; + splitLines(splitOptions?: { trim: boolean; removeEmptyEntries?: boolean }): string[]; /** * Appropriately formats a string so it can be used as an argument for a command in a shell. * E.g. if an argument contains a space, then it will be enclosed within double quotes. @@ -33,10 +33,10 @@ declare interface String { * By default lines are trimmed and empty lines are removed. * @param {SplitLinesOptions=} splitOptions - Options used for splitting the string. */ -String.prototype.splitLines = function (this: string, splitOptions: { trim: boolean, removeEmptyEntries: boolean } = { removeEmptyEntries: true, trim: true }): string[] { +String.prototype.splitLines = function (this: string, splitOptions: { trim: boolean; removeEmptyEntries: boolean } = { removeEmptyEntries: true, trim: true }): string[] { let lines = this.split(/\r?\n/g); if (splitOptions && splitOptions.trim) { - lines = lines.filter(line => line.trim()); + lines = lines.map(line => line.trim()); } if (splitOptions && splitOptions.removeEmptyEntries) { lines = lines.filter(line => line.length > 0); diff --git a/src/test/refactor/rename.test.ts b/src/test/refactor/rename.test.ts index cbc4f641ddf7..03bffd245899 100644 --- a/src/test/refactor/rename.test.ts +++ b/src/test/refactor/rename.test.ts @@ -9,6 +9,7 @@ import * as path from 'path'; import * as typeMoq from 'typemoq'; import { Range, TextEditorCursorStyle, TextEditorLineNumbersStyle, TextEditorOptions, window, workspace } from 'vscode'; import { EXTENSION_ROOT_DIR } from '../../client/common/constants'; +import '../../client/common/extensions'; import { BufferDecoder } from '../../client/common/process/decoder'; import { ProcessService } from '../../client/common/process/proc'; import { PythonExecutionFactory } from '../../client/common/process/pythonExecutionFactory'; @@ -47,7 +48,8 @@ suite('Refactor Rename', () => { test('Rename function in source without a trailing empty line', async () => { const sourceFile = path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'pythonFiles', 'refactoring', 'source folder', 'without empty line.py'); - const expectedDiff = `--- a/${path.basename(sourceFile)}${EOL}+++ b/${path.basename(sourceFile)}${EOL}@@ -1,8 +1,8 @@${EOL} import os${EOL} ${EOL}-def one():${EOL}+def three():${EOL} return True${EOL} ${EOL} def two():${EOL}- if one():${EOL}- print(\"A\" + one())${EOL}+ if three():${EOL}+ print(\"A\" + three())${EOL}`; + const expectedDiff = `--- a/${path.basename(sourceFile)}${EOL}+++ b/${path.basename(sourceFile)}${EOL}@@ -1,8 +1,8 @@${EOL} import os${EOL} ${EOL}-def one():${EOL}+def three():${EOL} return True${EOL} ${EOL} def two():${EOL}- if one():${EOL}- print(\"A\" + one())${EOL}+ if three():${EOL}+ print(\"A\" + three())${EOL}` + .splitLines({ removeEmptyEntries: false, trim: false }); const proxy = new RefactorProxy(EXTENSION_ROOT_DIR, pythonSettings.object, path.dirname(sourceFile), serviceContainer.object); const textDocument = await workspace.openTextDocument(sourceFile); @@ -55,11 +57,12 @@ suite('Refactor Rename', () => { const response = await proxy.rename(textDocument, 'three', sourceFile, new Range(7, 20, 7, 23), options); expect(response.results).to.be.lengthOf(1); - expect(response.results[0].diff).to.be.equal(expectedDiff); + expect(response.results[0].diff.splitLines({ removeEmptyEntries: false, trim: false })).to.be.deep.equal(expectedDiff); }); test('Rename function in source with a trailing empty line', async () => { const sourceFile = path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'pythonFiles', 'refactoring', 'source folder', 'with empty line.py'); - const expectedDiff = `--- a/${path.basename(sourceFile)}${EOL}+++ b/${path.basename(sourceFile)}${EOL}@@ -1,8 +1,8 @@${EOL} import os${EOL} ${EOL}-def one():${EOL}+def three():${EOL} return True${EOL} ${EOL} def two():${EOL}- if one():${EOL}- print(\"A\" + one())${EOL}+ if three():${EOL}+ print(\"A\" + three())${EOL}`; + const expectedDiff = `--- a/${path.basename(sourceFile)}${EOL}+++ b/${path.basename(sourceFile)}${EOL}@@ -1,8 +1,8 @@${EOL} import os${EOL} ${EOL}-def one():${EOL}+def three():${EOL} return True${EOL} ${EOL} def two():${EOL}- if one():${EOL}- print(\"A\" + one())${EOL}+ if three():${EOL}+ print(\"A\" + three())${EOL}` + .splitLines({ removeEmptyEntries: false, trim: false }); const proxy = new RefactorProxy(EXTENSION_ROOT_DIR, pythonSettings.object, path.dirname(sourceFile), serviceContainer.object); const textDocument = await workspace.openTextDocument(sourceFile); @@ -67,6 +70,6 @@ suite('Refactor Rename', () => { const response = await proxy.rename(textDocument, 'three', sourceFile, new Range(7, 20, 7, 23), options); expect(response.results).to.be.lengthOf(1); - expect(response.results[0].diff).to.be.equal(expectedDiff); + expect(response.results[0].diff.splitLines({ removeEmptyEntries: false, trim: false })).to.be.deep.equal(expectedDiff); }); });