summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2015-12-14 12:53:05 -0800
committerLamont Granquist <lamont@scriptkiddie.org>2015-12-14 12:53:05 -0800
commit491e99e796673b5a3762d5a47ed51fabf1b6f8f1 (patch)
treee0da8b81f1bb6c73afe06a85784f32dd8127348e
parentb60ad97446c064c71e5e1b03b94344eb0b5c136d (diff)
parentfe9fefeeb4c792e3db648482b0ad17b80ab5d578 (diff)
downloadchef-491e99e796673b5a3762d5a47ed51fabf1b6f8f1.tar.gz
Merge pull request #4278 from chef/lcg/file-resource-properties
make file resource use properties
-rw-r--r--CHANGELOG.md4
-rw-r--r--lib/chef/provider/file.rb2
-rw-r--r--lib/chef/resource/file.rb91
-rw-r--r--spec/support/shared/unit/provider/file.rb23
-rw-r--r--spec/unit/resource/cookbook_file_spec.rb2
5 files changed, 24 insertions, 98 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e1486c716a..3a843ce2a4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,4 +1,8 @@
+## Unreleased
+
+* [pr#4278](https://github.com/chef/chef/pull/4278) make file resource use properties
+
## 12.6.0
* [**Dave Eddy**](https://github.com/bahamas10)
diff --git a/lib/chef/provider/file.rb b/lib/chef/provider/file.rb
index 5ed7c6ac5b..4d9f07d2b2 100644
--- a/lib/chef/provider/file.rb
+++ b/lib/chef/provider/file.rb
@@ -1,7 +1,7 @@
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Author:: Lamont Granquist (<lamont@opscode.com>)
-# Copyright:: Copyright (c) 2008-2013 Opscode, Inc.
+# Copyright:: Copyright (c) 2008-2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
diff --git a/lib/chef/resource/file.rb b/lib/chef/resource/file.rb
index d278652cc3..31afecc156 100644
--- a/lib/chef/resource/file.rb
+++ b/lib/chef/resource/file.rb
@@ -1,7 +1,7 @@
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Author:: Seth Chisamore (<schisamo@opscode.com>)
-# Copyright:: Copyright (c) 2008, 2011 Opscode, Inc.
+# Copyright:: Copyright (c) 2008, 2011-2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -27,8 +27,6 @@ class Chef
class File < Chef::Resource
include Chef::Mixin::Securable
- identity_attr :path
-
if Platform.windows?
# Use Windows rights instead of standard *nix permissions
state_attrs :checksum, :rights, :deny_rights
@@ -50,80 +48,15 @@ class Chef
default_action :create
allowed_actions :create, :delete, :touch, :create_if_missing
- def initialize(name, run_context=nil)
- super
- @path = name
- @backup = 5
- @atomic_update = Chef::Config[:file_atomic_update]
- @force_unlink = false
- @manage_symlink_source = nil
- @diff = nil
- @verifications = []
- end
-
- def content(arg=nil)
- set_or_return(
- :content,
- arg,
- :kind_of => String
- )
- end
-
- def backup(arg=nil)
- set_or_return(
- :backup,
- arg,
- :kind_of => [ Integer, FalseClass ]
- )
- end
-
- def checksum(arg=nil)
- set_or_return(
- :checksum,
- arg,
- :regex => /^[a-zA-Z0-9]{64}$/
- )
- end
-
- def path(arg=nil)
- set_or_return(
- :path,
- arg,
- :kind_of => String
- )
- end
-
- def diff(arg=nil)
- set_or_return(
- :diff,
- arg,
- :kind_of => String
- )
- end
-
- def atomic_update(arg=nil)
- set_or_return(
- :atomic_update,
- arg,
- :kind_of => [ TrueClass, FalseClass ]
- )
- end
-
- def force_unlink(arg=nil)
- set_or_return(
- :force_unlink,
- arg,
- :kind_of => [ TrueClass, FalseClass ]
- )
- end
-
- def manage_symlink_source(arg=nil)
- set_or_return(
- :manage_symlink_source,
- arg,
- :kind_of => [ TrueClass, FalseClass ]
- )
- end
+ property :path, String, name_property: true, identity: true
+ property :atomic_update, [ true, false ], desired_state: false, default: Chef::Config[:file_atomic_update]
+ property :backup, [ Integer, false ], desired_state: false, default: 5
+ property :checksum, [ /^[a-zA-Z0-9]{64}$/, nil ]
+ property :content, [ String, nil ], desired_state: false
+ property :diff, [ String, nil ], desired_state: false
+ property :force_unlink, [ true, false ], desired_state: false, default: false
+ property :manage_symlink_source, [ true, false ], desired_state: false
+ property :verifications, Array, default: lazy { [] }
def verify(command=nil, opts={}, &block)
if ! (command.nil? || [String, Symbol].include?(command.class))
@@ -131,9 +64,9 @@ class Chef
end
if command || block_given?
- @verifications << Verification.new(self, command, opts, &block)
+ verifications << Verification.new(self, command, opts, &block)
else
- @verifications
+ verifications
end
end
diff --git a/spec/support/shared/unit/provider/file.rb b/spec/support/shared/unit/provider/file.rb
index ff9e271a0a..973cb047f7 100644
--- a/spec/support/shared/unit/provider/file.rb
+++ b/spec/support/shared/unit/provider/file.rb
@@ -1,6 +1,6 @@
#
# Author:: Lamont Granquist (<lamont@opscode.com>)
-# Copyright:: Copyright (c) 2013 Opscode, Inc.
+# Copyright:: Copyright (c) 2013-2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -465,28 +465,17 @@ shared_examples_for Chef::Provider::File do
t
}
- let(:verification) { instance_double(Chef::Resource::File::Verification) }
- let(:verification_fail) { instance_double(Chef::Resource::File::Verification) }
-
context "with user-supplied verifications" do
it "calls #verify on each verification with tempfile path" do
- allow(Chef::Resource::File::Verification).to(
- receive(:new).with(anything(), "true", anything()).and_return(verification))
- provider.new_resource.verify "true"
- provider.new_resource.verify "true"
- expect(verification).to receive(:verify).with(tempfile.path).twice.and_return(true)
+ provider.new_resource.verify windows? ? "REM" : "true"
+ provider.new_resource.verify windows? ? "REM" : "true"
provider.send(:do_validate_content)
end
it "raises an exception if any verification fails" do
- allow(Chef::Resource::File::Verification).to(
- receive(:new).with(anything(), "true", anything()).and_return(verification))
- allow(Chef::Resource::File::Verification).to(
- receive(:new).with(anything(), "false", anything()).and_return(verification_fail))
- provider.new_resource.verify "true"
- provider.new_resource.verify "false"
- expect(verification).to receive(:verify).with(tempfile.path).and_return(true)
- expect(verification_fail).to receive(:verify).with(tempfile.path).and_return(false)
+ allow(File).to receive(:directory?).with("C:\\Windows\\system32/cmd.exe").and_return(false)
+ provider.new_resource.verify windows? ? "REM" : "true"
+ provider.new_resource.verify windows? ? "cmd.exe /c exit 1" : "false"
expect{provider.send(:do_validate_content)}.to raise_error(Chef::Exceptions::ValidationFailed)
end
end
diff --git a/spec/unit/resource/cookbook_file_spec.rb b/spec/unit/resource/cookbook_file_spec.rb
index 834e08bba4..bd437893ce 100644
--- a/spec/unit/resource/cookbook_file_spec.rb
+++ b/spec/unit/resource/cookbook_file_spec.rb
@@ -1,7 +1,7 @@
#
# Author:: Daniel DeLeo (<dan@opscode.com>)
# Author:: Tyler Cloke (<tyler@opscode.com>)
-# Copyright:: Copyright (c) 2010 Opscode, Inc.
+# Copyright:: Copyright (c) 2010-2015 Chef Software, Inc.
#p License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");