summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/chef/exceptions.rb1
-rw-r--r--lib/chef/role.rb8
-rw-r--r--spec/unit/role_spec.rb6
3 files changed, 13 insertions, 2 deletions
diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb
index 782ecc3fd8..2e9cbbe0c0 100644
--- a/lib/chef/exceptions.rb
+++ b/lib/chef/exceptions.rb
@@ -64,6 +64,7 @@ class Chef
class PrivateKeyMissing < RuntimeError; end
class CannotWritePrivateKey < RuntimeError; end
class RoleNotFound < RuntimeError; end
+ class DuplicateRole < RuntimeError; end
class ValidationFailed < ArgumentError; end
class InvalidPrivateKey < ArgumentError; end
class ConfigurationError < ArgumentError; end
diff --git a/lib/chef/role.rb b/lib/chef/role.rb
index 6f2c6318fc..26bf574737 100644
--- a/lib/chef/role.rb
+++ b/lib/chef/role.rb
@@ -236,8 +236,12 @@ class Chef
paths = Array(Chef::Config[:role_path])
paths.each do |path|
roles_files = Dir.glob(File.join(path, "**", "**"))
- js_path = roles_files.detect { |file| file.match /#{name}\.json$/ }
- rb_path = roles_files.detect { |file| file.match /#{name}\.rb$/ }
+ js_files = roles_files.select { |file| file.match /#{name}\.json$/ }
+ rb_files = roles_files.select { |file| file.match /#{name}\.rb$/ }
+ if js_files.count > 1 or rb_files.count > 1
+ raise Chef::Exceptions::DuplicateRole, "Multiple roles of same type found named #{name}"
+ end
+ js_path, rb_path = js_files.first, rb_files.first
if js_path && (File.exists?(js_path) || force == "json")
# from_json returns object.class => json_class in the JSON.
diff --git a/spec/unit/role_spec.rb b/spec/unit/role_spec.rb
index caa98f66b5..5466a5b4e6 100644
--- a/spec/unit/role_spec.rb
+++ b/spec/unit/role_spec.rb
@@ -290,6 +290,12 @@ EOR
File.should_not_receive(:exists?)
lambda {@role.class.from_disk("lolcat")}.should raise_error(Chef::Exceptions::RoleNotFound)
end
+
+ it "should raise an exception if two files exist with the same name" do
+ Dir.should_receive(:glob).and_return(["#{Chef::Config[:role_path]}/memes/lolcat.rb", "#{Chef::Config[:role_path]}/lolcat.rb"])
+ File.should_not_receive(:exists?)
+ lambda {@role.class.from_disk("lolcat")}.should raise_error(Chef::Exceptions::DuplicateRole)
+ end
end
describe "when loading from disk and role_path is an array" do