pester / Pester

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

Contain doesn't support Unicode #377

Closed tkirill closed 9 years ago

tkirill commented 9 years ago

This test fails:

Set-StrictMode -Version Latest

InModuleScope Pester {
    Describe "PesterContain" {
        Context "when testing file contents" {
            Setup -File "test.txt" "this is line 1`nrush is awesome`nAnd this is Unicode: ☺"

            It "returns true if the file contains the specified UTF8 content" {
                Test-PositiveAssertion (PesterContain "$TestDrive\test.txt" "☺")
            }
        }
    }
}

Likely other assertions have this problem too. I see two ways to fix this:

  1. Assume files to be checked is in UTF8.
  2. Provide -Encoding option for Contain assertion and for other assertions if needed.

I like first option because it works for almost everyone, it is simple and intuitive and it has backward compatibility with ASCII. Also, it is trivial to implement if I don't miss some hidden difficulties.

Second option is more flexible but it is a question if somebody will ever need this flexibility.

dlwyatt commented 9 years ago

I wouldn't worry about this too much, as the Should Contain operator is pretty much on everyone's "get rid of this" list (or at least rename it). They want Should Contain to correspond to PowerShell's -contains operator, which is for searching arrays, not doing partial matches in files.

tkirill commented 9 years ago

@dlwyatt Does Pester have an alternative to Should Contain already? At the moment I use a workaround with Get-Content and Should Be.

dlwyatt commented 9 years ago

That's what I would do. (Or Should Match, etc, but I'd start out with Get-Content.)

tkirill commented 9 years ago

@dlwyatt Anyway, here is PR #378. It fixes this small issue and I think doesn't makes anything worse.