Skip to content

Commit

Permalink
feat: update test sidebar icon coloring with test results and add tes…
Browse files Browse the repository at this point in the history
…t coverage (#3381)

* feat: update test sidebar icon coloring

* feat: test writeResultFile args
  • Loading branch information
violetyao committed Jul 9, 2021
1 parent 3f3d622 commit 3fac758
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@
import {
ApexTestResultData,
LogService,
ResultFormat,
TestLevel,
TestResult,
TestService
} from '@salesforce/apex-node';
import { Connection } from '@salesforce/core';
import { LibraryCommandletExecutor } from '@salesforce/salesforcedx-utils-vscode/out/src';
import {
getLogDirPath,
getRootWorkspacePath,
LibraryCommandletExecutor
} from '@salesforce/salesforcedx-utils-vscode/out/src';
import { notificationService } from '@salesforce/salesforcedx-utils-vscode/out/src/commands';
import { getLogDirPath } from '@salesforce/salesforcedx-utils-vscode/out/src/index';
import { getTestResultsFolder } from '@salesforce/salesforcedx-utils-vscode/out/src/helpers';
import { ContinueResponse } from '@salesforce/salesforcedx-utils-vscode/out/src/types';
import * as path from 'path';
import { workspace } from 'vscode';
import { OUTPUT_CHANNEL } from '../channels';
import { workspaceContext } from '../context';
import { nls } from '../messages';
import { retrieveTestCodeCoverage } from '../utils';
import { launchFromLogFile } from './launchFromLogFile';
import { TraceFlags } from './traceFlags';
interface TestRunResult {
Expand Down Expand Up @@ -78,7 +85,18 @@ export class QuickLaunch {
testMethod ? `${testClass}.${testMethod}` : undefined,
testClass
);
const result: TestResult = await testService.runTestSynchronous(payload);
const result: TestResult = await testService.runTestSynchronous(payload, true);
if (workspace && workspace.workspaceFolders) {
const apexTestResultsPath = getTestResultsFolder(
getRootWorkspacePath(),
'apex'
);
await testService.writeResultFiles(
result,
{ dirPath: apexTestResultsPath, resultFormats: [ResultFormat.json] },
retrieveTestCodeCoverage()
);
}
const tests: ApexTestResultData[] = result.tests;
if (tests.length === 0) {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
SEND_METRIC_ERROR_EVENT,
SEND_METRIC_LAUNCH_EVENT
} from '@salesforce/salesforcedx-apex-replay-debugger/out/src/constants';
import { getLogDirPath } from '@salesforce/salesforcedx-utils-vscode/out/src';
import * as path from 'path';
import * as pathExists from 'path-exists';
import * as vscode from 'vscode';
Expand Down Expand Up @@ -236,13 +237,7 @@ function getDialogStartingPath(): vscode.Uri | undefined {
// If lastOpenedLogFolder isn't defined or doesn't exist then use the
// same directory that the SFDX download logs command would download to
// if it exists.
const sfdxCommandLogDir = path.join(
vscode.workspace.workspaceFolders![0].uri.fsPath,
'.sfdx',
'tools',
'debug',
'logs'
);
const sfdxCommandLogDir = getLogDirPath();
if (pathExists.sync(sfdxCommandLogDir)) {
return vscode.Uri.file(sfdxCommandLogDir);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Copyright (c) 2021, 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
*/

export { retrieveTestCodeCoverage } from './settings';
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (c) 2021, 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 * as vscode from 'vscode';

export function retrieveTestCodeCoverage(): boolean {
return vscode.workspace
.getConfiguration('salesforcedx-vscode-core')
.get<boolean>('retrieve-test-code-coverage', false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import { notificationService } from '@salesforce/salesforcedx-utils-vscode/out/s
import * as utils from '@salesforce/salesforcedx-utils-vscode/out/src/index';
import { ContinueResponse } from '@salesforce/salesforcedx-utils-vscode/out/src/types';
import { expect } from 'chai';
import { write } from 'fs';
import * as path from 'path';
import { createSandbox, SinonSandbox, SinonStub } from 'sinon';
import * as vscode from 'vscode';
import * as launcher from '../../../src/commands/launchFromLogFile';
import { TestDebuggerExecutor } from '../../../src/commands/quickLaunch';
import { TraceFlags } from '../../../src/commands/traceFlags';
Expand All @@ -38,9 +40,17 @@ describe('Quick launch apex tests', () => {
let logServiceStub: SinonStub;
let launcherStub: SinonStub;
let buildPayloadStub: SinonStub;
let writeResultFilesStub: SinonStub;
let settingStub: SinonStub;

beforeEach(async () => {
sb = createSandbox();
settingStub = sb.stub();
sb.stub(vscode.workspace, 'getConfiguration')
.withArgs('salesforcedx-vscode-core')
.returns({
get: settingStub
});
$$.setConfigStubContents('AuthInfoConfig', {
contents: await testData.getConfig()
});
Expand All @@ -58,13 +68,15 @@ describe('Quick launch apex tests', () => {
.stub(TestService.prototype, 'runTestSynchronous')
.resolves({ tests: [{ apexLogId: APEX_LOG_ID }] } as TestResult);
buildPayloadStub = sb.stub(TestService.prototype, 'buildSyncPayload');
writeResultFilesStub = sb.stub(TestService.prototype, 'writeResultFiles');
});

afterEach(() => {
sb.restore();
});

it('should debug an entire test class', async () => {
settingStub.withArgs('retrieve-test-code-coverage').returns(true);
buildPayloadStub.resolves({
tests: [{ className: 'MyClass' }],
testLevel: 'RunSpecifiedTests'
Expand Down Expand Up @@ -112,9 +124,18 @@ describe('Quick launch apex tests', () => {
undefined,
'MyClass'
]);
expect(writeResultFilesStub.called).to.equal(true);
const writeResultFilesArgs = writeResultFilesStub.getCall(0).args;
expect(writeResultFilesArgs[0]).to.eql({
tests: [{
apexLogId: APEX_LOG_ID
}]
});
expect(writeResultFilesArgs[2]).to.equal(true);
});

it('should debug a single test method', async () => {
settingStub.withArgs('retrieve-test-code-coverage').returns(true);
buildPayloadStub.resolves({
tests: [{ className: 'MyClass', testMethods: ['testSomeCode'] }],
testLevel: 'RunSpecifiedTests'
Expand Down Expand Up @@ -163,9 +184,18 @@ describe('Quick launch apex tests', () => {
const launcherArgs = launcherStub.getCall(0).args;
expect(launcherArgs[0]).to.equal(path.join('logs', 'abcd.log'));
expect(launcherArgs[1]).to.equal(false);
expect(writeResultFilesStub.called).to.equal(true);
const writeResultFilesArgs = writeResultFilesStub.getCall(0).args;
expect(writeResultFilesArgs[0]).to.eql({
tests: [{
apexLogId: APEX_LOG_ID
}]
});
expect(writeResultFilesArgs[2]).to.equal(true);
});

it('should debug a single test method that fails', async () => {
settingStub.withArgs('retrieve-test-code-coverage').returns(true);
buildPayloadStub.resolves({
tests: [{ className: 'MyClass', testMethods: ['testSomeCode'] }],
testLevel: 'RunSpecifiedTests'
Expand Down Expand Up @@ -212,9 +242,14 @@ describe('Quick launch apex tests', () => {
expect(notificationArgs[0]).to.equal(
"Cannot read property 'length' of undefined"
);
expect(writeResultFilesStub.called).to.equal(true);
const writeResultFilesArgs = writeResultFilesStub.getCall(0).args;
expect(writeResultFilesArgs[0]).to.eql({});
expect(writeResultFilesArgs[2]).to.equal(true);
});

it('should display an error for a missing test', async () => {
settingStub.withArgs('retrieve-test-code-coverage').returns(true);
buildPayloadStub.resolves({
tests: [{ className: 'MyClass', testMethods: ['testSomeCode'] }],
testLevel: 'RunSpecifiedTests'
Expand Down Expand Up @@ -255,9 +290,16 @@ describe('Quick launch apex tests', () => {
expect(notificationArgs[0]).to.equal(
nls.localize('debug_test_no_results_found')
);
expect(writeResultFilesStub.called).to.equal(true);
const writeResultFilesArgs = writeResultFilesStub.getCall(0).args;
expect(writeResultFilesArgs[0]).to.eql({
tests: []
});
expect(writeResultFilesArgs[2]).to.equal(true);
});

it('should display an error for a missing log file', async () => {
settingStub.withArgs('retrieve-test-code-coverage').returns(true);
buildPayloadStub.resolves({
tests: [{ className: 'MyClass', testMethods: ['testSomeCode'] }],
testLevel: 'RunSpecifiedTests'
Expand Down Expand Up @@ -298,5 +340,11 @@ describe('Quick launch apex tests', () => {
expect(notificationArgs[0]).to.equal(
nls.localize('debug_test_no_debug_log')
);
expect(writeResultFilesStub.called).to.equal(true);
const writeResultFilesArgs = writeResultFilesStub.getCall(0).args;
expect(writeResultFilesArgs[0]).to.eql({
tests: [{}]
});
expect(writeResultFilesArgs[2]).to.equal(true);
});
});
3 changes: 3 additions & 0 deletions packages/salesforcedx-vscode-apex/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export async function activate(context: vscode.ExtensionContext) {
testResultFileWatcher.onDidCreate(uri =>
testOutlineProvider.onResultFileCreate(apexDirPath, uri.fsPath)
);
testResultFileWatcher.onDidChange(uri =>
testOutlineProvider.onResultFileCreate(apexDirPath, uri.fsPath)
);

context.subscriptions.push(testResultFileWatcher);
} else {
Expand Down
16 changes: 12 additions & 4 deletions packages/salesforcedx-vscode-apex/src/views/testOutlineProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,18 @@ export class ApexTestOutlineProvider
) {
const testRunIdFile = path.join(apexTestPath, 'test-run-id.txt');
const testRunId = readFileSync(testRunIdFile);
const testResultFilePath = path.join(
apexTestPath,
`test-result-${testRunId}.json`
);
let testResultFilePath;
if ( testRunId.toString() === '' ) {
testResultFilePath = path.join(
apexTestPath,
`test-result.json`
);
} else {
testResultFilePath = path.join(
apexTestPath,
`test-result-${testRunId}.json`
);
}
if (testResultFile === testResultFilePath) {
await this.refresh();
this.updateTestResults(testResultFile);
Expand Down

0 comments on commit 3fac758

Please sign in to comment.