require "support/shared/integration/integration_helper" # Houses any classes we declare module ResourceActionSpec describe "Resource.action" do include IntegrationSupport shared_context "ActionJackson" do it "the default action is the first declared action" do converge <<-EOM, __FILE__, __LINE__ + 1 #{resource_dsl} "hi" do foo "foo!" end EOM expect(ActionJackson.ran_action).to eq :access_recipe_dsl expect(ActionJackson.succeeded).to eq true end context "when running in whyrun mode" do before do Chef::Config[:why_run] = true end it "the default action runs" do converge <<-EOM, __FILE__, __LINE__ + 1 #{resource_dsl} "hi" do foo "foo!" end EOM expect(ActionJackson.ran_action).to eq :access_recipe_dsl expect(ActionJackson.succeeded).to eq true end end it "the action can access recipe DSL" do converge <<-EOM, __FILE__, __LINE__ + 1 #{resource_dsl} "hi" do foo "foo!" action :access_recipe_dsl end EOM expect(ActionJackson.ran_action).to eq :access_recipe_dsl expect(ActionJackson.succeeded).to eq true end it "the action can access attributes" do converge <<-EOM, __FILE__, __LINE__ + 1 #{resource_dsl} "hi" do foo "foo!" action :access_attribute end EOM expect(ActionJackson.ran_action).to eq :access_attribute expect(ActionJackson.succeeded).to eq "foo!" end it "the action can access public methods" do converge <<-EOM, __FILE__, __LINE__ + 1 #{resource_dsl} "hi" do foo "foo!" action :access_method end EOM expect(ActionJackson.ran_action).to eq :access_method expect(ActionJackson.succeeded).to eq "foo_public!" end it "the action can access protected methods" do converge <<-EOM, __FILE__, __LINE__ + 1 #{resource_dsl} "hi" do foo "foo!" action :access_protected_method end EOM expect(ActionJackson.ran_action).to eq :access_protected_method expect(ActionJackson.succeeded).to eq "foo_protected!" end it "the action cannot access private methods" do expect do converge(<<-EOM, __FILE__, __LINE__ + 1) #{resource_dsl} "hi" do foo "foo!" action :access_private_method end EOM end.to raise_error(NameError) expect(ActionJackson.ran_action).to eq :access_private_method end it "the action cannot access resource instance variables" do converge <<-EOM, __FILE__, __LINE__ + 1 #{resource_dsl} "hi" do foo "foo!" action :access_instance_variable end EOM expect(ActionJackson.ran_action).to eq :access_instance_variable expect(ActionJackson.succeeded).to be_nil end it "the action does not compile until the prior resource has converged" do converge <<-EOM, __FILE__, __LINE__ + 1 ruby_block "wow" do block do ResourceActionSpec::ActionJackson.ruby_block_converged = "ruby_block_converged!" end end #{resource_dsl} "hi" do foo "foo!" action :access_class_method end EOM expect(ActionJackson.ran_action).to eq :access_class_method expect(ActionJackson.succeeded).to eq "ruby_block_converged!" end it "the action's resources converge before the next resource converges" do converge <<-EOM, __FILE__, __LINE__ + 1 #{resource_dsl} "hi" do foo "foo!" action :access_attribute end ruby_block "wow" do block do ResourceActionSpec::ActionJackson.ruby_block_converged = ResourceActionSpec::ActionJackson.succeeded end end EOM expect(ActionJackson.ran_action).to eq :access_attribute expect(ActionJackson.succeeded).to eq "foo!" expect(ActionJackson.ruby_block_converged).to eq "foo!" end end context "With resource 'action_jackson'" do class ActionJackson < Chef::Resource use_automatic_resource_name def foo(value = nil) @foo = value if value @foo end def blarghle(value = nil) @blarghle = value if value @blarghle end class <