pester / Pester

Pester is the ubiquitous test and mock framework for PowerShell.
https://pester.dev/
Other
3.11k stars 473 forks source link

Migration script #892

Closed indented-automation closed 6 years ago

indented-automation commented 7 years ago

I don't have time to do a PR right now, but in case it helps (anyone).

This is a different version of the migration script which (in part) rewrites the "Should Contain" assertion. This particular version also updates notation to parameterized assertions.

Posted because it does away with the use of regular expressions to find statements to update which means it should be a more robust approach.

https://gist.github.com/indented-automation/aeb14825e39dd8849beee44f681fbab3

it-praktyk commented 7 years ago

@indented-automation, thank you for your awesome script! I just migrated all Pester itself tests to the new syntax using it.

In the near future, I'll add it to documentation.

it-praktyk commented 7 years ago

@indented-automation, could you declare directly that your script 'Update-PesterTest.ps1' can be used under the Apache License 2.0?

Pester itself use that license.

Thank you.

indented-automation commented 7 years ago

Yep, I can confirm I am happy for this to be used under that licence.

Explicit version: I am happy for Update-PesterTest.ps1 to be used under the Apache License 2.0.

Added license to gist too, just in case :)

Chris

it-praktyk commented 7 years ago

@indented-automation, thank you.

it-praktyk commented 7 years ago

Some tests are not correctly rewritten.

screenshot from 2017-10-12 23-53-29

it-praktyk commented 7 years ago

The issue with some characters is probably related to the file encoding.

PS >Get-FileEncoding <BEFORE_UPDATE>\pester\Functions\Mock.ps1

BodyName          : utf-8
EncodingName      : Unicode (UTF-8)
HeaderName        : utf-8
WebName           : utf-8
WindowsCodePage   : 1200
IsBrowserDisplay  : True
IsBrowserSave     : True
IsMailNewsDisplay : True
IsMailNewsSave    : True
IsSingleByte      : False
EncoderFallback   : System.Text.EncoderReplacementFallback
DecoderFallback   : System.Text.DecoderReplacementFallback
IsReadOnly        : True
CodePage          : 65001

PS >Get-FileEncoding  <AFTER_UPDATE>\pester\Functions\Mock.ps1

BodyName          : utf-7
EncodingName      : Unicode (UTF-7)
HeaderName        : utf-7
WebName           : utf-7
WindowsCodePage   : 1200
IsBrowserDisplay  : False
IsBrowserSave     : False
IsMailNewsDisplay : True
IsMailNewsSave    : True
IsSingleByte      : False
EncoderFallback   : System.Text.EncoderReplacementFallback
DecoderFallback   : System.Text.UTF7Encoding+DecoderUTF7Fallback
IsReadOnly        : True
CodePage          : 65000

All files what were UTF-8 encoded before conversion and correction script are below

$Names = @( ".\Functions\Assertions\FileContentMatch.Tests.ps1"
            ".\Functions\Assertions\FileContentMatchExactly.Tests.ps1"
            ".\Functions\Mock.ps1"
            ".\Functions\Mock.Tests.ps1"
             ".\Functions\Output.Tests.ps1"
             ".\Functions\TestsRunningInCleanRunspace.Tests.ps1"
             ".\Pester.Tests.ps1")

ForEach ( $Name in $Names) {

    [String]$TemporaryName = "{0}.temp" -f $Name

    Get-Content $Name | Set-Content -Encoding UTF8 -Path $TemporaryName

    Move-Item -Path $TemporaryName -Destination $Name -force

}
indented-automation commented 7 years ago

It could likely do with injection of your original encoding detection function. I haven't needed that for my purposes, but I couldn't find fault with the approach.

Realistically this only replaces the regex matching part of the process.

it-praktyk commented 6 years ago

Finally, I found a time to add the function provided by @indented-automation to the Pester wiki.

Guys, can you review it?

nohwnd commented 6 years ago

@it-praktyk LGTM