Skip to content

Instantly share code, notes, and snippets.

@indented-automation
Last active October 4, 2021 18:17
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save indented-automation/aeb14825e39dd8849beee44f681fbab3 to your computer and use it in GitHub Desktop.
Save indented-automation/aeb14825e39dd8849beee44f681fbab3 to your computer and use it in GitHub Desktop.
Migrating to Pester 4.0.7
function Update-PesterTest {
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[Alias('FullName')]
[String]$Path
)
begin {
$shouldParams = [String[]](Get-Command Should).Parameters.Keys
}
process {
$Path = $pscmdlet.GetUnresolvedProviderPathFromPSPath($Path)
$errors = $tokens = @()
$ast = [System.Management.Automation.Language.Parser]::ParseFile(
$Path,
[Ref]$tokens,
[Ref]$errors
)
$script = Get-Content $Path -Raw
$ast.FindAll(
{
param ( $ast )
$ast -is [System.Management.Automation.Language.CommandAst] -and
$ast.GetCommandName() -eq 'Should'
},
$true
) |
ForEach-Object {
$_.CommandElements | Where-Object {
$_.StringConstantType -eq 'BareWord' -and
$_.Value -in $shouldParams -or
$_.Value -eq 'Contain'
}
} |
Sort-Object { $_.Extent.StartOffset } -Descending |
ForEach-Object {
if ($_.Value -eq 'Contain') {
$script = $script.Remove($_.Extent.StartOffset, 7).Insert($_.Extent.StartOffset, '-FileContentMatch')
} else {
$script = $script.Insert($_.Extent.Startoffset, '-')
}
}
Set-Content -Path $Path -Value $script -NoNewline
}
}
Copyright 2017 Chris Dent
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment