diff options
-rw-r--r-- | lib/chef/mixin/uris.rb | 33 | ||||
-rw-r--r-- | lib/chef/provider/package/windows.rb | 24 | ||||
-rw-r--r-- | lib/chef/resource/windows_package.rb | 16 | ||||
-rw-r--r-- | spec/unit/provider/package/windows_spec.rb | 4 |
4 files changed, 45 insertions, 32 deletions
diff --git a/lib/chef/mixin/uris.rb b/lib/chef/mixin/uris.rb new file mode 100644 index 0000000000..40b95d082e --- /dev/null +++ b/lib/chef/mixin/uris.rb @@ -0,0 +1,33 @@ +# +# Author:: Jay Mundrawala (<jdm@chef.io>) +# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# 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. +# + +require 'uri' + +class Chef + module Mixin + module Uris + def uri_scheme?(source) + begin + !URI.split(source).first.nil? + rescue URI::InvalidURIError + return false + end + end + end + end +end diff --git a/lib/chef/provider/package/windows.rb b/lib/chef/provider/package/windows.rb index e944352add..543c94fac8 100644 --- a/lib/chef/provider/package/windows.rb +++ b/lib/chef/provider/package/windows.rb @@ -16,15 +16,16 @@ # limitations under the License. # +require 'chef/mixin/uris' require 'chef/resource/windows_package' require 'chef/provider/package' require 'chef/util/path_helper' -require 'uri' class Chef class Provider class Package class Windows < Chef::Provider::Package + include Chef::Mixin::Uris provides :package, os: "windows" provides :windows_package, os: "windows" @@ -38,7 +39,7 @@ class Chef # load_current_resource is run in Chef::Provider#run_action when not in whyrun_mode? def load_current_resource @current_resource = Chef::Resource::WindowsPackage.new(@new_resource.name) - if should_download? + if download_file_missing? Chef::Log.debug("We do not know the version of #{new_resource.source} because the file is not downloaded") current_resource.version(:unknown.to_s) else @@ -77,7 +78,7 @@ class Chef end def action_install - if should_download? + if uri_scheme?(new_resource.source) download_source_file load_current_resource end @@ -102,8 +103,8 @@ class Chef private - def should_download? - is_url?(new_resource.source) && !::File.exists?(source_location) + def download_file_missing? + uri_scheme?(new_resource.source) && !::File.exists?(source_location) end def resource_for_provider @@ -129,7 +130,7 @@ class Chef def source_location @source_location ||= begin - if is_url?(new_resource.source) + if uri_scheme?(new_resource.source) uri = ::URI.parse(new_resource.source) filename = ::File.basename(::URI.unescape(uri.path)) file_cache_dir = Chef::FileCache.create_cache_path("package/") @@ -139,17 +140,6 @@ class Chef end end end - - def is_url?(source) - begin - scheme = URI.split(source).first - return false unless scheme - %w(http https ftp file).include?(scheme.downcase) - rescue URI::InvalidURIError - return false - end - end - end end end diff --git a/lib/chef/resource/windows_package.rb b/lib/chef/resource/windows_package.rb index de4f2573d4..c02d38fba9 100644 --- a/lib/chef/resource/windows_package.rb +++ b/lib/chef/resource/windows_package.rb @@ -16,6 +16,7 @@ # limitations under the License. # +require 'chef/mixin/uris' require 'chef/resource/package' require 'chef/provider/package/windows' require 'chef/win32/error' if RUBY_PLATFORM =~ /mswin|mingw|windows/ @@ -23,6 +24,7 @@ require 'chef/win32/error' if RUBY_PLATFORM =~ /mswin|mingw|windows/ class Chef class Resource class WindowsPackage < Chef::Resource::Package + include Chef::Mixin::Uris provides :package, os: "windows" provides :windows_package, os: "windows" @@ -69,7 +71,7 @@ class Chef @source else raise ArgumentError, "Bad type for WindowsPackage resource, use a String" unless arg.is_a?(String) - if is_url?(arg) + if uri_scheme?(arg) @source = arg else @source = ::File.absolute_path(arg).gsub(::File::SEPARATOR, ::File::ALT_SEPARATOR) @@ -77,18 +79,6 @@ class Chef end end - private - - def is_url?(source) - begin - scheme = URI.split(source).first - return false unless scheme - %w(http https ftp file).include?(scheme.downcase) - rescue URI::InvalidURIError - return false - end - end - end end end diff --git a/spec/unit/provider/package/windows_spec.rb b/spec/unit/provider/package/windows_spec.rb index b42f112037..ab1ce103e0 100644 --- a/spec/unit/provider/package/windows_spec.rb +++ b/spec/unit/provider/package/windows_spec.rb @@ -62,7 +62,7 @@ describe Chef::Provider::Package::Windows, :windows_only do context "when the source has not been downloaded" do before(:each) do - allow(provider).to receive(:should_download?).and_return(true) + allow(provider).to receive(:download_file_missing?).and_return(true) end it "sets the current version to unknown" do provider.load_current_resource @@ -72,7 +72,7 @@ describe Chef::Provider::Package::Windows, :windows_only do context "when the source has been downloaded" do before(:each) do - allow(provider).to receive(:should_download?).and_return(false) + allow(provider).to receive(:download_file_missing?).and_return(false) end it_behaves_like "a local file" end |