From a6f3855e4352b51165dc73871caa827c2dffca99 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Fri, 12 Jul 2024 14:54:35 +0200 Subject: [PATCH] chore: Cleanup code (#6) --- CHANGELOG.md | 5 ++ README.md | 2 +- source/PesterConverter.psd1 | 2 +- source/Private/Convert-ShouldBe.ps1 | 6 +-- source/Private/Convert-ShouldBeExactly.ps1 | 6 +-- source/Private/Convert-ShouldBeFalse.ps1 | 6 +-- .../Private/Convert-ShouldBeNullOrEmpty.ps1 | 6 +-- source/Private/Convert-ShouldBeOfType.ps1 | 6 +-- source/Private/Convert-ShouldBeTrue.ps1 | 6 +-- source/Private/Convert-ShouldContain.ps1 | 6 +-- source/Private/Convert-ShouldMatch.ps1 | 6 +-- source/Private/Convert-ShouldMatchExactly.ps1 | 6 +-- source/Private/Convert-ShouldNotThrow.ps1 | 6 +-- source/Private/Convert-ShouldThrow.ps1 | 6 +-- source/Private/Get-AstDefinition.ps1 | 10 +--- source/Private/Get-CommandAst.ps1 | 4 +- source/Private/Get-PesterCommandParameter.ps1 | 12 ++--- .../Get-PesterCommandSyntaxVersion.ps1 | 2 +- source/Private/Get-ShouldThrowScriptBlock.ps1 | 5 -- source/Public/Convert-PesterSyntax.ps1 | 50 +++++++++++++------ source/WikiSource/Pester_v5_Conversion.md | 2 +- source/en-US/PesterConverter.strings.psd1 | 30 +++++++++++ source/en-US/about_PesterConverter.help.txt | 4 +- 23 files changed, 117 insertions(+), 77 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c221a1d..80b1bf4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,3 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Public commands: - `Convert-PesterSyntax` + +### Fixed + +- Improve code to resolve ScriptAnalyzer warnings and errors. +- Localize all the strings. diff --git a/README.md b/README.md index 66a8934..e8ae0cf 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # PesterConverter -Commands ta convert Pester tests. +Commands to convert Pester tests. [![Build Status](https://dev.azure.com/viscalyx/PesterConverter/_apis/build/status/viscalyx.PesterConverter?branchName=main)](https://dev.azure.com/viscalyx/PesterConverter/_build/latest?definitionId=33&branchName=main) ![Azure DevOps coverage (branch)](https://img.shields.io/azure-devops/coverage/viscalyx/PesterConverter/33/main) diff --git a/source/PesterConverter.psd1 b/source/PesterConverter.psd1 index b056f1f..3dbfb9d 100644 --- a/source/PesterConverter.psd1 +++ b/source/PesterConverter.psd1 @@ -18,7 +18,7 @@ Copyright = 'Copyright the PesterConverter contributors. All rights reserved.' # Description of the functionality provided by this module - Description = 'Commands ta convert Pester tests.' + Description = 'Commands to convert Pester tests.' # Minimum version of the PowerShell engine required by this module PowerShellVersion = '7.0' diff --git a/source/Private/Convert-ShouldBe.ps1 b/source/Private/Convert-ShouldBe.ps1 index 7ce2cbd..df649fb 100644 --- a/source/Private/Convert-ShouldBe.ps1 +++ b/source/Private/Convert-ShouldBe.ps1 @@ -73,7 +73,7 @@ function Convert-ShouldBe Assert-BoundParameter @assertBoundParameterParameters - Write-Debug -Message ('Parsing the command AST: {0}' -f $CommandAst.Extent.Text) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ParsingCommandAst -f $CommandAst.Extent.Text) # Determine if the command is negated $isNegated = Test-PesterCommandNegated -CommandAst $CommandAst @@ -83,7 +83,7 @@ function Convert-ShouldBe # Parse the command elements and convert them to Pester 6 syntax if ($PSCmdlet.ParameterSetName -eq 'Pester6') { - Write-Debug -Message ('Converting from Pester v{0} to Pester v6 syntax.' -f $sourceSyntaxVersion) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertingFromTo -f $sourceSyntaxVersion, '6') # Add the correct Pester command based on negation if ($isNegated) @@ -196,7 +196,7 @@ function Convert-ShouldBe } } - Write-Debug -Message ('Converted the command `{0}` to `{1}`.' -f $CommandAst.Extent.Text, $newExtentText) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertedCommand -f $CommandAst.Extent.Text, $newExtentText) return $newExtentText } diff --git a/source/Private/Convert-ShouldBeExactly.ps1 b/source/Private/Convert-ShouldBeExactly.ps1 index d7871c8..eb7f241 100644 --- a/source/Private/Convert-ShouldBeExactly.ps1 +++ b/source/Private/Convert-ShouldBeExactly.ps1 @@ -73,7 +73,7 @@ function Convert-ShouldBeExactly Assert-BoundParameter @assertBoundParameterParameters - Write-Debug -Message ('Parsing the command AST: {0}' -f $CommandAst.Extent.Text) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ParsingCommandAst -f $CommandAst.Extent.Text) # Determine if the command is negated $isNegated = Test-PesterCommandNegated -CommandAst $CommandAst @@ -83,7 +83,7 @@ function Convert-ShouldBeExactly # Parse the command elements and convert them to Pester 6 syntax if ($PSCmdlet.ParameterSetName -eq 'Pester6') { - Write-Debug -Message ('Converting from Pester v{0} to Pester v6 syntax.' -f $sourceSyntaxVersion) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertingFromTo -f $sourceSyntaxVersion, '6') # Add the correct Pester command based on negation if ($isNegated) @@ -199,7 +199,7 @@ function Convert-ShouldBeExactly } } - Write-Debug -Message ('Converted the command `{0}` to `{1}`.' -f $CommandAst.Extent.Text, $newExtentText) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertedCommand -f $CommandAst.Extent.Text, $newExtentText) return $newExtentText } diff --git a/source/Private/Convert-ShouldBeFalse.ps1 b/source/Private/Convert-ShouldBeFalse.ps1 index 8bc0d5c..55f07f7 100644 --- a/source/Private/Convert-ShouldBeFalse.ps1 +++ b/source/Private/Convert-ShouldBeFalse.ps1 @@ -74,7 +74,7 @@ function Convert-ShouldBeFalse Assert-BoundParameter @assertBoundParameterParameters - Write-Debug -Message ('Parsing the command AST: {0}' -f $CommandAst.Extent.Text) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ParsingCommandAst -f $CommandAst.Extent.Text) # Determine if the command is negated $isNegated = Test-PesterCommandNegated -CommandAst $CommandAst @@ -84,7 +84,7 @@ function Convert-ShouldBeFalse # Parse the command elements and convert them to Pester 6 syntax if ($PSCmdlet.ParameterSetName -eq 'Pester6') { - Write-Debug -Message ('Converting from Pester v{0} to Pester v6 syntax.' -f $sourceSyntaxVersion) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertingFromTo -f $sourceSyntaxVersion, '6') # Add the correct Pester command based on negation if ($isNegated) @@ -174,7 +174,7 @@ function Convert-ShouldBeFalse } } - Write-Debug -Message ('Converted the command `{0}` to `{1}`.' -f $CommandAst.Extent.Text, $newExtentText) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertedCommand -f $CommandAst.Extent.Text, $newExtentText) return $newExtentText } diff --git a/source/Private/Convert-ShouldBeNullOrEmpty.ps1 b/source/Private/Convert-ShouldBeNullOrEmpty.ps1 index 554eb2e..89ab5e2 100644 --- a/source/Private/Convert-ShouldBeNullOrEmpty.ps1 +++ b/source/Private/Convert-ShouldBeNullOrEmpty.ps1 @@ -82,7 +82,7 @@ function Convert-ShouldBeNullOrEmpty Assert-BoundParameter @assertBoundParameterParameters - Write-Debug -Message ('Parsing the command AST: {0}' -f $CommandAst.Extent.Text) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ParsingCommandAst -f $CommandAst.Extent.Text) # Determine if the command is negated $isNegated = Test-PesterCommandNegated -CommandAst $CommandAst @@ -92,7 +92,7 @@ function Convert-ShouldBeNullOrEmpty # Parse the command elements and convert them to Pester 6 syntax if ($PSCmdlet.ParameterSetName -eq 'Pester6') { - Write-Debug -Message ('Converting from Pester v{0} to Pester v6 syntax.' -f $sourceSyntaxVersion) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertingFromTo -f $sourceSyntaxVersion, '6') # Add the correct Pester command based on negation if ($isNegated) @@ -182,7 +182,7 @@ function Convert-ShouldBeNullOrEmpty } } - Write-Debug -Message ('Converted the command `{0}` to `{1}`.' -f $CommandAst.Extent.Text, $newExtentText) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertedCommand -f $CommandAst.Extent.Text, $newExtentText) return $newExtentText } diff --git a/source/Private/Convert-ShouldBeOfType.ps1 b/source/Private/Convert-ShouldBeOfType.ps1 index 7d3b070..e152236 100644 --- a/source/Private/Convert-ShouldBeOfType.ps1 +++ b/source/Private/Convert-ShouldBeOfType.ps1 @@ -86,7 +86,7 @@ function Convert-ShouldBeOfType Assert-BoundParameter @assertBoundParameterParameters - Write-Debug -Message ('Parsing the command AST: {0}' -f $CommandAst.Extent.Text) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ParsingCommandAst -f $CommandAst.Extent.Text) # Determine if the command is negated $isNegated = Test-PesterCommandNegated -CommandAst $CommandAst @@ -96,7 +96,7 @@ function Convert-ShouldBeOfType # Parse the command elements and convert them to Pester 6 syntax if ($PSCmdlet.ParameterSetName -eq 'Pester6') { - Write-Debug -Message ('Converting from Pester v{0} to Pester v6 syntax.' -f $sourceSyntaxVersion) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertingFromTo -f $sourceSyntaxVersion, '6') # Add the correct Pester command based on negation if ($isNegated) @@ -221,7 +221,7 @@ function Convert-ShouldBeOfType } } - Write-Debug -Message ('Converted the command `{0}` to `{1}`.' -f $CommandAst.Extent.Text, $newExtentText) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertedCommand -f $CommandAst.Extent.Text, $newExtentText) return $newExtentText } diff --git a/source/Private/Convert-ShouldBeTrue.ps1 b/source/Private/Convert-ShouldBeTrue.ps1 index 0857342..a3f2a98 100644 --- a/source/Private/Convert-ShouldBeTrue.ps1 +++ b/source/Private/Convert-ShouldBeTrue.ps1 @@ -77,7 +77,7 @@ function Convert-ShouldBeTrue Assert-BoundParameter @assertBoundParameterParameters - Write-Debug -Message ('Parsing the command AST: {0}' -f $CommandAst.Extent.Text) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ParsingCommandAst -f $CommandAst.Extent.Text) # Determine if the command is negated $isNegated = Test-PesterCommandNegated -CommandAst $CommandAst @@ -87,7 +87,7 @@ function Convert-ShouldBeTrue # Parse the command elements and convert them to Pester 6 syntax if ($PSCmdlet.ParameterSetName -eq 'Pester6') { - Write-Debug -Message ('Converting from Pester v{0} to Pester v6 syntax.' -f $sourceSyntaxVersion) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertingFromTo -f $sourceSyntaxVersion, '6') # Add the correct Pester command based on negation if ($isNegated) @@ -177,7 +177,7 @@ function Convert-ShouldBeTrue } } - Write-Debug -Message ('Converted the command `{0}` to `{1}`.' -f $CommandAst.Extent.Text, $newExtentText) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertedCommand -f $CommandAst.Extent.Text, $newExtentText) return $newExtentText } diff --git a/source/Private/Convert-ShouldContain.ps1 b/source/Private/Convert-ShouldContain.ps1 index 35fc5a9..e69724e 100644 --- a/source/Private/Convert-ShouldContain.ps1 +++ b/source/Private/Convert-ShouldContain.ps1 @@ -72,7 +72,7 @@ function Convert-ShouldContain Assert-BoundParameter @assertBoundParameterParameters - Write-Debug -Message ('Parsing the command AST: {0}' -f $CommandAst.Extent.Text) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ParsingCommandAst -f $CommandAst.Extent.Text) # Determine if the command is negated $isNegated = Test-PesterCommandNegated -CommandAst $CommandAst @@ -82,7 +82,7 @@ function Convert-ShouldContain # Parse the command elements and convert them to Pester 6 syntax if ($PSCmdlet.ParameterSetName -eq 'Pester6') { - Write-Debug -Message ('Converting from Pester v{0} to Pester v6 syntax.' -f $sourceSyntaxVersion) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertingFromTo -f $sourceSyntaxVersion, '6') # Add the correct Pester command based on negation if ($isNegated) @@ -196,7 +196,7 @@ function Convert-ShouldContain } } - Write-Debug -Message ('Converted the command `{0}` to `{1}`.' -f $CommandAst.Extent.Text, $newExtentText) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertedCommand -f $CommandAst.Extent.Text, $newExtentText) return $newExtentText } diff --git a/source/Private/Convert-ShouldMatch.ps1 b/source/Private/Convert-ShouldMatch.ps1 index f7f8045..6798d57 100644 --- a/source/Private/Convert-ShouldMatch.ps1 +++ b/source/Private/Convert-ShouldMatch.ps1 @@ -74,7 +74,7 @@ function Convert-ShouldMatch Assert-BoundParameter @assertBoundParameterParameters - Write-Debug -Message ('Parsing the command AST: {0}' -f $CommandAst.Extent.Text) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ParsingCommandAst -f $CommandAst.Extent.Text) # Determine if the command is negated $isNegated = Test-PesterCommandNegated -CommandAst $CommandAst @@ -84,7 +84,7 @@ function Convert-ShouldMatch # Parse the command elements and convert them to Pester 6 syntax if ($PSCmdlet.ParameterSetName -eq 'Pester6') { - Write-Debug -Message ('Converting from Pester v{0} to Pester v6 syntax.' -f $sourceSyntaxVersion) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertingFromTo -f $sourceSyntaxVersion, '6') # Add the correct Pester command based on negation if ($isNegated) @@ -196,7 +196,7 @@ function Convert-ShouldMatch } } - Write-Debug -Message ('Converted the command `{0}` to `{1}`.' -f $CommandAst.Extent.Text, $newExtentText) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertedCommand -f $CommandAst.Extent.Text, $newExtentText) return $newExtentText } diff --git a/source/Private/Convert-ShouldMatchExactly.ps1 b/source/Private/Convert-ShouldMatchExactly.ps1 index 1e87390..42ac90e 100644 --- a/source/Private/Convert-ShouldMatchExactly.ps1 +++ b/source/Private/Convert-ShouldMatchExactly.ps1 @@ -73,7 +73,7 @@ function Convert-ShouldMatchExactly Assert-BoundParameter @assertBoundParameterParameters - Write-Debug -Message ('Parsing the command AST: {0}' -f $CommandAst.Extent.Text) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ParsingCommandAst -f $CommandAst.Extent.Text) # Determine if the command is negated $isNegated = Test-PesterCommandNegated -CommandAst $CommandAst @@ -83,7 +83,7 @@ function Convert-ShouldMatchExactly # Parse the command elements and convert them to Pester 6 syntax if ($PSCmdlet.ParameterSetName -eq 'Pester6') { - Write-Debug -Message ('Converting from Pester v{0} to Pester v6 syntax.' -f $sourceSyntaxVersion) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertingFromTo -f $sourceSyntaxVersion, '6') # Add the correct Pester command based on negation if ($isNegated) @@ -199,7 +199,7 @@ function Convert-ShouldMatchExactly } } - Write-Debug -Message ('Converted the command `{0}` to `{1}`.' -f $CommandAst.Extent.Text, $newExtentText) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertedCommand -f $CommandAst.Extent.Text, $newExtentText) return $newExtentText } diff --git a/source/Private/Convert-ShouldNotThrow.ps1 b/source/Private/Convert-ShouldNotThrow.ps1 index 57191d2..3c367a5 100644 --- a/source/Private/Convert-ShouldNotThrow.ps1 +++ b/source/Private/Convert-ShouldNotThrow.ps1 @@ -79,7 +79,7 @@ function Convert-ShouldNotThrow Assert-BoundParameter @assertBoundParameterParameters - Write-Debug -Message ('Parsing the command AST: {0}' -f $CommandAst.Extent.Text) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ParsingCommandAst -f $CommandAst.Extent.Text) # Determine if the command is negated $isNegated = Test-PesterCommandNegated -CommandAst $CommandAst @@ -89,7 +89,7 @@ function Convert-ShouldNotThrow # Parse the command elements and convert them to Pester 6 syntax if ($PSCmdlet.ParameterSetName -eq 'Pester6') { - Write-Debug -Message ('Converting from Pester v{0} to Pester v6 syntax.' -f $sourceSyntaxVersion) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertingFromTo -f $sourceSyntaxVersion, '6') if ($isNegated) { @@ -114,7 +114,7 @@ function Convert-ShouldNotThrow } } - Write-Debug -Message ('Converted the command `{0}` to `{1}`.' -f $CommandAst.Extent.Text, $newExtentText) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertedCommand -f $CommandAst.Extent.Text, $newExtentText) return $newExtentText } diff --git a/source/Private/Convert-ShouldThrow.ps1 b/source/Private/Convert-ShouldThrow.ps1 index 8798bf5..baa0d1a 100644 --- a/source/Private/Convert-ShouldThrow.ps1 +++ b/source/Private/Convert-ShouldThrow.ps1 @@ -84,7 +84,7 @@ function Convert-ShouldThrow Assert-BoundParameter @assertBoundParameterParameters - Write-Debug -Message ('Parsing the command AST: {0}' -f $CommandAst.Extent.Text) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ParsingCommandAst -f $CommandAst.Extent.Text) # Determine if the command is negated $isNegated = Test-PesterCommandNegated -CommandAst $CommandAst @@ -94,7 +94,7 @@ function Convert-ShouldThrow # Parse the command elements and convert them to Pester 6 syntax if ($PSCmdlet.ParameterSetName -eq 'Pester6') { - Write-Debug -Message ('Converting from Pester v{0} to Pester v6 syntax.' -f $sourceSyntaxVersion) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertingFromTo -f $sourceSyntaxVersion, '6') # Add the correct Pester command based on negation if ($isNegated) @@ -235,7 +235,7 @@ function Convert-ShouldThrow } } - Write-Debug -Message ('Converted the command `{0}` to `{1}`.' -f $CommandAst.Extent.Text, $newExtentText) + Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertedCommand -f $CommandAst.Extent.Text, $newExtentText) return $newExtentText } diff --git a/source/Private/Get-AstDefinition.ps1 b/source/Private/Get-AstDefinition.ps1 index ea54710..e7d11e6 100644 --- a/source/Private/Get-AstDefinition.ps1 +++ b/source/Private/Get-AstDefinition.ps1 @@ -23,13 +23,7 @@ Retrieves the ScriptBlockAst definition of the 'Script.ps1' file. .EXAMPLE - 'C:\Scripts\Script.ps1' | Get-AstDefinition - - Retrieves the ScriptBlockAst definition of the 'Script.ps1' file using - pipeline input. - - .EXAMPLE - Get-ChildItem -Path './scripts' | Get-AstDefinition + Get-AstDefinition -Path (Get-ChildItem -Path './scripts') Retrieves the ScriptBlockAst definition of all the files in the path pass as pipeline input. @@ -53,8 +47,6 @@ function Get-AstDefinition { $tokens, $parseErrors = $null - Write-Verbose -Message "Parsing the script file: $filePath" - [System.Management.Automation.Language.Parser]::ParseFile($filePath, [ref] $tokens, [ref] $parseErrors) if ($parseErrors) diff --git a/source/Private/Get-CommandAst.ps1 b/source/Private/Get-CommandAst.ps1 index 372c7a5..f21fe05 100644 --- a/source/Private/Get-CommandAst.ps1 +++ b/source/Private/Get-CommandAst.ps1 @@ -14,7 +14,7 @@ Specifies the PowerShell command for which to retrieve the AST. .EXAMPLE - Get-CommandAst -Command 'Should' + Get-CommandAst -CommandName 'Should' This example retrieves the AST of the 'Should' command. @@ -45,7 +45,7 @@ function Get-CommandAst process { - Write-Verbose -Message "Retrieving the AST of the command: $CommandName" + Write-Debug -Message ($script:localizedData.Get_CommandAst_Debug_RetrievingCommandAsts -f $CommandName) $commandAsts = $Ast.FindAll({ param diff --git a/source/Private/Get-PesterCommandParameter.ps1 b/source/Private/Get-PesterCommandParameter.ps1 index 3b37494..8710070 100644 --- a/source/Private/Get-PesterCommandParameter.ps1 +++ b/source/Private/Get-PesterCommandParameter.ps1 @@ -30,7 +30,7 @@ .EXAMPLE $commandAst = [System.Management.Automation.Language.Parser]::ParseInput('Should -Be "ExpectedString" "BecauseString" "ActualString"') - Get-PesterCommandParameter -CommandAst $commandAst -IgnoreParameter 'Be', 'Not' -PositionalParameter 'ExpectedValue', 'Because', 'ActualValue' + Get-PesterCommandParameter -CommandAst $commandAst -CommandName 'Should' -IgnoreParameter @('Be', 'Not') -PositionalParameter @('ExpectedValue', 'Because', 'ActualValue') Returns a hashtable with the parameters. #> @@ -63,8 +63,8 @@ function Get-PesterCommandParameter process { - Write-Debug -Message "Retrieving the parameters of the extent: $($CommandAst.Extent.Text)" - Write-Debug -Message "Command name: $CommandName" + Write-Debug -Message ($script:localizedData.Get_PesterCommandParameter_Debug_RetrievingParameters -f $CommandAst.Extent.Text) + Write-Debug -Message ($script:localizedData.Get_PesterCommandParameter_Debug_RetrievingCommandName -f $CommandName) # Filter out the command name from the command elements. $commandElement = $CommandAst.CommandElements | @@ -75,7 +75,7 @@ function Get-PesterCommandParameter ) } - Write-Debug -Message "Ignoring the parameters: $($IgnoreParameter -join ', ')" + Write-Debug -Message ($script:localizedData.Get_PesterCommandParameter_Debug_IgnoreParameters -f ($IgnoreParameter -join ', ')) <# Filter out the parameters to ignore from the command elements, e.g.: @@ -95,7 +95,7 @@ function Get-PesterCommandParameter if ($commandElement.Count -gt 0) { - Write-Debug -Message "Named parameters: $($NamedParameter -join ', ')" + Write-Debug -Message ($script:localizedData.Get_PesterCommandParameter_Debug_NamedParameters -f ($NamedParameter -join ', ')) <# Filter out the value parameters including its values from the command elements, e.g.: @@ -135,7 +135,7 @@ function Get-PesterCommandParameter # Get the positional parameters extent text that are left (if any). if ($commandElement.Count -gt 0) { - Write-Debug -Message "Positional parameters: $($PositionalParameter -join ', ')" + Write-Debug -Message ($script:localizedData.Get_PesterCommandParameter_Debug_PositionalParameters -f ($PositionalParameter -join ', ')) $elementCounter = 0 $positionalCounter = 1 diff --git a/source/Private/Get-PesterCommandSyntaxVersion.ps1 b/source/Private/Get-PesterCommandSyntaxVersion.ps1 index efc9dd3..80e2509 100644 --- a/source/Private/Get-PesterCommandSyntaxVersion.ps1 +++ b/source/Private/Get-PesterCommandSyntaxVersion.ps1 @@ -21,7 +21,7 @@ .EXAMPLE $ast = [System.Management.Automation.Language.Parser]::ParseInput('Should -BeExactly "value"', [ref]$null, [ref]$null) - Get-PesterCommandSyntaxVersion -CommandAst $ast -CommandName 'Should' -ParameterName 'BeExactly' + Get-PesterCommandSyntaxVersion -CommandAst $ast Returns the syntax version for the 'Should -BeExactly' command. #> diff --git a/source/Private/Get-ShouldThrowScriptBlock.ps1 b/source/Private/Get-ShouldThrowScriptBlock.ps1 index 193fa87..bcbb100 100644 --- a/source/Private/Get-ShouldThrowScriptBlock.ps1 +++ b/source/Private/Get-ShouldThrowScriptBlock.ps1 @@ -71,11 +71,6 @@ function Get-ShouldThrowScriptBlock # Retrieve the argument $scriptBlock = $argumentAst.Extent.Text - - # if ($argumentAst -and $argumentAst -is [System.Management.Automation.Language.ScriptBlockExpressionAst]) - # { - # $scriptBlock = $argumentAst.ScriptBlock - # } } } diff --git a/source/Public/Convert-PesterSyntax.ps1 b/source/Public/Convert-PesterSyntax.ps1 index 4bb61c1..5e861fd 100644 --- a/source/Public/Convert-PesterSyntax.ps1 +++ b/source/Public/Convert-PesterSyntax.ps1 @@ -36,7 +36,7 @@ Converts the syntax of the Test.ps1 file to Pester 6 syntax. .EXAMPLE - Get-ChildItem -Path "C:\Scripts" -Recurse -Filter "*.ps1" | Convert-PesterSyntax + Get-ChildItem -Path "C:\Scripts" -Recurse -Filter "*.ps1" | Convert-PesterSyntax -Pester6 Converts the syntax of all PowerShell files in the C:\Scripts directory and its subdirectories to the default (newest) Pester syntax. @@ -100,32 +100,44 @@ function Convert-PesterSyntax $convertParameters.Pester6 = $true } - if ($Pester6) + if ($Pester6.IsPresent) { - Write-Verbose 'Converting to Pester 6 syntax.' + Write-Verbose -Message $script:localizedData.Convert_PesterSyntax_StartPester6Conversion } else { - throw 'No version syntax specified. Please specify a format to convert to.' + $PSCmdlet.ThrowTerminatingError( + [System.Management.Automation.ErrorRecord]::new( + $script:localizedData.Convert_PesterSyntax_NoVersionSpecified, + 'CPS0001', # cSpell: disable-line + [System.Management.Automation.ErrorCategory]::InvalidOperation, + $null + ) + ) } } process { + $writeProgressId1Parameters = @{ + Id = 1 + Activity = $script:localizedData.Convert_PesterSyntax_WriteProgress_Id1_Activity + } + if ($Path.Count -gt 1) { - Write-Progress -Id 1 -Activity 'Converting Pester syntax' -Status ('Processing {0} file(s)' -f $Path.Count) -PercentComplete 0 + Write-Progress @writeProgressId1Parameters -PercentComplete 0 -Status ($script:localizedData.Convert_PesterSyntax_WriteProgress_Id1_Status_ProcessingFiles -f $Path.Count) } else { - Write-Progress -Id 1 -Activity 'Converting Pester syntax' -Status ('Processing file {0}' -f (Split-Path -Path $Path -Leaf)) -PercentComplete 0 + Write-Progress @writeProgressId1Parameters -PercentComplete 0 -Status ($script:localizedData.Convert_PesterSyntax_WriteProgress_Id1_Status_ProcessingFile -f (Split-Path -Path $Path -Leaf)) } foreach ($filePath in $Path) { if ($Path.Count -gt 1) { - Write-Progress -Id 1 -Activity 'Converting Pester syntax' -Status "Processing $filePath" -PercentComplete (($Path.IndexOf($filePath) / $Path.Count) * 100) + Write-Progress @writeProgressId1Parameters -PercentComplete (($Path.IndexOf($filePath) / $Path.Count) * 100) -Status ($script:localizedData.Convert_PesterSyntax_WriteProgress_Id1_Status_ProcessingFile -f (Split-Path -Path $filePath -Leaf)) } $verboseDescriptionMessage = $script:localizedData.Convert_PesterSyntax_ShouldProcessVerboseDescription -f $filePath @@ -147,7 +159,7 @@ function Convert-PesterSyntax # Get the script text from the script block AST that will be used to replace the original script text. $convertedScriptText = $scriptBlockAst.Extent.Text - Write-Debug -Message ('Parsing the script block AST: {0}' -f $scriptBlockAst.Extent.Text) + Write-Debug -Message ($script:localizedData.Convert_PesterSyntax_Debug_ScriptBlockAst -f $scriptBlockAst.Extent.Text) <# Get all the Should command AST's in the script block AST, and sort @@ -161,9 +173,15 @@ function Convert-PesterSyntax if ($shouldCommandAst) { - Write-Progress -Id 2 -ParentId 1 -Activity 'Converting Should command syntax' -Status ('Processing {0} command(s)' -f $shouldCommandAst.Count) -PercentComplete 0 + $writeProgressId2Parameters = @{ + Id = 2 + ParentId = 1 + Activity = $script:localizedData.Convert_PesterSyntax_WriteProgress_Id2_Activity + } + + Write-Progress @writeProgressId2Parameters -PercentComplete 0 -Status ($script:localizedData.Convert_PesterSyntax_WriteProgress_Id2_Status_ProcessingCommands -f $shouldCommandAst.Count) - Write-Debug -Message ('Found {0} ''Should'' command(s) in {1}.' -f $shouldCommandAst.Count, $filePath) + Write-Debug -Message ($script:localizedData.Convert_PesterSyntax_Debug_FoundShouldCommand -f $shouldCommandAst.Count, $filePath) foreach ($commandAst in $shouldCommandAst) { @@ -176,7 +194,7 @@ function Convert-PesterSyntax # If only one item was returned then there is no collection that has the method IndexOf. $percentComplete = $shouldCommandAst.Count -gt 1 ? (($shouldCommandAst.IndexOf($commandAst) / $shouldCommandAst.Count) * 100) : 100 - Write-Progress -Id 2 -ParentId 1 -Activity 'Converting Should command syntax' -Status "Processing extent on line $($commandAst.Extent.StartLineNumber)" -PercentComplete $percentComplete + Write-Progress @writeProgressId2Parameters -PercentComplete $percentComplete -Status ($script:localizedData.Convert_PesterSyntax_WriteProgress_Id2_Status_ProcessingLine -f $commandAst.Extent.StartLineNumber) $operatorName = Get-ShouldCommandOperatorName -CommandAst $commandAst -ErrorAction 'Stop' @@ -269,7 +287,7 @@ function Convert-PesterSyntax default { - Write-Warning -Message ('Unsupported command operator ''{0}'' in extent: `{1}`' -f $operatorName, $commandAst.Extent.Text) + Write-Warning -Message ($script:localizedData.Convert_PesterSyntax_Warning_UnsupportedCommandOperator -f $operatorName, $commandAst.Extent.Text) $apply = $false } @@ -277,7 +295,7 @@ function Convert-PesterSyntax } else { - Write-Warning -Message ('Did not found any of the supported command operators in extent: `{0}`' -f $commandAst.Extent.Text) + Write-Warning -Message ($script:localizedData.Convert_PesterSyntax_MissingSupportedCommandOperator -f $commandAst.Extent.Text) } if ($apply) @@ -287,11 +305,11 @@ function Convert-PesterSyntax } } - Write-Progress -Id 2 -ParentId 1 -Activity 'Converting Should command syntax' -Status 'Completed' -PercentComplete 100 -Completed + Write-Progress @writeProgressId2Parameters -Completed -PercentComplete 100 -Status $script:localizedData.Convert_PesterSyntax_WriteProgress_Id2_Status_Completed } else { - Write-Verbose -Message "No 'Should' command found in $filePath." + Write-Verbose -Message ($script:localizedData.Convert_PesterSyntax_NoShouldCommand -f $filePath) } if ($PassThru) @@ -307,6 +325,6 @@ function Convert-PesterSyntax end { - Write-Progress -Id 1 -Activity 'Converting Pester syntax' -Status 'Completed' -PercentComplete 100 -Completed + Write-Progress @writeProgressId1Parameters -Completed -PercentComplete 100 -Status $script:localizedData.Convert_PesterSyntax_WriteProgress_Id1_Status_Completed } } diff --git a/source/WikiSource/Pester_v5_Conversion.md b/source/WikiSource/Pester_v5_Conversion.md index a830126..9974640 100644 --- a/source/WikiSource/Pester_v5_Conversion.md +++ b/source/WikiSource/Pester_v5_Conversion.md @@ -26,7 +26,7 @@ BeTrue | `Should-BeTrue` | `Should-BeFalse` | - Contain | `Should-ContainCollection` | `Should-NotContainCollection` | - Match | `Should-MatchString` | `Should-NotMatchString` | - MatchExactly | `Should-MatchString -CaseSensitive` | `Should-NotMatchString -CaseSensitive` | - -Throw | `Should-Throw` | `$null = &()` | See 1) +Throw | `Should-Throw` | `$null = & ()` | See 1) 1) There is not a command that `Should -Not -Throw` can be converted to. In Pester 6 the `It`-block catch any exception if one occurs, that means diff --git a/source/en-US/PesterConverter.strings.psd1 b/source/en-US/PesterConverter.strings.psd1 index 5d1ba02..dad609b 100644 --- a/source/en-US/PesterConverter.strings.psd1 +++ b/source/en-US/PesterConverter.strings.psd1 @@ -16,4 +16,34 @@ ConvertFrom-StringData @' Convert_PesterSyntax_ShouldProcessVerboseWarning = Are you sure you want to convert the script file '{0}'? # This string shall not end with full stop (.) since it is used as a title of ShouldProcess messages. Convert_PesterSyntax_ShouldProcessCaption = Convert script file + Convert_PesterSyntax_StartPester6Conversion = Converting to Pester 6 syntax. + Convert_PesterSyntax_NoVersionSpecified = No version syntax specified. Please specify a syntax version to convert to. + Convert_PesterSyntax_WriteProgress_Id1_Activity = Converting to Pester 6 syntax. + Convert_PesterSyntax_WriteProgress_Id1_Status_ProcessingFiles = Processing {0} file(s). + Convert_PesterSyntax_WriteProgress_Id1_Status_ProcessingFile = Processing file '{0}'. + Convert_PesterSyntax_WriteProgress_Id1_Status_Completed = Completed. + Convert_PesterSyntax_WriteProgress_Id2_Activity = Converting Should command syntax. + Convert_PesterSyntax_WriteProgress_Id2_Status_ProcessingCommands = Processing {0} command(s). + Convert_PesterSyntax_WriteProgress_Id2_Status_ProcessingLine = Processing extent on line '{0}'. + Convert_PesterSyntax_WriteProgress_Id2_Status_Completed = Completed. + Convert_PesterSyntax_Debug_ScriptBlockAst = Parsing the script block AST: {0} + Convert_PesterSyntax_Debug_FoundShouldCommand = Found {0} `Should` command(s) in file '{1}'. + Convert_PesterSyntax_Warning_UnsupportedCommandOperator = Unsupported command operator '{0}' in extent: `{1}` + Convert_PesterSyntax_MissingSupportedCommandOperator = Did not found any of the supported command operators in extent: `{0}` + Convert_PesterSyntax_NoShouldCommand = "No 'Should' command found in '{0}'. + + # Common for all Convert-Should* functions + Convert_Should_Debug_ParsingCommandAst = Parsing the command AST: `{0}` + Convert_Should_Debug_ConvertingFromTo = Converting from Pester v{0} to Pester v{1} syntax. + Convert_Should_Debug_ConvertedCommand = Converted the command `{0}` to `{1}`. + + # Get-PesterCommandParameter + Get_PesterCommandParameter_Debug_RetrievingParameters = Retrieving the parameters of the extent: {0} + Get_PesterCommandParameter_Debug_RetrievingCommandName = Retrieving parameters for the command name: {0} + Get_PesterCommandParameter_Debug_IgnoreParameters = Ignoring (filtering out) the parameters: {0} + Get_PesterCommandParameter_Debug_NamedParameters = Parsing named parameters: {0} + Get_PesterCommandParameter_Debug_PositionalParameters = Parsing positional parameters: {0} + + # Get-CommandAst + Get_CommandAst_Debug_RetrievingCommandAsts = Retrieving AST's for command '{0}'. '@ diff --git a/source/en-US/about_PesterConverter.help.txt b/source/en-US/about_PesterConverter.help.txt index 6249f8d..bd54e26 100644 --- a/source/en-US/about_PesterConverter.help.txt +++ b/source/en-US/about_PesterConverter.help.txt @@ -2,10 +2,10 @@ TOPIC about_PesterConverter SHORT DESCRIPTION - Commands ta convert Pester tests. + Commands to convert Pester tests. LONG DESCRIPTION - Commands ta convert Pester tests. + Commands to convert Pester tests. EXAMPLES PS C:\> Get-Commands -Module PesterConverter