PowerShell 中 Pester 的 -FileContentMatchMultiline 的区分大小写选项

Case sensitive option for Pester's -FileContentMatchMultiline in PowerShell

我有十几行代码需要在多个 .ps1 文件中保持一致。我能够使用 Pester 中的 -FileContentMatchMultiline 功能来完成这项工作,但我需要匹配区分大小写。有什么简单的方法可以做到这一点吗?

这是我目前拥有的:

It "Has all the lines below, but case sensitive" {
        Get-ChildItem $directoryOfFilesToCheck | ForEach-Object {
            $matchstring = @'
$var1 = Line one blah blah blah
$var2 = Line two blah blah blah 
$var3 = Line three blah blah blah
'@
            $_ | Should -FileContentMatchMultiline $([regex]::escape($matchString))
        }

    }

问题是如果文件包含,它也会匹配:

$var1 = Line one BLAH Blah blAH
$var2 = Line two BLAH Blah blAH 
$var3 = Line three BLAH Blah blAH

这很重要,因为在文件中有一些函数调用区分大小写,因为它们被程序 运行 脚本使用。

不幸的是,Pester 目前似乎没有 FileContentMatchExactlyMultiline 断言,但看看 FileContentMatchMultiline 是如何工作的:

$succeeded = [bool] ((& $SafeCommands['Get-Content'] $ActualValue -Delimiter ([char]0)) -match $ExpectedContent)

所以看起来你可以通过这样做作为解决方法简单地推出你自己的等价物:

Describe 'MyTests' {

    It "Has all the lines below, but case sensitive" {

        $matchstring = @'
$var1 = Line one blah blah blah
$var2 = Line two blah blah blah 
$var3 = Line three blah blah blah
'@

        Get-ChildItem $directoryOfFilesToCheck | ForEach-Object {       
            $ActualValue = (Get-Content $_.FullName -Delimiter [char]0)
            $ActualValue -cmatch $([regex]::escape($matchstring)) | Should -Be $True
        }
    }
}

这只是将 -match 切换为 -cmatch,使其区分大小写。

另一种选择是使用 -MatchExactly 断言将文件内容放入 $ActualValue 中,如上所示:

$ActualValue | Should -MatchExactly $([regex]::escape($matchstring))

向 Pester 贡献一个 FileContentMatchExactlyMultiline 断言似乎并不是基于上述内容的那么多工作。值得在这里为它添加一个问题:https://github.com/pester/Pester/issues