-
Notifications
You must be signed in to change notification settings - Fork 100
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
(GH-148) Updates Tests to be compatible with Pester 5 #163
Conversation
4a11eec
to
0317618
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes look good to me. I have taken them for a test in the Vagrant file I've been finessing for overall testing of cChoco. One caveat is that you either need to install cChoco
from the PowerShell Gallery, or you need to put it on the PSModule Path. Otherwise you don't end up with all of the tests running.
Pester considers all files named *.Tests.ps1 to be test files. This is the default naming convention. This commit adjusts cChoco test filenames to match this. See: https://pester-docs.netlify.app/docs/usage/file-placement-and-naming#common-convention
There were some compatibility issues when running the existing tests in Pester 5, and it was noted that it would be good to update them. This commit does so. Tested using Pester 5.3.1.
The comment was possible copy-pasted from another file.
The following issues were raised with the updated tests, and needed to be fixed before builds could succeed. Tests which were mistaken have been overridden at the lowest scope possible. Unused parameters have, in general, been ignored rather than removed to make the smallest amount of change to the actual running code for this. ``` [-] Testing cChoco DSC Resources against PSScriptAnalyzer rule-set.Testing cChocoFeature against default PSScriptAnalyzer rules.Passes 'PSAvoidTrailingWhitespace' 2ms (2ms|0ms) Exception: cChocoFeature.psm1:80 Line has trailing whitespace cChocoFeature.psm1:134 Line has trailing whitespace at <ScriptBlock>, C:\tests\Tests\cChoco_ScriptAnalyzer.Tests.ps1:42 at <ScriptBlock>, C:\tests\Tests\cChoco_ScriptAnalyzer.Tests.ps1:41 [-] Testing cChoco DSC Resources against PSScriptAnalyzer rule-set.Testing cChocoFeature against default PSScriptAnalyzer rules.Passes 'PSReviewUnusedParameter' 2ms (1ms|0ms) Exception: cChocoFeature.psm1:139 The parameter 'FeatureName' has been declared but not used. at <ScriptBlock>, C:\tests\Tests\cChoco_ScriptAnalyzer.Tests.ps1:42 at <ScriptBlock>, C:\tests\Tests\cChoco_ScriptAnalyzer.Tests.ps1:41 [-] Testing cChoco DSC Resources against PSScriptAnalyzer rule-set.Testing cChocoInstaller against default PSScriptAnalyzer rules.Passes 'PSAvoidGlobalFunctions' 2ms (1ms|0ms) Exception: cChocoInstaller.psm1:125 Avoid creating functions with a Global scope. at <ScriptBlock>, C:\tests\Tests\cChoco_ScriptAnalyzer.Tests.ps1:42 at <ScriptBlock>, C:\tests\Tests\cChoco_ScriptAnalyzer.Tests.ps1:41 [-] Testing cChoco DSC Resources against PSScriptAnalyzer rule-set.Testing cChocoInstaller against default PSScriptAnalyzer rules.Passes 'PSReviewUnusedParameter' 2ms (1ms|0ms) Exception: cChocoInstaller.psm1:24 The parameter 'InstallDir' has been declared but not used. cChocoInstaller.psm1:72 The parameter 'ChocoInstallScriptUrl' has been declared but not used. cChocoInstaller.psm1:131 The parameter 'NoNewLine' has been declared but not used. cChocoInstaller.psm1:133 The parameter 'ForegroundColor' has been declared but not used. cChocoInstaller.psm1:135 The parameter 'BackgroundColor' has been declared but not used. at <ScriptBlock>, C:\tests\Tests\cChoco_ScriptAnalyzer.Tests.ps1:42 at <ScriptBlock>, C:\tests\Tests\cChoco_ScriptAnalyzer.Tests.ps1:41 [-] Testing cChoco DSC Resources against PSScriptAnalyzer rule-set.Testing cChocoPackageInstall against default PSScriptAnalyzer rules.Passes 'PSAvoidGlobalFunctions' 2ms (1ms|0ms) Exception: cChocoPackageInstall.psm1:427 Avoid creating functions with a Global scope. at <ScriptBlock>, C:\tests\Tests\cChoco_ScriptAnalyzer.Tests.ps1:42 at <ScriptBlock>, C:\tests\Tests\cChoco_ScriptAnalyzer.Tests.ps1:41 [-] Testing cChoco DSC Resources against PSScriptAnalyzer rule-set.Testing cChocoPackageInstall against default PSScriptAnalyzer rules.Passes 'PSAvoidTrailingWhitespace' 2ms (1ms|0ms) Exception: cChocoPackageInstall.psm1:286 Line has trailing whitespace cChocoPackageInstall.psm1:322 Line has trailing whitespace cChocoPackageInstall.psm1:347 Line has trailing whitespace cChocoPackageInstall.psm1:350 Line has trailing whitespace cChocoPackageInstall.psm1:473 Line has trailing whitespace at <ScriptBlock>, C:\tests\Tests\cChoco_ScriptAnalyzer.Tests.ps1:42 at <ScriptBlock>, C:\tests\Tests\cChoco_ScriptAnalyzer.Tests.ps1:41 [-] Testing cChoco DSC Resources against PSScriptAnalyzer rule-set.Testing cChocoPackageInstall against default PSScriptAnalyzer rules.Passes 'PSReviewUnusedParameter' 2ms (1ms|0ms) Exception: cChocoPackageInstall.psm1:33 The parameter 'MinimumVersion' has been declared but not used. cChocoPackageInstall.psm1:434 The parameter 'NoNewLine' has been declared but not used. cChocoPackageInstall.psm1:436 The parameter 'ForegroundColor' has been declared but not used. cChocoPackageInstall.psm1:438 The parameter 'BackgroundColor' has been declared but not used. cChocoPackageInstall.psm1:528 The parameter 'NoCache' has been declared but not used. at <ScriptBlock>, C:\tests\Tests\cChoco_ScriptAnalyzer.Tests.ps1:42 at <ScriptBlock>, C:\tests\Tests\cChoco_ScriptAnalyzer.Tests.ps1:41 ```
The xDscResources test for admin before running. This ensures Pester doesn't waste time on the file when running it in non-elevated sessions.
Tests were failing on systems without Chocolatey installed. This was embarrassing due to the test for Chocolatey not being installed being the bit that failed. This should fix that.
AppVeyor's version was behind. This has been tested.
Thanks for getting this added @JPRuskin 👍 |
Description
These commits update the tests to be compatible with Pester 5, as well as making some small maintenance commits to correct issues raised by the tests, e.g. trailing whitespace, unused parameters, etc. No tests were removed.
Related Issue
Fixes #148
Motivation and Context
This will allow us to use Pester 5 going forward. It should also result in easier addressing of PSSA test failures, as the new method (thanks to Jaykul) gives explicit failures with filepaths and line numbers.
How Has This Been Tested?
Primary testing was performed on a clean docker image running
mcr.microsoft.com/powershell
, without Chocolatey installed.Tests were also run on a Windows 10 machine, with Chocolatey installed, and are passing on AppVeyor.
The tests now work with Chocolatey there or not, and other than the xDscResource tests work without elevation. This should be great for future users wanting to add to the tests!
Screenshots (if appropriate):
Example of old PSSA failures, taken from another PR:
Example of new PSSA failures:
Types of changes
Checklist:
Notes: There's no current contributing document that I can see. I also think I've changed the code style of the tests a little, but I think this is in the spirit of the issue.