Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Commit

Permalink
Merge pull request #266 from charlesjohnson/add-chefspec-to-generators
Browse files Browse the repository at this point in the history
Add chefspec to generators
  • Loading branch information
charlesjohnson committed Jan 9, 2015
2 parents ee21e92 + e86b592 commit 6d3da5a
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 25 deletions.
5 changes: 5 additions & 0 deletions lib/chef-dk/command/generator_commands/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,17 @@ def setup_context
Generator.add_attr_to_context(:app_name, app_name)
Generator.add_attr_to_context(:cookbook_root, cookbook_root)
Generator.add_attr_to_context(:cookbook_name, cookbook_name)
Generator.add_attr_to_context(:recipe_name, recipe_name)
end

def recipe
"app"
end

def recipe_name
"default"
end

def app_name
File.basename(app_full_path)
end
Expand Down
5 changes: 5 additions & 0 deletions lib/chef-dk/command/generator_commands/cookbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,17 @@ def setup_context
Generator.add_attr_to_context(:skip_git_init, cookbook_path_in_git_repo?)
Generator.add_attr_to_context(:cookbook_root, cookbook_root)
Generator.add_attr_to_context(:cookbook_name, cookbook_name)
Generator.add_attr_to_context(:recipe_name, recipe_name)
end

def recipe
"cookbook"
end

def recipe_name
"default"
end

def cookbook_name
File.basename(cookbook_full_path)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def setup_context
Generator.add_attr_to_context(:cookbook_root, cookbook_root)
Generator.add_attr_to_context(:cookbook_name, cookbook_name)
Generator.add_attr_to_context(:new_file_basename, new_file_basename)
Generator.add_attr_to_context(:recipe_name, new_file_basename)
end

def cookbook_root
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
require 'chefspec'

current_dir = File.dirname(__FILE__)

RSpec.configure do |config|
# Point to the cookbooks directory
config.cookbook_path = File.join(current_dir, '../cookbooks')
end
require 'chefspec/berkshelf'
17 changes: 16 additions & 1 deletion lib/chef-dk/skeletons/code_generator/recipes/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,25 @@
directory "#{cookbook_dir}/recipes"

template "#{cookbook_dir}/recipes/default.rb" do
source "default_recipe.rb.erb"
source "recipe.rb.erb"
helpers(ChefDK::Generator::TemplateHelper)
end

# Chefspec
directory "#{cookbook_dir}/spec/unit/recipes" do
recursive true
end

cookbook_file "#{cookbook_dir}/spec/spec_helper.rb" do
action :create_if_missing
end

template "#{cookbook_dir}/spec/unit/recipes/default_spec.rb" do
source "recipe_spec.rb.erb"
helpers(ChefDK::Generator::TemplateHelper)
action :create_if_missing
end

# git
if context.have_git

Expand Down
22 changes: 21 additions & 1 deletion lib/chef-dk/skeletons/code_generator/recipes/cookbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,34 @@
action :create_if_missing
end

# Chefspec

directory "#{cookbook_dir}/spec/unit/recipes" do
recursive true
end

cookbook_file "#{cookbook_dir}/spec/spec_helper.rb" do
action :create_if_missing
end

template "#{cookbook_dir}/spec/unit/recipes/default_spec.rb" do
source "recipe_spec.rb.erb"
helpers(ChefDK::Generator::TemplateHelper)
action :create_if_missing
variables(
:recipe_name => 'default')
end

# Recipes

directory "#{cookbook_dir}/recipes"

template "#{cookbook_dir}/recipes/default.rb" do
source "default_recipe.rb.erb"
source "recipe.rb.erb"
helpers(ChefDK::Generator::TemplateHelper)
action :create_if_missing
variables(
:recipe_name => 'default')
end

# git
Expand Down
18 changes: 18 additions & 0 deletions lib/chef-dk/skeletons/code_generator/recipes/recipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,25 @@
context = ChefDK::Generator.context
cookbook_dir = File.join(context.cookbook_root, context.cookbook_name)
recipe_path = File.join(cookbook_dir, "recipes", "#{context.new_file_basename}.rb")
spec_helper_path = File.join(cookbook_dir, "spec", "spec_helper.rb")
spec_path = File.join(cookbook_dir, "spec", "unit", "recipes", "#{context.new_file_basename}_spec.rb")

# Chefspec
directory "#{cookbook_dir}/spec/unit/recipes" do
recursive true
end

cookbook_file spec_helper_path do
action :create_if_missing
end

template spec_path do
source "recipe_spec.rb.erb"
helpers(ChefDK::Generator::TemplateHelper)
action :create_if_missing
end

# Recipe
template recipe_path do
source "recipe.rb.erb"
helpers(ChefDK::Generator::TemplateHelper)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#
# Cookbook Name:: <%= cookbook_name %>
# Recipe:: <%= recipe_name %>
#
<%= license_description('#') %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# Cookbook Name:: <%= cookbook_name %>
# Spec:: default
#
<%= license_description('#') %>

require 'spec_helper'

describe '<%= cookbook_name %>::<%= recipe_name %>' do

context 'When all attributes are default, on an unspecified platform' do

let(:chef_run) do
runner = ChefSpec::ServerRunner.new
runner.converge(described_recipe)
end

it 'converges successfully' do
chef_run # This should not raise an error
end

end
end
1 change: 1 addition & 0 deletions spec/shared/a_file_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def generator_context
expect(generator_context.cookbook_root).to eq(expected_cookbook_root)
expect(generator_context.cookbook_name).to eq(cookbook_name)
expect(generator_context.new_file_basename).to eq(new_file_name)
expect(generator_context.recipe_name).to eq(new_file_name)
end
end

Expand Down
15 changes: 15 additions & 0 deletions spec/unit/command/generator_commands/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
cookbooks/new_app/metadata.rb
cookbooks/new_app/recipes
cookbooks/new_app/recipes/default.rb
cookbooks/new_app/spec
cookbooks/new_app/spec/spec_helper.rb
cookbooks/new_app/spec/unit
cookbooks/new_app/spec/unit/recipes
cookbooks/new_app/spec/unit/recipes/default_spec.rb
]
end

Expand Down Expand Up @@ -76,6 +81,7 @@ def generator_context
expect(generator_context.app_name).to eq("new_app")
expect(generator_context.cookbook_root).to eq(File.join(Dir.pwd, "new_app", "cookbooks"))
expect(generator_context.cookbook_name).to eq("new_app")
expect(generator_context.recipe_name).to eq("default")
end

describe "generated files" do
Expand Down Expand Up @@ -134,6 +140,15 @@ def generator_context
let(:line) { "# Cookbook Name:: new_app" }
end
end

describe "cookbooks/new_app/spec/unit/recipes/default_spec.rb" do
let(:file) { File.join(tempdir, "new_app", "cookbooks", "new_app", "spec", "unit", "recipes", "default_spec.rb") }

include_examples "a generated file", :cookbook_name do
let(:line) { "describe \'new_app::default\' do" }
end
end

end
end
end
Expand Down
14 changes: 14 additions & 0 deletions spec/unit/command/generator_commands/cookbook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
README.md
recipes
recipes/default.rb
spec
spec/spec_helper.rb
spec/unit
spec/unit/recipes
spec/unit/recipes/default_spec.rb
]
end

Expand Down Expand Up @@ -106,6 +111,7 @@ def with_argv(argv)
cookbook_generator.setup_context
expect(generator_context.cookbook_root).to eq(Dir.pwd)
expect(generator_context.cookbook_name).to eq("new_cookbook")
expect(generator_context.recipe_name).to eq("default")
end

it "creates a new cookbook" do
Expand Down Expand Up @@ -164,6 +170,14 @@ def with_argv(argv)
end
end

describe "spec/unit/recipes/default_spec.rb" do
let(:file) { File.join(tempdir, "new_cookbook", "spec", "unit", "recipes", "default_spec.rb") }

include_examples "a generated file", :cookbook_name do
let(:line) { "describe \'new_cookbook::default\' do" }
end
end

end

context "when given the path to the cookbook to generate" do
Expand Down
6 changes: 4 additions & 2 deletions spec/unit/command/generator_commands/recipe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
include_examples "a file generator" do

let(:generator_name) { "recipe" }
let(:generated_files) { [ "recipes/new_recipe.rb" ] }
let(:generated_files) { [ "recipes/new_recipe.rb",
"spec/spec_helper.rb",
"spec/unit/recipes/new_recipe_spec.rb" ] }
let(:new_file_name) { "new_recipe" }

end
end

end

0 comments on commit 6d3da5a

Please sign in to comment.