chefspec 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,10 @@ require 'fauxhai'
5
5
 
6
6
  require 'chefspec/matchers/shared'
7
7
 
8
+ begin
9
+ require 'berkshelf'
10
+ rescue LoadError; end
11
+
8
12
  # ChefSpec allows you to write rspec examples for Chef recipes to gain faster feedback without the need to converge a
9
13
  # node.
10
14
  module ChefSpec
@@ -26,7 +30,7 @@ module ChefSpec
26
30
  # @option options [Symbol] :log_level The log level to use (default is :warn)
27
31
  # @option options [String] :platform The platform to load Ohai attributes from (must be present in fauxhai)
28
32
  # @option options [String] :version The version of the platform to load Ohai attributes from (must be present in fauxhai)
29
- # @option options [String] :ohai_data_path Path of a json file that will be passed to fauxhai as :path option
33
+ # @option options [String] :ohai_data_path Path of a json file that will be passed to fauxhai as :path option
30
34
  # @yield [node] Configuration block for Chef::Node
31
35
  def initialize(options={})
32
36
  defaults = {:cookbook_path => default_cookbook_path, :log_level => :warn, :dry_run => false, :step_into => []}
@@ -75,7 +79,7 @@ module ChefSpec
75
79
  Chef::Config[:cache_type] = "Memory"
76
80
  Chef::Config[:cache_options] = { :path => File.join(File.expand_path('~'), '.chef', 'checksums') }
77
81
  Chef::Cookbook::FileVendor.on_create { |manifest| Chef::Cookbook::FileSystemFileVendor.new(manifest) }
78
- Chef::Config[:cookbook_path] = @options[:cookbook_path]
82
+ Chef::Config[:cookbook_path] = cookbook_paths
79
83
  Chef::Config[:client_key] = nil
80
84
 
81
85
  # As of Chef 11, Chef uses custom formatters which munge the RSpec output.
@@ -140,7 +144,7 @@ module ChefSpec
140
144
  return "chef_run: #{@node.run_list.to_s}" unless @node.run_list.empty?
141
145
  'chef_run'
142
146
  end
143
-
147
+
144
148
  # Find the resource with the declared type and name
145
149
  #
146
150
  # @param [String] type The type of resource - e.g. 'file' or 'directory'
@@ -149,7 +153,7 @@ module ChefSpec
149
153
  def find_resource(type, name)
150
154
  resources.find{|resource| resource_type(resource) == type and resource.name == name}
151
155
  end
152
-
156
+
153
157
  private
154
158
 
155
159
  # Populate basic OHAI attributes required to get recipes working. This is a minimal set - if your recipe example
@@ -177,6 +181,22 @@ module ChefSpec
177
181
  Pathname.new(File.join(caller(2).first.split(':').slice(0..-3).join(':'), '..', '..', '..')).cleanpath.to_s
178
182
  end
179
183
 
184
+ # The cookbook path, appended with some "common" directories to search
185
+ # as well (such as vendor/cookbooks)
186
+ #
187
+ # @return [Array<String>] The cookbook_paths
188
+ def cookbook_paths
189
+ vendor_path = File.expand_path(File.join('vendor', 'cookbooks'))
190
+ test_path = File.expand_path(File.join('test', 'cookbooks'))
191
+ spec_path = File.expand_path(File.join('spec', 'cookbooks'))
192
+
193
+ Array(@options[:cookbook_path]).
194
+ push(vendor_path).
195
+ push(test_path).
196
+ push(spec_path).
197
+ select { |path| File.exists?(path) }
198
+ end
199
+
180
200
  end
181
201
 
182
202
  end
@@ -9,7 +9,8 @@ module ChefSpec
9
9
  if (Array(resource.action).map { |action| action.to_sym } & [:create, :create_if_missing]).any?
10
10
  case resource_type(resource)
11
11
  when 'template'
12
- @actual_content = render(resource, chef_run.node)
12
+ template_finder = template_finder(chef_run.run_context, resource, chef_run.node)
13
+ @actual_content = render(resource, chef_run.node, template_finder)
13
14
  when 'file'
14
15
  @actual_content = resource.content
15
16
  when 'cookbook_file'
@@ -1,3 +1,8 @@
1
+ begin
2
+ require 'chef/mixin/template'
3
+ require 'chef/provider/template_finder'
4
+ rescue LoadError
5
+ end
1
6
  # Given a resource return the unqualified type it is
2
7
  #
3
8
  # @param [String] resource A Chef Resource
@@ -44,11 +49,16 @@ end
44
49
  #
45
50
  # @param [Chef::Resource::Template] template The template to render
46
51
  # @param [Chef::Node] node The node which may be required to render the template
52
+ # @param [Chef::Provider::TemplateFinder] a TemplateFinder use for rendering templates containing partials
47
53
  # @return [String] The result result of rendering the template
48
- def render(template, node)
54
+ def render(template, node, template_finder)
49
55
  # Duplicates functionality in the Chef Template provider
50
56
  context = {}; context.merge!(template.variables)
51
57
  context[:node] = node
58
+ unless template_finder.nil?
59
+ context[:template_finder] = template_finder
60
+ Erubis::Context.send(:include, Chef::Mixin::Template::ChefContext)
61
+ end
52
62
  Erubis::Eruby.new(IO.read(template_path(template, node))).evaluate(context)
53
63
  end
54
64
 
@@ -65,3 +75,9 @@ def template_path(template, node)
65
75
  end
66
76
  cookbook.preferred_filename_on_disk_location(node, :templates, template.source)
67
77
  end
78
+
79
+ def template_finder(run_context, template, node)
80
+ if Chef::Provider.const_defined?(:TemplateFinder)
81
+ Chef::Provider::TemplateFinder.new(run_context,template.cookbook_name, node)
82
+ end
83
+ end
@@ -154,7 +154,7 @@ module ChefSpec
154
154
  when :file
155
155
  f.content
156
156
  when :template
157
- render(f, node)
157
+ render(f, node, template_finder(run_context, f, node))
158
158
  else raise NotImplementedError,
159
159
  ":#{f.resource_name} not supported for comparison"
160
160
  end.include?(content)
@@ -1,4 +1,4 @@
1
1
  module ChefSpec
2
2
  # The gem version
3
- VERSION = '1.1.0'
3
+ VERSION = '1.2.0'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chefspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-10 00:00:00.000000000 Z
12
+ date: 2013-05-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chef
@@ -239,7 +239,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
239
239
  version: '0'
240
240
  segments:
241
241
  - 0
242
- hash: -3143489054534407734
242
+ hash: -3769472040999822905
243
243
  required_rubygems_version: !ruby/object:Gem::Requirement
244
244
  none: false
245
245
  requirements:
@@ -248,12 +248,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
248
248
  version: '0'
249
249
  segments:
250
250
  - 0
251
- hash: -3143489054534407734
251
+ hash: -3769472040999822905
252
252
  requirements: []
253
253
  rubyforge_project:
254
254
  rubygems_version: 1.8.23
255
255
  signing_key:
256
256
  specification_version: 3
257
- summary: chefspec-1.1.0
257
+ summary: chefspec-1.2.0
258
258
  test_files: []
259
259
  has_rdoc: