Skip to content

Add code coverage report generation instructions#6515

Merged
adityapatwardhan merged 10 commits intoPowerShell:masterfrom
rjmholt:code-coverage-doc
May 4, 2018
Merged

Add code coverage report generation instructions#6515
adityapatwardhan merged 10 commits intoPowerShell:masterfrom
rjmholt:code-coverage-doc

Conversation

@rjmholt
Copy link
Copy Markdown
Collaborator

@rjmholt rjmholt commented Mar 27, 2018

PR Summary

Adds a document to the repo that details how to obtain code coverage analysis for PowerShell using OpenCover.

Would be happy with any feedback on:

  • Extra steps or info that should be included
  • Using PowerShell highlighting in code blocks, rather than no highlighting
  • Whether to include a prompt or not
  • Chopping down parameters with newlines (was thinking of smaller laptop screens)

We should also update the code coverage blog posts to possibly point at this document.

PR Checklist

@SteveL-MSFT
Copy link
Copy Markdown
Member

@rjmholt FYI - the assignees should only be maintainers. You can add people to reviewers.

@rjmholt
Copy link
Copy Markdown
Collaborator Author

rjmholt commented Mar 27, 2018

@SteveL-MSFT Understood

@@ -0,0 +1,127 @@
Getting Code Coverage Analysis for PowerShell
===
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I believe we've standardized on using hashes for headers

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'll change it over, although my opinion is that the hashes are less readable in the raw view.


**Note: Code coverage is currently only supported on Windows, since we use OpenCover**

The PowerShell code base is configured to build with code coverage support using [OpenCover](https://github.com/OpenCover/opencover). You can see the testing coverage of the current [`master`](https://github.com/PowerShell/PowerShell) branch build at any time at [codecov.io](https://codecov.io/gh/PowerShell/PowerShell).
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Put the second sentence on a separate line (semantic linebreak)

The PowerShell code base is configured to build with code coverage support using [OpenCover](https://github.com/OpenCover/opencover). You can see the testing coverage of the current [`master`](https://github.com/PowerShell/PowerShell) branch build at any time at [codecov.io](https://codecov.io/gh/PowerShell/PowerShell).

To run test coverage analysis of PowerShell on your own branch/machine
however, you will need to take the following steps (and be aware that running
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"however," seems out of place and unnecessary

Running tests with code coverage analysis
---

**First**: Open PowerShell in an **Administrator** session. OpenCover
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

replace Administrator with elevated

PS> Import-Module $PWD\test\tools\OpenCover

# Install OpenCover to a temporary directory
PS> Install-OpenCover -TargetDirectory $env:TEMP -Force
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I thought we updated this cmdlet so it has a default TargetDirectory so it doesn't need to be specified?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The original blog post gives an argument (except with no parameter, and Install-OpenCover's first positional argument is actually the URL from which to download OpenCover). I'm happy with using a default, but in that case I think we should write some documentation of build function parameters so that Get-Help has some description of what the parameters do.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Looking at the file, the current default TargetDirectory is $HOME

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If the defaults work, I would recommend not adding additional parameters as it makes it more confusing to new users

# If you want to run only the continuous integration tests,
# add -CIOnly, which will take less time
PS> Invoke-OpenCover `
>> -OutputLog coverage.xml `
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I believe we also have reasonable defaults for these

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

If we use the default OpenCoverPath in Install-OpenCover, we should do that here too.

I assume the function is designed to be run in a specific place, and in that case has sane defaults for TestPath and PowerShellExeDirectory (in which case we can take out the line defining $psDir).

But in the case of OutputLog I think we should keep that so anyone following along knows where the output will be.

In general, I'm sort of in favour of using explicit parameters in the documentation, only because there's more information to work with if something goes wrong. It's why PowerShell has named parameters after all.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I say all this after having to read the scripts to work out what the parameters do

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The defaults here are:

  • OutputLog: "$home/Documents/OpenCover.xml"
  • TestPath: "${script:psRepoPath}/test/powershell"
  • OpenCoverPath: "$home/OpenCover"
  • PowerShellExePath: "${script:psRepoPath}/src/powershell-win-core/bin/CodeCoverage/netcoreapp2.0/win7-x64/publish"

So I feel like TestPath and PowerShellExePath are pretty sane and we can omit them happily.

For OpenCoverPath we should only use the default path if Install-OpenCover does (I'm not a huge fan of installing test tools in $HOME, especially if the user has no knowledge of that, but we could change the default).

And OpenCoverPath I think is definitely better being explicit. Nobody will ever find OpenCover.xml in their $HOME directory.

```powershell
# Collect the coverage data using Get-CodeCoverage from the OpenCover
# module that was imported above
PS> $coverageData = Get-CodeCoverage .\coverage.xml
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

May be worth mentioning that this is an expensive operation so recommendation is to store it in a variable for reuse/re-viewing


**Note: Code coverage is currently only supported on Windows, since we use OpenCover.**

The PowerShell code base is configured to build with code coverage support using [OpenCover](https://github.com/OpenCover/opencover). You can see the testing coverage of the current [`master`](https://github.com/PowerShell/PowerShell)
Copy link
Copy Markdown
Contributor

@markekraus markekraus Mar 27, 2018

Choose a reason for hiding this comment

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

Would you be so kind as to switch to semantic linefeeds and use reference links throughout the document?

Example:

The PowerShell code base is configured to build with code coverage support using [OpenCover].
You can see the testing coverage of the current [`master`] branch build at any time at [codecov.io].

[OpenCover]: https://github.com/OpenCover/opencover
[`master`]: https://github.com/PowerShell/PowerShell
[codecov.io]: https://codecov.io/gh/PowerShell/PowerShell

Becomes:

The PowerShell code base is configured to build with code coverage support using OpenCover. You can see the testing coverage of the current master branch build at any time at codecov.io.

Makes it easier to track changes.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Ah that's a nice feature! Will do.


## Running tests with code coverage analysis

**First**: Open PowerShell in an **elevated** session. OpenCover needs elevated privileges to work.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Second sentence should be on newline. The renderer will put it back.

PS> Import-Module $PWD\test\tools\OpenCover

# Install OpenCover to a temporary directory
PS> Install-OpenCover -TargetDirectory $env:TEMP -Force
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If the defaults work, I would recommend not adding additional parameters as it makes it more confusing to new users

@rjmholt
Copy link
Copy Markdown
Collaborator Author

rjmholt commented Mar 29, 2018

I've removed a couple of the parameters given. The rest are the coverage file itself and the OpenCover installation directory. In the case of the code coverage XML, there's no indication of where it is unless the user explicitly states it, so I think better to document it. In the case of the OpenCover installation directory, the default is $HOME, and we don't provide or give directions to clean up afterward. I don't think we should be installing things into $HOME by default and not telling users or cleaning up afterward, so I think for now we should just give them the parameter to set (and recommend the temp directory). I feel like I'm a fairly new user and the most confusing parts to me are where something is not documented or implicit -- if I found OpenCover under ~ a few days later, I would have no idea where it came from or whether I could delete it -- so I'm going to leave those parameters in.

@SteveL-MSFT
Copy link
Copy Markdown
Member

@rjmholt separate from this PR, perhaps you can submit a new PR to have OpenCover installed to ~/.opencover which is consistent with where we stick dotnet and rcedit. In general, I would prefer the user not to have to specify parameters where not absolutely necessary as we should have good defaults. If the defaults are not good, let's fix it. If you think there's any confusion to the end user on where things are being put, it would make sense to have a verbose message.

@adityapatwardhan
Copy link
Copy Markdown
Member


# Build PowerShell. You may need to add other flags here like
# -ResGen, -Restore or -PSModuleRestore
PS> Start-PSBuild -Configuration CodeCoverage
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would add -PSModuleRestore in the example instead of comment, since it will be required for some tests. Maybe even -Clean.

Microsoft.WSMan.Runtime 80.95 80.33
Microsoft.PowerShell.Commands.Diagnostics 0 0
```

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Compare-CodeCoverage and Compare-FileCoverage might be worth mentioning for comparing two runs.


# Finally, open the report in your browser
Invoke-Item C:\temp\Coverage\index.htm
```
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also mention Format-FileCoverage for a console visualizer as an alternative.

Copy link
Copy Markdown
Collaborator Author

@rjmholt rjmholt Apr 5, 2018

Choose a reason for hiding this comment

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

I tried using Format-FileCoverage, but I'm not sure it does what the help message states. I read through the implementation and it currently only seems to compare two codebases (as in, I couldn't get it to just show me one codebase's test coverage, because the oldBase and newBase parameters not being provided cause the command to fail). I'll do some work on updating that and the other stuff that @SteveL-MSFT mentioned in another PR, and then update the documentation with it.

@rjmholt
Copy link
Copy Markdown
Collaborator Author

rjmholt commented Apr 4, 2018

@adityapatwardhan Happy to add the extra documentation on the other coverage functions. My documentation is based on this blog post. Is there any documentation I can work off with respect to the other commands you mentioned?

Also, I noticed that mdspell is locale-specific - so we should perhaps add the --en-us option to our instructions? Actually, mdspell defaults to en-GB and is planning to change in the next major version. But there is still option support for --en-us.

@rjmholt
Copy link
Copy Markdown
Collaborator Author

rjmholt commented Apr 4, 2018

mdspell found a few other spelling warnings:

> mdspell.cmd "**/*.md" --ignore-numbers --ignore-acronyms -r
    docs/git/basics.md
       37 | references to our favorites.
       49 | #### Katacoda
       52 | [Git lessons on katacoda](https://www.katacoda.com/cou
       54 | #### Githug
       56 | [Githug](https://github.com/Gazler/gi

    docs/git/README.md
       30 | * Use lowercase-with-dashes for naming.

    docs/learning-powershell/powershell-beginners-guide.md
       44 | interested in the instance of firefox process that are running on y
      134 | ld!" as a file content to the test.txt.
      135 | Because the test.txt file exists already, we use *
      173 | **8. del - Remove-Item**: Deletes the
      175 | ill delete the file /home/jen/test.txt:
      180 | **9. $PSVersionTable**: Displays the version of Po
      182 | Type **$PSVersionTable** in your PowerShell session,
      271 | th PowerShell][remoting] from Channel9
      272 | - [eBooks from PowerShell.org](https://
      272 | - [eBooks from PowerShell.org](https://powershell.org/ebook
      273 | - [eBooks from PowerShell.com][ebooks-p
      273 | - [eBooks from PowerShell.com][ebooks-powershell.com]
      274 | - [eBooks List][ebook-list] by Martin S
      274 | s List][ebook-list] by Martin Schvartzman
      279 | ting in Depth][in-depth] from Channel9
      280 | Management][remote-mgmt] from ITPro

    docs/learning-powershell/README.md
       91 | | pwd                 |pwd, $pwd, G
       91 | | pwd                 |pwd, $pwd, Get-Location      |Sho
       91 | | pwd                 |pwd, $pwd, Get-Location      |Show work
       92 | | clear, Ctrl+L, reset| cls, clear                  |Clea
       93 | | mkdir               |New-Item -Item
       93 | kdir               |New-Item -ItemType Directory |Create a new folde
       94 | | touch test.txt      |New-Item -Path test.txt
       94 | test.txt      |New-Item -Path test.txt      |Create a new empty file
       95 | | cat test1.txt test2.txt         |Get-Conten
       95 | | cat test1.txt test2.txt         |Get-Content test1.tx
       95 | est2.txt         |Get-Content test1.txt, test2.txt       |Display fil
       95 |       |Get-Content test1.txt, test2.txt       |Display files contents
       96 | | cp ./source.txt ./dest/dest.txt |Copy-Item so
       96 | | cp ./source.txt ./dest/dest.txt |Copy-Item source.tx
       96 | | cp ./source.txt ./dest/dest.txt |Copy-Item source.txt dest/de
       96 | xt ./dest/dest.txt |Copy-Item source.txt dest/dest.txt     |Copy a fil
       96 | est.txt |Copy-Item source.txt dest/dest.txt     |Copy a file
       96 | xt |Copy-Item source.txt dest/dest.txt     |Copy a file
       97 | | cp -r ./source ./dest           |Copy-Item ./source
       97 |         |Copy-Item ./source ./dest -Recurse     |Recursively cop
       98 | | mv ./source.txt ./dest/dest.txt
       98 | | mv ./source.txt ./dest/dest.txt |Move-Item ./
       98 | | mv ./source.txt ./dest/dest.txt |Move-Item ./source.
       98 | | mv ./source.txt ./dest/dest.txt |Move-Item ./source.txt ./des
       98 |  ./dest/dest.txt |Move-Item ./source.txt ./dest/dest.txt |Move a file
       98 | txt |Move-Item ./source.txt ./dest/dest.txt |Move a file to othe
       98 | Move-Item ./source.txt ./dest/dest.txt |Move a file to other folder
       99 | | rm test.txt
       99 | | rm test.txt                     |Remove-I
       99 |                  |Remove-Item test.txt                   |Delete a f
      100 | | rm -r <folderName>
      100 | | rm -r <folderName>           |Remove-Item <f
      100 | e>           |Remove-Item <folderName> -Recurse   |Delete a folder
      102 | | grep -Rin "sometext" --include="*.
      102 | | grep -Rin "sometext" --include="*.cs" |
      102 | | grep -Rin "sometext" --include="*.cs" |Get-ChildI
      102 | r> \| Select-String -Pattern "sometext" | Recursively case-insensiti

    docs/learning-powershell/working-with-powershell-objects.md
       64 | To confirm, we can call the GetType() method interactively from t

>> 60 spelling errors found in 68 files

I'll leave those for another PR unless it's preferred I fix them here.

@adityapatwardhan
Copy link
Copy Markdown
Member

@rjmholt Just fix the errors introduced in this change.
The for en-GB to en-US should be done in a separate PR.
For more information about other OpenCover cmdlets, Get-Help <cmdletname> -Full

@adityapatwardhan adityapatwardhan merged commit 5cc27e2 into PowerShell:master May 4, 2018
@rjmholt rjmholt deleted the code-coverage-doc branch June 22, 2018 22:55
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.

5 participants