Skip to content

Commit aa52cfd

Browse files
committed
Move test logic to a new Assertion
1 parent 5c56021 commit aa52cfd

File tree

2 files changed

+131
-29
lines changed

2 files changed

+131
-29
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
function Should-BeZipArchiveWithUnixPermissions {
2+
<#
3+
.SYNOPSIS
4+
Checks if a zip archive contains entries with the expected Unix permissions
5+
.EXAMPLE
6+
"C:\Users\<user>\archive.zip" | Should -BeZipArchiveWithUnixPermissions "d---------" "-rw-------"
7+
8+
Checks if archive.zip only contains file1.txt
9+
#>
10+
11+
[CmdletBinding()]
12+
Param (
13+
[string] $ActualValue,
14+
[string] $TempDirectory,
15+
[string] $ExpectedDirectoryPermissions,
16+
[string] $ExpectedFilePermissions,
17+
[string] $Because,
18+
[switch] $LiteralPath,
19+
$CallerSessionState
20+
)n
21+
22+
$previousAliasDef = (Get-Alias ls).Definition
23+
Remove-Alias ls
24+
25+
try {
26+
# ActualValue is supposed to be a path to an archive
27+
# It could be a path to a custom PSDrive, so it needes to be converted
28+
if ($LiteralPath) {
29+
$ActualValue = Convert-Path -LiteralPath $ActualValue
30+
}
31+
else {
32+
$ActualValue = Convert-Path -Path $ActualValue
33+
}
34+
35+
36+
# Ensure ActualValue is a valid path
37+
if ($LiteralPath) {
38+
$testPathResult = Test-Path -LiteralPath $ActualValue
39+
}
40+
else {
41+
$testPathResult = Test-Path -Path $ActualValue
42+
}
43+
44+
# Don't continue processing if ActualValue is not an actual path
45+
if (-not $testPathResult) {
46+
return [pscustomobject]@{
47+
Succeeded = $false
48+
FailureMessage = $failureMessage
49+
}
50+
}
51+
52+
$unzipPath = "$TempDirectory\unzipped"
53+
54+
unzip $ActualValue -d $unzipPath
55+
56+
# Get ls to list the unzipped contents of the archive with permissions
57+
$output = ls -Rl $unzipPath
58+
59+
# Check if the output is null
60+
if ($null -eq $output) {
61+
return [pscustomobject]@{
62+
Succeeded = $false
63+
FailureMessage = "Archive {0} contains nothing, but it was expected to contain something"
64+
}
65+
}
66+
67+
# Filter the output line by line
68+
$lines = $output -split [System.Environment]::NewLine
69+
70+
# Go through each line and split it by whitespace
71+
foreach ($line in $lines) {
72+
73+
#Skip non-file/directory lines from recursive output
74+
#eg. directory path and total blocks count
75+
#./src/obj/Release/ref:
76+
#total 12
77+
if (-not $line.StartsWith("-") -and -not $line.StartsWith("d")) {
78+
continue;
79+
}
80+
81+
$lineComponents = $line -split " +"
82+
83+
# Example of some lines:
84+
#-rw-r--r-- 1 owner group 26112 Mar 22 00:36 Microsoft.PowerShell.Archive.dll
85+
#drwxr-xr-x 2 owner group 4096 Mar 22 00:19 ref
86+
87+
# First component contains attributes
88+
# 2nd component is link count
89+
# 3rd componnent is owner
90+
# 4th component is group
91+
# 5th component is file size
92+
# 6th component is last modified month
93+
# 7th component is last modified day
94+
# 8th component is last modified time
95+
# 9th component is file name
96+
97+
$permissionString = $lineComponents[0];
98+
99+
100+
if ($permissionString[0] -eq 'd') {
101+
if ($permissionString -ne $expectedDirectoryPermissions) {
102+
return [pscustomobject]@{
103+
Succeeded = $false
104+
FailureMessage = "Expected directory permissions '$expectedDirectoryPermissions' but got '$permissionString'"
105+
}
106+
}
107+
}
108+
else {
109+
if ($permissionString -ne $expectedFilePermissions) {
110+
return [pscustomobject]@{
111+
Succeeded = $false
112+
FailureMessage = "Expected directory permissions '$expectedFilePermissions' but got '$permissionString'"
113+
}
114+
}
115+
}
116+
}
117+
118+
119+
$ObjProperties = @{
120+
Succeeded = $true
121+
}
122+
return New-Object PSObject -Property $ObjProperties
123+
}
124+
finally {
125+
Set-Alias ls $previousAliasDef
126+
}
127+
}
128+
129+
Add-ShouldOperator -Name BeZipArchiveWithUnixPermissions -InternalName 'Should-BeZipArchiveWithUnixPermissions' -Test ${function:Should-BeZipArchiveWithUnixPermissions}

Tests/Compress-Archive.Tests.ps1

+2-29
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
BeforeDiscovery {
55
# Loads and registers custom assertion. Ignores usage of unapproved verb with -DisableNameChecking
66
Import-Module "$PSScriptRoot/Assertions/Should-BeZipArchiveOnlyContaining.psm1" -DisableNameChecking
7+
Import-Module "$PSScriptRoot/Assertions/Should-BeZipArchiveWithUnixPermissions.psm1" -DisableNameChecking
78
}
89

910
Describe("Microsoft.PowerShell.Archive tests") {
@@ -304,39 +305,11 @@ BeforeDiscovery {
304305
$sourcePath = "TestDrive:/SourceDir"
305306
$sourcePathAbsolute = "$testDriveRoot/SourceDir"
306307
$destinationPath = "TestDrive:/archive4.zip"
307-
$destinationPathAbsolute = "$testDriveRoot/archive4.zip"
308-
$unzipPathAbsolute = "$testDriveRoot/test-unzip"
309-
310-
$expectedDirectoryPermissions = "d---------"
311-
$expectedFilePermissions = "-rw-------"
312308

313309
chmod -R u+rw "$sourcePathAbsolute"
314310
Compress-Archive -Path $sourcePath -DestinationPath $destinationPath
315311
$destinationPath | Should -BeZipArchiveOnlyContaining @('SourceDir/', 'SourceDir/ChildDir-1/', 'SourceDir/ChildDir-2/', 'SourceDir/ChildEmptyDir/', 'SourceDir/Sample-1.txt', 'SourceDir/ChildDir-1/Sample-2.txt', 'SourceDir/ChildDir-2/Sample-3.txt')
316-
unzip $destinationPathAbsolute -d $unzipPathAbsolute -v
317-
318-
$lsOutput = ls -Rl $unzipPathAbsolute
319-
$lines = $lsOutput -split [System.Environment]::NewLine
320-
321-
foreach ($line in $lines) {
322-
if (-not $line.StartsWith("-") -and -not $line.StartsWith("d")) {
323-
continue;
324-
}
325-
326-
$lineComponents = $line -split " +"
327-
328-
$permissionString = $lineComponents[0];
329-
if ($permissionString[0] -eq 'd') {
330-
if ($permissionString -ne $expectedDirectoryPermissions) {
331-
throw "Expected directory permissions '$expectedDirectoryPermissions' but got '$permissionString'"
332-
}
333-
}
334-
else {
335-
if ($permissionString -ne $expectedFilePermissions) {
336-
throw "Expected file permissions '$expectedFilePermissions' but got '$permissionString'"
337-
}
338-
}
339-
}
312+
$destinationPath | Should -BeZipArchiveOnlyContaining $testDriveRoot "d---------" "-rw-------"
340313
}
341314
}
342315

0 commit comments

Comments
 (0)