Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: PowerShell Archive Module Tests

on:
push:
branches: [ master, servicing ]
pull_request:
branches: [ master, servicing ]
workflow_dispatch:

jobs:
test:
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]

runs-on: ${{ matrix.os }}

name: Test on ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 1000

- name: Install Pester
shell: pwsh
run: |
if (!(Get-Module -ListAvailable -Name Pester | Where-Object {$_.Version -le "4.99" -and $_.Version -ge "4.0"})) {
Install-Module -Name Pester -Force -SkipPublisherCheck -MaximumVersion 4.99
}

Import-Module "Pester" -MaximumVersion "4.99"

- name: Run Tests
shell: pwsh
run: |
Get-Module "Pester"
$testResultsFile = "./ArchiveTestResults.xml"
Import-Module "./Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psd1" -Force
$testResults = Invoke-Pester -Script "./Tests" -OutputFormat NUnitXml -OutputFile $testResultsFile -PassThru
if ($testResults.FailedCount -gt 0) {
throw "$($testResults.FailedCount) tests failed."
}

- name: Upload test results
uses: actions/upload-artifact@v4
if: always()
with:
name: test-results-${{ matrix.os }}
path: ArchiveTestResults.xml
22 changes: 0 additions & 22 deletions .travis.yml

This file was deleted.

245 changes: 11 additions & 234 deletions Tests/Pester.Commands.Cmdlets.Archive.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
<############################################################################################
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

<############################################################################################
# File: Pester.Commands.Cmdlets.ArchiveTests.ps1
# Commands.Cmdlets.ArchiveTests suite contains Tests that are
# used for validating Microsoft.PowerShell.Archive module.
############################################################################################>
$script:TestSourceRoot = $PSScriptRoot
$modPath = "$psscriptroot/Pester.Commands.Cmdlets.Archive.Tests.psm1"
Import-Module $modPath -Force -Verbose

$DS = [System.IO.Path]::DirectorySeparatorChar
if ($IsWindows -eq $null) {
$IsWindows = $PSVersionTable.PSEdition -eq "Desktop"
}
Describe "Test suite for Microsoft.PowerShell.Archive module" -Tags "BVT" {

BeforeAll {
$originalProgressPref = $ProgressPreference
$ProgressPreference = "SilentlyContinue"
$originalPSModulePath = $env:PSModulePath
$testSourceRoot = $PSScriptRoot
# make sure we use the one in this repo
$env:PSModulePath = "$($script:TestSourceRoot)\..;$($env:PSModulePath)"
$env:PSModulePath = "$($testSourceRoot)\..;$($env:PSModulePath)"

New-Item $TestDrive$($DS)SourceDir -Type Directory | Out-Null
New-Item $TestDrive$($DS)SourceDir$($DS)ChildDir-1 -Type Directory | Out-Null
Expand All @@ -33,10 +36,10 @@ Describe "Test suite for Microsoft.PowerShell.Archive module" -Tags "BVT" {
"Some Text" > $TestDrive$($DS)Sample.unzip
"Some Text" > $TestDrive$($DS)Sample.cab

$preCreatedArchivePath = Join-Path $script:TestSourceRoot "SamplePreCreatedArchive.archive"
$preCreatedArchivePath = Join-Path $testSourceRoot "SamplePreCreatedArchive.archive"
Copy-Item $preCreatedArchivePath $TestDrive$($DS)SamplePreCreatedArchive.zip -Force

$preCreatedArchivePath = Join-Path $script:TestSourceRoot "TrailingSpacer.archive"
$preCreatedArchivePath = Join-Path $testSourceRoot "TrailingSpacer.archive"
Copy-Item $preCreatedArchivePath $TestDrive$($DS)TrailingSpacer.zip -Force
}

Expand All @@ -45,232 +48,6 @@ Describe "Test suite for Microsoft.PowerShell.Archive module" -Tags "BVT" {
$env:PSModulePath = $originalPSModulePath
}

function Add-CompressionAssemblies {
Add-Type -AssemblyName System.IO.Compression
if ($psedition -eq "Core")
{
Add-Type -AssemblyName System.IO.Compression.ZipFile
}
else
{
Add-Type -AssemblyName System.IO.Compression.FileSystem
}
}

function CompressArchivePathParameterSetValidator {
param
(
[string[]] $path,
[string] $destinationPath,
[string] $compressionLevel = "Optimal"
)

try
{
Compress-Archive -Path $path -DestinationPath $destinationPath -CompressionLevel $compressionLevel
throw "ValidateNotNullOrEmpty attribute is missing on one of parameters belonging to Path parameterset."
}
catch
{
$_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationError,Compress-Archive"
}
}

function CompressArchiveLiteralPathParameterSetValidator {
param
(
[string[]] $literalPath,
[string] $destinationPath,
[string] $compressionLevel = "Optimal"
)

try
{
Compress-Archive -LiteralPath $literalPath -DestinationPath $destinationPath -CompressionLevel $compressionLevel
throw "ValidateNotNullOrEmpty attribute is missing on one of parameters belonging to LiteralPath parameterset."
}
catch
{
$_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationError,Compress-Archive"
}
}


function CompressArchiveInValidPathValidator {
param
(
[string[]] $path,
[string] $destinationPath,
[string] $invalidPath,
[string] $expectedFullyQualifiedErrorId
)

try
{
Compress-Archive -Path $path -DestinationPath $destinationPath
throw "Failed to validate that an invalid Path $invalidPath was supplied as input to Compress-Archive cmdlet."
}
catch
{
$_.FullyQualifiedErrorId | Should Be $expectedFullyQualifiedErrorId
}
}

function CompressArchiveInValidArchiveFileExtensionValidator {
param
(
[string[]] $path,
[string] $destinationPath,
[string] $invalidArchiveFileExtension
)

try
{
Compress-Archive -Path $path -DestinationPath $destinationPath
throw "Failed to validate that an invalid archive file format $invalidArchiveFileExtension was supplied as input to Compress-Archive cmdlet."
}
catch
{
$_.FullyQualifiedErrorId | Should Be "NotSupportedArchiveFileExtension,Compress-Archive"
}
}

function Validate-ArchiveEntryCount {
param
(
[string] $path,
[int] $expectedEntryCount
)

Add-CompressionAssemblies
try
{
$archiveFileStreamArgs = @($path, [System.IO.FileMode]::Open)
$archiveFileStream = New-Object -TypeName System.IO.FileStream -ArgumentList $archiveFileStreamArgs

$zipArchiveArgs = @($archiveFileStream, [System.IO.Compression.ZipArchiveMode]::Read, $false)
$zipArchive = New-Object -TypeName System.IO.Compression.ZipArchive -ArgumentList $zipArchiveArgs

$actualEntryCount = $zipArchive.Entries.Count
$actualEntryCount | Should Be $expectedEntryCount
}
finally
{
if ($null -ne $zipArchive) { $zipArchive.Dispose()}
if ($null -ne $archiveFileStream) { $archiveFileStream.Dispose() }
}
}

function ArchiveFileEntryContentValidator {
param
(
[string] $path,
[string] $entryFileName,
[string] $expectedEntryFileContent
)

Add-CompressionAssemblies
try
{
$destFile = "$TestDrive$($DS)ExpandedFile"+([System.Guid]::NewGuid().ToString())+".txt"

$archiveFileStreamArgs = @($path, [System.IO.FileMode]::Open)
$archiveFileStream = New-Object -TypeName System.IO.FileStream -ArgumentList $archiveFileStreamArgs

$zipArchiveArgs = @($archiveFileStream, [System.IO.Compression.ZipArchiveMode]::Read, $false)
$zipArchive = New-Object -TypeName System.IO.Compression.ZipArchive -ArgumentList $zipArchiveArgs

$entryToBeUpdated = $zipArchive.Entries | ? {$_.FullName -eq $entryFileName.replace([System.IO.Path]::DirectorySeparatorChar, [System.IO.Path]::AltDirectorySeparatorChar)}

if($entryToBeUpdated -ne $null)
{
$srcStream = $entryToBeUpdated.Open()
$destStream = New-Object "System.IO.FileStream" -ArgumentList( $destFile, [System.IO.FileMode]::Create )
$srcStream.CopyTo( $destStream )
$destStream.Dispose()
$srcStream.Dispose()
Get-Content $destFile | Should Be $expectedEntryFileContent
}
else
{
throw "Failed to find the file $entryFileName in the archive file $path"
}
}
finally
{
if ($zipArchive)
{
$zipArchive.Dispose()
}
if ($archiveFileStream)
{
$archiveFileStream.Dispose()
}
}
}

function ArchiveFileEntrySeparatorValidator {
param
(
[string] $path
)

Add-CompressionAssemblies
try
{
$destFile = "$TestDrive$($DS)ExpandedFile"+([System.Guid]::NewGuid().ToString())+".txt"

$archiveFileStreamArgs = @($path, [System.IO.FileMode]::Open)
$archiveFileStream = New-Object -TypeName System.IO.FileStream -ArgumentList $archiveFileStreamArgs

$zipArchiveArgs = @($archiveFileStream, [System.IO.Compression.ZipArchiveMode]::Read, $false)
$zipArchive = New-Object -TypeName System.IO.Compression.ZipArchive -ArgumentList $zipArchiveArgs

$badEntries = $zipArchive.Entries | Where-Object {$_.FullName.Contains('\')}

$badEntries.Count | Should Be 0
}
finally
{
if ($zipArchive)
{
$zipArchive.Dispose()
}
if ($archiveFileStream)
{
$archiveFileStream.Dispose()
}
}
}

function ExpandArchiveInvalidParameterValidator {
param
(
[boolean] $isLiteralPathParameterSet,
[string[]] $path,
[string] $destinationPath,
[string] $expectedFullyQualifiedErrorId
)

try
{
if($isLiteralPathParameterSet)
{
Expand-Archive -LiteralPath $literalPath -DestinationPath $destinationPath
}
else
{
Expand-Archive -Path $path -DestinationPath $destinationPath
}

throw "Expand-Archive did NOT throw expected error"
}
catch
{
$_.FullyQualifiedErrorId | Should Be $expectedFullyQualifiedErrorId
}
}

Context "Compress-Archive - Parameter validation test cases" {

It "Validate errors from Compress-Archive with NULL & EMPTY values for Path, LiteralPath, DestinationPath, CompressionLevel parameters" {
Expand Down
Loading