1+ # Copyright (c) Microsoft Corporation. All rights reserved.
2+ # Licensed under the MIT License.
3+
4+ $script :fileContent = @'
5+ $assetDir = 'C:\ImportantFiles\'
6+ $archiveDir = 'C:\Archived\'
7+
8+ $runningOnWindows = -not ($IsLinux -or $IsMacOS)
9+ $zipCompressionLevel = 'Optimal'
10+ $includeBaseDirInZips = $true
11+
12+ if (-not (Test-Path $archiveDir))
13+ {
14+ New-Item -Type Directory $archiveDir
15+ }
16+
17+ Import-Module -FullyQualifiedName @{ ModuleName = 'MyArchiveUtilities'; ModuleVersion = '2.0' }
18+
19+ $sigs = [System.Collections.Generic.List[System.Management.Automation.Signature]]::new()
20+ $filePattern = [System.Management.Automation.WildcardPattern]::Get('system*')
21+ foreach ($file in Get-ChildItem $assetDir -Recurse -Depth 1)
22+ {
23+ if (-not $filePattern.IsMatch($file.Name))
24+ {
25+ continue
26+ }
27+
28+ if ($file.Name -like '*.dll')
29+ {
30+ $sig = Get-AuthenticodeSignature $file
31+ $sigs.Add($sig)
32+ continue
33+ }
34+
35+ if (Test-WithFunctionFromMyModule -File $file)
36+ {
37+ $destZip = Join-Path $archiveDir $file.BaseName
38+ [System.IO.Compression.ZipFile]::CreateFromDirectory($file.FullName, "$destZip.zip", $zipCompressionLevel, $includeBaseDirInZips)
39+ }
40+ }
41+
42+ Write-Output $sigs
43+ '@
44+
45+ $script :settingsContent = @'
46+ @{
47+ Rules = @{
48+ PSUseCompatibleCommands = @{
49+ Enable = $true
50+ TargetProfiles = @(
51+ 'win-8_x64_10.0.17763.0_5.1.17763.316_x64_4.0.30319.42000_framework' # Server 2019 - PS 5.1 (the platform it already runs on)
52+ 'win-8_x64_6.2.9200.0_3.0_x64_4.0.30319.42000_framework' # Server 2012 - PS 3
53+ 'ubuntu_x64_18.04_6.1.3_x64_4.0.30319.42000_core' # Ubuntu 18.04 - PS 6.1
54+ )
55+ }
56+ PSUseCompatibleTypes = @{
57+ Enable = $true
58+ # Same as for command targets
59+ TargetProfiles = @(
60+ 'win-8_x64_10.0.17763.0_5.1.17763.316_x64_4.0.30319.42000_framework'
61+ 'win-8_x64_6.2.9200.0_3.0_x64_4.0.30319.42000_framework'
62+ 'ubuntu_x64_18.04_6.1.3_x64_4.0.30319.42000_core'
63+ )
64+ }
65+ PSUseCompatibleSyntax = @{
66+ Enable = $true
67+ TargetVersions = @('3.0', '5.1', '6.1')
68+ }
69+ }
70+ }
71+ '@
72+
73+ $script :expectedCommandDiagnostics = @ (
74+ @ { Command = ' Import-Module' ; Parameter = ' FullyQualifiedName' ; Line = 13 }
75+ @ { Command = ' Get-ChildItem' ; Parameter = ' Depth' ; Line = 17 }
76+ @ { Command = ' Get-AuthenticodeSignature' ; Line = 26 }
77+ )
78+
79+ $script :expectedTypeDiagnostics = @ (
80+ @ { Type = ' System.Management.Automation.WildcardPattern' ; Member = ' Get' ; Line = 16 }
81+ @ { Type = ' System.IO.Compression.ZipFile' ; Line = 34 }
82+ )
83+
84+ $script :expectedSyntaxDiagnostics = @ (
85+ @ { Line = 15 ; CorrectionText = " New-Object 'System.Collections.Generic.List[System.Management.Automation.Signature]'" }
86+ )
87+
88+ Describe " Running all compatibility rules with a settings file" {
89+ BeforeAll {
90+ $testFile = New-Item - Path " $TestDrive /test.ps1" - Value $script :fileContent - ItemType File
91+ $settingsFile = New-Item - Path " $TestDrive /settings.psd1" - Value $script :settingsContent - ItemType File
92+ $diagnostics = Invoke-ScriptAnalyzer - Path $testFile.FullName - Settings $settingsFile.FullName |
93+ Group-Object - Property RuleName - AsHashtable
94+ $commandDiagnostics = $diagnostics.PSUseCompatibleCommands
95+ $typeDiagnostics = $diagnostics.PSUseCompatibleTypes
96+ $syntaxDiagnostics = $diagnostics.PSUseCompatibleSyntax
97+ }
98+
99+ It " Finds the problem with command <Command> on line <Line> in the file" - TestCases $script :expectedCommandDiagnostics {
100+ param ([string ]$Command , [string ]$Parameter , [int ]$Line )
101+
102+ $actualDiagnostic = $commandDiagnostics | Where-Object { $_.Command -eq $Command -and $_.Line -eq $Line }
103+ $actualDiagnostic.Command | Should - BeExactly $Command
104+ $actualDiagnostic.Line | Should - Be $Line
105+ if ($Parameter )
106+ {
107+ $actualDiagnostic.Parameter | Should - Be $Parameter
108+ }
109+ }
110+
111+ It " Finds the problem with type <Type> on line <Line> in the file" - TestCases $script :expectedTypeDiagnostics {
112+ param ([string ]$Type , [string ]$Member , [int ]$Line )
113+
114+ $actualDiagnostic = $typeDiagnostics | Where-Object { $_.Type -eq $Type -and $_.Line -eq $Line }
115+ $actualDiagnostic.Type | Should - BeExactly $Type
116+ $actualDiagnostic.Line | Should - Be $Line
117+ if ($Member )
118+ {
119+ $actualDiagnostic.Member | Should - Be $Member
120+ }
121+ }
122+
123+ It " Finds the problem with syntax on line <Line> in the file" - TestCases $script :expectedSyntaxDiagnostics {
124+ param ([string ]$Suggestion , [int ]$Line )
125+
126+ $actualDiagnostic = $syntaxDiagnostics | Where-Object { $_.Line -eq $Line }
127+ $actualDiagnostic.Line | Should - Be $Line
128+ if ($Suggestion )
129+ {
130+ $actualDiagnostic.Suggestion | Should - Be $Suggestion
131+ }
132+ }
133+ }
0 commit comments