Skip to content

Commit dc557a5

Browse files
committed
updated CI to run tests using new assertion, updated README with Azure CI status, fixed a bug where a path is determined to be relative to the working directory if the working directory is on a different drive than the path
1 parent fcaaa8e commit dc557a5

File tree

7 files changed

+78
-72
lines changed

7 files changed

+78
-72
lines changed

.azdevops/RunTests.ps1

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,22 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4-
# Load the module
5-
$module = Get-Module -Name "Microsoft.PowerShell.Archive"
6-
if ($null -ne $module)
7-
{
8-
Remove-Module $module
9-
}
10-
114
# Import the built module
125
Import-Module "$env:PIPELINE_WORKSPACE/ModuleBuild/Microsoft.PowerShell.Archive.psd1"
136

14-
$pesterRequiredVersion = "5.3"
7+
Get-ChildItem "$env:PIPELINE_WORKSPACE/ModuleBuild" | Write-Verbose -Verbose
158

16-
# If Pester 5.3.3 is not installed, install it
17-
$shouldInstallPester = $true
18-
19-
if ($pesterModules = Get-Module -Name "Pester" -ListAvailable) {
20-
foreach ($module in $pesterModules) {
21-
if ($module.Version.ToString() -eq $pesterRequiredVersion) {
22-
$shouldInstallPester = $false
23-
break
24-
}
25-
}
26-
}
9+
$pesterMinVersion = "5.3.0"
10+
$pesterMaxVersion = "5.3.9"
2711

28-
if ($shouldInstallPester) {
29-
Install-Module -Name "Pester" -RequiredVersion $pesterRequiredVersion -Force
12+
# If Pester 5.3.x is not installed, install it
13+
$pesterModule = Get-InstalledModule -Name "Pester" -MinimumVersion $pesterMinVersion -MaximumVersion $pesterMaxVersion
14+
if ($null -eq $pesterModule) {
15+
Install-Module -Name "Pester" -MinimumVersion $pesterMinVersion -MaximumVersion $pesterMaxVersion -Force
3016
}
3117

3218
# Load Pester
33-
Import-Module -Name "Pester" -RequiredVersion $pesterRequiredVersion
19+
Import-Module -Name "Pester" -MinimumVersion $pesterMinVersion -MaximumVersion $pesterMaxVersion
3420

3521
# Run tests
3622
$OutputFile = "$PWD/build-unit-tests.xml"

.azdevops/TestsTemplate.yml

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,50 +22,65 @@ jobs:
2222

2323
- pwsh: |
2424
Write-Output ${{ parameters.vmImageName }}
25-
$url = "https://github.com/PowerShell/PowerShell/releases/download/v7.3.0-preview.6/PowerShell-7.3.0-preview.6-win-x64.zip"
26-
$isTar = $false
25+
26+
if ("${{ parameters.vmImageName }}" -like 'windows-*')
27+
{
28+
$url = "https://github.com/PowerShell/PowerShell/releases/download/v7.3.0-preview.6/PowerShell-7.3.0-preview.6-win-x64.zip"
29+
$downloadFilename = "pwsh_download.msi"
30+
}
31+
2732
if ("${{ parameters.vmImageName }}" -like 'macos-*')
2833
{
29-
$url = "https://github.com/PowerShell/PowerShell/releases/download/v7.3.0-preview.6/powershell-7.3.0-preview.6-osx-x64.tar.gz"
30-
$isTar = $true
31-
Write-Output "Choose macOS"
34+
$url = "https://github.com/PowerShell/PowerShell/releases/download/v7.3.0-preview.6/powershell-7.3.0-preview.6-osx-x64.pkg"
35+
$downloadFilename = "pwsh_download.pkg"
3236
}
3337
if ("${{ parameters.vmImageName }}" -like 'ubuntu-*')
3438
{
3539
$url = "https://github.com/PowerShell/PowerShell/releases/download/v7.3.0-preview.6/powershell-7.3.0-preview.6-linux-x64.tar.gz"
36-
$isTar = $true
37-
Write-Output "Choose Ubuntu"
40+
$downloadFilename = "pwsh_download.tar.gz"
3841
}
39-
$destination = "powershell-preview-archive"
40-
if ($isTar) {
41-
$destination += ".tar.gz"
42-
} else {
43-
$destination += ".zip"
42+
43+
$downloadDestination = Join-Path $pwd $downloadFilename
44+
Invoke-WebRequest -Uri $url -OutFile $downloadDestination
45+
46+
# Installation steps for windows
47+
if ("${{ parameters.vmImageName }}" -like 'windows-*') {
48+
Expand-Archive -Path $downloadDestination -DestinationPath "pwsh-preview"
49+
$powerShellPreview = Join-Path $pwd "pwsh-preview" "pwsh.exe"
4450
}
45-
Invoke-WebRequest -Uri $url -OutFile $destination
46-
## unpack the downloaded file
47-
$powershellPreview = Join-Path $pwd "powershell-preview"
48-
mkdir $powershellPreview
49-
if ($isTar)
51+
if ("${{ parameters.vmImageName }}" -like 'ubuntu-*')
5052
{
51-
gunzip -d $destination
52-
$destination = $destination.Replace(".gz", "")
53-
tar -x -f $destination -C $powershellPreview
54-
} else {
55-
Expand-Archive -Path $destination -DestinationPath $powershellPreview
53+
gunzip -d $downloadDestination
54+
$downloadDestination = $downloadDestination.Replace(".gz", "")
55+
mkdir "pwsh-preview"
56+
tar -x -f $downloadDestination -C "pwsh-preview"
57+
$powerShellPreview = Join-Path $pwd "pwsh-preview" "pwsh"
5658
}
57-
# Print contents of $powershellPreview
58-
#Get-ChildItem $powershellPreview | Format-Table "Name"
59-
$powershellPreview = Join-Path $powershellPreview "pwsh"
60-
if ("${{ parameters.vmImageName }}" -like 'windows-*')
59+
if ("${{ parameters.vmImageName }}" -like 'macos-*')
6160
{
62-
$powerShellPreview += ".exe"
61+
sudo xattr -rd com.apple.quarantine "${downloadDestination}"
62+
sudo installer -pkg "${downloadDestination}" -target /
63+
$powerShellPreview = "pwsh-preview"
6364
}
6465
# Write the location of PowerShell Preview
6566
Write-Host "##vso[task.setvariable variable=PowerShellPreviewExecutablePath;]$powershellPreview"
6667
displayName: Download and Install PowerShell Preview
6768
6869
- pwsh: |
70+
$destination = Join-Path $pwd "7z.exe"
71+
$installUrl = "https://www.7-zip.org/a/7z2201-x64.exe"
72+
Invoke-WebRequest -Uri $installUrl -OutFile $destination
73+
# Run the installer in silent mode
74+
.$destination /S /D="C:\Program Files\7-Zip"
75+
displayName: Install 7-zip
76+
condition: and(succeeded(), startswith('${{ parameters.vmImageName }}', 'windows'))
77+
78+
- pwsh: |
79+
if ("${{ parameters.vmImageName }}" -like 'windows-*')
80+
{
81+
# Add 7-zip to PATH on Windows
82+
[System.Environment]::SetEnvironmentVariable('PATH',$Env:PATH+';C:\Program Files\7-zip')
83+
}
6984
"$(PowerShellPreviewExecutablePath) .azdevops/RunTests.ps1" | Invoke-Expression
7085
displayName: Run Tests
7186

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# Microsoft.PowerShell.Archive Module
22
[Microsoft.PowerShell.Archive module](https://technet.microsoft.com/en-us/library/dn818910.aspx) contains cmdlets that let you create and extract ZIP archives.
33

4-
|AppVeyor (Windows) | Travis CI (Linux) |
5-
|:-------------------:|:------------------:|
6-
|[![Build status](https://ci.appveyor.com/api/projects/status/npvhboe2nbdbtteg/branch/master?svg=true)](https://ci.appveyor.com/project/PowerShell/microsoft-powershell-archive/branch/master)|[![Build Status](https://travis-ci.org/PowerShell/Microsoft.PowerShell.Archive.svg?branch=master)](https://travis-ci.org/PowerShell/Microsoft.PowerShell.Archive)|
7-
4+
| Azure CI |
5+
|:-------------------:|
6+
|[![Build Status](https://dev.azure.com/powershell/Archive/_apis/build/status/PowerShell.Microsoft.PowerShell.Archive?repoName=PowerShell%2FMicrosoft.PowerShell.Archive&branchName=refs%2Fpull%2F131%2Fmerge)](https://dev.azure.com/powershell/Archive/_build/latest?definitionId=130&repoName=PowerShell%2FMicrosoft.PowerShell.Archive&branchName=refs%2Fpull%2F131%2Fmerge)|
87
## [Compress-Archive](https://technet.microsoft.com/library/dn841358.aspx) examples
98
1. Create an archive from an entire folder including subdirectories: `Compress-Archive -Path C:\Reference -DestinationPath C:\Archives\Draft.zip`
109
2. Update an existing archive file: `Compress-Archive -Path C:\Reference\* -DestinationPath C:\Archives\Draft.zip -Update`

Tests/Assertions/Should-BeZipArchiveOnlyContaining.psm1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ function Should-BeZipArchiveOnlyContaining {
4848
}
4949

5050
# Get 7-zip to list the contents of the archive
51-
$output = 7z.exe l $ActualValue -ba
51+
if ($IsWindows) {
52+
$output = 7z.exe l $ActualValue -ba
53+
} else {
54+
$output = 7z l $ActualValue -ba
55+
}
5256

5357
# Check if the output is null
5458
if ($null -eq $output) {

Tests/Compress-Archive.Tests.ps1

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ BeforeDiscovery {
5858
}
5959

6060
It "Validate errors from Compress-Archive when invalid path is supplied for Path or LiteralPath parameters" -ForEach @(
61-
@{ Path = "Env:/Path" }
62-
@{ Path = @("TestDrive:/", "Env:/Path") }
63-
) -Tag this1 {
61+
@{ Path = "Variable:/PWD" }
62+
@{ Path = @("TestDrive:/", "Variable:/PWD") }
63+
) {
6464
$DestinationPath = "TestDrive:/archive2.zip"
6565

6666
Compress-Archive -Path $Path -DestinationPath $DestinationPath -ErrorAction SilentlyContinue -ErrorVariable error
@@ -249,8 +249,14 @@ BeforeDiscovery {
249249
It "-WriteMode Create works" -Tag td1 {
250250
$sourcePath = "TestDrive:/SourceDir"
251251
$destinationPath = "TestDrive:/archive1.zip"
252-
Compress-Archive -Path $sourcePath -DestinationPath $destinationPath
252+
Compress-Archive -Path $sourcePath -DestinationPath $destinationPath -Verbose
253+
if ($IsWindows) {
254+
$t = Convert-Path $destinationPath
255+
7z l "${t}" | Write-Verbose -Verbose
256+
}
253257
$destinationPath | Should -BeZipArchiveOnlyContaining @('SourceDir/', 'SourceDir/Sample-1.txt')
258+
259+
254260
}
255261
}
256262

Tests/Install-7Zip.ps1

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/PathHelper.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ private string GetEntryName(FileSystemInfo fileSystemInfo, out bool doesPreserve
137137
{
138138
entryName += System.IO.Path.DirectorySeparatorChar;
139139
}
140+
141+
140142
return entryName;
141143
}
142144

@@ -198,7 +200,14 @@ private static string GetPrefixForPath(System.IO.DirectoryInfo directoryInfo)
198200
/// <returns></returns>
199201
private bool TryGetPathRelativeToCurrentWorkingDirectory(string path, out string? relativePathToWorkingDirectory)
200202
{
201-
string relativePath = System.IO.Path.GetRelativePath(_cmdlet.SessionState.Path.CurrentFileSystemLocation.Path, path);
203+
Debug.Assert(!string.IsNullOrEmpty(path));
204+
string? workingDirectoryRoot = Path.GetPathRoot(_cmdlet.SessionState.Path.CurrentFileSystemLocation.Path);
205+
string? pathRoot = Path.GetPathRoot(path);
206+
if (workingDirectoryRoot != pathRoot) {
207+
relativePathToWorkingDirectory = null;
208+
return false;
209+
}
210+
string relativePath = Path.GetRelativePath(_cmdlet.SessionState.Path.CurrentFileSystemLocation.Path, path);
202211
relativePathToWorkingDirectory = relativePath.Contains("..") ? null : relativePath;
203212
return relativePathToWorkingDirectory is not null;
204213
}

0 commit comments

Comments
 (0)