diff --git a/CHANGELOG.md b/CHANGELOG.md index 28f86d56e..6e59a380d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ # Unreleased +* Fix a regression where `chef generate template` fails with + `undefined method `content_source' for #` + in 2.1.1 + ## Last Release: 0.2.1 Other than some minor bug fixes, here is a list of included changes: diff --git a/lib/chef-dk/command/generator_commands/template.rb b/lib/chef-dk/command/generator_commands/template.rb index b4bf8e09a..77edfedb9 100644 --- a/lib/chef-dk/command/generator_commands/template.rb +++ b/lib/chef-dk/command/generator_commands/template.rb @@ -23,7 +23,7 @@ module GeneratorCommands # chef generate template [path/to/cookbook_root] name --source=source_file class Template < CookbookCodeFile - option :content_source, + option :source, :short => "-s SOURCE_FILE", :long => "--source SOURCE_FILE", :description => "Copy content from SOURCE_FILE" @@ -38,6 +38,7 @@ def recipe def setup_context super + Generator.add_attr_to_context(:content_source, config[:source]) end end end diff --git a/lib/chef-dk/generator.rb b/lib/chef-dk/generator.rb index fe460b2c8..a02edfd91 100644 --- a/lib/chef-dk/generator.rb +++ b/lib/chef-dk/generator.rb @@ -22,7 +22,22 @@ module Generator # This is here to hold attr_accessor data for Generator context variables class Context def self.add_attr(name) - attr_accessor(name) + @attributes ||= [ ] + + if !@attributes.include?(name) + @attributes << name + attr_accessor(name) + end + end + + def self.reset + return if @attributes.nil? + + @attributes.each do |attr| + remove_method(attr) + end + + @attributes = nil end end diff --git a/spec/shared/a_file_generator.rb b/spec/shared/a_file_generator.rb index 9760b8f52..794d69ad0 100644 --- a/spec/shared/a_file_generator.rb +++ b/spec/shared/a_file_generator.rb @@ -32,6 +32,10 @@ def generator_context reset_tempdir end + after(:each) do + ChefDK::Generator::Context.reset + end + context "when argv is empty" do let(:argv) { [] } @@ -118,4 +122,3 @@ def generator_context end end -