MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe "Verify macOS Package" {5 BeforeAll {6 Write-Verbose "In Describe BeforeAll" -Verbose7 Import-Module $PSScriptRoot/../../../build.psm18 9 # Find the macOS package10 $packagePath = $env:PACKAGE_FOLDER11 if (-not $packagePath) {12 $packagePath = Get-Location13 }14 15 Write-Verbose "Looking for package in: $packagePath" -Verbose16 $package = Get-ChildItem -Path $packagePath -Filter "*.pkg" -ErrorAction SilentlyContinue | Select-Object -First 117 18 if (-not $package) {19 Write-Warning "No .pkg file found in $packagePath"20 } else {21 Write-Verbose "Found package: $($package.FullName)" -Verbose22 }23 24 # Set up test directories25 $script:package = $package26 $script:expandDir = $null27 $script:payloadDir = $null28 $script:extractedFiles = @()29 30 if ($package) {31 # Use TestDrive for temporary directories - pkgutil will create the expand directory32 $script:expandDir = Join-Path "TestDrive:" -ChildPath "package-contents-test"33 $expandDirResolved = (Resolve-Path "TestDrive:").ProviderPath34 $script:expandDir = Join-Path $expandDirResolved -ChildPath "package-contents-test"35 36 Write-Verbose "Expanding package to: $($script:expandDir)" -Verbose37 # pkgutil will create the directory itself, so don't pre-create it38 Start-NativeExecution {39 pkgutil --expand $package.FullName $script:expandDir40 }41 42 # Extract the payload to verify files43 $script:payloadDir = Join-Path "TestDrive:" -ChildPath "package-payload-test"44 $payloadDirResolved = (Resolve-Path "TestDrive:").ProviderPath45 $script:payloadDir = Join-Path $payloadDirResolved -ChildPath "package-payload-test"46 47 # Create payload directory since cpio needs it48 if (-not (Test-Path $script:payloadDir)) {49 $null = New-Item -ItemType Directory -Path $script:payloadDir -Force50 }51 52 $componentPkg = Get-ChildItem -Path $script:expandDir -Filter "*.pkg" -Recurse | Select-Object -First 153 if ($componentPkg) {54 Write-Verbose "Extracting payload from: $($componentPkg.FullName)" -Verbose55 Push-Location $script:payloadDir56 try {57 $payloadFile = Join-Path $componentPkg.FullName "Payload"58 Get-Content -Path $payloadFile -Raw -AsByteStream | & cpio -i 2>&1 | Out-Null59 } finally {60 Pop-Location61 }62 }63 64 # Get all extracted files for verification65 $script:extractedFiles = Get-ChildItem -Path $script:payloadDir -Recurse -ErrorAction SilentlyContinue66 Write-Verbose "Extracted $($script:extractedFiles.Count) files" -Verbose67 }68 }69 70 AfterAll {71 # TestDrive automatically cleans up, but we can ensure cleanup happens72 # No manual cleanup needed as TestDrive handles it73 }74 75 Context "Package existence and structure" {76 It "Package file should exist" {77 $script:package | Should -Not -BeNullOrEmpty -Because "A .pkg file should be created"78 $script:package.Extension | Should -Be ".pkg"79 }80 81 It "Package name should follow correct naming convention" {82 $script:package | Should -Not -BeNullOrEmpty83 84 # Regex pattern for valid macOS PKG package names.85 # This pattern matches the validation used in release-validate-packagenames.yml86 # Valid examples:87 # - powershell-7.4.13-osx-x64.pkg (Stable release)88 # - powershell-7.6.0-preview.6-osx-x64.pkg (Preview version string)89 # - powershell-7.4.13-rebuild.5-osx-arm64.pkg (Rebuild version)90 # - powershell-lts-7.4.13-osx-arm64.pkg (LTS package)91 $pkgPackageNamePattern = '^powershell-(lts-)?\d+\.\d+\.\d+\-([a-z]*.\d+\-)?osx\-(x64|arm64)\.pkg$'92 93 $script:package.Name | Should -Match $pkgPackageNamePattern -Because "Package name should follow the standard naming convention"94 }95 96 It "Package name should NOT use x86_64 with underscores" {97 $script:package | Should -Not -BeNullOrEmpty98 99 $script:package.Name | Should -Not -Match 'x86_64' -Because "Package should use 'x64' not 'x86_64' (with underscores) for compatibility"100 }101 102 It "Package should expand successfully" {103 $script:expandDir | Should -Exist104 Get-ChildItem -Path $script:expandDir | Should -Not -BeNullOrEmpty105 }106 107 It "Package should have a component package" {108 $componentPkg = Get-ChildItem -Path $script:expandDir -Filter "*.pkg" -Recurse -ErrorAction SilentlyContinue109 $componentPkg | Should -Not -BeNullOrEmpty -Because "Package should contain a component.pkg"110 }111 112 It "Payload should extract successfully" {113 $script:payloadDir | Should -Exist114 $script:extractedFiles | Should -Not -BeNullOrEmpty -Because "Package payload should contain files"115 }116 }117 118 Context "Required files in package" {119 BeforeAll {120 $expectedFilePatterns = @{121 "PowerShell executable" = "usr/local/microsoft/powershell/*/pwsh"122 "PowerShell symlink in /usr/local/bin" = "usr/local/bin/pwsh*"123 "Man page" = "usr/local/share/man/man1/pwsh*.gz"124 "Launcher application plist" = "Applications/PowerShell*.app/Contents/Info.plist"125 }126 127 $testCases = @()128 foreach ($key in $expectedFilePatterns.Keys) {129 $testCases += @{130 Description = $key131 Pattern = $expectedFilePatterns[$key]132 }133 }134 135 $script:testCases = $testCases136 }137 138 It "Should contain <Description>" -TestCases $script:testCases {139 param($Description, $Pattern)140 141 $found = $script:extractedFiles | Where-Object { $_.FullName -like "*$Pattern*" }142 $found | Should -Not -BeNullOrEmpty -Because "$Description should exist in the package at path matching '$Pattern'"143 }144 }145 146 Context "PowerShell binary verification" {147 It "PowerShell executable should be executable" {148 $pwshBinary = $script:extractedFiles | Where-Object { $_.FullName -like "*/pwsh" -and $_.FullName -like "*/microsoft/powershell/*" }149 $pwshBinary | Should -Not -BeNullOrEmpty150 151 # Check if file has executable permissions (on Unix-like systems)152 if ($IsLinux -or $IsMacOS) {153 $permissions = (Get-Item $pwshBinary[0].FullName).UnixFileMode154 # Executable bit should be set155 $permissions.ToString() | Should -Match 'x' -Because "pwsh binary should have execute permissions"156 }157 }158 }159 160 Context "Launcher application" {161 It "Launcher app should have proper bundle structure" {162 $plistFile = $script:extractedFiles | Where-Object { $_.FullName -like "*PowerShell*.app/Contents/Info.plist" }163 $plistFile | Should -Not -BeNullOrEmpty164 165 # Verify the bundle has required components166 $appPath = Split-Path (Split-Path $plistFile[0].FullName -Parent) -Parent167 $macOSDir = Join-Path $appPath "Contents/MacOS"168 $resourcesDir = Join-Path $appPath "Contents/Resources"169 170 Test-Path $macOSDir | Should -Be $true -Because "App bundle should have Contents/MacOS directory"171 Test-Path $resourcesDir | Should -Be $true -Because "App bundle should have Contents/Resources directory"172 }173 174 It "Launcher script should exist and be executable" {175 $launcherScript = $script:extractedFiles | Where-Object {176 $_.FullName -like "*PowerShell*.app/Contents/MacOS/PowerShell.sh"177 }178 $launcherScript | Should -Not -BeNullOrEmpty -Because "Launcher script should exist"179 180 if ($IsLinux -or $IsMacOS) {181 $permissions = (Get-Item $launcherScript[0].FullName).UnixFileMode182 $permissions.ToString() | Should -Match 'x' -Because "Launcher script should have execute permissions"183 }184 }185 }186}187 