From 4ea113f9533b4687fc4a4f45c9c645772c81aaed Mon Sep 17 00:00:00 2001 From: Wojciech Sciesinski Date: Tue, 28 Mar 2017 00:02:33 +0100 Subject: [PATCH 1/2] Style rules corrected, trailing spaces removed, tabs replaces by spaces --- Pester.Tests.ps1 | 26 ++++- Pester.psm1 | 235 +++++++++++++++++++++--------------------- chocolateyInstall.ps1 | 2 +- 3 files changed, 142 insertions(+), 121 deletions(-) diff --git a/Pester.Tests.ps1 b/Pester.Tests.ps1 index 0626dc3f8..429dda1e9 100644 --- a/Pester.Tests.ps1 +++ b/Pester.Tests.ps1 @@ -119,8 +119,8 @@ Describe 'Style rules' { $pesterRoot = (Get-Module Pester).ModuleBase $files = @( - Get-ChildItem $pesterRoot -Include *.ps1,*.psm1 - Get-ChildItem $pesterRoot\Functions -Include *.ps1,*.psm1 -Recurse + Get-ChildItem $pesterRoot\* -Include *.ps1,*.psm1 + Get-ChildItem (Join-Path $pesterRoot 'Functions') -Include *.ps1,*.psm1 -Recurse ) It 'Pester source files contain no trailing whitespace' { @@ -146,6 +146,28 @@ Describe 'Style rules' { } } + It 'Pester source files lines start with a tab character' { + $badLines = @( + foreach ($file in $files) + { + $lines = [System.IO.File]::ReadAllLines($file.FullName) + $lineCount = $lines.Count + + for ($i = 0; $i -lt $lineCount; $i++) + { + if ($lines[$i] -match '^[ ]*\t|^\t|^\t[ ]*') { + 'File: {0}, Line: {1}' -f $file.FullName, ($i + 1) + } + } + } + ) + + if ($badLines.Count -gt 0) + { + throw "The following $($badLines.Count) lines start with a tab character: `r`n`r`n$($badLines -join "`r`n")" + } + } + It 'Pester Source Files all end with a newline' { $badFiles = @( foreach ($file in $files) diff --git a/Pester.psm1 b/Pester.psm1 index df1a96612..691db35e2 100644 --- a/Pester.psm1 +++ b/Pester.psm1 @@ -83,7 +83,7 @@ $script:SafeCommands = @{ # Not all platforms have Get-WmiObject (Nano) # Get-CimInstance is prefered, but we can use Get-WmiObject if it exists # Moreover, it shouldn't really be fatal if neither of those cmdlets -# exist +# exist if ( Get-Command -ea SilentlyContinue Get-CimInstance ) { $script:SafeCommands['Get-CimInstance'] = Get-Command -Name Get-CimInstance -Module CimCmdlets @safeCommandLookupParameters @@ -120,9 +120,9 @@ using System; namespace Pester { - [Flags] - public enum OutputTypes - { + [Flags] + public enum OutputTypes + { None = 0, Default = 1, Passed = 2, @@ -135,7 +135,7 @@ namespace Pester Summary = 256, All = Default | Passed | Failed | Pending | Skipped | Inconclusive | Describe | Context | Summary, Fails = Default | Failed | Pending | Skipped | Inconclusive | Describe | Context | Summary - } + } } "@ @@ -150,7 +150,7 @@ function Has-Flag { $Value ) - 0 -ne ($Setting -band $Value) + 0 -ne ($Setting -band $Value) } function Invoke-Pester { @@ -159,80 +159,79 @@ function Invoke-Pester { Runs Pester tests .DESCRIPTION -The Invoke-Pester function runs Pester tests, including *.Tests.ps1 files and +The Invoke-Pester function runs Pester tests, including *.Tests.ps1 files and Pester tests in PowerShell scripts. -You can run scripts that include Pester tests just as you would any other -Windows PowerShell script, including typing the full path at the command line -and running in a script editing program. Typically, you use Invoke-Pester to run -all Pester tests in a directory, or to use its many helpful parameters, +You can run scripts that include Pester tests just as you would any other +Windows PowerShell script, including typing the full path at the command line +and running in a script editing program. Typically, you use Invoke-Pester to run +all Pester tests in a directory, or to use its many helpful parameters, including parameters that generate custom objects or XML files. -By default, Invoke-Pester runs all *.Tests.ps1 files in the current directory -and all subdirectories recursively. You can use its parameters to select tests -by file name, test name, or tag. +By default, Invoke-Pester runs all *.Tests.ps1 files in the current directory +and all subdirectories recursively. You can use its parameters to select tests +by file name, test name, or tag. -To run Pester tests in scripts that take parameter values, use the Script -parameter with a hash table value. +To run Pester tests in scripts that take parameter values, use the Script +parameter with a hash table value. -Also, by default, Pester tests write test results to the console host, much like -Write-Host does, but you can use the Quiet parameter to suppress the host -messages, use the PassThru parameter to generate a custom object -(PSCustomObject) that contains the test results, use the OutputXml and -OutputFormat parameters to write the test results to an XML file, and use the -EnableExit parameter to return an exit code that contains the number of failed -tests. +Also, by default, Pester tests write test results to the console host, much like +Write-Host does, but you can use the Quiet parameter to suppress the host +messages, use the PassThru parameter to generate a custom object +(PSCustomObject) that contains the test results, use the OutputXml and +OutputFormat parameters to write the test results to an XML file, and use the +EnableExit parameter to return an exit code that contains the number of failed +tests. -You can also use the Strict parameter to fail all pending and skipped tests. -This feature is ideal for build systems and other processes that require success -on every test. +You can also use the Strict parameter to fail all pending and skipped tests. +This feature is ideal for build systems and other processes that require success +on every test. -To help with test design, Invoke-Pester includes a CodeCoverage parameter that -lists commands, functions, and lines of code that did not run during test +To help with test design, Invoke-Pester includes a CodeCoverage parameter that +lists commands, functions, and lines of code that did not run during test execution and returns the code that ran as a percentage of all tested code. -Invoke-Pester, and the Pester module that exports it, are products of an -open-source project hosted on GitHub. To view, comment, or contribute to the +Invoke-Pester, and the Pester module that exports it, are products of an +open-source project hosted on GitHub. To view, comment, or contribute to the repository, see https://github.com/Pester. .PARAMETER Script -Specifies the test files that Pester runs. You can also use the Script parameter -to pass parameter names and values to a script that contains Pester tests. The -value of the Script parameter can be a string, a hash table, or a collection +Specifies the test files that Pester runs. You can also use the Script parameter +to pass parameter names and values to a script that contains Pester tests. The +value of the Script parameter can be a string, a hash table, or a collection of hash tables and strings. Wildcard characters are supported. -The Script parameter is optional. If you omit it, Invoke-Pester runs all -*.Tests.ps1 files in the local directory and its subdirectories recursively. - +The Script parameter is optional. If you omit it, Invoke-Pester runs all +*.Tests.ps1 files in the local directory and its subdirectories recursively. + To run tests in other files, such as .ps1 files, enter the path and file name of the file. (The file name is required. Name patterns that end in "*.ps1" run only -*.Tests.ps1 files.) +*.Tests.ps1 files.) -To run a Pester test with parameter names and/or values, use a hash table as the +To run a Pester test with parameter names and/or values, use a hash table as the value of the script parameter. The keys in the hash table are: --- Path [string] (required): Specifies a test to run. The value is a path\file - name or name pattern. Wildcards are permitted. All hash tables in a Script - parameter value must have a Path key. - --- Parameters [hashtable]: Runs the script with the specified parameters. The - value is a nested hash table with parameter name and value pairs, such as - @{UserName = 'User01'; Id = '28'}. - --- Arguments [array]: An array or comma-separated list of parameter values - without names, such as 'User01', 28. Use this key to pass values to positional +-- Path [string] (required): Specifies a test to run. The value is a path\file + name or name pattern. Wildcards are permitted. All hash tables in a Script + parameter value must have a Path key. + +-- Parameters [hashtable]: Runs the script with the specified parameters. The + value is a nested hash table with parameter name and value pairs, such as + @{UserName = 'User01'; Id = '28'}. + +-- Arguments [array]: An array or comma-separated list of parameter values + without names, such as 'User01', 28. Use this key to pass values to positional parameters. - .PARAMETER TestName Runs only tests in Describe blocks that have the specified name or name pattern. -Wildcard characters are supported. - -If you specify multiple TestName values, Invoke-Pester runs tests that have any +Wildcard characters are supported. + +If you specify multiple TestName values, Invoke-Pester runs tests that have any of the values in the Describe name (it ORs the TestName values). - + .PARAMETER EnableExit -Will cause Invoke-Pester to exit with a exit code equal to the number of failed +Will cause Invoke-Pester to exit with a exit code equal to the number of failed tests once all tests have been run. Use this to "fail" a build when any tests fail. .PARAMETER OutputFile @@ -244,7 +243,7 @@ If this path is not provided, no log will be generated. .PARAMETER OutputFormat The format of output. Two formats of output are supported: NUnitXML and LegacyNUnitXML. - + .PARAMETER OutputXml The parameter OutputXml is deprecated, please use OutputFile and OutputFormat instead. @@ -255,85 +254,85 @@ the xml extension. If this path is not provided, no log will be generated. .PARAMETER Tag -Runs only tests in Describe blocks with the specified Tag parameter values. -Wildcard characters and Tag values that include spaces or whitespace characters +Runs only tests in Describe blocks with the specified Tag parameter values. +Wildcard characters and Tag values that include spaces or whitespace characters are not supported. - -When you specify multiple Tag values, Invoke-Pester runs tests that have any -of the listed tags (it ORs the tags). However, when you specify TestName -and Tag values, Invoke-Pester runs only describe blocks that have one of the + +When you specify multiple Tag values, Invoke-Pester runs tests that have any +of the listed tags (it ORs the tags). However, when you specify TestName +and Tag values, Invoke-Pester runs only describe blocks that have one of the specified TestName values and one of the specified Tag values. If you use both Tag and ExcludeTag, ExcludeTag takes precedence. - + .PARAMETER ExcludeTag Omits tests in Describe blocks with the specified Tag parameter values. Wildcard -characters and Tag values that include spaces or whitespace characters are not +characters and Tag values that include spaces or whitespace characters are not supported. - + When you specify multiple ExcludeTag values, Invoke-Pester omits tests that have -any of the listed tags (it ORs the tags). However, when you specify TestName -and ExcludeTag values, Invoke-Pester omits only describe blocks that have one +any of the listed tags (it ORs the tags). However, when you specify TestName +and ExcludeTag values, Invoke-Pester omits only describe blocks that have one of the specified TestName values and one of the specified Tag values. If you use both Tag and ExcludeTag, ExcludeTag takes precedence .PARAMETER PassThru -Returns a custom object (PSCustomObject) that contains the test results. - -By default, Invoke-Pester writes to the host program, not to the output stream (stdout). -If you try to save the result in a variable, the variable is empty unless you +Returns a custom object (PSCustomObject) that contains the test results. + +By default, Invoke-Pester writes to the host program, not to the output stream (stdout). +If you try to save the result in a variable, the variable is empty unless you use the PassThru parameter. - + To suppress the host output, use the Quiet parameter. .PARAMETER CodeCoverage Adds a code coverage report to the Pester tests. Takes strings or hash table values. - -A code coverage report lists the lines of code that did and did not run during -a Pester test. This report does not tell whether code was tested; only whether + +A code coverage report lists the lines of code that did and did not run during +a Pester test. This report does not tell whether code was tested; only whether the code ran during the test. -By default, the code coverage report is written to the host program -(like Write-Host). When you use the PassThru parameter, the custom object -that Invoke-Pester returns has an additional CodeCoverage property that contains -a custom object with detailed results of the code coverage test, including lines +By default, the code coverage report is written to the host program +(like Write-Host). When you use the PassThru parameter, the custom object +that Invoke-Pester returns has an additional CodeCoverage property that contains +a custom object with detailed results of the code coverage test, including lines hit, lines missed, and helpful statistics. - -However, NUnitXML and LegacyNUnitXML output (OutputXML, OutputFormat) do not include + +However, NUnitXML and LegacyNUnitXML output (OutputXML, OutputFormat) do not include any code coverage information, because it's not supported by the schema. - -Enter the path to the files of code under test (not the test file). -Wildcard characters are supported. If you omit the path, the default is local + +Enter the path to the files of code under test (not the test file). +Wildcard characters are supported. If you omit the path, the default is local directory, not the directory specified by the Script parameter. -To run a code coverage test only on selected functions or lines in a script, +To run a code coverage test only on selected functions or lines in a script, enter a hash table value with the following keys: - + -- Path (P)(mandatory) . Enter one path to the files. Wildcard characters are supported, but only one string is permitted. One of the following: Function or StartLine/EndLine - --- Function (F) : Enter the function name. Wildcard characters are + +-- Function (F) : Enter the function name. Wildcard characters are supported, but only one string is permitted. - + -or- - --- StartLine (S): Performs code coverage analysis beginning with the specified + +-- StartLine (S): Performs code coverage analysis beginning with the specified line. Default is line 1. --- EndLine (E): Performs code coverage analysis ending with the specified line. +-- EndLine (E): Performs code coverage analysis ending with the specified line. Default is the last line of the script. - + .PARAMETER Strict -Makes Pending and Skipped tests to Failed tests. Useful for continuous +Makes Pending and Skipped tests to Failed tests. Useful for continuous integration where you need to make sure all tests passed. .PARAMETER Quiet -Suppresses the output that Pester writes to the host program, including the +Suppresses the output that Pester writes to the host program, including the result summary and CodeCoverage output. - + This parameter does not affect the PassThru custom object or the XML output that is written when you use the Output parameters. @@ -341,7 +340,7 @@ is written when you use the Output parameters. Customizes the output Pester writes to the screen. Available options are None, Default, Passed, Failed, Pending, Skipped, Inconclusive, Describe, Context, Summary, Header, All, Fails. -The options can be combined to define presets. +The options can be combined to define presets. Common use cases are: None - to write no output to the screen. @@ -349,13 +348,13 @@ All - to write all available information (this is default option). Fails - to write everything except Passed (but including Describes etc.). A common setting is also Failed, Summary, to write only failed tests and test summary. - + This parameter does not affect the PassThru custom object or the XML output that is written when you use the Output parameters. .PARAMETER PesterOption -Sets advanced options for the test execution. Enter a PesterOption object, +Sets advanced options for the test execution. Enter a PesterOption object, such as one that you create by using the New-PesterOption cmdlet, or a hash table in which the keys are option names and the values are option values. For more information on the options available, see the help for New-PesterOption. @@ -370,19 +369,19 @@ Invoke-Pester -Script .\Util* This commands runs all *.Tests.ps1 files in subdirectories with names that begin with 'Util' and their subdirectories. - + .Example Invoke-Pester -Script D:\MyModule, @{ Path = '.\Tests\Utility\ModuleUnit.Tests.ps1'; Parameters = @{ Name = 'User01' }; Arguments = srvNano16 } -This command runs all *.Tests.ps1 files in D:\MyModule and its subdirectories. +This command runs all *.Tests.ps1 files in D:\MyModule and its subdirectories. It also runs the tests in the ModuleUnit.Tests.ps1 file using the following -parameters: .\Tests\Utility\ModuleUnit.Tests.ps1 srvNano16 -Name User01 +parameters: .\Tests\Utility\ModuleUnit.Tests.ps1 srvNano16 -Name User01 .Example Invoke-Pester -TestName "Add Numbers" This command runs only the tests in the Describe block named "Add Numbers". - + .EXAMPLE $results = Invoke-Pester -Script D:\MyModule -PassThru -Quiet $failed = $results.TestResult | where Result -eq 'Failed' @@ -393,7 +392,7 @@ help for Force parameter in Compress-Archive has wrong Mandatory value help for Compress-Archive has wrong parameter type for Force help for Update parameter in Compress-Archive has wrong Mandatory value help for DestinationPath parameter in Expand-Archive has wrong Mandatory value - + $failed[0] Describe : Test help for Compress-Archive in Microsoft.PowerShell.Archive (1.0.0.0) Context : Test parameter help for Compress-Archive @@ -407,17 +406,17 @@ StackTrace : at line: 279 in C:\GitHub\PesterTdd\Module.Help.Tests.p ErrorRecord : Expected: value to not be empty ParameterizedSuiteName : Parameters : {} - -This examples uses the PassThru parameter to return a custom object with the -Pester test results. By default, Invoke-Pester writes to the host program, but not + +This examples uses the PassThru parameter to return a custom object with the +Pester test results. By default, Invoke-Pester writes to the host program, but not to the output stream. It also uses the Quiet parameter to suppress the host output. - + The first command runs Invoke-Pester with the PassThru and Quiet parameters and saves the PassThru output in the $results variable. - + The second command gets only failing results and saves them in the $failed variable. - -The third command gets the names of the failing results. The result name is the + +The third command gets the names of the failing results. The result name is the name of the It block that contains the test. The fourth command uses an array index to get the first failing result. The @@ -428,32 +427,32 @@ useful values, including a stack tace. Invoke-Pester -EnableExit -OutputFile ".\artifacts\TestResults.xml" -OutputFormat NUnitXml This command runs all tests in the current directory and its subdirectories. It -writes the results to the TestResults.xml file using the NUnitXml schema. The +writes the results to the TestResults.xml file using the NUnitXml schema. The test returns an exit code equal to the number of test failures. .EXAMPLE Invoke-Pester -CodeCoverage 'ScriptUnderTest.ps1' -Runs all *.Tests.ps1 scripts in the current directory, and generates a coverage +Runs all *.Tests.ps1 scripts in the current directory, and generates a coverage report for all commands in the "ScriptUnderTest.ps1" file. .EXAMPLE Invoke-Pester -CodeCoverage @{ Path = 'ScriptUnderTest.ps1'; Function = 'FunctionUnderTest' } -Runs all *.Tests.ps1 scripts in the current directory, and generates a coverage +Runs all *.Tests.ps1 scripts in the current directory, and generates a coverage report for all commands in the "FunctionUnderTest" function in the "ScriptUnderTest.ps1" file. .EXAMPLE Invoke-Pester -CodeCoverage @{ Path = 'ScriptUnderTest.ps1'; StartLine = 10; EndLine = 20 } -Runs all *.Tests.ps1 scripts in the current directory, and generates a coverage +Runs all *.Tests.ps1 scripts in the current directory, and generates a coverage report for all commands on lines 10 through 20 in the "ScriptUnderTest.ps1" file. .EXAMPLE Invoke-Pester -Script C:\Tests -Tag UnitTest, Newest -ExcludeTag Bug - + This command runs *.Tests.ps1 files in C:\Tests and its subdirectories. In those files, it runs only tests that have UnitTest or Newest tags, unless the test also has a Bug tag. - + .LINK https://github.com/pester/Pester/wiki/Invoke-Pester Describe @@ -517,7 +516,7 @@ New-PesterOption if ($Quiet) { - $Show = [Pester.OutputTypes]::None + $Show = [Pester.OutputTypes]::None } $pester = New-PesterState -TestNameFilter $TestName -TagFilter ($Tag -split "\s") -ExcludeTagFilter ($ExcludeTag -split "\s") -SessionState $PSCmdlet.SessionState -Strict:$Strict -Show:$Show -PesterOption $PesterOption @@ -754,7 +753,7 @@ function Get-OperatingSystem if ($PSVersionTable.PSVersion.Major -lt 6 -or $IsWindows) { 'Windows' - } + } elseif ($IsOSX) { 'OSX' diff --git a/chocolateyInstall.ps1 b/chocolateyInstall.ps1 index 8dfefb33a..462e12e29 100644 --- a/chocolateyInstall.ps1 +++ b/chocolateyInstall.ps1 @@ -16,7 +16,7 @@ end } Update-Directory -Source $sourceDirectory -Destination $targetDirectory - + $binPath = Join-Path -Path $targetDirectory -ChildPath bin Install-ChocolateyPath $binPath From 1a864cc82895b8d8bc02ccbb5855e7ff4af24702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Wed, 29 Mar 2017 18:02:00 +0200 Subject: [PATCH 2/2] Rename indentation test --- Pester.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pester.Tests.ps1 b/Pester.Tests.ps1 index 429dda1e9..6b5b36a14 100644 --- a/Pester.Tests.ps1 +++ b/Pester.Tests.ps1 @@ -146,7 +146,7 @@ Describe 'Style rules' { } } - It 'Pester source files lines start with a tab character' { + It 'Spaces are used for indentation in all code files, not tabs' { $badLines = @( foreach ($file in $files) {