From 743b627632a89d76252572ea91ba1c781b9816e3 Mon Sep 17 00:00:00 2001 From: Scott Matthews Date: Thu, 8 Aug 2019 13:37:47 +0100 Subject: [PATCH 01/13] adding support for specifying minimum version --- .../cChocoPackageInstall.psm1 | 89 +++++++++++++++---- .../cChocoPackageInstall.schema.mof | 1 + Tests/cChocoPackageInstall_Tests.ps1 | 38 ++++++++ 3 files changed, 113 insertions(+), 15 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 156de19..358ab42 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -28,6 +28,9 @@ function Get-TargetResource [ValidateNotNullOrEmpty()] [string] $Version, + [ValidateNotNull()] + [string] + $MinimumVersion, [ValidateNotNullOrEmpty()] [string] $Source @@ -69,6 +72,9 @@ function Set-TargetResource [ValidateNotNullOrEmpty()] [string] $Version, + [ValidateNotNull()] + [string] + $MinimumVersion, [string] $Source, [String] @@ -77,6 +83,11 @@ function Set-TargetResource $AutoUpgrade = $false ) Write-Verbose -Message 'Start Set-TargetResource' + $isVersionPresent = $PSBoundParameters.ContainsKey('Version') + $isMinimumVersionPresent = $PSBoundParameters.ContainsKey('MinimumVersion') + if ($isVersionPresent -and $isMinimumVersionPresent ) { + throw "Cannot specify 'Version' and 'MinimumVersion' in the same configuration" + } if (-Not (Test-ChocoInstalled)) { throw "cChocoPackageInstall requires Chocolatey to be installed, consider using cChocoInstaller with 'dependson' in dsc config" @@ -84,6 +95,16 @@ function Set-TargetResource $isInstalled = IsPackageInstalled -pName $Name + #Determine the correct package version to use get to desired state + if ($isVersionPresent -or $isMinimumVersionPresent) { + if ($isVersionPresent) { + $versionToInstall = $PSBoundParameters['Version'] + } + else { + $versionToInstall = $PSBoundParameters['MinimumVersion'] + } + } + #Uninstall if Ensure is set to absent and the package is installed if ($isInstalled) { if ($Ensure -eq 'Absent') { @@ -98,9 +119,15 @@ function Set-TargetResource if ($Version) { Write-Verbose -Message "Uninstalling $Name due to version mis-match" UninstallPackage -pName $Name -pParams $Params - Write-Verbose -Message "Re-Installing $Name with correct version $version" - InstallPackage -pName $Name -pParams $Params -pVersion $Version -pSource $Source -cParams $chocoParams - } elseif ($AutoUpgrade) { + Write-Verbose -Message "Re-Installing $Name with correct version $versionToInstall" + InstallPackage -pName $Name -pParams $Params -pVersion $versionToInstall -pSource $Source -cParams $chocoParams + } + elseif ($MinimumVersion) { + Write-Verbose -Message "Upgrading $Name becuase installed version is lower that the specified minimum" + $chocoParams += " --version '$versionToInstall'" + Upgrade-Package -pName $Name -pParams $Params -pSource $Source -cParams $chocoParams + } + elseif ($AutoUpgrade) { Write-Verbose -Message "Upgrading $Name due to version mis-match" Upgrade-Package -pName $Name -pParams $Params -pSource $Source -cParams $chocoParams } @@ -109,7 +136,7 @@ function Set-TargetResource } else { $whatIfShouldProcess = $pscmdlet.ShouldProcess("$Name", 'Install package from Chocolatey') if ($whatIfShouldProcess) { - InstallPackage -pName $Name -pParams $Params -pVersion $Version -pSource $Source -cParams $chocoParams + InstallPackage -pName $Name -pParams $Params -pVersion $versionToInstall -pSource $Source -cParams $chocoParams } } } @@ -133,6 +160,9 @@ function Test-TargetResource [ValidateNotNullOrEmpty()] [string] $Version, + [ValidateNotNull()] + [string] + $MinimumVersion, [string] $Source, [ValidateNotNullOrEmpty()] @@ -143,6 +173,11 @@ function Test-TargetResource ) Write-Verbose -Message 'Start Test-TargetResource' + $isVersionPresent = $PSBoundParameters.ContainsKey('Version') + $isMinimumVersionPresent = $PSBoundParameters.ContainsKey('MinimumVersion') + if ($isVersionPresent -and $isMinimumVersionPresent ) { + throw "Cannot specify 'Version' and 'MinimumVersion' in the same configuration" + } if (-Not (Test-ChocoInstalled)) { return $false @@ -161,7 +196,12 @@ function Test-TargetResource if ($version) { Write-Verbose -Message "Checking if $Name is installed and if version matches $version" $result = IsPackageInstalled -pName $Name -pVersion $Version - } else { + } + elseif ($MinimumVersion) { + Write-Verbose -Message "Checking if $Name is installed and version is $MinimumVersion or higher" + $result = IsPackageInstalled -pName $Name -pMinimumVersion $MinimumVersion + } + else { Write-Verbose -Message "Checking if $Name is installed" if ($AutoUpgrade -and $isInstalled) { @@ -181,7 +221,7 @@ function Test-ChocoInstalled Write-Verbose -Message 'Test-ChocoInstalled' $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') - Write-Verbose -Message "Env:Path contains: $env:Path" + #Write-Verbose -Message "Env:Path contains: $env:Path" if (Test-Command -command choco) { Write-Verbose -Message 'YES - Choco is Installed' @@ -296,23 +336,41 @@ function UninstallPackage function IsPackageInstalled { + [CmdletBinding(DefaultParameterSetName = 'RequiredVersion')] param( - [Parameter(Position=0,Mandatory)][string]$pName, - [Parameter(Position=1)][string]$pVersion + [Parameter(Position=0, Mandatory)] + [string]$pName, + + [Parameter(ParameterSetName = 'RequiredVersion')] + [string]$pVersion, + + [Parameter(ParameterSetName = 'MinimumVersion')] + [string]$pMinimumVersion ) Write-Verbose -Message "Start IsPackageInstalled $pName" $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') - Write-Verbose -Message "Path variables: $env:Path" + #Write-Verbose -Message "Path variables: $env:Path" $installedPackages = Get-ChocoInstalledPackage if ($pVersion) { - Write-Verbose 'Comparing version' - $installedPackages = $installedPackages | Where-object { $_.Name -eq $pName -and $_.Version -eq $pVersion} - } else { + Write-Verbose 'Comparing required version' + $installedPackages = $installedPackages | Where-Object { $_.Name -eq $pName -and $_.Version -eq $pVersion} + } + elseif ($pMinimumVersion) { + Write-Verbose 'Comparing minimum version' + $comparablePackages = $installedPackages | Where-Object { $_.Name -eq $pName} | ForEach-Object { + # to do version comparision we first need to convert the version from string to System.Version + # we also need to ignore any pre release version, i.e. anythign after "-" + $v = [System.Version](($_.Version -split "-")[0]) + $_ | Add-Member -MemberType NoteProperty -Name ComparableVersion -Value $v -PassThru + } + $installedPackages = $comparablePackages | Where-Object {$_.ComparableVersion -ge $pMinimumVersion} + } + else { Write-Verbose "Finding packages -eq $pName" - $installedPackages = $installedPackages | Where-object { $_.Name -eq $pName} + $installedPackages = $installedPackages | Where-Object { $_.Name -eq $pName} } $count = @($installedPackages).Count @@ -326,6 +384,7 @@ function IsPackageInstalled return $false } + Function Test-LatestVersionInstalled { [Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')] param( @@ -388,7 +447,7 @@ Function Upgrade-Package { ) $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') - Write-Verbose -Message "Path variables: $env:Path" + #Write-Verbose -Message "Path variables: $env:Path" [string]$chocoParams = '-dv -y' if ($pParams) { @@ -434,7 +493,7 @@ function Get-ChocoInstalledPackage { $ChocoInstallList = Join-Path -Path $ChocoInstallLP -ChildPath 'ChocoInstalled.xml' if ($Purge.IsPresent) { - Remove-Item $ChocoInstallList -Force + Remove-Item $ChocoInstallList -Force -ErrorAction SilentlyContinue $res = $true } else { $PackageCacheSec = (Get-Date).AddSeconds('-60') diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.schema.mof b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.schema.mof index 079bbd5..03b8e62 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.schema.mof +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.schema.mof @@ -5,6 +5,7 @@ class cChocoPackageInstall : OMI_BaseResource [Write,ValueMap{"Present", "Absent"},Values{"Present", "Absent"}] String Ensure; [write] string Params; [write] string Version; + [write] string MinimumVersion; [write] string Source; [Write] String chocoParams; [Write] Boolean AutoUpgrade; diff --git a/Tests/cChocoPackageInstall_Tests.ps1 b/Tests/cChocoPackageInstall_Tests.ps1 index e522f64..73aa392 100644 --- a/Tests/cChocoPackageInstall_Tests.ps1 +++ b/Tests/cChocoPackageInstall_Tests.ps1 @@ -76,6 +76,24 @@ Describe -Name "Testing $ResourceName loaded from $ResourceFile" -Fixture { It -name "Test-TargetResource -ensure 'Absent' -version '1.0.0' -AutoUpgrade should return True" -test { Test-TargetResource @Scenario5 | Should Be $True } + + $Scenario6 = @{ + Name = 'GoogleChrome' + Ensure = 'Absent' + MinimumVersion = '1.0' + } + It -name "Test-TargetResource -ensure 'Absent' -MinimumVersion '1.0' should return True" -test { + Test-TargetResource @Scenario6 | Should Be $True + } + + $Scenario7 = @{ + Name = 'GoogleChrome' + Ensure = 'Present' + MinimumVersion = '1.0' + } + It -name "Test-TargetResource -ensure 'Present' -MinimumVersion '1.0' should return False" -test { + Test-TargetResource @Scenario7 | Should Be $false + } } Context -Name "Package is installed with version 1.0.0" -Fixture { @@ -121,6 +139,26 @@ Describe -Name "Testing $ResourceName loaded from $ResourceFile" -Fixture { It -name "Test-TargetResource -ensure 'Present' -version '1.0.1' should return False" -test { Test-TargetResource @Scenario4 | Should Be $False } + + $Scenario5 = @{ + Name = 'GoogleChrome' + Ensure = 'Present' + MinimumVersion = '0.9.0' + } + + It -name "Test-TargetResource -ensure 'Present' -MinimumVersion '0.9.0' should return True" -test { + Test-TargetResource @Scenario5 | Should Be $true + } + + $Scenario6 = @{ + Name = 'GoogleChrome' + Ensure = 'Present' + MinimumVersion = '1.0.1' + } + + It -name "Test-TargetResource -ensure 'Present' -MinimumVersion '1.0.1' should return False" -test { + Test-TargetResource @Scenario6 | Should Be $false + } } } From 77885016cc4b4186d07faeaf08e0189e6a9c585d Mon Sep 17 00:00:00 2001 From: Scott Matthews Date: Wed, 14 Aug 2019 16:52:40 +0100 Subject: [PATCH 02/13] DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 --- .../cChocoPackageInstall/cChocoPackageInstall.psm1 | 7 ++++--- Tests/cChoco_ScriptAnalyzerTests.ps1 | 9 ++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 358ab42..58b14c7 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -221,7 +221,7 @@ function Test-ChocoInstalled Write-Verbose -Message 'Test-ChocoInstalled' $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') - #Write-Verbose -Message "Env:Path contains: $env:Path" + Write-Verbose -Message "Env:Path contains: $env:Path" if (Test-Command -command choco) { Write-Verbose -Message 'YES - Choco is Installed' @@ -337,6 +337,7 @@ function UninstallPackage function IsPackageInstalled { [CmdletBinding(DefaultParameterSetName = 'RequiredVersion')] + [OutputType([bool])] param( [Parameter(Position=0, Mandatory)] [string]$pName, @@ -350,7 +351,7 @@ function IsPackageInstalled Write-Verbose -Message "Start IsPackageInstalled $pName" $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') - #Write-Verbose -Message "Path variables: $env:Path" + Write-Verbose -Message "Path variables: $env:Path" $installedPackages = Get-ChocoInstalledPackage @@ -447,7 +448,7 @@ Function Upgrade-Package { ) $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') - #Write-Verbose -Message "Path variables: $env:Path" + Write-Verbose -Message "Path variables: $env:Path" [string]$chocoParams = '-dv -y' if ($pParams) { diff --git a/Tests/cChoco_ScriptAnalyzerTests.ps1 b/Tests/cChoco_ScriptAnalyzerTests.ps1 index 699758b..2866e4b 100644 --- a/Tests/cChoco_ScriptAnalyzerTests.ps1 +++ b/Tests/cChoco_ScriptAnalyzerTests.ps1 @@ -30,7 +30,14 @@ if ($Modules.count -gt 0) { Context “Testing Module '$($module.FullName)'” { foreach ($rule in $rules) { It “passes the PSScriptAnalyzer Rule $rule“ { - (Invoke-ScriptAnalyzer -Path $module.FullName -IncludeRule $rule.RuleName ).Count | Should Be 0 + $Failures = Invoke-ScriptAnalyzer -Path $module.FullName -IncludeRule $rule.RuleName + $FailuresCount = ($Failures | Measure-Object).Count + if ($FailuresCount -gt 0) { + $Failures | ForEach-Object { + Write-Warning "Script: $($_.ScriptName), Line $($_.Line), Message $($_.Message)" + } + } + $FailuresCount | Should Be 0 } } } From 023071326a35f1a9c065c4a645554e558fce58e1 Mon Sep 17 00:00:00 2001 From: Scott Matthews Date: Wed, 4 Mar 2020 13:05:46 +0000 Subject: [PATCH 03/13] quick fixes --- .../cChocoPackageInstall/cChocoPackageInstall.psm1 | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 58b14c7..d0f3556 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -123,8 +123,8 @@ function Set-TargetResource InstallPackage -pName $Name -pParams $Params -pVersion $versionToInstall -pSource $Source -cParams $chocoParams } elseif ($MinimumVersion) { - Write-Verbose -Message "Upgrading $Name becuase installed version is lower that the specified minimum" - $chocoParams += " --version '$versionToInstall'" + Write-Verbose -Message "Upgrading $Name because installed version is lower that the specified minimum" + $chocoParams += " --version='$versionToInstall'" Upgrade-Package -pName $Name -pParams $Params -pSource $Source -cParams $chocoParams } elseif ($AutoUpgrade) { @@ -337,7 +337,7 @@ function UninstallPackage function IsPackageInstalled { [CmdletBinding(DefaultParameterSetName = 'RequiredVersion')] - [OutputType([bool])] + [OutputType([Boolean])] param( [Parameter(Position=0, Mandatory)] [string]$pName, @@ -351,7 +351,7 @@ function IsPackageInstalled Write-Verbose -Message "Start IsPackageInstalled $pName" $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') - Write-Verbose -Message "Path variables: $env:Path" + #Write-Verbose -Message "Path variables: $env:Path" $installedPackages = Get-ChocoInstalledPackage @@ -385,7 +385,6 @@ function IsPackageInstalled return $false } - Function Test-LatestVersionInstalled { [Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')] param( @@ -448,7 +447,7 @@ Function Upgrade-Package { ) $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') - Write-Verbose -Message "Path variables: $env:Path" + #Write-Verbose -Message "Path variables: $env:Path" [string]$chocoParams = '-dv -y' if ($pParams) { @@ -494,7 +493,7 @@ function Get-ChocoInstalledPackage { $ChocoInstallList = Join-Path -Path $ChocoInstallLP -ChildPath 'ChocoInstalled.xml' if ($Purge.IsPresent) { - Remove-Item $ChocoInstallList -Force -ErrorAction SilentlyContinue + Remove-Item $ChocoInstallList -Force $res = $true } else { $PackageCacheSec = (Get-Date).AddSeconds('-60') From 549f94badf0b40682f92db0ff8ee94a97f0e64cb Mon Sep 17 00:00:00 2001 From: Scott Matthews Date: Wed, 4 Mar 2020 13:46:55 +0000 Subject: [PATCH 04/13] throw if using prerelease with minimumversion --- .../cChocoPackageInstall/cChocoPackageInstall.psm1 | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index d0f3556..eb552d3 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -351,7 +351,7 @@ function IsPackageInstalled Write-Verbose -Message "Start IsPackageInstalled $pName" $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') - #Write-Verbose -Message "Path variables: $env:Path" + Write-Verbose -Message "Path variables: $($env:Path)" $installedPackages = Get-ChocoInstalledPackage @@ -361,9 +361,14 @@ function IsPackageInstalled } elseif ($pMinimumVersion) { Write-Verbose 'Comparing minimum version' + # version comparison can be done with [System.Version] but this lacks the ability to compare pre-release versions + # because of this limitation MinimumVersion cannot be used in conjuction with pre-release packages + $pre = ($pMinimumVersion -split "-")[1] + if ($pre) { + throw "MinimumVersion does not support comparing pre-releases, please use Version parameter instead" + } $comparablePackages = $installedPackages | Where-Object { $_.Name -eq $pName} | ForEach-Object { - # to do version comparision we first need to convert the version from string to System.Version - # we also need to ignore any pre release version, i.e. anythign after "-" + # ignoring pre-release as per above comment $v = [System.Version](($_.Version -split "-")[0]) $_ | Add-Member -MemberType NoteProperty -Name ComparableVersion -Value $v -PassThru } @@ -447,7 +452,7 @@ Function Upgrade-Package { ) $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') - #Write-Verbose -Message "Path variables: $env:Path" + Write-Verbose -Message "Path variables: $($env:Path)" [string]$chocoParams = '-dv -y' if ($pParams) { From 4d519bac47bba6f1d2508ba42f95b2d8987e4c2d Mon Sep 17 00:00:00 2001 From: Paul Broadwith Date: Sun, 10 May 2020 18:25:31 +0100 Subject: [PATCH 05/13] (doc) Update issue templates --- .github/ISSUE_TEMPLATE/bug-report.md | 31 +++++++++++++++++++ .github/ISSUE_TEMPLATE/bug_report.md | 31 +++++++++++++++++++ .../feature---enhancement-request.md | 20 ++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/bug-report.md create mode 100644 .github/ISSUE_TEMPLATE/bug_report.md create mode 100644 .github/ISSUE_TEMPLATE/feature---enhancement-request.md diff --git a/.github/ISSUE_TEMPLATE/bug-report.md b/.github/ISSUE_TEMPLATE/bug-report.md new file mode 100644 index 0000000..127dcb1 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug-report.md @@ -0,0 +1,31 @@ +--- +name: Bug Report +about: Create a report to help us improve cChoco +title: '' +labels: 0 - _Triaging, Bug, Up For Grabs +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Software (please complete the following information):** + - OS: [e.g. Windows 10 1903 Build 12345] + - PowerShell Version [e.g. 5.1.123] + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..8fe9089 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,31 @@ +--- +name: Bug report +about: Create a report to help us improve cChoco +title: "(GH-ISSUE#) [BUG]" +labels: Bug, Up For Grabs, 0 - _Triaging +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Software (please complete the following information):** + - OS: [e.g. Windows 10 1903 Build 12345] + - PowerShell Version [e.g. 5.1.123] + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature---enhancement-request.md b/.github/ISSUE_TEMPLATE/feature---enhancement-request.md new file mode 100644 index 0000000..b8e2920 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature---enhancement-request.md @@ -0,0 +1,20 @@ +--- +name: Feature / Enhancement Request +about: Suggest an idea for improving cChoco. +title: '' +labels: 0 - _Triaging, Enhancement, Up For Grabs +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. From fe199bb4187649daeaac25cd3d054186818c5980 Mon Sep 17 00:00:00 2001 From: Paul Broadwith Date: Sun, 10 May 2020 18:26:28 +0100 Subject: [PATCH 06/13] (doc) Remove invalid issue template --- .github/ISSUE_TEMPLATE/bug_report.md | 31 ---------------------------- 1 file changed, 31 deletions(-) delete mode 100644 .github/ISSUE_TEMPLATE/bug_report.md diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md deleted file mode 100644 index 8fe9089..0000000 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ /dev/null @@ -1,31 +0,0 @@ ---- -name: Bug report -about: Create a report to help us improve cChoco -title: "(GH-ISSUE#) [BUG]" -labels: Bug, Up For Grabs, 0 - _Triaging -assignees: '' - ---- - -**Describe the bug** -A clear and concise description of what the bug is. - -**To Reproduce** -Steps to reproduce the behavior: -1. Go to '...' -2. Click on '....' -3. Scroll down to '....' -4. See error - -**Expected behavior** -A clear and concise description of what you expected to happen. - -**Screenshots** -If applicable, add screenshots to help explain your problem. - -**Software (please complete the following information):** - - OS: [e.g. Windows 10 1903 Build 12345] - - PowerShell Version [e.g. 5.1.123] - -**Additional context** -Add any other context about the problem here. From 22551ade59cca6e11f3c8bb7918d6407df49fe03 Mon Sep 17 00:00:00 2001 From: Paul Broadwith Date: Sun, 10 May 2020 18:29:39 +0100 Subject: [PATCH 07/13] (doc) Add issue template configuration --- .github/ISSUE_TEMPLATE/config.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/config.yml diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..3115469 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,5 @@ +blank_issues_enabled: false +contact_links: + - name: cChoco Community Support + url: https://gitter.im/chocolatey/cchoco/ + about: Please ask and answer questions here. From e9768a8d2a39978509710b1b83448453c025ef41 Mon Sep 17 00:00:00 2001 From: Paul Broadwith Date: Sun, 10 May 2020 18:31:00 +0100 Subject: [PATCH 08/13] Add pull request template --- .github/pull_request_template.md | 38 ++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 .github/pull_request_template.md diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..79090c7 --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,38 @@ + + + +## Description + + +## Related Issue + + + + +Fixes # + +## Motivation and Context + + +## How Has This Been Tested? + + + + +## Screenshots (if appropriate): + +## Types of changes + +- [ ] Bug fix (non-breaking change which fixes an issue) +- [ ] New feature (non-breaking change which adds functionality) +- [ ] Breaking change (fix or feature that would cause existing functionality to change) + +## Checklist: + + +- [ ] My code follows the code style of this project. +- [ ] My change requires a change to the documentation. +- [ ] I have updated the documentation accordingly. +- [ ] I have read the **CONTRIBUTING** document. +- [ ] I have added tests to cover my changes. +- [ ] All new and existing tests passed. From fcce72969f5cc1d20f505466866e6bae42e4a06d Mon Sep 17 00:00:00 2001 From: Scott Matthews Date: Sat, 23 May 2020 08:22:04 +0100 Subject: [PATCH 09/13] Adding comments and tests --- .../cChocoPackageInstall.psm1 | 7 +++-- Tests/cChocoPackageInstall_Tests.ps1 | 29 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index eb552d3..d088804 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -367,9 +367,12 @@ function IsPackageInstalled if ($pre) { throw "MinimumVersion does not support comparing pre-releases, please use Version parameter instead" } + $comparablePackages = $installedPackages | Where-Object { $_.Name -eq $pName} | ForEach-Object { - # ignoring pre-release as per above comment - $v = [System.Version](($_.Version -split "-")[0]) + # as mentioned above we cant convert prerelease versions to [Sytem.Version] so we ignore anything after "-" + # leaving just the . seperated numeric version. this is loosely equivalent to "rounding down" + $parseableVersion = ($_.Version -split "-")[0] + $v = [System.Version]($parseableVersion) $_ | Add-Member -MemberType NoteProperty -Name ComparableVersion -Value $v -PassThru } $installedPackages = $comparablePackages | Where-Object {$_.ComparableVersion -ge $pMinimumVersion} diff --git a/Tests/cChocoPackageInstall_Tests.ps1 b/Tests/cChocoPackageInstall_Tests.ps1 index 73aa392..4d790e2 100644 --- a/Tests/cChocoPackageInstall_Tests.ps1 +++ b/Tests/cChocoPackageInstall_Tests.ps1 @@ -160,6 +160,35 @@ Describe -Name "Testing $ResourceName loaded from $ResourceFile" -Fixture { Test-TargetResource @Scenario6 | Should Be $false } } + + Context -Name "Package is installed with prerelease version 1.0.0-1" -Fixture { + Mock -CommandName 'Get-ChocoInstalledPackage' -ModuleName 'cChocoPackageInstall' -MockWith { + return [pscustomobject]@{ + 'Name' = 'GoogleChrome' + 'Version' = '1.0.0-1' + } + } + + $Scenario1 = @{ + Name = 'GoogleChrome' + Ensure = 'Present' + MinimumVersion = '0.9.0' + } + + It -name "Test-TargetResource -ensure 'Present' -MinimumVersion '0.9.0' should return True" -test { + Test-TargetResource @Scenario1 | Should Be $true + } + + $Scenario2 = @{ + Name = 'GoogleChrome' + Ensure = 'Present' + MinimumVersion = '1.0.1' + } + + It -name "Test-TargetResource -ensure 'Present' -MinimumVersion '1.0.1' should return False" -test { + Test-TargetResource @Scenario2 | Should Be $false + } + } } #Clean-up From f8bb5f167aa293061f25e52fce817ef8d20a1b3e Mon Sep 17 00:00:00 2001 From: Maurice Kevenaar Date: Thu, 28 May 2020 17:08:38 +0200 Subject: [PATCH 10/13] (GH-20) add cChocoConfig resource This resource allows users to change Chocolatey Config settings using an DSC resource. --- DSCResources/cChocoConfig/cChocoConfig.psm1 | 188 ++++++++++++++++++ .../cChocoConfig/cChocoConfig.schema.mof | 9 + Examples/cChocoConfigExample.ps1 | 39 ++++ Tests/cChocoConfig_Tests.ps1 | 105 ++++++++++ 4 files changed, 341 insertions(+) create mode 100644 DSCResources/cChocoConfig/cChocoConfig.psm1 create mode 100644 DSCResources/cChocoConfig/cChocoConfig.schema.mof create mode 100644 Examples/cChocoConfigExample.ps1 create mode 100644 Tests/cChocoConfig_Tests.ps1 diff --git a/DSCResources/cChocoConfig/cChocoConfig.psm1 b/DSCResources/cChocoConfig/cChocoConfig.psm1 new file mode 100644 index 0000000..3163c83 --- /dev/null +++ b/DSCResources/cChocoConfig/cChocoConfig.psm1 @@ -0,0 +1,188 @@ +# Copyright (c) 2017 Chocolatey Software, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +<# +.Description +Returns the configuration for cChocoConfig. + +.Example +Get-TargetResource -ConfigName cacheLocation -Ensure 'Present' -Value 'c:\temp\choco' +#> +function Get-TargetResource +{ + [CmdletBinding()] + [OutputType([Hashtable])] + param + ( + [parameter(Mandatory = $true)] + [string] + $ConfigName, + + [ValidateSet('Present','Absent')] + [string] + $Ensure='Present', + + [parameter(Mandatory = $false)] + [string] + $Value + ) + + Write-Verbose "Starting cChocoConfig Get-TargetResource - Config Name: $ConfigName, Ensure: $Ensure" + + $returnValue = @{ + ConfigName = $ConfigName + Ensure = $Ensure + Value = $Value + } + + $returnValue + +} + +<# +.Description +Performs the set for the cChocoConfig resource. + +.Example +Set-TargetResource -ConfigName cacheLocation -Ensure 'Present' -Value 'c:\temp\choco' + +#> +function Set-TargetResource +{ + [CmdletBinding(SupportsShouldProcess=$true)] + param + ( + [parameter(Mandatory = $true)] + [string] + $ConfigName, + + [ValidateSet('Present','Absent')] + [string] + $Ensure='Present', + + [parameter(Mandatory = $false)] + [string] + $Value + ) + + + Write-Verbose "Starting cChocoConfig Set-TargetResource - Config Name: $ConfigName, Ensure: $Ensure" + + if ($pscmdlet.ShouldProcess("Choco config $ConfigName will be ensured $Ensure.")) + { + if ($Ensure -eq 'Present') + { + Write-Verbose "Setting choco config $ConfigName." + choco config set --name "'$ConfigName'" --value "'$Value'" + } + else + { + Write-Verbose "Unsetting choco config $ConfigName." + choco config unset --name "'$ConfigName'" + } + } + +} + +<# +.Description +Performs the test for cChocoFeature. + +.Example +Test-TargetResource -ConfigName cacheLocation -Ensure 'Present' -Value 'c:\temp\choco' +#> +function Test-TargetResource +{ + [CmdletBinding()] + [OutputType([Boolean])] + param + ( + [parameter(Mandatory = $true)] + [string] + $ConfigName, + + [ValidateSet('Present','Absent')] + [string] + $Ensure='Present', + + [parameter(Mandatory = $false)] + [string] + $Value + ) + + Write-Verbose "Starting cChocoConfig Test-TargetResource - Config Name: $ConfigName, Ensure: $Ensure." + + # validate value is given when ensure present + if ($Ensure -eq 'Present' -and (-not $PSBoundParameters.ContainsKey('Value') -or [String]::IsNullOrEmpty($Value))) { + throw "Missing parameter 'Value' when ensuring config is present!" + } + + if($env:ChocolateyInstall -eq "" -or $null -eq $env:ChocolateyInstall) + { + $command = Get-Command -Name choco.exe -ErrorAction SilentlyContinue + + if(!$command) { + throw "Unable to find choco.exe. Please make sure Chocolatey is installed correctly." + } + + $chocofolder = Split-Path $command.Source + + if( $chocofolder.EndsWith("bin") ) + { + $chocofolder = Split-Path $chocofolder + } + } + else + { + $chocofolder = $env:ChocolateyInstall + } + + if(!(Get-Item -Path $chocofolder -ErrorAction SilentlyContinue)) { + throw "Unable to find Chocolatey installation folder. Please make sure Chocolatey is installed and configured properly." + } + + $configfolder = Join-Path -Path $chocofolder -ChildPath "config" + $configfile = Get-ChildItem -Path $configfolder | Where-Object {$_.Name -match "chocolatey.config$"} + + if(!(Get-Item -Path $configfile.FullName -ErrorAction SilentlyContinue)) { + throw "Unable to find Chocolatey config file. Please make sure Chocolatey is installed and configured properly." + } + + # There is currently no choco command that only returns the settings in an CSV format. + # choco config list -r shows settings, sources, features and a note about API keys. + $xml = [xml](Get-Content -Path $configfile.FullName) + $settings = $xml.chocolatey.config.add + foreach($setting in $settings) + { + # If the config name matches and it should be present, check the value and + # if it matches it returns true. + if($setting.key -eq $ConfigName -and $Ensure -eq 'Present') + { + return ($setting.value -eq $Value) + } + # If the config name matches and it should be absent, check the value and + # if it is null or empty, return true + elseif($setting.key -eq $ConfigName -and $Ensure -eq 'Absent') + { + return ([String]::IsNullOrEmpty($setting.value)) + } + } + + # If we get this far, the configuraion item hasn't been found. + # There is currently no value, so return false if it should be present. + # True otherwise. + return !($Ensure -eq 'Present') +} + +Export-ModuleMember -Function *-TargetResource diff --git a/DSCResources/cChocoConfig/cChocoConfig.schema.mof b/DSCResources/cChocoConfig/cChocoConfig.schema.mof new file mode 100644 index 0000000..a371774 --- /dev/null +++ b/DSCResources/cChocoConfig/cChocoConfig.schema.mof @@ -0,0 +1,9 @@ + +[ClassVersion("1.0.0.0"), FriendlyName("cChocoConfig")] +class cChocoConfig : OMI_BaseResource +{ + [Key] String ConfigName; + [Write,ValueMap{"Present", "Absent"},Values{"Present", "Absent"}] String Ensure; + [Write] String Value; +}; + diff --git a/Examples/cChocoConfigExample.ps1 b/Examples/cChocoConfigExample.ps1 new file mode 100644 index 0000000..d0a3693 --- /dev/null +++ b/Examples/cChocoConfigExample.ps1 @@ -0,0 +1,39 @@ +# Copyright (c) 2017 Chocolatey Software, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +configuration ChocoConfig { + + Import-DscResource -ModuleName cChoco + + Node 'localhost' { + + cChocoConfig webRequestTimeoutSeconds { + ConfigName = "webRequestTimeoutSeconds" + Ensure = 'Present' + Value = 30 + } + + cChocoConfig proxy { + ConfigName = "proxy" + Ensure = 'Absent' + } + } + +} + + +$config = ChocoConfig + +Start-DscConfiguration -Path $config.psparentpath -Wait -Verbose -Force diff --git a/Tests/cChocoConfig_Tests.ps1 b/Tests/cChocoConfig_Tests.ps1 new file mode 100644 index 0000000..8a5e897 --- /dev/null +++ b/Tests/cChocoConfig_Tests.ps1 @@ -0,0 +1,105 @@ +# Copyright (c) 2017 Chocolatey Software, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +$ResourceName = ((Split-Path $MyInvocation.MyCommand.Path -Leaf) -split '_')[0] +$ResourceFile = (Get-DscResource -Name $ResourceName).Path + +$TestsPath = (split-path -path $MyInvocation.MyCommand.Path -Parent) +$ResourceFile = Get-ChildItem -Recurse $TestsPath\.. -File | Where-Object {$_.name -eq "$ResourceName.psm1"} + +Import-Module -Name $ResourceFile.FullName + + +#---------------------------------# +# Pester tests for cChocoConfig # +#---------------------------------# +Describe "Testing cChocoConfig" { + + Context "Test-TargetResource" { + + mock -ModuleName cChocoConfig -CommandName Get-Content -MockWith {' + + + + + + + + +' + } -Verifiable + + it 'Test-TargetResource returns true when Present and Configured.' { + Test-TargetResource -ConfigName 'commandExecutionTimeoutSeconds' -Ensure 'Present' -Value '1339' | Should be $true + } + + it 'Test-TargetResource returns false when Present and Not configured' { + Test-TargetResource -ConfigName 'proxy' -Ensure 'Present' -Value 'http://myproxy.url' | Should be $false + } + + it 'Test-TargetResource returns false when Present and Unknown' { + Test-TargetResource -ConfigName 'MyParam' -Ensure 'Present' -Value 'MyValue' | Should be $false + } + + it 'Test-TargetResource throws when Present and no value' { + { Test-TargetResource -ConfigName 'MyParam' -Ensure 'Present' } | Should -Throw "Missing parameter 'Value' when ensuring config is present!" + } + + it 'Test-TargetResource throws when Present and no value' { + { Test-TargetResource -ConfigName 'MyParam' -Ensure 'Present' -Value '' } | Should -Throw "Missing parameter 'Value' when ensuring config is present!" + } + + it 'Test-TargetResource throws when Present and no value' { + { Test-TargetResource -ConfigName 'MyParam' -Ensure 'Present' -Value $null } | Should -Throw "Missing parameter 'Value' when ensuring config is present!" + } + + it 'Test-TargetResource returns false when Absent and Configured' { + Test-TargetResource -ConfigName 'commandExecutionTimeoutSeconds' -Ensure 'Absent' | Should be $false + } + + it 'Test-TargetResource returns true when Absent and Not configured' { + Test-TargetResource -ConfigName 'proxy' -Ensure 'Absent' | Should be $true + } + + it 'Test-TargetResource returns true when Absent and Unknown' { + Test-TargetResource -ConfigName 'MyParam' -Ensure 'Absent' | Should be $true + } + + } + + Context "Set-TargetResource" { + + InModuleScope -ModuleName cChocoConfig -ScriptBlock { + function choco {} + mock choco {} + } + + Set-TargetResource -ConfigName "TestConfig" -Ensure "Present" -Value "MyValue" + + it "Present - Should have called choco, with set" { + Assert-MockCalled -CommandName choco -ModuleName cChocoConfig -ParameterFilter { + $args -contains "'MyValue'" + } + } + + Set-TargetResource -ConfigName "TestConfig" -Ensure "Absent" + + it "Absent - Should have called choco, with unset" { + Assert-MockCalled -CommandName choco -ModuleName cChocoConfig -ParameterFilter { + $args -contains "unset" + } + } + } +} \ No newline at end of file From cf289923765c390bcd601314f330aa4286a3ba8a Mon Sep 17 00:00:00 2001 From: Maurice Kevenaar Date: Thu, 28 May 2020 17:12:28 +0200 Subject: [PATCH 11/13] (maint) Set pester to a max version of 4.10.1 --- AppVeyor/AppVeyorInstall.ps1 | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/AppVeyor/AppVeyorInstall.ps1 b/AppVeyor/AppVeyorInstall.ps1 index 51693d8..c47628a 100644 --- a/AppVeyor/AppVeyorInstall.ps1 +++ b/AppVeyor/AppVeyorInstall.ps1 @@ -18,6 +18,11 @@ #---------------------------------# Write-Host 'Running AppVeyor install script' -ForegroundColor Yellow +#---------------------------------# +# Enable TLS 1.2 # +#---------------------------------# +[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072 + #---------------------------------# # Install NuGet # #---------------------------------# @@ -29,8 +34,9 @@ Write-Host "Installed NuGet version '$($pkg.version)'" # Install Modules # #---------------------------------# [version]$ScriptAnalyzerVersion = '1.8.1' +[version]$PesterVersion = '4.10.1' Install-Module -Name 'PSScriptAnalyzer' -Repository PSGallery -Force -ErrorAction Stop -MaximumVersion $ScriptAnalyzerVersion -Install-Module -Name 'Pester' -SkipPublisherCheck -Repository PSGallery -Force -ErrorAction Stop +Install-Module -Name 'Pester' -SkipPublisherCheck -Repository PSGallery -Force -ErrorAction Stop -MaximumVersion $PesterVersion Install-Module -Name 'xDSCResourceDesigner' -Repository PSGallery -Force -ErrorAction Stop #---------------------------------# From fbb7835ab44e555a23256126074a59ac09a31777 Mon Sep 17 00:00:00 2001 From: Paul Broadwith Date: Tue, 9 Feb 2021 14:27:07 +0000 Subject: [PATCH 12/13] (GH-151) Download install.ps1 to temp folder. Changes in the Chocolatey install.ps1 means downloading to Choco install folder stops the install. Amended the code to download the install.ps1 file to a temporary folder and execute it from there. --- DSCResources/cChocoInstaller/cChocoInstaller.psm1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/DSCResources/cChocoInstaller/cChocoInstaller.psm1 b/DSCResources/cChocoInstaller/cChocoInstaller.psm1 index e4e096c..81c7e5f 100644 --- a/DSCResources/cChocoInstaller/cChocoInstaller.psm1 +++ b/DSCResources/cChocoInstaller/cChocoInstaller.psm1 @@ -190,7 +190,9 @@ Function Install-Chocolatey { Write-Verbose "Env:ChocolateyInstall has $env:ChocolateyInstall" #Download an execute install script - $file = Join-Path -Path $InstallDir -ChildPath 'install.ps1' + $tempPath = Join-Path -Path $env:TEMP -ChildPath ([GUID]::NewGuid().ToString()) + New-Item -Path $tempPath -ItemType Directory | Out-Null + $file = Join-Path -Path $tempPath -ChildPath 'install.ps1' Get-FileDownload -url $ChocoInstallScriptUrl -file $file . $file From 9f5af032530d2eaf7534af27a9b55a75b54786f9 Mon Sep 17 00:00:00 2001 From: Paul Broadwith Date: Tue, 9 Feb 2021 14:47:34 +0000 Subject: [PATCH 13/13] Update module versions for release --- appveyor.yml | 2 +- cChoco.psd1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 9eccc71..fbd661c 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,7 +1,7 @@ #---------------------------------# # environment configuration # #---------------------------------# -version: 2.4.0.{build} +version: 2.5.0.{build} os: WMF 5 install: - ps: . .\AppVeyor\AppVeyorInstall.ps1 diff --git a/cChoco.psd1 b/cChoco.psd1 index aa74da1..6e16d52 100644 --- a/cChoco.psd1 +++ b/cChoco.psd1 @@ -8,5 +8,5 @@ CLRVersion = "4.0"; CmdletsToExport = "*"; Author = "Chocolatey Software, Lawrence Gripper, Javy de Koning"; - ModuleVersion = "2.4.0.0" + ModuleVersion = "2.5.0.0" }