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

Visual display of apex code coverage #1145

Merged
merged 10 commits into from
Mar 16, 2019
Merged

Visual display of apex code coverage #1145

merged 10 commits into from
Mar 16, 2019

Conversation

lcampos
Copy link
Contributor

@lcampos lcampos commented Mar 13, 2019

What does this PR do?

It takes the info from the test run results that contain code coverage and highlights which lines those test results cover and which they don't.

code-coverage-colorizer

What issues does this PR fix or reference?

#973

const codeCoverage = JSON.parse(testResultOutput) as CoverageTestResult;
if (codeCoverage.coverage === undefined) {
throw new Error(
`No code coverage information was found for test run ${testRunId}.`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error showed when the test results do not include code coverage information.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would we recommend that people do next? Set "salesforcedx-vscode-core.retrieve-test-code-coverage": true? Or did something else probably cause this error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, running tests without that setting on.

function getTestRunId() {
const testRunIdFile = path.join(apexDirPath, 'test-run-id.txt');
if (!fs.existsSync(testRunIdFile)) {
throw new Error('No test run information was found for this project.');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error when the project does not have any test run information, this would happen if they've never run tests via vscode/cli.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a next step? Something like, 'No test run information was found for this project. Set "salesforcedx-vscode-core.retrieve-test-code-coverage": true in your user or workspace settings, then run Apex tests from the Apex Tests sidebar or from within a test class file.'


if (!codeCovItem) {
throw new Error(
'No code coverage information was found for the current file.'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll show this when the test results do have code coverage information but not for the apex file currently in focus.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a next step? Something like, 'No code coverage information was found for this file. Set "salesforcedx-vscode-core.retrieve-test-code-coverage": true in your user or workspace settings. Then, run Apex tests that include methods in this file from the Apex Tests sidebar or using the Run Tests or Run All Tests code lens within the file.

* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { window } from 'vscode';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the color definitions used (green & red).

const statusBarToggle = new StatusBarToggle();
const colorizer = new CodeCoverage(statusBarToggle);
const colorizerCmd = vscode.commands.registerCommand(
'sfdx.force.apex.toggle.colorizer',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tied everything under one command so that it's easy to trigger the feature using keyboard shortcuts.

@@ -0,0 +1,105 @@
{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to add these files under testresults in order to run the tests. Let me know if you have a better idea for generating these for testing purposes.

'sfdx.force.apex.toggle.colorizer';
private static readonly showIcon = '$(tasklist)';
private static readonly hideIcon = '$(three-bars)';
private static readonly toolTip = 'Apex Code Coverage highlighter';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Text showed when you hover the status bar icon.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other nearby hover text seems to use imperative verbs and title-case capitalization. How about this? 'Highlight Apex Code Coverage'

@codecov
Copy link

codecov bot commented Mar 13, 2019

Codecov Report

Merging #1145 into develop will increase coverage by 0.1%.
The diff coverage is 79.59%.

Impacted file tree graph

@@            Coverage Diff            @@
##           develop   #1145     +/-   ##
=========================================
+ Coverage    72.59%   72.7%   +0.1%     
=========================================
  Files          189     193      +4     
  Lines         7124    7222     +98     
  Branches       731     746     +15     
=========================================
+ Hits          5172    5251     +79     
- Misses        1803    1814     +11     
- Partials       149     157      +8
Impacted Files Coverage Δ
...salesforcedx-vscode-apex/src/codecoverage/index.ts 0% <0%> (ø)
...orcedx-vscode-apex/src/codecoverage/decorations.ts 100% <100%> (ø)
...sforcedx-vscode-apex/src/codecoverage/colorizer.ts 76.05% <76.05%> (ø)
...dx-vscode-apex/src/codecoverage/statusBarToggle.ts 95% <95%> (ø)
...forcedx-vscode-core/src/channels/channelService.ts 85.71% <0%> (+2.38%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cdedef7...e808340. Read the comment docs.

@ruthemmanuelle
Copy link
Contributor

Glad to see this, @lcampos . I added some comments on the messages. Also, shouldn't the messages be externalized into messages files?

@@ -130,6 +139,7 @@ function registerCommands(
forceApexTestMethodRunCodeAction
);
return vscode.Disposable.from(
colorizerCmd,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be forceApexToggleColorizerCmd to match the rest?

// Colorize code coverage
const statusBarToggle = new StatusBarToggle();
const colorizer = new CodeCoverage(statusBarToggle);
const colorizerCmd = vscode.commands.registerCommand(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the pattern i am seeing for other commands, i think this could be renamed to forceApexToggleColorizerCmd.

@praksb
Copy link
Contributor

praksb commented Mar 13, 2019

@lcampos what happens if we try this feature on a stale run result? Let's say that the apex class has since been edited and a bunch of methods have been removed. Does attempting to map nonexistent lines fail gracefully?

@lcampos
Copy link
Contributor Author

lcampos commented Mar 15, 2019

@praksb it does fail gracefully. If the code just shifted but the lines that will be colored are still in the range of the document, then we will just color whatever code, comment, empty space available. If the document's length has shortened, then it will just display a warning about it if the lines that need to be colored are not part of the range of the document.
I'll add a message to let the customer know they need to refresh their code coverage run but it will only be displayed in the last case described above since we do not do further validations to the code in the file or code coverage run. I wanted to keep the feature as simple as possible in the first version and then see if we needed to add more validations via apex lsp, etc.

@lcampos
Copy link
Contributor Author

lcampos commented Mar 15, 2019

@ruthemmanuelle thanks for the review, yes we will have these messages on the extension's i18n.ts file. Since I didn't know how many different labels we were gonna need I just kept it on the code.

Copy link
Collaborator

@AnanyaJha AnanyaJha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command renames Prakash mentioned seem like a good idea, otherwise the changes look cool!

@lcampos lcampos force-pushed the lcampos/colorizer branch from 8981e06 to d198fe0 Compare March 15, 2019 18:40
@@ -40,5 +40,14 @@ export const messages = {

client_name: 'Apex Language Server',
cannot_determine_workspace:
'Unable to determine workspace folders for workspace'
'Unable to determine workspace folders for workspace',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ruthemmanuelle here are the new error messages, let me know if any changes are needed.

@lcampos lcampos force-pushed the lcampos/colorizer branch from d198fe0 to e808340 Compare March 16, 2019 01:39
@lcampos lcampos merged commit be06b64 into develop Mar 16, 2019
@lcampos lcampos deleted the lcampos/colorizer branch March 16, 2019 02:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants