Skip to main content

Chef/Correctness/PlatformVersionStringComparison

Cookstyle cops page

The Cookstyle cops department: Chef/Correctness

Enabled by defaultSupports autocorrectionTarget Chef Version
EnabledNoAll Versions

Ohai reports version attributes as strings, so comparing one with <, >, <=, or >= compares them character by character rather than as versions. That gives the wrong answer whenever the version numbers have different digit counts:

‘7.9’ >= ‘10’ # => true, because ‘7’ sorts after ‘1’ ‘9’ > ‘10’ # => true

Compare the major version as an integer with node['platform_version'].to_i, or compare full versions with Gem::Version.

Examples

# bad
node['platform_version'] >= '10'
node['platform_version'] < '7.4'

# good
node['platform_version'].to_i >= 10
Gem::Version.new(node['platform_version']) >= Gem::Version.new('7.4')

# good - equality is a plain string match, which is fine
node['platform_version'] == '10'

Configurable attributes

NameDefault valueConfigurable values
Version Added9.0.0String
Include
    Array

    Thank you for your feedback!

    ×