Module: ChefGen::Snippet::ChefSpec

Defined in:
lib/chef_gen/snippet/chef_spec.rb

Overview

creates a framework for ChefSpec unit testing

Instance Method Summary collapse

Instance Method Details

#content_chefspec_files(path) ⇒ void

This method returns an undefined value.

copies snippet content

Parameters:

  • path (String)

    the path to the temporary generator cookbook



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/chef_gen/snippet/chef_spec.rb', line 93

def content_chefspec_files(path)
  %w(
    _rspec.erb
    spec_spec_helper_rb.erb
    spec_recipes_default_spec_rb.erb
    spec_chef_runner_context_rb.erb
  ).each do |file|
    copy_snippet_file(
      File.join(
        File.dirname(__FILE__), '..', '..', '..',
        'shared', 'snippet', 'chef_spec', file
      ),
      File.join(path, 'templates', 'default', file)
    )
  end
end

#snippet_chefspec_dirs(recipe) ⇒ void

This method returns an undefined value.

declares directories

Parameters:

  • recipe (Chef::Recipe)

    the recipe into which resources will be injected



9
10
11
12
# File 'lib/chef_gen/snippet/chef_spec.rb', line 9

def snippet_chefspec_dirs(recipe)
  @directories << 'spec'
  @directories << File.join('spec', 'recipes')
end

#snippet_chefspec_files(recipe) ⇒ void

This method returns an undefined value.

declares files

Parameters:

  • recipe (Chef::Recipe)

    the recipe into which resources will be injected



18
19
20
21
22
23
# File 'lib/chef_gen/snippet/chef_spec.rb', line 18

def snippet_chefspec_files(recipe)
  @templates << '.rspec'
  @templates_if_missing << File.join('spec', 'spec_helper.rb')
  @templates_if_missing << File.join('spec', 'recipes', 'default_spec.rb')
  @templates_if_missing << File.join('spec', 'chef_runner_context.rb')
end

#snippet_chefspec_gems(recipe) ⇒ void

This method returns an undefined value.

adds chefspec gems to the Gemfile

Parameters:

  • recipe (Chef::Recipe)

    the recipe into which resources will be injected



44
45
46
47
48
49
# File 'lib/chef_gen/snippet/chef_spec.rb', line 44

def snippet_chefspec_gems(recipe)
  return unless respond_to?(:cookbook_gems)
  cookbook_gems['chefspec'] = '~> 4.1'
  cookbook_gems['guard-rspec'] = '~> 4.5'
  cookbook_gems['ci_reporter_rspec'] = '~> 1.0'
end

#snippet_chefspec_guardsets(recipe) ⇒ void

This method returns an undefined value.

adds chefspec sets to the Guardfile

Parameters:

  • recipe (Chef::Recipe)

    the recipe into which resources will be injected



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chef_gen/snippet/chef_spec.rb', line 74

def snippet_chefspec_guardsets(recipe)
  return unless respond_to?(:guard_sets)
  guard_sets['chefspec'] = <<'END'
rspec_command = ENV.key?('DISABLE_PRY_RESCUE') ? 'rspec' : 'rescue rspec'
guard :rspec, all_on_start: true, cmd: "bundle exec #{rspec_command}" do
  watch(%r{^spec/recipes/.+_spec\.rb$})
  watch(%r{^spec/spec_helper\.rb$}) { 'spec' }
  watch(%r{^attributes/.+\.rb$})    { 'spec' }
  watch(%r{^resources/.+\.rb$})     { 'spec' }
  watch(%r{^providers/.+\.rb$})     { 'spec' }
  watch(%r{^libraries/.+\.rb$})     { 'spec' }
  watch(%r{^recipes/(.+)\.rb$})     { |m| "spec/recipes/#{m[1]}_spec.rb" }
end
END
end

#snippet_chefspec_ignore(recipe) ⇒ void

This method returns an undefined value.

adds chefspec files to the git and chefignore patterns

Parameters:

  • recipe (Chef::Recipe)

    the recipe into which resources will be injected



29
30
31
32
33
34
35
36
37
38
# File 'lib/chef_gen/snippet/chef_spec.rb', line 29

def snippet_chefspec_ignore(recipe)
  return unless respond_to?(:chefignore_patterns)
  %w(
    .rspec
    spec/*
    spec/fixtures/*
  ).each do |e|
    chefignore_patterns << e
  end
end

#snippet_chefspec_raketasks(recipe) ⇒ void

This method returns an undefined value.

adds chefspec rake tasks to the Rakefile

Parameters:

  • recipe (Chef::Recipe)

    the recipe into which resources will be injected



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/chef_gen/snippet/chef_spec.rb', line 55

def snippet_chefspec_raketasks(recipe)
  return unless respond_to?(:rake_tasks)
  rake_tasks['chefspec'] = <<'END'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:chefspec)

desc 'Generate ChefSpec coverage report'
task :coverage do
  ENV['COVERAGE'] = 'true'
  Rake::Task[:chefspec].invoke
end
task spec: :chefspec
END
end