-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(#4) Adds Windows Sandbox to Test-Package #26
Open
JPRuskin
wants to merge
1
commit into
chocolatey-community:master
Choose a base branch
from
JPRuskin:addsSandboxTesting
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,12 @@ | |
https://github.com/chocolatey/choco/wiki/CreatePackages#testing-your-package | ||
#> | ||
function Test-Package { | ||
[CmdletBinding(DefaultParameterSetName="Local")] | ||
param( | ||
# If file, path to the .nupkg or .nuspec file for the package. | ||
# If directory, latest .nupkg or .nuspec file wil be looked in it. | ||
# If ommited current directory will be used. | ||
[Parameter(Position=0)] | ||
$Nu, | ||
|
||
# Test chocolateyInstall.ps1 only. | ||
|
@@ -37,13 +39,28 @@ function Test-Package { | |
[string] $Parameters, | ||
|
||
# Path to chocolatey-test-environment: https://github.com/majkinetor/chocolatey-test-environment | ||
[string] $Vagrant = $Env:au_Vagrant, | ||
[Parameter(ParameterSetName="Vagrant")] | ||
[string] $Vagrant = $env:au_Vagrant, | ||
|
||
[Parameter(ParameterSetName="Vagrant")] | ||
# Open new shell window | ||
[switch] $VagrantOpen, | ||
|
||
# Do not remove existing packages from vagrant package directory | ||
[switch] $VagrantNoClear | ||
[Parameter(ParameterSetName="Vagrant")] | ||
[switch] $VagrantNoClear, | ||
|
||
# Invokes the package test within Windows Sandbox, if available | ||
[Parameter(ParameterSetName="Sandbox")] | ||
[switch] $Sandbox, | ||
|
||
# If set, does not destroy the Windows Sandbox instance or files. | ||
[Parameter(ParameterSetName="Sandbox")] | ||
[switch] $NoDestroy, | ||
|
||
# Timeout for attempt to install and uninstall to Windows Sandbox, in minutes. | ||
[Parameter(ParameterSetName="Sandbox")] | ||
[uint16] $Timeout = 5 | ||
) | ||
|
||
if (!$Install -and !$Uninstall) { $Install = $true } | ||
|
@@ -68,7 +85,7 @@ function Test-Package { | |
$Nu = Get-Item ([System.IO.Path]::Combine($Nu.DirectoryName, '*.nupkg')) | Sort-Object -Property CreationTime -Descending | Select-Object -First 1 | ||
} elseif ($Nu.Extension -ne '.nupkg') { throw "File is not nupkg or nuspec file" } | ||
|
||
#At this point Nu is nupkg file | ||
# At this point Nu is nupkg file | ||
|
||
$package_name = $Nu.Name -replace '(\.\d+)+(-[^-]+)?\.nupkg$' | ||
$package_version = ($Nu.BaseName -replace $package_name).Substring(1) | ||
|
@@ -78,9 +95,103 @@ function Test-Package { | |
Write-Host " Name:".PadRight(15) $package_name | ||
Write-Host " Version:".PadRight(15) $package_version | ||
if ($Parameters) { Write-Host " Parameters:".PadRight(15) $Parameters } | ||
if ($Vagrant) { Write-Host " Vagrant: ".PadRight(15) $Vagrant } | ||
|
||
if ($Sandbox) { | ||
if (-not (Get-Command WindowsSandbox -ErrorAction SilentlyContinue)) { | ||
Write-Error "Windows Sandbox is not available. Please try Local or Vagrant methods." -ErrorAction Stop | ||
} | ||
Write-Host "`nTesting package using Windows Sandbox" | ||
|
||
$SandboxTempFolder = Join-Path $env:TEMP "$(New-Guid)" | ||
$null = New-Item -Path $SandboxTempFolder\ChocoTestingSandbox.wsb -Force -Value @" | ||
<Configuration> | ||
<Networking>Enable</Networking> | ||
<MappedFolders> | ||
<MappedFolder> | ||
<HostFolder>$($SandboxTempFolder)</HostFolder> | ||
<SandboxFolder>C:\packages\</SandboxFolder> | ||
<ReadOnly>false</ReadOnly> | ||
</MappedFolder> | ||
</MappedFolders> | ||
<LogonCommand> | ||
<Command>C:\packages\Test-Package.cmd</Command> | ||
</LogonCommand> | ||
</Configuration> | ||
"@ | ||
|
||
$TestCommand = @( | ||
'Start-Transcript -Path c:\packages\log.txt' | ||
'$Results = @{}' | ||
if ($Install) { | ||
"choco install -y $package_name --version $package_version --no-progress --source `"'C:\packages\;https://community.chocolatey.org/api/v2/'`"$(if ($Parameters) {[string]::format(' --packageParameters="''{0}''"', $Parameters)}) | Write-Host" | ||
'$Results.InstallExitCode = $LASTEXITCODE' | ||
} | ||
if ($Uninstall) { | ||
"choco uninstall -y $package_name | Write-Host" | ||
'$Results.UninstallExitCode = $LASTEXITCODE' | ||
} | ||
'Stop-Transcript' | ||
'Copy-Item C:\ProgramData\chocolatey\logs\* -Destination C:\packages\' | ||
'$Results | Export-Clixml -Path C:\packages\results.clixml' | ||
) -join '; ' | ||
$null = New-Item -Path $SandboxTempFolder\Test-Package.cmd -Value @" | ||
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin" | ||
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "$($TestCommand)" | ||
"@ | ||
Copy-Item -Path $Nu -Destination $SandboxTempFolder | ||
|
||
try { | ||
& WindowsSandbox "$SandboxTempFolder\ChocoTestingSandbox.wsb" | ||
|
||
$Timer = [System.Diagnostics.Stopwatch]::StartNew() | ||
$BytesShown = 0 # Nothing shown yet | ||
while ($Timer.IsRunning -and $Timer.Elapsed.TotalMinutes -lt $Timeout) { | ||
if (Test-Path $SandboxTempFolder\log.txt) { | ||
$Log = Get-Content $SandboxTempFolder\log.txt -Raw | ||
-join $Log[$BytesShown..$Log.Length] | Write-Host -NoNewLine | ||
$BytesShown = $Log.Length | ||
} | ||
|
||
if (Test-Path $SandboxTempFolder\results.clixml) { | ||
$Timer.Stop() | ||
} | ||
|
||
Start-Sleep -Milliseconds 500 | ||
} | ||
} finally { | ||
if (Test-Path $SandboxTempFolder\results.clixml) { | ||
$Results = Import-Clixml $SandboxTempFolder\results.clixml | ||
|
||
if ($Install) { | ||
if ($Results.InstallExitCode -ne 0) { throw "choco install failed with $($Results.InstallExitCode)"} | ||
} | ||
if ($Uninstall) { | ||
if ($Results.UninstallExitCode -ne 0) { throw "choco install failed with $($Results.UninstallExitCode)"} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be choco uninstall failed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very true! I'll get that sorted |
||
} | ||
} else { | ||
Write-Error "Test failed after $($Timeout) minutes without a results file." | ||
} | ||
|
||
if (-not $NoDestroy) { | ||
Get-Process WindowsSandboxClient | Stop-Process | ||
|
||
$Tries = 0 | ||
while ($Tries++ -lt 4 -and (Test-Path $SandboxTempFolder)) { | ||
try { | ||
Remove-Item $SandboxTempFolder -Recurse -Force -ErrorAction Stop | ||
} catch { | ||
Start-Sleep -Seconds 1 | ||
} | ||
} | ||
} else { | ||
Write-Host "Logs and Results can be found in '$($SandboxTempFolder)'" | ||
} | ||
} | ||
return | ||
} | ||
|
||
if ($Vagrant) { | ||
Write-Host " Vagrant: ".PadRight(15) $Vagrant | ||
Write-Host "`nTesting package using vagrant" | ||
|
||
if (!$VagrantNoClear) { | ||
|
@@ -102,7 +213,7 @@ function Test-Package { | |
|
||
if ($Install) { | ||
Write-Host "`nTesting package install" | ||
choco install -y -r $package_name --version $package_version --source "'$($Nu.DirectoryName);https://chocolatey.org/api/v2/'" --force --packageParameters "'$Parameters'" | Write-Host | ||
choco install -y -r $package_name --version $package_version --source "'$($Nu.DirectoryName);https://community.chocolatey.org/api/v2/'" --force --packageParameters "'$Parameters'" | Write-Host | ||
if ($LASTEXITCODE -ne 0) { throw "choco install failed with $LastExitCode"} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should check for an existing windowssandbox process. If there is already one, then this will fail and we'll be waiting for the timeout to hit needlessly.
I would recommend the check happen early before we've done a bunch of setup.