Skip to main content

Chef/Modernize/ConditionalUsingTest

Cookstyle cops page

The Cookstyle cops department: Chef/Modernize

Enabled by defaultSupports autocorrectionTarget Chef Version
EnabledYesAll Versions

Use ::File.exist?('/foo/bar') in an only_if or not_if guard instead of the slower 'test -f /foo/bar', which requires shelling out. The Ruby check has to be passed as a block. Passing it directly, as not_if ::File.exist?('/foo/bar'), raises ArgumentError: Invalid only_if/not_if command, expected a string.

Examples

# bad
only_if 'test -f /bin/foo'
not_if 'test -f /bin/foo'

# bad - a Ruby guard has to be a block, this raises at converge time
only_if ::File.exist?('/bin/foo')

# good
only_if { ::File.exist?('/bin/foo') }
not_if { ::File.exist?('/bin/foo') }

Configurable attributes

NameDefault valueConfigurable values
Version Added6.11.0String
Include
    Array

    Thank you for your feedback!

    ×