summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2018-12-07 09:31:08 -0800
committerStuart Preston <stuart@chef.io>2018-12-14 09:19:04 +0000
commit277302b92c723557e8f49d59c30c14dc19144a9e (patch)
tree0805091bcf4a9a35b3633c0314aa00cc655fac6e
parent9a3c016feebcb2116a016e1eb8a96427aa581b3e (diff)
downloadchef-277302b92c723557e8f49d59c30c14dc19144a9e.tar.gz
Merge pull request #8025 from Intility/bugfixessp/chef14-backport-8025
Bugfixes to powershell_package_source
-rw-r--r--lib/chef/resource/powershell_package_source.rb18
-rw-r--r--spec/unit/resource/powershell_package_source_spec.rb4
2 files changed, 13 insertions, 9 deletions
diff --git a/lib/chef/resource/powershell_package_source.rb b/lib/chef/resource/powershell_package_source.rb
index b3bad41f65..9d2a0ae6cc 100644
--- a/lib/chef/resource/powershell_package_source.rb
+++ b/lib/chef/resource/powershell_package_source.rb
@@ -55,10 +55,14 @@ class Chef
description: "The location where scripts will be published to for this source. Only valid if the provider is 'PowerShellGet'."
load_current_value do
- cmd = load_resource_state_script(name)
+ cmd = load_resource_state_script(source_name)
repo = powershell_out!(cmd)
- status = Chef::JSONCompat.from_json(repo.stdout)
- url status["url"].nil? ? "not_set" : status["url"]
+ if repo.stdout.empty?
+ current_value_does_not_exist!
+ else
+ status = Chef::JSONCompat.from_json(repo.stdout)
+ end
+ url status["url"]
trusted status["trusted"]
provider_name status["provider_name"]
publish_location status["publish_location"]
@@ -113,7 +117,7 @@ class Chef
action_class do
def package_source_exists?
- cmd = powershell_out!("(Get-PackageSource -Name '#{new_resource.source_name}').Name")
+ cmd = powershell_out!("(Get-PackageSource -Name '#{new_resource.source_name}' -WarningAction SilentlyContinue).Name")
cmd.stdout.downcase.strip == new_resource.source_name.downcase
end
@@ -145,6 +149,9 @@ class Chef
def load_resource_state_script(name)
<<-EOH
+ $PSDefaultParameterValues = @{
+ "*:WarningAction" = "SilentlyContinue"
+ }
if(Get-PackageSource -Name '#{name}' -ErrorAction SilentlyContinue) {
if ((Get-PackageSource -Name '#{name}').ProviderName -eq 'PowerShellGet') {
(Get-PSRepository -Name '#{name}') | Select @{n='source_name';e={$_.Name}}, @{n='url';e={$_.SourceLocation}},
@@ -156,9 +163,6 @@ class Chef
@{n='provider_name';e={$_.ProviderName}}, @{n='trusted';e={$_.IsTrusted}} | ConvertTo-Json
}
}
- else {
- "" | Select source_name, url, provider_name, trusted | ConvertTo-Json
- }
EOH
end
end
diff --git a/spec/unit/resource/powershell_package_source_spec.rb b/spec/unit/resource/powershell_package_source_spec.rb
index 4033f4515f..2640d9f3c5 100644
--- a/spec/unit/resource/powershell_package_source_spec.rb
+++ b/spec/unit/resource/powershell_package_source_spec.rb
@@ -205,13 +205,13 @@ describe Chef::Resource::PowershellPackageSource do
describe "#package_source_exists?" do
it "returns true if it exists" do
- allow(provider).to receive(:powershell_out!).with("(Get-PackageSource -Name 'MyGallery').Name").and_return(double("powershell_out!", stdout: "MyGallery\r\n"))
+ allow(provider).to receive(:powershell_out!).with("(Get-PackageSource -Name 'MyGallery' -WarningAction SilentlyContinue).Name").and_return(double("powershell_out!", stdout: "MyGallery\r\n"))
resource.source_name("MyGallery")
expect(provider.package_source_exists?).to eql(true)
end
it "returns false if it doesn't exist" do
- allow(provider).to receive(:powershell_out!).with("(Get-PackageSource -Name 'MyGallery').Name").and_return(double("powershell_out!", stdout: ""))
+ allow(provider).to receive(:powershell_out!).with("(Get-PackageSource -Name 'MyGallery' -WarningAction SilentlyContinue).Name").and_return(double("powershell_out!", stdout: ""))
resource.source_name("MyGallery")
expect(provider.package_source_exists?).to eql(false)
end