|
1 | 1 | # Copyright (c) Microsoft Corporation. |
2 | 2 | # Licensed under the MIT License. |
3 | 3 |
|
| 4 | +BeforeDiscovery { |
| 5 | + # Loads and registers custom assertion. Ignores usage of unapproved verb with -DisableNameChecking |
| 6 | + Import-Module "$PSScriptRoot/Assertions/Should-BeZipArchiveOnlyContaining.psm1" -DisableNameChecking |
| 7 | +} |
| 8 | + |
4 | 9 | Describe("Microsoft.PowerShell.Archive tests") { |
5 | 10 | BeforeAll { |
6 | 11 |
|
7 | 12 | $originalProgressPref = $ProgressPreference |
8 | 13 | $ProgressPreference = "SilentlyContinue" |
9 | 14 | $originalPSModulePath = $env:PSModulePath |
10 | | - |
11 | | - # Add compression assemblies |
12 | | - function Add-CompressionAssemblies { |
13 | | - Add-Type -AssemblyName System.IO.Compression |
14 | | - if ($psedition -eq "Core") |
15 | | - { |
16 | | - Add-Type -AssemblyName System.IO.Compression.ZipFile |
17 | | - } |
18 | | - else |
19 | | - { |
20 | | - Add-Type -AssemblyName System.IO.Compression.FileSystem |
21 | | - } |
22 | | - } |
23 | | - |
24 | | - Add-CompressionAssemblies |
25 | | - |
26 | | - # Used for validating an archive's contents |
27 | | - function Test-ZipArchive { |
28 | | - param |
29 | | - ( |
30 | | - [string] $archivePath, |
31 | | - [string[]] $expectedEntries, |
32 | | - [switch] $Literal |
33 | | - ) |
34 | | - |
35 | | - try |
36 | | - { |
37 | | - if ($Literal) { |
38 | | - $archivePath = Convert-Path -LiteralPath $archivePath |
39 | | - } else { |
40 | | - $archivePath = Convert-Path -Path $archivePath |
41 | | - } |
42 | | - |
43 | | - |
44 | | - $archiveFileStreamArgs = @($archivePath, [System.IO.FileMode]::Open) |
45 | | - $archiveFileStream = New-Object -TypeName System.IO.FileStream -ArgumentList $archiveFileStreamArgs |
46 | | - |
47 | | - $zipArchiveArgs = @($archiveFileStream, [System.IO.Compression.ZipArchiveMode]::Read, $false) |
48 | | - $zipArchive = New-Object -TypeName System.IO.Compression.ZipArchive -ArgumentList $zipArchiveArgs |
49 | | - |
50 | | - $actualEntryCount = $zipArchive.Entries.Count |
51 | | - $actualEntryCount | Should -Be $expectedEntries.Length |
52 | | - |
53 | | - # Get a list of entry names in the zip archive |
54 | | - $archiveEntries = @() |
55 | | - ForEach ($archiveEntry in $zipArchive.Entries) { |
56 | | - $archiveEntries += $archiveEntry.FullName |
57 | | - } |
58 | | - |
59 | | - # Ensure each entry in the archive is in the list of expected entries |
60 | | - ForEach ($expectedEntry in $expectedEntries) { |
61 | | - $expectedEntry | Should -BeIn $archiveEntries |
62 | | - } |
63 | | - |
64 | | - } |
65 | | - finally |
66 | | - { |
67 | | - if ($null -ne $zipArchive) { $zipArchive.Dispose()} |
68 | | - if ($null -ne $archiveFileStream) { $archiveFileStream.Dispose() } |
69 | | - } |
70 | | - } |
71 | | - |
72 | | - # This function gets a list of a directories descendants formatted as archive entries |
73 | | - function Get-Descendants { |
74 | | - param ( |
75 | | - [string] $Path |
76 | | - ) |
77 | | - |
78 | | - |
79 | | - # Get the folder name |
80 | | - $folderName = Split-Path -Path $Path -Leaf |
81 | | - |
82 | | - # Get descendents |
83 | | - $descendants = Get-ChildItem -Path $Path -Recurse -Name |
84 | | - |
85 | | - $output = @() |
86 | | - |
87 | | - # Prefix each descendant name with folder name |
88 | | - foreach ($name in $descendants) { |
89 | | - $output += ($folderName + '/' + $name).Replace([System.IO.Path]::DirectorySeparatorChar, [System.IO.Path]::AltDirectorySeparatorChar) |
90 | | - } |
91 | | - |
92 | | - return $output |
93 | | - } |
94 | 15 | } |
95 | 16 |
|
96 | 17 | AfterAll { |
|
325 | 246 | } |
326 | 247 | } |
327 | 248 |
|
328 | | - It "-WriteMode Create works" -Tag this2 { |
| 249 | + It "-WriteMode Create works" -Tag td1 { |
329 | 250 | $sourcePath = "TestDrive:/SourceDir" |
330 | 251 | $destinationPath = "TestDrive:/archive1.zip" |
331 | 252 | Compress-Archive -Path $sourcePath -DestinationPath $destinationPath |
332 | | - Test-Path $destinationPath |
333 | | - Test-ZipArchive $destinationPath @('SourceDir/', 'SourceDir/Sample-1.txt') |
| 253 | + $destinationPath | Should -BeZipArchiveOnlyContaining @('SourceDir/', 'SourceDir/Sample-1.txt') |
334 | 254 | } |
335 | 255 | } |
336 | 256 |
|
|
354 | 274 | $sourcePath = "TestDrive:/SourceDir/ChildDir-1/Sample-2.txt" |
355 | 275 | $destinationPath = "TestDrive:/archive1.zip" |
356 | 276 | Compress-Archive -Path $sourcePath -DestinationPath $destinationPath |
357 | | - $destinationPath | Should -Exist |
358 | | - Test-ZipArchive $destinationPath @('Sample-2.txt') |
| 277 | + $destinationPath | Should -BeZipArchiveOnlyContaining @('Sample-2.txt') |
359 | 278 | } |
360 | 279 |
|
361 | 280 | It "Validate that an empty folder can be compressed" { |
362 | 281 | $sourcePath = "TestDrive:/EmptyDir" |
363 | 282 | $destinationPath = "TestDrive:/archive2.zip" |
364 | 283 | Compress-Archive -Path $sourcePath -DestinationPath $destinationPath |
365 | | - $destinationPath | Should -Exist |
366 | | - Test-ZipArchive $destinationPath @('EmptyDir/') |
| 284 | + $destinationPath | Should -BeZipArchiveOnlyContaining @('EmptyDir/') |
367 | 285 | } |
368 | 286 |
|
369 | 287 | It "Validate a folder containing files, non-empty folders, and empty folders can be compressed" { |
370 | 288 | $sourcePath = "TestDrive:/SourceDir" |
371 | 289 | $destinationPath = "TestDrive:/archive3.zip" |
372 | 290 | Compress-Archive -Path $sourcePath -DestinationPath $destinationPath |
373 | | - $destinationPath | Should -Exist |
374 | | - $contents = Get-Descendants -Path $sourcePath |
375 | | - $contents += "SourceDir/" |
376 | | - Test-ZipArchive $destinationPath $contents |
| 291 | + $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') |
377 | 292 | } |
378 | 293 | } |
379 | 294 |
|
|
515 | 430 | $sourcePath = "TestDrive:/SourceDir" |
516 | 431 | $destinationPath = "TestDrive:/EmptyDirectory" |
517 | 432 |
|
518 | | - (Get-Item $destinationPath) -is [System.IO.DirectoryInfo] | Should -Be $true |
| 433 | + # Ensure $destinationPath is a directory |
| 434 | + Test-Path $destinationPath -PathType Container | Should -Be $true |
| 435 | + |
519 | 436 | Compress-Archive -Path $sourcePath -DestinationPath $destinationPath -WriteMode Overwrite |
520 | 437 |
|
521 | | - # Ensure $destiationPath is now a file |
522 | | - $destinationPathInfo = Get-Item $destinationPath |
523 | | - $destinationPathInfo -is [System.IO.DirectoryInfo] | Should -Be $false |
524 | | - $destinationPathInfo -is [System.IO.FileInfo] | Should -Be $true |
| 438 | + # Ensure $destinationPath is now a file |
| 439 | + Test-Path $destinationPath -PathType Leaf | Should -Be $true |
525 | 440 | } |
526 | 441 |
|
527 | 442 | It "Overwrites an archive that already exists" { |
528 | 443 | $destinationPath = "TestDrive:/archive.zip" |
529 | 444 |
|
530 | | - # Get the entries of the original zip archive |
531 | | - Test-ZipArchive $destinationPath @("Sample-1.txt") |
| 445 | + # Ensure the original archive contains Sample-1.txt |
| 446 | + $destinationPath | Should -BeZipArchiveOnlyContaining @("Sample-1.txt") |
532 | 447 |
|
533 | 448 | # Overwrite the archive |
534 | 449 | $sourcePath = "TestDrive:/Sample-2.txt" |
535 | 450 | Compress-Archive -Path $sourcePath -DestinationPath "TestDrive:/archive.zip" -WriteMode Overwrite |
536 | 451 |
|
537 | 452 | # Ensure the original entries and different than the new entries |
538 | | - Test-ZipArchive $destinationPath @("Sample-2.txt") |
| 453 | + $destinationPath | Should -BeZipArchiveOnlyContaining @("Sample-2.txt") |
539 | 454 | } |
540 | 455 | } |
541 | 456 |
|
|
620 | 535 | $destinationPath = "TestDrive:/archive[2.zip" |
621 | 536 |
|
622 | 537 | Compress-Archive -Path $sourcePath -DestinationPath $destinationPath |
623 | | - Test-Path -LiteralPath $destinationPath | Should -Be $true |
624 | | - Test-ZipArchive $destinationPath @("SourceDir/", "SourceDir/Sample-1.txt") -Literal |
| 538 | + $destinationPath | Should -BeZipArchiveOnlyContaining @("SourceDir/", "SourceDir/Sample-1.txt") -LiteralPath |
625 | 539 | Remove-Item -LiteralPath $destinationPath |
626 | 540 | } |
627 | 541 | } |
| 542 | + |
| 543 | + Context "test" -Tag lol { |
| 544 | + BeforeAll { |
| 545 | + $content = "Some Data" |
| 546 | + $content | Out-File -FilePath TestDrive:/Sample-1.txt |
| 547 | + Compress-Archive -Path TestDrive:/Sample-1.txt -DestinationPath TestDrive:/archive1.zip |
| 548 | + } |
| 549 | + |
| 550 | + It "test custom assetion" { |
| 551 | + "${TestDrive}/archive1.zip" | Should -BeZipArchiveOnlyContaining @("Sample-1.txt") |
| 552 | + } |
| 553 | + } |
628 | 554 | } |
0 commit comments