22
33. " $PSScriptRoot \FileIntegrity.Exceptions.ps1"
44
5- function Get-FileEncoding
6- {
7- <#
5+ function Get-FileEncoding {
6+ <#
87 . SYNOPSIS
98 Tests a file for encoding.
109
@@ -14,78 +13,74 @@ function Get-FileEncoding
1413 . PARAMETER Path
1514 The file to test
1615#>
17- [CmdletBinding ()]
18- Param (
19- [Parameter (Mandatory = $True , ValueFromPipelineByPropertyName = $True )]
20- [Alias (' FullName' )]
21- [string ]
22- $Path
23- )
24-
25- [byte []]$byte = get-content - Encoding byte - ReadCount 4 - TotalCount 4 - Path $Path
26-
27- if ($byte [0 ] -eq 0xef -and $byte [1 ] -eq 0xbb -and $byte [2 ] -eq 0xbf ) { ' UTF8' }
28- elseif ($byte [0 ] -eq 0x73 -and $byte [1 ] -eq 0x64 -and $byte [2 ] -eq 0x6C ) { ' UTF8' }
29- elseif ($byte [0 ] -eq 0xfe -and $byte [1 ] -eq 0xff ) { ' Unicode' }
30- elseif ($byte [0 ] -eq 0 -and $byte [1 ] -eq 0 -and $byte [2 ] -eq 0xfe -and $byte [3 ] -eq 0xff ) { ' UTF32' }
31- elseif ($byte [0 ] -eq 0x2b -and $byte [1 ] -eq 0x2f -and $byte [2 ] -eq 0x76 ) { ' UTF7' }
32- else { ' Unknown, possible ASCII' }
16+ [CmdletBinding ()]
17+ Param (
18+ [Parameter (Mandatory = $True , ValueFromPipelineByPropertyName = $True )]
19+ [Alias (' FullName' )]
20+ [string ]
21+ $Path
22+ )
23+
24+ [byte []]$byte = get-content - Encoding byte - ReadCount 4 - TotalCount 4 - Path $Path
25+
26+ if ($byte [0 ] -eq 0xef -and $byte [1 ] -eq 0xbb -and $byte [2 ] -eq 0xbf ) { ' UTF8' }
27+ elseif ($byte [0 ] -eq 0x73 -and $byte [1 ] -eq 0x64 -and $byte [2 ] -eq 0x6C ) { ' UTF8' }
28+ elseif ($byte [0 ] -eq 0xfe -and $byte [1 ] -eq 0xff ) { ' Unicode' }
29+ elseif ($byte [0 ] -eq 0 -and $byte [1 ] -eq 0 -and $byte [2 ] -eq 0xfe -and $byte [3 ] -eq 0xff ) { ' UTF32' }
30+ elseif ($byte [0 ] -eq 0x2b -and $byte [1 ] -eq 0x2f -and $byte [2 ] -eq 0x76 ) { ' UTF7' }
31+ else { ' Unknown, possible ASCII' }
3332}
3433
3534Describe " Verifying integrity of module files" {
36- Context " Validating PS1 Script files" {
37- $allFiles = Get-ChildItem - Path $moduleRoot - Recurse - Filter " *.ps1" | Where-Object FullName -NotLike " $moduleRoot \tests\*"
38-
39- foreach ($file in $allFiles )
40- {
41- $name = $file.FullName.Replace (" $moduleRoot \" , ' ' )
42-
43- It " [$name ] Should have UTF8 encoding" {
44- Get-FileEncoding - Path $file.FullName | Should Be ' UTF8'
45- }
46-
47- It " [$name ] Should have no trailing space" {
48- ($file | Select-String " \s$" | Where-Object { $_.Line.Trim ().Length -gt 0 } | Measure-Object ).Count | Should Be 0
49- }
50-
51- $tokens = $null
52- $parseErrors = $null
53- $ast = [System.Management.Automation.Language.Parser ]::ParseFile($file.FullName , [ref ]$tokens , [ref ]$parseErrors )
54-
55- It " [$name ] Should have no syntax errors" {
56- $parseErrors | Should Be $Null
57- }
58-
59- foreach ($command in $global :BannedCommands )
60- {
61- if ($global :MayContainCommand [" $command " ] -notcontains $file.Name )
62- {
63- It " [$name ] Should not use $command " {
64- $tokens | Where-Object Text -EQ $command | Should Be $null
65- }
66- }
67- }
68-
69- It " [$name ] Should not contain aliases" {
70- $tokens | Where-Object TokenFlags -eq CommandName | Where-Object { Test-Path " alias:\$ ( $_.Text ) " } | Measure-Object | Select-Object - ExpandProperty Count | Should Be 0
71- }
72- }
73- }
74-
75- Context " Validating help.txt help files" {
76- $allFiles = Get-ChildItem - Path $moduleRoot - Recurse - Filter " *.help.txt" | Where-Object FullName -NotLike " $moduleRoot \tests\*"
77-
78- foreach ($file in $allFiles )
79- {
80- $name = $file.FullName.Replace (" $moduleRoot \" , ' ' )
81-
82- It " [$name ] Should have UTF8 encoding" {
83- Get-FileEncoding - Path $file.FullName | Should Be ' UTF8'
84- }
85-
86- It " [$name ] Should have no trailing space" {
87- ($file | Select-String " \s$" | Where-Object { $_.Line.Trim ().Length -gt 0 } | Measure-Object ).Count | Should Be 0
88- }
89- }
90- }
35+ Context " Validating PS1 Script files" {
36+ $allFiles = Get-ChildItem - Path $moduleRoot - Recurse - Filter " *.ps1" | Where-Object FullName -NotLike " $moduleRoot \tests\*"
37+
38+ foreach ($file in $allFiles ) {
39+ $name = $file.FullName.Replace (" $moduleRoot \" , ' ' )
40+
41+ It " [$name ] Should have UTF8 encoding" {
42+ Get-FileEncoding - Path $file.FullName | Should Be ' UTF8'
43+ }
44+
45+ It " [$name ] Should have no trailing space" {
46+ ($file | Select-String " \s$" | Where-Object { $_.Line.Trim ().Length -gt 0 } | Measure-Object ).Count | Should Be 0
47+ }
48+
49+ $tokens = $null
50+ $parseErrors = $null
51+ $ast = [System.Management.Automation.Language.Parser ]::ParseFile($file.FullName , [ref ]$tokens , [ref ]$parseErrors )
52+
53+ It " [$name ] Should have no syntax errors" {
54+ $parseErrors | Should Be $Null
55+ }
56+
57+ foreach ($command in $global :BannedCommands ) {
58+ if ($global :MayContainCommand [" $command " ] -notcontains $file.Name ) {
59+ It " [$name ] Should not use $command " {
60+ $tokens | Where-Object Text -EQ $command | Should Be $null
61+ }
62+ }
63+ }
64+
65+ It " [$name ] Should not contain aliases" {
66+ $tokens | Where-Object TokenFlags -eq CommandName | Where-Object { Test-Path " alias:\$ ( $_.Text ) " } | Measure-Object | Select-Object - ExpandProperty Count | Should Be 0
67+ }
68+ }
69+ }
70+
71+ Context " Validating help.txt help files" {
72+ $allFiles = Get-ChildItem - Path $moduleRoot - Recurse - Filter " *.help.txt" | Where-Object FullName -NotLike " $moduleRoot \tests\*"
73+
74+ foreach ($file in $allFiles ) {
75+ $name = $file.FullName.Replace (" $moduleRoot \" , ' ' )
76+
77+ It " [$name ] Should have UTF8 encoding" {
78+ Get-FileEncoding - Path $file.FullName | Should Be ' UTF8'
79+ }
80+
81+ It " [$name ] Should have no trailing space" {
82+ ($file | Select-String " \s$" | Where-Object { $_.Line.Trim ().Length -gt 0 } | Measure-Object ).Count | Should Be 0
83+ }
84+ }
85+ }
9186}
0 commit comments