summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/chef/cookbook/syntax_check.rb7
-rw-r--r--spec/data/cookbooks/openldap/recipes/default.rb1
-rw-r--r--spec/data/cookbooks/openldap/recipes/return.rb2
-rw-r--r--spec/unit/cookbook/syntax_check_spec.rb9
4 files changed, 14 insertions, 5 deletions
diff --git a/lib/chef/cookbook/syntax_check.rb b/lib/chef/cookbook/syntax_check.rb
index effc7dd01d..9fee7c2a54 100644
--- a/lib/chef/cookbook/syntax_check.rb
+++ b/lib/chef/cookbook/syntax_check.rb
@@ -247,7 +247,12 @@ class Chef
tmp_stderr = $stderr = StringIO.new
abs_path = File.expand_path(ruby_file)
file_content = IO.read(abs_path)
- RubyVM::InstructionSequence.new(file_content, ruby_file, abs_path, 0)
+ # We have to wrap this in a block so the user code evaluates in a
+ # similar context as what Chef does normally. Otherwise RubyVM
+ # will reject some common idioms, like using `return` to end evaluation
+ # of a recipe. See also CHEF-5199
+ wrapped_content = "Object.new.instance_eval do\n#{file_content}\nend\n"
+ RubyVM::InstructionSequence.new(wrapped_content, ruby_file, abs_path, 0)
true
rescue SyntaxError
$stderr = old_stderr
diff --git a/spec/data/cookbooks/openldap/recipes/default.rb b/spec/data/cookbooks/openldap/recipes/default.rb
index 0ac8a9bb4b..ba5c9d1507 100644
--- a/spec/data/cookbooks/openldap/recipes/default.rb
+++ b/spec/data/cookbooks/openldap/recipes/default.rb
@@ -1,3 +1,4 @@
+
cat "blanket" do
pretty_kitty true
end
diff --git a/spec/data/cookbooks/openldap/recipes/return.rb b/spec/data/cookbooks/openldap/recipes/return.rb
new file mode 100644
index 0000000000..79bfb5e441
--- /dev/null
+++ b/spec/data/cookbooks/openldap/recipes/return.rb
@@ -0,0 +1,2 @@
+# CHEF-5199 regression test.
+return nil
diff --git a/spec/unit/cookbook/syntax_check_spec.rb b/spec/unit/cookbook/syntax_check_spec.rb
index a674f6ab40..7f7a914115 100644
--- a/spec/unit/cookbook/syntax_check_spec.rb
+++ b/spec/unit/cookbook/syntax_check_spec.rb
@@ -24,7 +24,7 @@ describe Chef::Cookbook::SyntaxCheck do
let(:cookbook_path) { File.join(CHEF_SPEC_DATA, 'cookbooks', 'openldap') }
let(:syntax_check) { Chef::Cookbook::SyntaxCheck.new(cookbook_path) }
- let(:open_ldap_cookbook_files) {
+ let(:open_ldap_cookbook_files) do
%w{ attributes/default.rb
attributes/smokey.rb
definitions/client.rb
@@ -32,8 +32,9 @@ describe Chef::Cookbook::SyntaxCheck do
metadata.rb
recipes/default.rb
recipes/gigantor.rb
- recipes/one.rb }.map{ |f| File.join(cookbook_path, f) }
-}
+ recipes/one.rb
+ recipes/return.rb }.map{ |f| File.join(cookbook_path, f) }
+ end
before do
Chef::Log.logger = Logger.new(StringIO.new)
@@ -41,7 +42,7 @@ describe Chef::Cookbook::SyntaxCheck do
@attr_files = %w{default.rb smokey.rb}.map { |f| File.join(cookbook_path, 'attributes', f) }
@defn_files = %w{client.rb server.rb}.map { |f| File.join(cookbook_path, 'definitions', f)}
- @recipes = %w{default.rb gigantor.rb one.rb}.map { |f| File.join(cookbook_path, 'recipes', f) }
+ @recipes = %w{default.rb gigantor.rb one.rb return.rb}.map { |f| File.join(cookbook_path, 'recipes', f) }
@ruby_files = @attr_files + @defn_files + @recipes + [File.join(cookbook_path, "metadata.rb")]
basenames = %w{ helpers_via_partial_test.erb
helper_test.erb