Module: ChefSpec::API::FileMatchers

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

Overview

Since:

  • 0.0.1

Instance Method Summary collapse

Instance Method Details

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

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

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

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

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

Examples:

Assert that a file was created

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

Assert that a file was created with predicate matchers

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

Assert that a file was created with attributes

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

Assert that a file was created using a regex

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

Assert that a file was not created

expect(chef_run).to_not create_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/file.rb', line 42

def create_file(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:file, :create, resource_name)
end

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

Assert that a 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 file:

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

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

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

Examples:

Assert that a file was created if missing

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

Assert that a file was created if missing with predicate matchers

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

Assert that a file was created if missing with attributes

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

Assert that a file was created if missing using a regex

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

Assert that a file was not created if missing

expect(chef_run).to_not create_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/file.rb', line 82

def create_file_if_missing(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:file, :create_if_missing, resource_name)
end

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

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

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

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

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

Examples:

Assert that a file was deleted

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

Assert that a file was deleted with predicate matchers

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

Assert that a file was deleted with attributes

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

Assert that a file was deleted using a regex

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

Assert that a file was not deleted

expect(chef_run).to_not delete_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/file.rb', line 122

def delete_file(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:file, :delete, resource_name)
end

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

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

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

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

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

Examples:

Assert that a file was touched

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

Assert that a file was touched with predicate matchers

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

Assert that a file was touched with attributes

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

Assert that a file was touched using a regex

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

Assert that a file was not touched

expect(chef_run).to_not touch_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/file.rb', line 162

def touch_file(resource_name)
  ChefSpec::Matchers::ResourceMatcher.new(:file, :touch, resource_name)
end