Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update test sidebar icon coloring with test results and add test coverage #3381

Merged
merged 6 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -38,6 +38,7 @@ describe('Quick launch apex tests', () => {
let logServiceStub: SinonStub;
let launcherStub: SinonStub;
let buildPayloadStub: SinonStub;
let writeResultFilesStub: SinonStub;

beforeEach(async () => {
sb = createSandbox();
Expand All @@ -58,6 +59,7 @@ 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(() => {
Expand Down Expand Up @@ -112,6 +114,7 @@ describe('Quick launch apex tests', () => {
undefined,
'MyClass'
]);
expect(writeResultFilesStub.called).to.equal(true);
violetyao marked this conversation as resolved.
Show resolved Hide resolved
});

it('should debug a single test method', async () => {
Expand Down Expand Up @@ -163,6 +166,7 @@ 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);
});

it('should debug a single test method that fails', async () => {
Expand Down Expand Up @@ -212,6 +216,7 @@ describe('Quick launch apex tests', () => {
expect(notificationArgs[0]).to.equal(
"Cannot read property 'length' of undefined"
);
expect(writeResultFilesStub.called).to.equal(true);
});

it('should display an error for a missing test', async () => {
Expand Down Expand Up @@ -255,6 +260,7 @@ describe('Quick launch apex tests', () => {
expect(notificationArgs[0]).to.equal(
nls.localize('debug_test_no_results_found')
);
expect(writeResultFilesStub.called).to.equal(true);
});

it('should display an error for a missing log file', async () => {
Expand Down Expand Up @@ -298,5 +304,6 @@ describe('Quick launch apex tests', () => {
expect(notificationArgs[0]).to.equal(
nls.localize('debug_test_no_debug_log')
);
expect(writeResultFilesStub.called).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
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