Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions test/common/FileEncoding/FileEncoding.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) Microsoft Corporation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should actually run this test somewhere

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, add another job to here
https://github.com/PowerShell/PowerShell/blob/master/.vsts-ci/misc-analysis.yml

Or add another workflow:

workflows

Whichever you are more comfortable with

# Licensed under the MIT License.

using namespace System

Describe 'Verify file encoding' {
BeforeAll {
Push-Location $PSScriptRoot\..\..\..
$nonBinaryFiles = (git grep -I --files-with-matches -e $'')
}

AfterAll {
Pop-Location
}

It 'No UTF-8 BOM' {
filter hasUtf8Bom {
$_.Where{
$bom = [Text.UnicodeEncoding]::UTF8.GetPreamble()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it is better to detect the encoding. C# code:

            Encoding GetPathEncoding()
            {
                using (StreamReader reader = new StreamReader(this.Path, Utils.utf8NoBom, detectEncodingFromByteOrderMarks: true))
                {
                    _ = reader.Read();
                    return reader.CurrentEncoding;
                }
            }

$bytes = [byte[]]::new($bom.Length)
$stream = [IO.File]::OpenRead($_)
$stream.Read($bytes, 0, $bytes.Length) > $null
$stream.Close()
[Linq.Enumerable]::SequenceEqual($bom, $bytes)
}
}

$nonBinaryFiles | hasUtf8Bom | Should -BeNullOrEmpty
}
}