diff --git a/packages/salesforcedx-vscode-apex/src/codecoverage/colorizer.ts b/packages/salesforcedx-vscode-apex/src/codecoverage/colorizer.ts index 6b4a91fe97..412ff35803 100644 --- a/packages/salesforcedx-vscode-apex/src/codecoverage/colorizer.ts +++ b/packages/salesforcedx-vscode-apex/src/codecoverage/colorizer.ts @@ -115,7 +115,7 @@ export class CodeCoverageHandler { public onDidChangeActiveTextEditor(editor?: TextEditor) { if (editor && this.statusBar.isHighlightingEnabled) { - const coverage = colorizer(editor); + const coverage = applyCoverageToSource(editor.document); this.coveredLines = coverage.coveredLines; this.uncoveredLines = coverage.uncoveredLines; } @@ -132,8 +132,8 @@ export class CodeCoverageHandler { } } else { try { - if (editor) { - const coverage = colorizer(window.activeTextEditor); + if (editor?.document) { + const coverage = applyCoverageToSource(editor.document); this.coveredLines = coverage.coveredLines; this.uncoveredLines = coverage.uncoveredLines; this.setCoverageDecorators(editor); @@ -147,13 +147,13 @@ export class CodeCoverageHandler { } private setCoverageDecorators(editor: TextEditor) { - editor.setDecorations(coveredLinesDecorationType, []); - editor.setDecorations(uncoveredLinesDecorationType, []); + editor.setDecorations(coveredLinesDecorationType, this.coveredLines); + editor.setDecorations(uncoveredLinesDecorationType, this.uncoveredLines); } } -export const colorizer = ( - editor?: TextEditor +const applyCoverageToSource = ( + document?: TextDocument ): { coveredLines: Range[]; uncoveredLines: Range[]; @@ -161,13 +161,13 @@ export const colorizer = ( let coveredLines = Array(); let uncoveredLines = Array(); if ( - editor && - !editor.document.uri.fsPath.includes(SFDX_FOLDER) && - isApexMetadata(editor.document.uri.fsPath) && - !IS_TEST_REG_EXP.test(editor.document.getText()) + document && + !document.uri.fsPath.includes(SFDX_FOLDER) && + isApexMetadata(document.uri.fsPath) && + !IS_TEST_REG_EXP.test(document.getText()) ) { const codeCovArray = getCoverageData() as { name: string }[]; - const apexMemberName = getApexMemberName(editor.document.uri.fsPath); + const apexMemberName = getApexMemberName(document.uri.fsPath); const codeCovItem = codeCovArray.find( covItem => covItem.name === apexMemberName ); @@ -176,7 +176,7 @@ export const colorizer = ( channelService.appendLine( nls.localize( 'colorizer_no_code_coverage_current_file', - editor.document.uri.fsPath + document.uri.fsPath ) ); return { coveredLines: [], uncoveredLines: [] }; @@ -189,20 +189,25 @@ export const colorizer = ( const covItem = codeCovItem as CoverageItem; for (const key in covItem.lines) { if (covItem.lines[key] === 1) { - coveredLines.push(getLineRange(editor.document, Number(key))); + coveredLines.push(getLineRange(document, Number(key))); } else { - uncoveredLines.push(getLineRange(editor.document, Number(key))); + uncoveredLines.push(getLineRange(document, Number(key))); } } } else { const covResult = codeCovItem as CodeCoverageResult; coveredLines = covResult.coveredLines.map(cov => - getLineRange(editor.document, Number(cov)) + getLineRange(document, Number(cov)) ); uncoveredLines = covResult.uncoveredLines.map(uncov => - getLineRange(editor.document, Number(uncov)) + getLineRange(document, Number(uncov)) ); } } return { coveredLines, uncoveredLines }; }; + +// export is for testing +export const colorizer = { + applyCoverageToSource +}; diff --git a/packages/salesforcedx-vscode-apex/src/commands/forceApexTestRun.ts b/packages/salesforcedx-vscode-apex/src/commands/forceApexTestRun.ts index 35910a94f3..c63c8a7e69 100644 --- a/packages/salesforcedx-vscode-apex/src/commands/forceApexTestRun.ts +++ b/packages/salesforcedx-vscode-apex/src/commands/forceApexTestRun.ts @@ -36,7 +36,8 @@ import { workspace, window, CancellationToken, - QuickPickItem + QuickPickItem, + Uri } from 'vscode'; import { channelService, OUTPUT_CHANNEL } from '../channels'; import { @@ -64,12 +65,23 @@ export class TestsSelector public async gather(): Promise< CancelResponse | ContinueResponse > { - const foundFiles = await workspace.findFiles( - `{**/*${APEX_TESTSUITE_EXT},**/*${APEX_CLASS_EXT}}`, - SFDX_FOLDER - ); - const testSuites = foundFiles.filter(file => - file.path.endsWith('.testSuite-meta.xml') + const { testSuites, apexClasses } = ( + await workspace.findFiles( + `{**/*${APEX_TESTSUITE_EXT},**/*${APEX_CLASS_EXT}}`, + SFDX_FOLDER + ) + ) + .sort((a, b) => a.fsPath.localeCompare(b.fsPath)) + .reduce( + (acc: { testSuites: Uri[]; apexClasses: Uri[] }, file) => { + if (file.path.endsWith('.cls')) { + acc.apexClasses.push(file); + } else { + acc.testSuites.push(file); + } + return acc; + }, + { testSuites: [], apexClasses: [] } ); const fileItems = testSuites @@ -79,8 +91,7 @@ export class TestsSelector description: testSuite.fsPath, type: TestType.Suite }; - }) - .sort((a, b) => a.label.localeCompare(b.label)); + }); fileItems.push({ label: nls.localize('force_apex_test_run_all_local_test_label'), @@ -98,10 +109,6 @@ export class TestsSelector type: TestType.All }); - const apexClasses = foundFiles.filter(file => - file.path.endsWith(APEX_CLASS_EXT) - ); - fileItems.push( ...apexClasses .filter(apexClass => { @@ -115,7 +122,6 @@ export class TestsSelector type: TestType.Class }; }) - .sort((a, b) => a.label.localeCompare(b.label)) ); const selection = (await window.showQuickPick( diff --git a/packages/salesforcedx-vscode-apex/test/jest/codeCoverage/colorizer.test.ts b/packages/salesforcedx-vscode-apex/test/jest/codeCoverage/colorizer.test.ts deleted file mode 100644 index 0c5854083a..0000000000 --- a/packages/salesforcedx-vscode-apex/test/jest/codeCoverage/colorizer.test.ts +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright (c) 2019, salesforce.com, inc. - * All rights reserved. - * Licensed under the BSD 3-Clause license. - * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause - */ - -import { expect } from 'chai'; -import { Range, RelativePattern, Uri, window, workspace } from 'vscode'; -import { - CodeCoverageHandler, - getLineRange -} from '../../../src/codecoverage/colorizer'; -import { StatusBarToggle } from '../../../src/codecoverage/statusBarToggle'; - -describe('Code coverage colorizer', () => { - let testCoverage: Uri[]; - - beforeEach(async () => { - testCoverage = await workspace.findFiles( - new RelativePattern( - workspace.workspaceFolders![0], - '**/DemoController.cls' - ), - new RelativePattern( - workspace.workspaceFolders![0], - '**/DemoControllerTest.cls' - ) - ); - }); - - it('Should report correct status on statusbaritem', async () => { - const statusBarToggle = new StatusBarToggle(); - const colorizer = new CodeCoverageHandler(statusBarToggle); - - expect(statusBarToggle.isHighlightingEnabled).to.equal(false); - colorizer.toggleCoverage(); - expect(statusBarToggle.isHighlightingEnabled).to.equal(true); - colorizer.toggleCoverage(); - expect(statusBarToggle.isHighlightingEnabled).to.equal(false); - }); - - it('Should report correct covered and uncovered lines for apex with code coverage', async () => { - const apexDocument = await workspace.openTextDocument(testCoverage[0]); - await window.showTextDocument(apexDocument); - - const statusBarToggle = new StatusBarToggle(); - const colorizer = new CodeCoverageHandler(statusBarToggle); - - expect(statusBarToggle.isHighlightingEnabled).to.equal(false); - expect(colorizer.coveredLines).to.be.empty; - expect(colorizer.uncoveredLines).to.be.empty; - - colorizer.toggleCoverage(); - expect(colorizer.coveredLines.length).to.equal(6); - expect(colorizer.uncoveredLines.length).to.equal(1); - const uncovered = Array(); - uncovered.push(getLineRange(apexDocument, 5)); - expect(uncovered).to.deep.equal(colorizer.uncoveredLines); - const covered = Array(); - covered.push(getLineRange(apexDocument, 10)); - covered.push(getLineRange(apexDocument, 12)); - covered.push(getLineRange(apexDocument, 14)); - covered.push(getLineRange(apexDocument, 16)); - covered.push(getLineRange(apexDocument, 19)); - covered.push(getLineRange(apexDocument, 27)); - expect(covered).to.deep.equal(colorizer.coveredLines); - expect(statusBarToggle.isHighlightingEnabled).to.equal(true); - - colorizer.toggleCoverage(); - expect(statusBarToggle.isHighlightingEnabled).to.equal(false); - // tslint:disable-next-line:no-unused-expression - expect(colorizer.coveredLines).to.be.empty; - // tslint:disable-next-line:no-unused-expression - expect(colorizer.uncoveredLines).to.be.empty; - }); - - it('Should report no lines for apex with out code coverage', async () => { - const apexTestDoc = await workspace.openTextDocument(testCoverage[1]); - await window.showTextDocument(apexTestDoc); - - const statusBarToggle = new StatusBarToggle(); - const colorizer = new CodeCoverageHandler(statusBarToggle); - - expect(statusBarToggle.isHighlightingEnabled).to.equal(false); - // tslint:disable-next-line:no-unused-expression - expect(colorizer.coveredLines).to.be.empty; - // tslint:disable-next-line:no-unused-expression - expect(colorizer.uncoveredLines).to.be.empty; - - colorizer.toggleCoverage(); - expect(colorizer.coveredLines.length).to.equal(0); - expect(colorizer.uncoveredLines.length).to.equal(0); - - colorizer.toggleCoverage(); - expect(statusBarToggle.isHighlightingEnabled).to.equal(false); - // tslint:disable-next-line:no-unused-expression - expect(colorizer.coveredLines).to.be.empty; - // tslint:disable-next-line:no-unused-expression - expect(colorizer.uncoveredLines).to.be.empty; - }); -});