Module: ChefSpec::API::CookbookFileMatchers

Defined in:
lib/chefspec/api/cookbook_file.rb

Overview

Since:

  • 0.0.1

Instance Method Summary collapse

Instance Method Details

#create_cookbook_file(resource_name) ⇒ ChefSpec::Matchers::ResourceMatcher

Assert that a cookbook_file resource exists in the Chef run with the action :create. Given a Chef Recipe that creates “/tmp/config” as a cookbook_file:

cookbook_file '/tmp/config' do
  action :create
end

To test the content rendered by a cookbook_file, see RenderFileMatchers.

The Examples section demonstrates the different ways to test a cookbook_file resource with ChefSpec.

Examples:

Assert that a cookbook_file was created

expect(chef_run).to create_cookbook_file('/tmp/config')

Assert that a cookbook_file was created with predicate matchers

expect(chef_run).to create_cookbook_file('/tmp/config').with_backup(false)

Assert that a cookbook_file was created with attributes

expect(chef_run).to create_cookbook_file('/tmp/config').with(backup: false)

Assert that a cookbook_file was created using a regex

expect(chef_run).to create_cookbook_file('/tmp/config').with(user: /apa(.+)/)

Assert that a cookbook_file was not created

expect(chef_run).to_not create_cookbook_file('/tmp/config')

Parameters:

  • resource_name (String, Regex)

    the name of the resource to match

Returns:

Since:

  • 0.0.1



42
43
44
# File 'lib/chefspec/api/cookbook_file.rb', line 42

def create_cookbook_file(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:cookbook_file, :create, resource_name)
end

#create_cookbook_file_if_missing(resource_name) ⇒ ChefSpec::Matchers::ResourceMatcher

Assert that a cookbook_file resource exists in the Chef run with the action :create_if_missing. Given a Chef Recipe that creates “/tmp/config” if missing as a cookbook_file:

cookbook_file '/tmp/config' do
  action :create_if_missing
end

To test the content rendered by a cookbook_file, see RenderFileMatchers.

The Examples section demonstrates the different ways to test a cookbook_file resource with ChefSpec.

Examples:

Assert that a cookbook_file was created if missing

expect(chef_run).to create_cookbook_file_if_missing('/tmp/config')

Assert that a cookbook_file was created if missing with predicate matchers

expect(chef_run).to create_cookbook_file_if_missing('/tmp/config').with_backup(false)

Assert that a cookbook_file was created if missing with attributes

expect(chef_run).to create_cookbook_file_if_missing('/tmp/config').with(backup: false)

Assert that a cookbook_file was created if missing using a regex

expect(chef_run).to create_cookbook_file_if_missing('/tmp/config').with(user: /apa(.+)/)

Assert that a cookbook_file was not created if missing

expect(chef_run).to_not create_cookbook_file('/tmp/config')

Parameters:

  • resource_name (String, Regex)

    the name of the resource to match

Returns:

Since:

  • 0.0.1



82
83
84
# File 'lib/chefspec/api/cookbook_file.rb', line 82

def create_cookbook_file_if_missing(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:cookbook_file, :create_if_missing, resource_name)
end

#delete_cookbook_file(resource_name) ⇒ ChefSpec::Matchers::ResourceMatcher

Assert that a cookbook_file resource exists in the Chef run with the action :delete. Given a Chef Recipe that deletes “/tmp/config” as a cookbook_file:

cookbook_file '/tmp/config' do
  action :delete
end

To test the content rendered by a cookbook_file, see RenderFileMatchers.

The Examples section demonstrates the different ways to test a cookbook_file resource with ChefSpec.

Examples:

Assert that a cookbook_file was deleted

expect(chef_run).to delete_cookbook_file('/tmp/config')

Assert that a cookbook_file was deleted with predicate matchers

expect(chef_run).to delete_cookbook_file('/tmp/config').with_backup(false)

Assert that a cookbook_file was deleted with attributes

expect(chef_run).to delete_cookbook_file('/tmp/config').with(backup: false)

Assert that a cookbook_file was deleted using a regex

expect(chef_run).to delete_cookbook_file('/tmp/config').with(user: /apa(.+)/)

Assert that a cookbook_file was not deleted

expect(chef_run).to_not delete_cookbook_file('/tmp/config')

Parameters:

  • resource_name (String, Regex)

    the name of the resource to match

Returns:

Since:

  • 0.0.1



122
123
124
# File 'lib/chefspec/api/cookbook_file.rb', line 122

def delete_cookbook_file(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:cookbook_file, :delete, resource_name)
end

#touch_cookbook_file(resource_name) ⇒ ChefSpec::Matchers::ResourceMatcher

Assert that a cookbook_file resource exists in the Chef run with the action :touch. Given a Chef Recipe that touches “/tmp/config” as a cookbook_file:

cookbook_file '/tmp/config' do
  action :touch
end

To test the content rendered by a cookbook_file, see RenderFileMatchers.

The Examples section demonstrates the different ways to test a cookbook_file resource with ChefSpec.

Examples:

Assert that a cookbook_file was touched

expect(chef_run).to touch_cookbook_file('/tmp/config')

Assert that a cookbook_file was touched with predicate matchers

expect(chef_run).to touch_cookbook_file('/tmp/config').with_backup(false)

Assert that a cookbook_file was touched with attributes

expect(chef_run).to touch_cookbook_file('/tmp/config').with(backup: false)

Assert that a cookbook_file was touched using a regex

expect(chef_run).to touch_cookbook_file('/tmp/config').with(user: /apa(.+)/)

Assert that a cookbook_file was not touched

expect(chef_run).to_not touch_cookbook_file('/tmp/config')

Parameters:

  • resource_name (String, Regex)

    the name of the resource to match

Returns:

Since:

  • 0.0.1



162
163
164
# File 'lib/chefspec/api/cookbook_file.rb', line 162

def touch_cookbook_file(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:cookbook_file, :touch, resource_name)
end