tests/general/FileIntegrity.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
$moduleRoot = (Resolve-Path "$PSScriptRoot\..\..").Path

. "$PSScriptRoot\FileIntegrity.Exceptions.ps1"

Describe "Verifying integrity of module files" {
    Context "Validating PS1 Script files" {
        $allFiles = Get-ChildItem -Path $moduleRoot -Recurse | Where-Object Name -like "*.ps1" | Where-Object FullName -NotLike "$moduleRoot\tests\*"
        
        foreach ($file in $allFiles)
        {
            $name = $file.FullName.Replace("$moduleRoot\", '')
            
            It "[$name] Should have UTF8 encoding with Byte Order Mark" {
                function Get-FileEncoding
                {
                <#
                    .SYNOPSIS
                        Tests a file for encoding.
                    
                    .DESCRIPTION
                        Tests a file for encoding.
                    
                    .PARAMETER Path
                        The file to test
                #>

                    [CmdletBinding()]
                    Param (
                        [Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)]
                        [Alias('FullName')]
                        [string]
                        $Path
                    )
                    
                    if ($PSVersionTable.PSVersion.Major -lt 6)
                    {
                        [byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path
                    }
                    else
                    {
                        [byte[]]$byte = Get-Content -AsByteStream -ReadCount 4 -TotalCount 4 -Path $Path
                    }
                    
                    if ($byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf) { 'UTF8 BOM' }
                    elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff) { 'Unicode' }
                    elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff) { 'UTF32' }
                    elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76) { 'UTF7' }
                    else { 'Unknown' }
                }
                Get-FileEncoding -Path $file.FullName | Should -Be 'UTF8 BOM'
            }
            
            It "[$name] Should have no trailing space" {
                ($file | Select-String "\s$" | Where-Object { $_.Line.Trim().Length -gt 0}).LineNumber | Should -BeNullOrEmpty
            }
            
            $tokens = $null
            $parseErrors = $null
            $ast = [System.Management.Automation.Language.Parser]::ParseFile($file.FullName, [ref]$tokens, [ref]$parseErrors)
            
            It "[$name] Should have no syntax errors" {
                $parseErrors | Should -Be $Null
            }
            
            foreach ($command in $global:BannedCommands)
            {
                if ($global:MayContainCommand["$command"] -notcontains $file.Name)
                {
                    It "[$name] Should not use $command" {
                        $tokens | Where-Object Text -EQ $command | Should -BeNullOrEmpty
                    }
                }
            }
        }
    }
    
    Context "Validating help.txt help files" {
        $allFiles = Get-ChildItem -Path $moduleRoot -Recurse | Where-Object Name -like "*.help.txt" | Where-Object FullName -NotLike "$moduleRoot\tests\*"
        
        foreach ($file in $allFiles)
        {
            $name = $file.FullName.Replace("$moduleRoot\", '')
            
            It "[$name] Should have UTF8 encoding" {
                function Get-FileEncoding
                {
                <#
                    .SYNOPSIS
                        Tests a file for encoding.
                    
                    .DESCRIPTION
                        Tests a file for encoding.
                    
                    .PARAMETER Path
                        The file to test
                #>

                    [CmdletBinding()]
                    Param (
                        [Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)]
                        [Alias('FullName')]
                        [string]
                        $Path
                    )
                    
                    if ($PSVersionTable.PSVersion.Major -lt 6)
                    {
                        [byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path
                    }
                    else
                    {
                        [byte[]]$byte = Get-Content -AsByteStream -ReadCount 4 -TotalCount 4 -Path $Path
                    }
                    
                    if ($byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf) { 'UTF8 BOM' }
                    elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff) { 'Unicode' }
                    elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff) { 'UTF32' }
                    elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76) { 'UTF7' }
                    else { 'Unknown' }
                }
                Get-FileEncoding -Path $file.FullName | Should -Be 'UTF8 BOM'
            }
            
            It "[$name] Should have no trailing space" {
                ($file | Select-String "\s$" | Where-Object { $_.Line.Trim().Length -gt 0 } | Measure-Object).Count | Should -Be 0
            }
        }
    }
}