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}
0 commit comments