Commit 345ccdb8 authored by danielsdeleo's avatar danielsdeleo Committed by sersut
Browse files

Wrap code in an instance_eval context for syntax check.

This evaluates the code in a context similar to what Chef does when
compiling recipes, so that idioms like using `return` to end evaluation
of a recipe will work.
parent 92f6e83a
......@@ -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
......
# CHEF-5199 regression test.
return nil
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment