11# Copyright (c) Microsoft Corporation. All rights reserved.
22# Licensed under the MIT License.
3-
3+ # Tests for UseConstrainedLanguageMode rule
4+ #
5+ # Some tests are Windows-specific (COM objects, XAML) and will be skipped on non-Windows platforms.
6+
7+ BeforeDiscovery {
8+ # Detect OS for platform-specific tests
9+ $script :IsWindowsOS = $true
10+ $script :IsLinuxOS = $false
11+ $script :IsMacOSOS = $false
12+
13+ if ($PSVersionTable.PSVersion.Major -ge 6 ) {
14+ # PowerShell Core has built-in OS detection variables
15+ $script :IsWindowsOS = $IsWindows
16+ $script :IsLinuxOS = $IsLinux
17+ $script :IsMacOSOS = $IsMacOS
18+ }
19+ }
420BeforeAll {
521 $testRootDirectory = Split-Path - Parent $PSScriptRoot
622 Import-Module (Join-Path $testRootDirectory " PSScriptAnalyzerTestHelper.psm1" )
@@ -20,7 +36,7 @@ BeforeAll {
2036}
2137
2238Describe " UseConstrainedLanguageMode" {
23- Context " When Add-Type is used" {
39+ Context " When Add-Type is used" {
2440 It " Should detect Add-Type usage" {
2541 $def = @'
2642Add-Type -TypeDefinition @"
@@ -43,27 +59,27 @@ Add-Type -TypeDefinition @"
4359 }
4460
4561 Context " When New-Object with COM is used" {
46- It " Should detect disallowed New-Object -ComObject usage" {
62+ It " Should detect disallowed New-Object -ComObject usage" - Skip:( -not $ script :IsWindowsOS ) {
4763 $def = ' New-Object -ComObject "Excel.Application"'
4864 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
4965 $matchingViolations = $violations | Where-Object { $_.RuleName -eq $violationName }
5066 $matchingViolations.Count | Should - Be 1
5167 $matchingViolations [0 ].Message | Should - BeLike " *COM object*"
5268 }
5369
54- It " Should NOT flag allowed COM objects - Scripting.Dictionary" {
70+ It " Should NOT flag allowed COM objects - Scripting.Dictionary" - Skip:( -not $ script :IsWindowsOS ) {
5571 $def = ' New-Object -ComObject "Scripting.Dictionary"'
5672 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
5773 $violations | Where-Object { $_.RuleName -eq $violationName } | Should - BeNullOrEmpty
5874 }
5975
60- It " Should NOT flag allowed COM objects - Scripting.FileSystemObject" {
76+ It " Should NOT flag allowed COM objects - Scripting.FileSystemObject" - Skip:( -not $ script :IsWindowsOS ) {
6177 $def = ' New-Object -ComObject "Scripting.FileSystemObject"'
6278 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
6379 $violations | Where-Object { $_.RuleName -eq $violationName } | Should - BeNullOrEmpty
6480 }
6581
66- It " Should NOT flag allowed COM objects - VBScript.RegExp" {
82+ It " Should NOT flag allowed COM objects - VBScript.RegExp" - Skip:( -not $ script :IsWindowsOS ) {
6783 $def = ' New-Object -ComObject "VBScript.RegExp"'
6884 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
6985 $violations | Where-Object { $_.RuleName -eq $violationName } | Should - BeNullOrEmpty
@@ -85,7 +101,7 @@ Add-Type -TypeDefinition @"
85101 }
86102
87103 Context " When XAML is used" {
88- It " Should detect XAML usage" {
104+ It " Should detect XAML usage" - Skip:( -not $ script :IsWindowsOS ) {
89105 $def = @'
90106$xaml = @"
91107<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
0 commit comments