Skip to content

Commit

Permalink
Change default search to use exact package name if both name and vers…
Browse files Browse the repository at this point in the history
…ion are specified

Changed
* Change default search to use exact package name if both package name and required version are specified (#20)
  * Requires downgrade to Chocolatey 0.10.13 due to [a Chocolatey defect](chocolatey/choco#1843) until 0.10.16 is released

Fixed
* AppVeyor builds no longer fail due to change in build image permissions
* Version min/max comparison should now work properly
  • Loading branch information
ethanbergstrom committed Jan 23, 2021
1 parent bbe8d5a commit fa79d06
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 17 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.1.0] - 2021-01-23
#### Changed
* Change default search to use exact package name if both package name and required version are specified (#20)
* Requires downgrade to Chocolatey 0.10.13 due to [a Chocolatey defect](https://github.com/chocolatey/choco/issues/1843) until 0.10.16 is released

#### Fixed
* AppVeyor builds no longer fail due to change in build image permissions
* Version min/max comparison should now work properly

## [2.0.0] - 2020-10-05
#### Added
* Searching/installing/managing multiple Chocolatey sources (#5)
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,27 @@ When used with CoreCLR, PowerShell 7.0.1 is a minimum requirement due to [a comp
### Save a package
Save-Package is not supported with the ChocolateyGet provider, due to Chocolatey not supporting package downloads without special licensing.

### CLI Package search with MaximumVersion / AllVersions return unexpected results
Due to [a bug with Chocolatey](https://github.com/chocolatey/choco/issues/1843) versions 0.10.14 through 0.10.15, ChocolateyGet is unable to search packages by package range via command line as of version 2.1.0.

Until [Chocolatey 0.10.16 is released](https://github.com/chocolatey/choco/milestone/43), the following workarounds are available:
- Specify `RequiredVersion` if possible
```PowerShell
Install-Package ninja -RequiredVersion 1.9.0 -Provider ChocolateyGet
```
- Downgrade Chocolatey to 0.10.13 until 0.10.16 is released (ChocolateyGet installs 0.10.13 by default)
```PowerShell
Install-Package chocolatey -RequiredVersion 0.10.13 -Provider ChocolateyGet -Force
Install-Package ninja -MaximumVersion 1.9.0 -Provider ChocolateyGet
```
- If you **must** use Chocolatey 0.10.14 or 0.10.15 for some reason, include the environment variable CHOCO_NONEXACT_SEARCH
```PowerShell
$env:CHOCO_NONEXACT_SEARCH = $true
Install-Package ninja -MaximumVersion 1.9.0 -Provider ChocolateyGet
```
- Please note - this will revert the default search behavior change requested in [Issue #20](https://github.com/jianyunt/ChocolateyGet/issues/20)
- Use ChocolateyGet via PowerShell v5 or below in Native API mode, which uses Chocolatey version 0.10.13


## Legal and Licensing
ChocolateyGet is licensed under the [MIT license](./LICENSE.txt).
9 changes: 5 additions & 4 deletions Test/ChocolateyGet.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Describe "$platform DSC-compliant package installation and uninstallation" {
}
Context 'with additional arguments' {
$package = 'sysinternals'
$argsAndParams = '--paramsglobal --params "/InstallDir=c:\windows\temp\sysinternals /QuickLaunchShortcut=false" -y --installargs MaintenanceService=false'
$argsAndParams = '--paramsglobal --params "/InstallDir='+$env:TEMP+'\sysinternals /QuickLaunchShortcut=false" -y --installargs MaintenanceService=false'

It 'searches for the latest version of a package' {
Find-Package -Provider $ChocolateyGet -Name $package -AdditionalArguments $argsAndParams | Where-Object {$_.Name -contains $package} | Should Not BeNullOrEmpty
Expand Down Expand Up @@ -84,7 +84,7 @@ Describe "$platform pipline-based package installation and uninstallation" {
}
Context 'with additional arguments' {
$package = 'sysinternals'
$argsAndParams = '--paramsglobal --params "/InstallDir=c:\windows\temp\sysinternals /QuickLaunchShortcut=false" -y --installargs MaintenanceService=false'
$argsAndParams = '--paramsglobal --params "/InstallDir='+$env:TEMP+'\sysinternals /QuickLaunchShortcut=false" -y --installargs MaintenanceService=false'

It 'searches for and silently installs the latest version of a package' {
Find-Package -Provider $ChocolateyGet -Name $package | Install-Package -Force -AdditionalArguments $argsAndParams | Where-Object {$_.Name -contains $package} | Should Not BeNullOrEmpty
Expand Down Expand Up @@ -129,8 +129,9 @@ Describe "$platform multi-source support" {
}

Describe "$platform version filters" {
$package = 'cpu-z'
$version = '1.87'
$package = 'ninja'
# Keep at least one version back, to test the 'latest' feature
$version = '1.10.1'

AfterAll {
Uninstall-Package -Name $package -Provider $ChocolateyGet -ErrorAction SilentlyContinue
Expand Down
3 changes: 3 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ build_script:
test_script:
- ps: Invoke-Pester -EnableExit -OutputFormat NUnitXml -OutputFile TestsResults.NativeAPI.xml
- ps: (New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path .\TestsResults.NativeAPI.xml))
- pwsh: Install-Package chocolatey -RequiredVersion 0.10.13 -ProviderName ChocolateyGet -Force
- pwsh: Invoke-Pester -EnableExit -OutputFormat NUnitXml -OutputFile TestsResults.CLI.xml
- pwsh: (New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path .\TestsResults.CLI.xml))

after_build:
- ps: Get-ChildItem $env:programdata\chocolatey\logs | % { Push-AppveyorArtifact $_.FullName -FileName $_.Name }
2 changes: 1 addition & 1 deletion paket.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NUGET
remote: https://api.nuget.org/v3/index.json
chocolatey.lib (0.10.15)
chocolatey.lib (0.10.13)
log4net (>= 2.0.3)
log4net (2.0.3)
2 changes: 1 addition & 1 deletion src/ChocolateyGet.psd1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@{
RootModule = 'ChocolateyGet.psm1'
ModuleVersion = '2.0.0'
ModuleVersion = '2.1.0'
GUID = 'c1735ed7-8b2f-426a-8cbc-b7feb6b8288d'
Author = 'Jianyun'
Copyright = ''
Expand Down
4 changes: 3 additions & 1 deletion src/private/Install-ChocoBinaries.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ function Install-ChocoBinaries {
if (-not ([Net.ServicePointManager]::SecurityProtocol -eq [Net.SecurityProtocolType]::SystemDefault)) {
[Net.ServicePointManager]::SecurityProtocol = ([Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12)
}

# Have to keep choco pinned to 0.10.13 due to https://github.com/chocolatey/choco/issues/1843 - should be fixed in 0.10.16, which is still in beta
# https://docs.chocolatey.org/en-us/choco/setup#installing-a-particular-version-of-chocolatey
$env:chocolateyVersion = '0.10.13'
Invoke-WebRequest 'https://chocolatey.org/install.ps1' -UseBasicParsing | Invoke-Expression > $null
} catch {
ThrowError -ExceptionName 'System.OperationCanceledException' `
Expand Down
16 changes: 13 additions & 3 deletions src/private/Invoke-Choco.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ function Invoke-Choco {
Invoke-Command $genericParams
if ($Package) {
$config.Input = $Package
if (($Version -or $AllVersions) -and -not $env:CHOCO_NONEXACT_SEARCH) {
# Limit NuGet API result set to just the specific package name if version is specified
# Have to keep choco pinned to 0.10.13 due to https://github.com/chocolatey/choco/issues/1843 - should be fixed in 0.10.16, which is still in beta
$config.ListCommand.Exact = $true
}
}
$config.CommandName = [chocolatey.infrastructure.app.domain.CommandNameType]::list
}) | Out-Null
Expand Down Expand Up @@ -259,6 +264,11 @@ function Invoke-Choco {

if ($Package) {
$cmdString += "$Package "
if (($Version -or $AllVersions) -and -not $env:CHOCO_NONEXACT_SEARCH) {
# Limit NuGet API result set to just the specific package name if version is specified
# Have to keep choco pinned to 0.10.13 due to https://github.com/chocolatey/choco/issues/1843 - should be fixed in 0.10.16, which is still in beta
$cmdString += "--exact "
}
}

if ($Version) {
Expand Down Expand Up @@ -291,12 +301,12 @@ function Invoke-Choco {
# Save the output to a variable so we can inspect the exit code before submitting the output to the pipeline
$output = & $ChocoExePath $cmdString

if ($LASTEXITCODE -ne 0) {
# Add support for Error Code 2 (no results) for basic enhanced error code support
if ($LASTEXITCODE -ne 0 -and $LASTEXITCODE -ne 2) {
ThrowError -ExceptionName 'System.OperationCanceledException' `
-ExceptionMessage $($output | Out-String) `
-ExceptionMessage "The following command $ChocoExePath $cmdString failed with error code $LASTEXITCODE" `
-ErrorID 'JobFailure' `
-ErrorCategory InvalidOperation `
-ExceptionObject $job
} else {
if ($Install -or ($Search -and $SourceName)) {
$output | ConvertTo-SoftwareIdentity -RequestedName $Package -Source $SourceName
Expand Down
10 changes: 5 additions & 5 deletions src/private/PackageVersionHelpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,32 @@ function Test-PackageVersion {
$MaximumVersion
)

$version = $Package.Version.TrimStart('v')
[System.Version]$version = $Package.Version.TrimStart('v')

if (-not ($RequiredVersion -or $MinimumVersion -or $MaximumVersion)) {
return $true
}

if ($RequiredVersion) {
return ($Version -eq $RequiredVersion)
return ($Version -eq [System.Version]$RequiredVersion)
}

$isMatch = $false

if($MinimumVersion)
{
$isMatch = $version -ge $MinimumVersion
$isMatch = $version -ge [System.Version]$MinimumVersion
}

if($MaximumVersion)
{
if($MinimumVersion)
{
$isMatch = $isMatch -and ($version -le $MaximumVersion)
$isMatch = $isMatch -and ($version -le [System.Version]$MaximumVersion)
}
else
{
$isMatch = $version -le $MaximumVersion
$isMatch = $version -le [System.Version]$MaximumVersion
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/public/Install-Package.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ function Install-Package {
return
}

$swid = Invoke-Choco -Install -Package $Matches.name -Version $Matches.version -SourceName $Matches.source |
Where-Object {Test-PackageVersion -Package $_ -RequiredVersion $Matches.version}
$swid = Invoke-Choco -Install -Package $Matches.name -Version $Matches.version -SourceName $Matches.source |
Where-Object {Test-PackageVersion -Package $_ -RequiredVersion $Matches.version -ErrorAction SilentlyContinue}

if (-not $swid) {
# Invoke-Choco didn't throw an exception but we also couldn't pull a Software Identity from the output.
Expand Down

0 comments on commit fa79d06

Please sign in to comment.