Fix how processor architecture is validated in Import-Module (#24265) (#24317)

This commit is contained in:
Aditya Patwardhan
2024-09-20 12:42:16 -07:00
committed by GitHub
parent ea39b567a5
commit 95850df6e9
2 changed files with 18 additions and 4 deletions
@@ -16,6 +16,7 @@ using System.Management.Automation.Language;
using System.Management.Automation.Runspaces;
using System.Management.Automation.Security;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml;
using System.Diagnostics;
@@ -1920,11 +1921,12 @@ namespace Microsoft.PowerShell.Commands
else if ((requiredProcessorArchitecture != ProcessorArchitecture.None) &&
(requiredProcessorArchitecture != ProcessorArchitecture.MSIL))
{
#pragma warning disable SYSLIB0037
ProcessorArchitecture currentArchitecture = typeof(object).Assembly.GetName().ProcessorArchitecture;
#pragma warning restore SYSLIB0037
Architecture currentArchitecture = RuntimeInformation.ProcessArchitecture;
if (currentArchitecture != requiredProcessorArchitecture)
if ((requiredProcessorArchitecture == ProcessorArchitecture.X86 && currentArchitecture != Architecture.X86) ||
(requiredProcessorArchitecture == ProcessorArchitecture.Amd64 && currentArchitecture != Architecture.X64) ||
(requiredProcessorArchitecture == ProcessorArchitecture.Arm && (currentArchitecture != Architecture.Arm && currentArchitecture != Architecture.Arm64)) ||
requiredProcessorArchitecture == ProcessorArchitecture.IA64)
{
containedErrors = true;
if (writingErrors)
@@ -56,6 +56,18 @@ Describe "Import-Module" -Tags "CI" {
Import-Module TestModule -RequiredVersion 1.1
(Get-Module TestModule).Version | Should -BeIn "1.1"
}
It 'ProcessorArchitecture should work' {
$currentProcessorArchitecture = switch ([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture) {
'X86' { 'x86' }
'X64' { 'amd64' }
'Arm64' { 'arm' }
default { throw "Unknown processor architecture" }
}
New-ModuleManifest -Path "$TestDrive\TestModule.psd1" -ProcessorArchitecture $currentProcessorArchitecture
$module = Import-Module -Name "$TestDrive\TestModule.psd1" -PassThru
$module.ProcessorArchitecture | Should -Be $currentProcessorArchitecture
}
}
Describe "Import-Module with ScriptsToProcess" -Tags "CI" {