Tests/TestHelpers/MSFT_Archive.TestHelper.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
$errorActionPreference = 'Stop'
Set-StrictMode -Version 'Latest'

Add-Type -AssemblyName 'System.IO.Compression.FileSystem'

<#
    .SYNOPSIS
        Converts the specified hashtable to a file structure under the specified parent path.
 
    .PARAMETER ParentPath
        The path to the directory that should contain the specified file structure.
 
    .PARAMETER FileStructure
        The hashtable defining the file structure.
        Nested hashtable values denote directories with the key as the name of the directory and the
        hashtable value as the contents.
        String values denote files with the key as the name of the file and the value as the contents.
 
    .EXAMPLE
        $fileStructure = @{
            DirectoryName = @{
                FileName = 'File contents'
            }
        }
 
        ConvertTo-FileStructure -ParentPath $env:temp -FileStructure $fileStructure
#>

function ConvertTo-FileStructure
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $ParentPath,

        [Parameter(Mandatory = $true)]
        [ValidateNotNull()]
        [Hashtable]
        $FileStructure
    )

    foreach ($itemName in $FileStructure.Keys)
    {
        if ($FileStructure[$itemName] -is [Hashtable])
        {
            $newDirectoryPath = Join-Path -Path $ParentPath -ChildPath $itemName
            $null = New-Item -Path $newDirectoryPath -ItemType 'Directory'
            ConvertTo-FileStructure -ParentPath $newDirectoryPath -FileStructure $FileStructure[$itemName]
        }
        elseif ($FileStructure[$itemName] -is [String])
        {
            $newFilePath = Join-Path -Path $ParentPath -ChildPath $itemName
            $null = New-Item -Path $newFilePath -ItemType 'File'
            Set-Content -LiteralPath $newFilePath -Value $FileStructure[$itemName]
        }
        else
        {
            throw 'Zip file structure must be made of strings and hashtable values. Found a different type.'
        }
    }
}

<#
    .SYNOPSIS
        Creates a new zip file with the specified name and file structure under the specified parent path.
        Returns the file path to the compressed zip file.
     
    .PARAMETER ParentPath
        The path under which the new zip file should be created.
 
    .PARAMETER Name
        The name of the zip file to create.
 
    .PARAMETER ZipFileStructure
        The hashtable defining the file structure.
        Nested hashtable values denote directories with the key as the name of the directory and the
        hashtable value as the contents.
        String values denote files with the key as the name of the file and the value as the contents.
     
    .EXAMPLE
        $zipFileStructure = @{
            DirectoryName = @{
                FileName = 'File contents'
            }
        }
 
        New-ZipFileFromHashtable -ParentPath $env:temp -Name 'ArchiveName' -ZipFileStructure $zipFileStructure
 
    .NOTES
        The expanded file structure that the zip file is created from is not removed as part of this function.
        This is to allow tests to compare the structure of the 'zip file' against another file structure.
#>

function New-ZipFileFromHashtable
{
    [OutputType([String])]
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $ParentPath,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Name,

        [Parameter(Mandatory = $true)]
        [ValidateNotNull()]
        [Hashtable]
        $ZipFileStructure
    )

    $expandedZipPath = Join-Path -Path $ParentPath -ChildPath $Name
    $compressedZipPath = Join-Path -Path $ParentPath -ChildPath "$Name.zip"

    $null = New-Item -Path $expandedZipPath -ItemType 'Directory'
    ConvertTo-FileStructure -ParentPath $expandedZipPath -FileStructure $ZipFileStructure

    $null = [System.IO.Compression.ZipFile]::CreateFromDirectory($expandedZipPath, $compressedZipPath, 'NoCompression', $false)

    return $compressedZipPath
}

<#
    .SYNOPSIS
        Tests if the two specified file structures match.
 
    .PARAMETER SourcePath
        The path to the root of the file structure to test against.
 
    .PARAMETER DestinationPath
        The path to the root of the file structure to test
 
    .PARAMETER CheckLastWriteTime
        Indicates whether or not to test that the last write times of the files in the structure match.
 
    .PARAMETER CheckCreationTime
        Indicates whether or not to test that the creation times of the files in the structure match.
 
    .PARAMETER CheckContents
        Indicates whether or not to test that the contents of the files in the structure match.
#>

function Test-FileStructuresMatch
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String] $SourcePath,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String] $DestinationPath,

        [Parameter()]
        [Switch] $CheckLastWriteTime,

        [Parameter()]
        [Switch] $CheckCreationTime,

        [Parameter()]
        [Switch] $CheckContents
    )

    $sourcePathLength = $SourcePath.Length
    $destinationPathLength = $DestinationPath.Length

    $destinationContents = @{}
    $destinationChildItems = Get-ChildItem -Path $DestinationPath -Recurse

    foreach ($destinationChildItem in $destinationChildItems)
    {
        $destinationChildItemName = Split-Path -Path $destinationChildItem.FullName -Leaf
        $destinationContents[$destinationChildItemName] = $destinationChildItem
    }

    $sourceChildItems = Get-ChildItem -Path $SourcePath -Recurse

    foreach ($sourceChildItem in $sourceChildItems)
    {
        $sourceChildItemName = Split-Path -Path $sourceChildItem.FullName -Leaf
        $destinationChildItem = $destinationContents[$sourceChildItemName]

        if ($destinationChildItem -eq $null)
        {
            return $false
        }
        else
        {
            if (-not $destinationChildItem.GetType() -eq $sourceChildItem.GetType())
            {
                return $false
            }

            if ($destinationChildItem.GetType() -eq [System.IO.FileInfo])
            {
                if ($CheckLastWriteTime)
                {
                    if ($sourceChildItem.LastWriteTime -ne $destinationChildItem.LastWriteTime)
                    {
                        return $false
                    }
                }

                if ($CheckCreationTime)
                {
                    if ($sourceChildItem.CreationTime -ne $destinationChildItem.CreationTime)
                    {
                        return $false
                    }
                }

                if ($CheckContents)
                {
                    $sourceFileContents = Get-Content -Path $sourceChildItem.FullName -Raw
                    $destinationFileContents = Get-Content -Path $destinationChildItem.FullName -Raw

                    if ($sourceFileContents -ne $destinationFileContents)
                    {
                        return $false
                    }
                }
            }
        }
    }

    return $true
}

Export-ModuleMember -Function @( 'New-ZipFileFromHashtable', 'Test-FileStructuresMatch' )