cookstyle 8.7.6 → 9.0.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.
- checksums.yaml +4 -4
- data/config/chefstyle.yml +2 -0
- data/config/cookstyle.yml +145 -6
- data/lib/cookstyle/version.rb +2 -2
- data/lib/rubocop/chef/cookbook_helpers.rb +23 -0
- data/lib/rubocop/chef.rb +1 -1
- data/lib/rubocop/cop/chef/correctness/chef_application_fatal.rb +16 -4
- data/lib/rubocop/cop/chef/correctness/conditional_unified_mode_true.rb +91 -0
- data/lib/rubocop/cop/chef/correctness/empty_resource_guard.rb +18 -2
- data/lib/rubocop/cop/chef/correctness/execute_delete_file.rb +127 -0
- data/lib/rubocop/cop/chef/correctness/invalid_checksum.rb +79 -0
- data/lib/rubocop/cop/chef/correctness/invalid_platform_family_helper.rb +1 -1
- data/lib/rubocop/cop/chef/correctness/invalid_platform_family_values_in_case.rb +10 -0
- data/lib/rubocop/cop/chef/correctness/invalid_platform_values_in_case.rb +10 -0
- data/lib/rubocop/cop/chef/correctness/invalid_value_for_platform_helper.rb +1 -0
- data/lib/rubocop/cop/chef/correctness/openssl_password_helpers.rb +1 -1
- data/lib/rubocop/cop/chef/correctness/platform_version_string_comparison.rb +66 -0
- data/lib/rubocop/cop/chef/correctness/powershell_delete_file.rb +5 -5
- data/lib/rubocop/cop/chef/correctness/powershell_file_exists.rb +1 -1
- data/lib/rubocop/cop/chef/correctness/resource_with_none_action.rb +3 -3
- data/lib/rubocop/cop/chef/correctness/ruby_guard_without_block.rb +91 -0
- data/lib/rubocop/cop/chef/correctness/service_resource.rb +71 -20
- data/lib/rubocop/cop/chef/deprecation/chef_handler_recipe.rb +8 -1
- data/lib/rubocop/cop/chef/deprecation/chef_rest.rb +4 -1
- data/lib/rubocop/cop/chef/deprecation/chef_shellout.rb +2 -2
- data/lib/rubocop/cop/chef/deprecation/chef_sugar_helpers.rb +6 -0
- data/lib/rubocop/cop/chef/deprecation/chef_windows_platform_helper.rb +1 -1
- data/lib/rubocop/cop/chef/deprecation/chocolatey_package_uninstall_action.rb +3 -3
- data/lib/rubocop/cop/chef/deprecation/depends_partial_search.rb +4 -0
- data/lib/rubocop/cop/chef/deprecation/deprecated_mixins.rb +2 -2
- data/lib/rubocop/cop/chef/deprecation/hwrp_without_provides.rb +4 -4
- data/lib/rubocop/cop/chef/deprecation/hwrp_without_unified_mode_true.rb +2 -2
- data/lib/rubocop/cop/chef/deprecation/inherits_compat_resource.rb +3 -3
- data/lib/rubocop/cop/chef/deprecation/legacy_yum_cookbook_recipes.rb +9 -1
- data/lib/rubocop/cop/chef/deprecation/locale_lc_all_property.rb +5 -0
- data/lib/rubocop/cop/chef/deprecation/resource_uses_only_resource_name.rb +9 -1
- data/lib/rubocop/cop/chef/deprecation/resource_uses_provider_base_method.rb +4 -0
- data/lib/rubocop/cop/chef/deprecation/ruby_block_create_action.rb +42 -3
- data/lib/rubocop/cop/chef/deprecation/use_automatic_resource_name.rb +9 -0
- data/lib/rubocop/cop/chef/deprecation/use_yaml_dump.rb +5 -0
- data/lib/rubocop/cop/chef/deprecation/xml_ruby_recipe.rb +5 -1
- data/lib/rubocop/cop/chef/modernize/apt_default_recipe.rb +1 -1
- data/lib/rubocop/cop/chef/modernize/build_essential.rb +4 -1
- data/lib/rubocop/cop/chef/modernize/chef_14_resources.rb +8 -0
- data/lib/rubocop/cop/chef/modernize/chef_15_resources.rb +11 -0
- data/lib/rubocop/cop/chef/modernize/chef_gem_nokogiri.rb +4 -0
- data/lib/rubocop/cop/chef/modernize/conditional_using_test.rb +10 -3
- data/lib/rubocop/cop/chef/modernize/cron_d_file_or_template.rb +6 -4
- data/lib/rubocop/cop/chef/modernize/databag_helpers.rb +6 -3
- data/lib/rubocop/cop/chef/modernize/depends_chef_vault_cookbook.rb +4 -0
- data/lib/rubocop/cop/chef/modernize/depends_chocolatey_cookbooks.rb +7 -0
- data/lib/rubocop/cop/chef/modernize/depends_kernel_module_cookbook.rb +6 -0
- data/lib/rubocop/cop/chef/modernize/depends_locale_cookbook.rb +6 -0
- data/lib/rubocop/cop/chef/modernize/depends_openssl_cookbook.rb +9 -0
- data/lib/rubocop/cop/chef/modernize/depends_timezone_lwrp_cookbook.rb +4 -0
- data/lib/rubocop/cop/chef/modernize/depends_windows_firewall_cookbook.rb +8 -0
- data/lib/rubocop/cop/chef/modernize/depends_zypper_cookbook.rb +7 -0
- data/lib/rubocop/cop/chef/modernize/dsl_include_in_resource.rb +1 -1
- data/lib/rubocop/cop/chef/modernize/execute_apt_update.rb +3 -1
- data/lib/rubocop/cop/chef/modernize/execute_archive_extract.rb +121 -0
- data/lib/rubocop/cop/chef/modernize/execute_update_alternatives.rb +100 -0
- data/lib/rubocop/cop/chef/modernize/includes_mixin_shellout.rb +4 -0
- data/lib/rubocop/cop/chef/modernize/libarchive_file.rb +5 -3
- data/lib/rubocop/cop/chef/modernize/ohai_default_recipe.rb +6 -1
- data/lib/rubocop/cop/chef/modernize/powershell_download_file.rb +64 -0
- data/lib/rubocop/cop/chef/modernize/powershell_expand_archive.rb +8 -1
- data/lib/rubocop/cop/chef/modernize/powershell_guard_interpreter.rb +4 -4
- data/lib/rubocop/cop/chef/modernize/sc_windows_resource.rb +2 -2
- data/lib/rubocop/cop/chef/modernize/seven_zip_archive.rb +7 -1
- data/lib/rubocop/cop/chef/modernize/shellouts_to_chocolatey.rb +15 -4
- data/lib/rubocop/cop/chef/modernize/use_require_relative.rb +1 -1
- data/lib/rubocop/cop/chef/modernize/windows_default_recipe.rb +1 -1
- data/lib/rubocop/cop/chef/modernize/windows_zipfile.rb +8 -2
- data/lib/rubocop/cop/chef/modernize/zipfile_resource.rb +7 -1
- data/lib/rubocop/cop/chef/modernize/zypper_repo.rb +2 -2
- data/lib/rubocop/cop/chef/redundant/custom_resource_with_allowed_actions.rb +10 -0
- data/lib/rubocop/cop/chef/redundant/double_compile_time.rb +22 -8
- data/lib/rubocop/cop/chef/redundant/service_guard_on_stop_disable.rb +117 -0
- data/lib/rubocop/cop/chef/redundant/string_property_with_nil_default.rb +2 -2
- data/lib/rubocop/cop/chef/redundant/unnecessary_name_property.rb +1 -1
- data/lib/rubocop/cop/chef/security/insecure_remote_file_source.rb +82 -0
- data/lib/rubocop/cop/chef/security/ssh_private_key.rb +9 -0
- data/lib/rubocop/cop/chef/sharing/empty_property_description.rb +62 -0
- data/lib/rubocop/cop/chef/style/include_recipe_with_parentheses.rb +5 -2
- data/lib/rubocop/cop/chef/style/true_false_resource_properties.rb +1 -1
- data/lib/rubocop/monkey_patches/directive_comment.rb +21 -3
- metadata +15 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b6cd3dcea2607da8e1f1393a8af2331944ea9e30b502447ed862fdf6d79e703a
|
|
4
|
+
data.tar.gz: bd596e0a8956d56b9e44c1dfd287263e1e04bac60d4767ab690deb14e093d9ea
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1432d8dadec991fdc89d85ef38379bfa938f74cc26153a71e9e0a3c51de9f1ffa1e070e7a3eaf459491880a0922fad113517c0f8f8cea58c5b3f69a0cb6f48f3
|
|
7
|
+
data.tar.gz: 718d4c62405c34b6e392eb00f8e8a11e797086439f1c47e514ac3a49885704a46553d4217025951b9c882fae0fbe65d73acba300d46e41c923bc2dd49e502954
|
data/config/chefstyle.yml
CHANGED
data/config/cookstyle.yml
CHANGED
|
@@ -133,6 +133,7 @@ Chef/Style/TrueClassFalseClassResourceProperties:
|
|
|
133
133
|
StyleGuide: 'chef_style_trueclassfalseclassresourceproperties'
|
|
134
134
|
Enabled: true
|
|
135
135
|
VersionAdded: '5.16.0'
|
|
136
|
+
VersionChanged: '9.0.0'
|
|
136
137
|
Include:
|
|
137
138
|
- '**/libraries/*.rb'
|
|
138
139
|
- '**/resources/*.rb'
|
|
@@ -173,6 +174,7 @@ Chef/Style/IncludeRecipeWithParentheses:
|
|
|
173
174
|
Description: "There is no need to wrap the recipe name in parentheses when using `include_recipe`. Write `include_recipe 'foo'` instead of `include_recipe('foo')`."
|
|
174
175
|
StyleGuide: 'chef_style_includerecipewithparentheses'
|
|
175
176
|
VersionAdded: '6.11.0'
|
|
177
|
+
VersionChanged: '9.0.0'
|
|
176
178
|
Enabled: true
|
|
177
179
|
Exclude:
|
|
178
180
|
- '**/attributes/*.rb'
|
|
@@ -187,11 +189,21 @@ Chef/Correctness:
|
|
|
187
189
|
Enabled: true
|
|
188
190
|
StyleGuideBaseURL: https://docs.chef.io/workstation/cookstyle/
|
|
189
191
|
|
|
192
|
+
Chef/Correctness/ConditionalUnifiedModeTrue:
|
|
193
|
+
Description: "Set `unified_mode true` unconditionally. Setting it conditionally leaves a resource running in unified mode on newer Chef Infra Client releases and in legacy mode on older ones, so it has to be written and tested against both execution models."
|
|
194
|
+
StyleGuide: 'chef_correctness_conditionalunifiedmodetrue'
|
|
195
|
+
Enabled: true
|
|
196
|
+
VersionAdded: '9.0.0'
|
|
197
|
+
Include:
|
|
198
|
+
- '**/libraries/*.rb'
|
|
199
|
+
- '**/resources/*.rb'
|
|
200
|
+
|
|
190
201
|
Chef/Correctness/ServiceResource:
|
|
191
|
-
Description: Use the `service` resource to
|
|
202
|
+
Description: Use the `service` resource to manage services instead of shelling out to an init script, `service`, `systemctl`, or a similar tool. The `service` resource is idempotent and handles platform differences automatically.
|
|
192
203
|
StyleGuide: 'chef_correctness_serviceresource'
|
|
193
204
|
Enabled: true
|
|
194
205
|
VersionAdded: '5.0.0'
|
|
206
|
+
VersionChanged: '9.0.0'
|
|
195
207
|
Exclude:
|
|
196
208
|
- '**/attributes/*.rb'
|
|
197
209
|
- '**/metadata.rb'
|
|
@@ -284,6 +296,7 @@ Chef/Correctness/ResourceWithNoneAction:
|
|
|
284
296
|
StyleGuide: 'chef_correctness_resourcewithnoneaction'
|
|
285
297
|
Enabled: true
|
|
286
298
|
VersionAdded: '5.5.0'
|
|
299
|
+
VersionChanged: '9.0.0'
|
|
287
300
|
Exclude:
|
|
288
301
|
- '**/attributes/*.rb'
|
|
289
302
|
- '**/metadata.rb'
|
|
@@ -391,11 +404,51 @@ Chef/Correctness/DnfPackageAllowDowngrades:
|
|
|
391
404
|
- '**/metadata.rb'
|
|
392
405
|
- '**/Berksfile'
|
|
393
406
|
|
|
407
|
+
Chef/Correctness/ExecuteDeleteFile:
|
|
408
|
+
Description: "Use the `file` or `directory` resources built into Chef Infra Client with the `:delete` action to remove files and directories instead of shelling out to `rm`. The resource has to match what is on disk: `file` for a file, `directory` with `recursive true` for a directory."
|
|
409
|
+
StyleGuide: 'chef_correctness_executedeletefile'
|
|
410
|
+
Enabled: true
|
|
411
|
+
VersionAdded: '9.0.0'
|
|
412
|
+
Exclude:
|
|
413
|
+
- '**/attributes/*.rb'
|
|
414
|
+
- '**/metadata.rb'
|
|
415
|
+
- '**/Berksfile'
|
|
416
|
+
|
|
417
|
+
Chef/Correctness/RubyGuardWithoutBlock:
|
|
418
|
+
Description: "A Ruby expression used as a resource guard (`not_if`/`only_if`) has to be wrapped in a block. String guards are run as shell commands and are fine as they are; it is only a bare Ruby expression that is a problem, since the guard receives the expression result rather than the expression."
|
|
419
|
+
StyleGuide: 'chef_correctness_rubyguardwithoutblock'
|
|
420
|
+
Enabled: true
|
|
421
|
+
VersionAdded: '9.0.0'
|
|
422
|
+
Exclude:
|
|
423
|
+
- '**/attributes/*.rb'
|
|
424
|
+
- '**/metadata.rb'
|
|
425
|
+
- '**/Berksfile'
|
|
426
|
+
|
|
427
|
+
Chef/Correctness/InvalidChecksum:
|
|
428
|
+
Description: "The `checksum` property of the file downloading resources takes a SHA-256 digest. An MD5 or SHA-1 digest can never match, so the run fails with a checksum mismatch rather than installing anything."
|
|
429
|
+
StyleGuide: 'chef_correctness_invalidchecksum'
|
|
430
|
+
Enabled: true
|
|
431
|
+
VersionAdded: '9.0.0'
|
|
432
|
+
Exclude:
|
|
433
|
+
- '**/attributes/*.rb'
|
|
434
|
+
- '**/metadata.rb'
|
|
435
|
+
- '**/Berksfile'
|
|
436
|
+
|
|
437
|
+
Chef/Correctness/PlatformVersionStringComparison:
|
|
438
|
+
Description: "Ohai version attributes are strings, so comparing one with an ordering operator compares them character by character rather than as versions. `'7.9' >= '10'` is true. Use `.to_i` for a major version comparison, or `Gem::Version` to compare full versions."
|
|
439
|
+
StyleGuide: 'chef_correctness_platformversionstringcomparison'
|
|
440
|
+
Enabled: true
|
|
441
|
+
VersionAdded: '9.0.0'
|
|
442
|
+
Exclude:
|
|
443
|
+
- '**/metadata.rb'
|
|
444
|
+
- '**/Berksfile'
|
|
445
|
+
|
|
394
446
|
Chef/Correctness/EmptyResourceGuard:
|
|
395
|
-
Description: "Resource guards (`not_if`/`only_if`) should not be empty
|
|
447
|
+
Description: "Resource guards (`not_if`/`only_if`) should not be empty. In Ruby, an empty string is 'truthy', so an empty guard string always passes. An empty block returns `nil`, so it always fails. Neither is likely to be what you intended."
|
|
396
448
|
StyleGuide: 'chef_correctness_emptyresourceguard'
|
|
397
449
|
Enabled: true
|
|
398
450
|
VersionAdded: '8.4.0'
|
|
451
|
+
VersionChanged: '9.0.0'
|
|
399
452
|
Exclude:
|
|
400
453
|
- '**/attributes/*.rb'
|
|
401
454
|
- '**/metadata.rb'
|
|
@@ -406,6 +459,7 @@ Chef/Correctness/ChefApplicationFatal:
|
|
|
406
459
|
StyleGuide: 'chef_correctness_chefapplicationfatal'
|
|
407
460
|
Enabled: true
|
|
408
461
|
VersionAdded: '6.0.0'
|
|
462
|
+
VersionChanged: '9.0.0'
|
|
409
463
|
Exclude:
|
|
410
464
|
- '**/metadata.rb'
|
|
411
465
|
- '**/Berksfile'
|
|
@@ -444,6 +498,7 @@ Chef/Correctness/OpenSSLPasswordHelpers:
|
|
|
444
498
|
StyleGuide: 'chef_correctness_opensslpasswordhelpers'
|
|
445
499
|
Enabled: true
|
|
446
500
|
VersionAdded: '6.6.0'
|
|
501
|
+
VersionChanged: '9.0.0'
|
|
447
502
|
Exclude:
|
|
448
503
|
- '**/metadata.rb'
|
|
449
504
|
- '**/Berksfile'
|
|
@@ -607,6 +662,15 @@ Chef/Sharing/EmptyMetadataField:
|
|
|
607
662
|
Include:
|
|
608
663
|
- '**/metadata.rb'
|
|
609
664
|
|
|
665
|
+
Chef/Sharing/EmptyPropertyDescription:
|
|
666
|
+
Description: Resource properties should not set an empty `description`. Automated documentation tools have nothing to render from it, so it does no more than a missing field would while looking like the property has been documented.
|
|
667
|
+
StyleGuide: 'chef_sharing_emptypropertydescription'
|
|
668
|
+
Enabled: true
|
|
669
|
+
VersionAdded: '9.0.0'
|
|
670
|
+
Include:
|
|
671
|
+
- '**/libraries/*.rb'
|
|
672
|
+
- '**/resources/*.rb'
|
|
673
|
+
|
|
610
674
|
Chef/Sharing/IncludePropertyDescriptions:
|
|
611
675
|
Description: Resource properties should include a `description` field so that automated documentation tools can generate helpful documentation for your custom resources. Requires Chef Infra Client 13.9 or later.
|
|
612
676
|
StyleGuide: 'chef_sharing_includepropertydescriptions'
|
|
@@ -745,6 +809,7 @@ Chef/Deprecations/UsesDeprecatedMixins:
|
|
|
745
809
|
StyleGuide: 'chef_deprecations_usesdeprecatedmixins'
|
|
746
810
|
Enabled: true
|
|
747
811
|
VersionAdded: '5.4.0'
|
|
812
|
+
VersionChanged: '9.0.0'
|
|
748
813
|
Include:
|
|
749
814
|
- '**/libraries/*.rb'
|
|
750
815
|
- '**/providers/*.rb'
|
|
@@ -755,6 +820,7 @@ Chef/Deprecations/IncludingXMLRubyRecipe:
|
|
|
755
820
|
StyleGuide: 'chef_deprecations_includingxmlrubyrecipe'
|
|
756
821
|
Enabled: true
|
|
757
822
|
VersionAdded: '5.4.0'
|
|
823
|
+
VersionChanged: '9.0.0'
|
|
758
824
|
Exclude:
|
|
759
825
|
- '**/attributes/*.rb'
|
|
760
826
|
- '**/metadata.rb'
|
|
@@ -765,6 +831,7 @@ Chef/Deprecations/LegacyYumCookbookRecipes:
|
|
|
765
831
|
StyleGuide: 'chef_deprecations_legacyyumcookbookrecipes'
|
|
766
832
|
Enabled: true
|
|
767
833
|
VersionAdded: '5.4.0'
|
|
834
|
+
VersionChanged: '9.0.0'
|
|
768
835
|
Exclude:
|
|
769
836
|
- '**/metadata.rb'
|
|
770
837
|
|
|
@@ -773,6 +840,7 @@ Chef/Deprecations/UsesChefRESTHelpers:
|
|
|
773
840
|
StyleGuide: 'chef_deprecations_useschefresthelpers'
|
|
774
841
|
Enabled: true
|
|
775
842
|
VersionAdded: '5.5.0'
|
|
843
|
+
VersionChanged: '9.0.0'
|
|
776
844
|
Exclude:
|
|
777
845
|
- '**/metadata.rb'
|
|
778
846
|
|
|
@@ -781,6 +849,7 @@ Chef/Deprecations/ChocolateyPackageUninstallAction:
|
|
|
781
849
|
StyleGuide: 'chef_deprecations_chocolateypackageuninstallaction'
|
|
782
850
|
Enabled: true
|
|
783
851
|
VersionAdded: '5.5.0'
|
|
852
|
+
VersionChanged: '9.0.0'
|
|
784
853
|
Exclude:
|
|
785
854
|
- '**/metadata.rb'
|
|
786
855
|
|
|
@@ -941,6 +1010,7 @@ Chef/Deprecations/ResourceInheritsFromCompatResource:
|
|
|
941
1010
|
StyleGuide: 'chef_deprecations_resourceinheritsfromcompatresource'
|
|
942
1011
|
Enabled: true
|
|
943
1012
|
VersionAdded: '5.10.0'
|
|
1013
|
+
VersionChanged: '9.0.0'
|
|
944
1014
|
Include:
|
|
945
1015
|
- '**/libraries/*.rb'
|
|
946
1016
|
|
|
@@ -1029,6 +1099,7 @@ Chef/Deprecations/RubyBlockCreateAction:
|
|
|
1029
1099
|
StyleGuide: 'chef_deprecations_rubyblockcreateaction'
|
|
1030
1100
|
Enabled: true
|
|
1031
1101
|
VersionAdded: '5.16.0'
|
|
1102
|
+
VersionChanged: '9.0.0'
|
|
1032
1103
|
Exclude:
|
|
1033
1104
|
- '**/metadata.rb'
|
|
1034
1105
|
- '**/attributes/*.rb'
|
|
@@ -1077,6 +1148,7 @@ Chef/Deprecations/ChefWindowsPlatformHelper:
|
|
|
1077
1148
|
StyleGuide: 'chef_deprecations_chefwindowsplatformhelper'
|
|
1078
1149
|
Enabled: true
|
|
1079
1150
|
VersionAdded: '6.0.0'
|
|
1151
|
+
VersionChanged: '9.0.0'
|
|
1080
1152
|
Exclude:
|
|
1081
1153
|
- '**/metadata.rb'
|
|
1082
1154
|
- '**/Berksfile'
|
|
@@ -1095,7 +1167,7 @@ Chef/Deprecations/HWRPWithoutProvides:
|
|
|
1095
1167
|
StyleGuide: 'chef_deprecations_hwrpwithoutprovides'
|
|
1096
1168
|
Enabled: true
|
|
1097
1169
|
VersionAdded: '6.0.0'
|
|
1098
|
-
VersionChanged: '
|
|
1170
|
+
VersionChanged: '9.0.0'
|
|
1099
1171
|
Include:
|
|
1100
1172
|
- '**/libraries/*.rb'
|
|
1101
1173
|
|
|
@@ -1159,6 +1231,7 @@ Chef/Deprecations/ChefHandlerRecipe:
|
|
|
1159
1231
|
StyleGuide: 'chef_deprecations_chefhandlerrecipe'
|
|
1160
1232
|
Enabled: true
|
|
1161
1233
|
VersionAdded: '6.12.0'
|
|
1234
|
+
VersionChanged: '9.0.0'
|
|
1162
1235
|
Exclude:
|
|
1163
1236
|
- '**/metadata.rb'
|
|
1164
1237
|
- '**/Berksfile'
|
|
@@ -1195,6 +1268,7 @@ Chef/Deprecations/ChefShellout:
|
|
|
1195
1268
|
StyleGuide: 'chef_deprecations_chefshellout'
|
|
1196
1269
|
Enabled: true
|
|
1197
1270
|
VersionAdded: '6.17.0'
|
|
1271
|
+
VersionChanged: '9.0.0'
|
|
1198
1272
|
Exclude:
|
|
1199
1273
|
- '**/metadata.rb'
|
|
1200
1274
|
- '**/attributes/*.rb'
|
|
@@ -1287,6 +1361,7 @@ Chef/Deprecations/HWRPWithoutUnifiedTrue:
|
|
|
1287
1361
|
StyleGuide: 'chef_deprecations_hwrpwithoutunifiedtrue'
|
|
1288
1362
|
Enabled: true
|
|
1289
1363
|
VersionAdded: '7.12.0'
|
|
1364
|
+
VersionChanged: '9.0.0'
|
|
1290
1365
|
Include:
|
|
1291
1366
|
- '**/libraries/*.rb'
|
|
1292
1367
|
|
|
@@ -1437,6 +1512,7 @@ Chef/Modernize/IncludingAptDefaultRecipe:
|
|
|
1437
1512
|
StyleGuide: 'chef_modernize_includingaptdefaultrecipe'
|
|
1438
1513
|
Enabled: true
|
|
1439
1514
|
VersionAdded: '5.3.0'
|
|
1515
|
+
VersionChanged: '9.0.0'
|
|
1440
1516
|
Exclude:
|
|
1441
1517
|
- '**/metadata.rb'
|
|
1442
1518
|
|
|
@@ -1445,6 +1521,7 @@ Chef/Modernize/IncludingWindowsDefaultRecipe:
|
|
|
1445
1521
|
StyleGuide: 'chef_modernize_includingwindowsdefaultrecipe'
|
|
1446
1522
|
Enabled: true
|
|
1447
1523
|
VersionAdded: '5.3.0'
|
|
1524
|
+
VersionChanged: '9.0.0'
|
|
1448
1525
|
Exclude:
|
|
1449
1526
|
- '**/metadata.rb'
|
|
1450
1527
|
|
|
@@ -1461,6 +1538,7 @@ Chef/Modernize/ExecuteAptUpdate:
|
|
|
1461
1538
|
StyleGuide: 'chef_modernize_executeaptupdate'
|
|
1462
1539
|
Enabled: true
|
|
1463
1540
|
VersionAdded: '5.3.0'
|
|
1541
|
+
VersionChanged: '9.0.0'
|
|
1464
1542
|
Exclude:
|
|
1465
1543
|
- '**/metadata.rb'
|
|
1466
1544
|
|
|
@@ -1487,6 +1565,7 @@ Chef/Modernize/UseBuildEssentialResource:
|
|
|
1487
1565
|
StyleGuide: 'chef_modernize_usebuildessentialresource'
|
|
1488
1566
|
Enabled: true
|
|
1489
1567
|
VersionAdded: '5.1.0'
|
|
1568
|
+
VersionChanged: '9.0.0'
|
|
1490
1569
|
Exclude:
|
|
1491
1570
|
- '**/metadata.rb'
|
|
1492
1571
|
|
|
@@ -1511,6 +1590,7 @@ Chef/Modernize/LibarchiveFileResource:
|
|
|
1511
1590
|
StyleGuide: 'chef_modernize_libarchivefileresource'
|
|
1512
1591
|
Enabled: true
|
|
1513
1592
|
VersionAdded: '5.5.0'
|
|
1593
|
+
VersionChanged: '9.0.0'
|
|
1514
1594
|
Exclude:
|
|
1515
1595
|
- '**/metadata.rb'
|
|
1516
1596
|
|
|
@@ -1522,6 +1602,16 @@ Chef/Modernize/PowershellScriptExpandArchive:
|
|
|
1522
1602
|
Exclude:
|
|
1523
1603
|
- '**/metadata.rb'
|
|
1524
1604
|
|
|
1605
|
+
Chef/Modernize/PowershellDownloadFile:
|
|
1606
|
+
Description: "Use the `remote_file` resource to download files instead of shelling out to PowerShell. `remote_file` is idempotent and supports checksum verification, proxies, and conditional downloads."
|
|
1607
|
+
StyleGuide: 'chef_modernize_powershelldownloadfile'
|
|
1608
|
+
Enabled: true
|
|
1609
|
+
VersionAdded: '9.0.0'
|
|
1610
|
+
Exclude:
|
|
1611
|
+
- '**/metadata.rb'
|
|
1612
|
+
- '**/attributes/*.rb'
|
|
1613
|
+
- '**/Berksfile'
|
|
1614
|
+
|
|
1525
1615
|
Chef/Modernize/PowershellInstallPackage:
|
|
1526
1616
|
Description: Use the package resource built into Chef Infra Client instead of using Install-Package in a powershell_script resource
|
|
1527
1617
|
StyleGuide: 'chef_modernize_powershellinstallpackage'
|
|
@@ -1711,7 +1801,7 @@ Chef/Modernize/IncludingOhaiDefaultRecipe:
|
|
|
1711
1801
|
StyleGuide: 'chef_modernize_includingohaidefaultrecipe'
|
|
1712
1802
|
Enabled: true
|
|
1713
1803
|
VersionAdded: '5.4.0'
|
|
1714
|
-
VersionChanged: '
|
|
1804
|
+
VersionChanged: '9.0.0'
|
|
1715
1805
|
Exclude:
|
|
1716
1806
|
- '**/metadata.rb'
|
|
1717
1807
|
- '**/Berksfile'
|
|
@@ -1768,6 +1858,7 @@ Chef/Modernize/DslIncludeInResource:
|
|
|
1768
1858
|
StyleGuide: 'chef_modernize_dslincludeinresource'
|
|
1769
1859
|
Enabled: true
|
|
1770
1860
|
VersionAdded: '5.17.0'
|
|
1861
|
+
VersionChanged: '9.0.0'
|
|
1771
1862
|
Include:
|
|
1772
1863
|
- '**/resources/*.rb'
|
|
1773
1864
|
- '**/providers/*.rb'
|
|
@@ -1782,6 +1873,26 @@ Chef/Modernize/ResourceForcingCompileTime:
|
|
|
1782
1873
|
- '**/attributes/*.rb'
|
|
1783
1874
|
- '**/Berksfile'
|
|
1784
1875
|
|
|
1876
|
+
Chef/Modernize/ExecuteUpdateAlternatives:
|
|
1877
|
+
Description: "Chef Infra Client 16.0 and later includes an `alternatives` resource that should be used to manage alternatives links instead of shelling out to `update-alternatives` or `alternatives`."
|
|
1878
|
+
StyleGuide: 'chef_modernize_executeupdatealternatives'
|
|
1879
|
+
Enabled: true
|
|
1880
|
+
VersionAdded: '9.0.0'
|
|
1881
|
+
Exclude:
|
|
1882
|
+
- '**/metadata.rb'
|
|
1883
|
+
- '**/attributes/*.rb'
|
|
1884
|
+
- '**/Berksfile'
|
|
1885
|
+
|
|
1886
|
+
Chef/Modernize/ExecuteArchiveExtract:
|
|
1887
|
+
Description: "Use the `archive_file` resource built into Chef Infra Client 15+ instead of shelling out to `tar` or `unzip` to extract an archive."
|
|
1888
|
+
StyleGuide: 'chef_modernize_executearchiveextract'
|
|
1889
|
+
Enabled: true
|
|
1890
|
+
VersionAdded: '9.0.0'
|
|
1891
|
+
Exclude:
|
|
1892
|
+
- '**/metadata.rb'
|
|
1893
|
+
- '**/attributes/*.rb'
|
|
1894
|
+
- '**/Berksfile'
|
|
1895
|
+
|
|
1785
1896
|
Chef/Modernize/ExecuteSysctl:
|
|
1786
1897
|
Description: Chef Infra Client 14.0 and later includes a sysctl resource that should be used to idempotently load sysctl values instead of templating files and using execute to load them.
|
|
1787
1898
|
StyleGuide: 'chef_modernize_executesysctl'
|
|
@@ -1807,6 +1918,7 @@ Chef/Modernize/UseRequireRelative:
|
|
|
1807
1918
|
StyleGuide: 'chef_modernize_userequirerelative'
|
|
1808
1919
|
Enabled: true
|
|
1809
1920
|
VersionAdded: '5.22.0'
|
|
1921
|
+
VersionChanged: '9.0.0'
|
|
1810
1922
|
Exclude:
|
|
1811
1923
|
- '**/metadata.rb'
|
|
1812
1924
|
- '**/attributes/*.rb'
|
|
@@ -1856,6 +1968,7 @@ Chef/Modernize/DatabagHelpers:
|
|
|
1856
1968
|
StyleGuide: 'chef_modernize_databaghelpers'
|
|
1857
1969
|
Enabled: true
|
|
1858
1970
|
VersionAdded: '6.0.0'
|
|
1971
|
+
VersionChanged: '9.0.0'
|
|
1859
1972
|
Exclude:
|
|
1860
1973
|
- '**/metadata.rb'
|
|
1861
1974
|
- '**/Berksfile'
|
|
@@ -1889,10 +2002,11 @@ Chef/Modernize/ShellOutHelper:
|
|
|
1889
2002
|
- '**/libraries/*.rb'
|
|
1890
2003
|
|
|
1891
2004
|
Chef/Modernize/ConditionalUsingTest:
|
|
1892
|
-
Description: Use
|
|
2005
|
+
Description: "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."
|
|
1893
2006
|
StyleGuide: 'chef_modernize_conditionalusingtest'
|
|
1894
2007
|
Enabled: true
|
|
1895
2008
|
VersionAdded: '6.11.0'
|
|
2009
|
+
VersionChanged: '9.0.0'
|
|
1896
2010
|
Exclude:
|
|
1897
2011
|
- '**/metadata.rb'
|
|
1898
2012
|
- '**/Berksfile'
|
|
@@ -2114,7 +2228,7 @@ Chef/RedundantCode/UnnecessaryNameProperty:
|
|
|
2114
2228
|
StyleGuide: 'chef_redundantcode_unnecessarynameproperty'
|
|
2115
2229
|
Enabled: true
|
|
2116
2230
|
VersionAdded: '5.8.0'
|
|
2117
|
-
VersionChanged: '
|
|
2231
|
+
VersionChanged: '9.0.0'
|
|
2118
2232
|
Include:
|
|
2119
2233
|
- '**/resources/*.rb'
|
|
2120
2234
|
- '**/libraries/*.rb'
|
|
@@ -2199,6 +2313,7 @@ Chef/RedundantCode/StringPropertyWithNilDefault:
|
|
|
2199
2313
|
StyleGuide: 'chef_redundantcode_stringpropertywithnildefault'
|
|
2200
2314
|
Enabled: true
|
|
2201
2315
|
VersionAdded: '5.21.0'
|
|
2316
|
+
VersionChanged: '9.0.0'
|
|
2202
2317
|
Include:
|
|
2203
2318
|
- '**/resources/*.rb'
|
|
2204
2319
|
- '**/libraries/*.rb'
|
|
@@ -2245,6 +2360,17 @@ Chef/RedundantCode/DoubleCompileTime:
|
|
|
2245
2360
|
StyleGuide: 'chef_redundantcode_doublecompiletime'
|
|
2246
2361
|
Enabled: true
|
|
2247
2362
|
VersionAdded: '6.13.0'
|
|
2363
|
+
VersionChanged: '9.0.0'
|
|
2364
|
+
Exclude:
|
|
2365
|
+
- '**/metadata.rb'
|
|
2366
|
+
- '**/attributes/*.rb'
|
|
2367
|
+
- '**/Berksfile'
|
|
2368
|
+
|
|
2369
|
+
Chef/RedundantCode/ServiceGuardOnStopDisable:
|
|
2370
|
+
Description: "The `:stop` and `:disable` actions of the `service` resource are already no-ops when the service isn't installed, so guarding them with a check for the init script or unit file is redundant. It's also fragile, since the guard hardcodes a single path while the provider searches several."
|
|
2371
|
+
StyleGuide: 'chef_redundantcode_serviceguardonstopdisable'
|
|
2372
|
+
Enabled: true
|
|
2373
|
+
VersionAdded: '9.0.0'
|
|
2248
2374
|
Exclude:
|
|
2249
2375
|
- '**/metadata.rb'
|
|
2250
2376
|
- '**/attributes/*.rb'
|
|
@@ -2366,6 +2492,15 @@ Chef/Security:
|
|
|
2366
2492
|
Enabled: true
|
|
2367
2493
|
StyleGuideBaseURL: https://docs.chef.io/workstation/cookstyle/
|
|
2368
2494
|
|
|
2495
|
+
Chef/Security/InsecureRemoteFileSource:
|
|
2496
|
+
Description: "Fetch remote files over HTTPS. A file downloaded over plain HTTP or FTP can be modified in transit and the resource will use whatever it receives."
|
|
2497
|
+
StyleGuide: 'chef_security_insecureremotefilesource'
|
|
2498
|
+
Enabled: true
|
|
2499
|
+
VersionAdded: '9.0.0'
|
|
2500
|
+
Exclude:
|
|
2501
|
+
- '**/metadata.rb'
|
|
2502
|
+
- '**/Berksfile'
|
|
2503
|
+
|
|
2369
2504
|
Chef/Security/SshPrivateKey:
|
|
2370
2505
|
Description: Do not include plain text SSH private keys in your cookbook code. This sensitive data should be fetched from secrets management systems so that secrets are not uploaded in plain text to the Chef Infra Server or committed to source control systems.
|
|
2371
2506
|
StyleGuide: 'chef_security_sshprivatekey'
|
|
@@ -2704,6 +2839,8 @@ Lint/EnsureReturn:
|
|
|
2704
2839
|
Enabled: true
|
|
2705
2840
|
Security/Eval:
|
|
2706
2841
|
Enabled: true
|
|
2842
|
+
Security/YAMLLoad:
|
|
2843
|
+
Enabled: true
|
|
2707
2844
|
Lint/FloatOutOfRange:
|
|
2708
2845
|
Enabled: true
|
|
2709
2846
|
Lint/FormatParameterMismatch:
|
|
@@ -2750,6 +2887,8 @@ Lint/UnusedMethodArgument:
|
|
|
2750
2887
|
Enabled: true
|
|
2751
2888
|
Lint/UnreachableCode:
|
|
2752
2889
|
Enabled: true
|
|
2890
|
+
Lint/UriEscapeUnescape:
|
|
2891
|
+
Enabled: true
|
|
2753
2892
|
Lint/UselessAccessModifier:
|
|
2754
2893
|
Enabled: true
|
|
2755
2894
|
Lint/UselessAssignment:
|
data/lib/cookstyle/version.rb
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
module Cookstyle
|
|
3
3
|
# version of cookstyle
|
|
4
|
-
VERSION = "
|
|
4
|
+
VERSION = "9.0.0" # rubocop: disable Style/StringLiterals
|
|
5
5
|
|
|
6
6
|
# version of rubocop we pin to
|
|
7
|
-
RUBOCOP_VERSION = '1.
|
|
7
|
+
RUBOCOP_VERSION = '1.90.0'
|
|
8
8
|
end
|
|
@@ -67,6 +67,29 @@ module RuboCop
|
|
|
67
67
|
end
|
|
68
68
|
end
|
|
69
69
|
|
|
70
|
+
# Yield each action symbol declared by an `action` property, however it's written:
|
|
71
|
+
# `action :foo`, `action :foo, :bar`, `action [:foo, :bar]`, or `action %i(foo bar)`.
|
|
72
|
+
#
|
|
73
|
+
# The yielded prefix is what a corrector must put in front of a replacement action name.
|
|
74
|
+
# Inside a %i() literal the elements carry no colon, so replacing `%i(create)` with `:run`
|
|
75
|
+
# would produce the invalid `%i(:run)`.
|
|
76
|
+
#
|
|
77
|
+
# @param [RuboCop::AST::SendNode] action_property the `action` property node
|
|
78
|
+
#
|
|
79
|
+
# @yield [RuboCop::AST::SymbolNode, String] the action symbol and the replacement prefix
|
|
80
|
+
#
|
|
81
|
+
def each_action_symbol(action_property)
|
|
82
|
+
action_property.arguments.each do |arg|
|
|
83
|
+
if arg.sym_type?
|
|
84
|
+
yield(arg, ':')
|
|
85
|
+
elsif arg.array_type?
|
|
86
|
+
prefix = arg.percent_literal? ? '' : ':'
|
|
87
|
+
symbols = arg.values.select(&:sym_type?)
|
|
88
|
+
symbols.each { |symbol| yield(symbol, prefix) }
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
70
93
|
def method_arg_ast_to_string(ast)
|
|
71
94
|
# a property without a value. This is totally bogus, but they exist
|
|
72
95
|
return if ast.children[2].nil?
|
data/lib/rubocop/chef.rb
CHANGED
|
@@ -4,7 +4,7 @@ module RuboCop
|
|
|
4
4
|
module Chef
|
|
5
5
|
PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
|
|
6
6
|
CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'cookstyle.yml').freeze
|
|
7
|
-
CONFIG = YAML.
|
|
7
|
+
CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
|
|
8
8
|
|
|
9
9
|
private_constant(*constants(false))
|
|
10
10
|
end
|
|
@@ -21,10 +21,15 @@ module RuboCop
|
|
|
21
21
|
module Correctness
|
|
22
22
|
# Use `raise` to force Chef Infra Client to fail instead of using `Chef::Application.fatal`, which masks the full stack trace of the failure and makes debugging difficult.
|
|
23
23
|
#
|
|
24
|
+
# `Chef::Application.fatal!` optionally takes an exit code as a second argument. That form is
|
|
25
|
+
# reported but not autocorrected, since `raise` has no way to express a specific exit code and
|
|
26
|
+
# rewriting it would silently change how the run terminates.
|
|
27
|
+
#
|
|
24
28
|
# @example
|
|
25
29
|
#
|
|
26
30
|
# # bad
|
|
27
31
|
# Chef::Application.fatal!('Something horrible happened!')
|
|
32
|
+
# Chef::Application.fatal!('Something horrible happened!', 1)
|
|
28
33
|
#
|
|
29
34
|
# # good
|
|
30
35
|
# raise "Something horrible happened!"
|
|
@@ -38,14 +43,21 @@ module RuboCop
|
|
|
38
43
|
def_node_matcher :application_fatal?, <<-PATTERN
|
|
39
44
|
(send
|
|
40
45
|
(const
|
|
41
|
-
(const nil? :Chef) :Application) :fatal!
|
|
42
|
-
|
|
46
|
+
(const {nil? cbase} :Chef) :Application) :fatal!
|
|
47
|
+
$...)
|
|
43
48
|
PATTERN
|
|
44
49
|
|
|
45
50
|
def on_send(node)
|
|
46
|
-
application_fatal?(node) do |
|
|
51
|
+
application_fatal?(node) do |args|
|
|
52
|
+
# Only the single message argument form maps cleanly onto raise. With an exit code (or
|
|
53
|
+
# with no message at all) we still report, but leave the rewrite to a human.
|
|
54
|
+
unless args.one?
|
|
55
|
+
add_offense(node, severity: :refactor)
|
|
56
|
+
next
|
|
57
|
+
end
|
|
58
|
+
|
|
47
59
|
add_offense(node, severity: :refactor) do |corrector|
|
|
48
|
-
corrector.replace(node, "raise(#{
|
|
60
|
+
corrector.replace(node, "raise(#{args.first.source})")
|
|
49
61
|
end
|
|
50
62
|
end
|
|
51
63
|
end
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
#
|
|
3
|
+
# Copyright:: Copyright (c) 2016-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
|
|
4
|
+
# Author:: Tim Smith (<tsmith84@gmail.com>)
|
|
5
|
+
#
|
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
# you may not use this file except in compliance with the License.
|
|
8
|
+
# You may obtain a copy of the License at
|
|
9
|
+
#
|
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
#
|
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
# See the License for the specific language governing permissions and
|
|
16
|
+
# limitations under the License.
|
|
17
|
+
#
|
|
18
|
+
module RuboCop
|
|
19
|
+
module Cop
|
|
20
|
+
module Chef
|
|
21
|
+
module Correctness
|
|
22
|
+
# Setting `unified_mode true` conditionally leaves a resource running in unified mode on newer
|
|
23
|
+
# Chef Infra Client releases and in legacy mode on older ones. That's not a coherent thing to
|
|
24
|
+
# want: the resource has to be written, tested, and debugged against both execution models.
|
|
25
|
+
#
|
|
26
|
+
# Pick one. Set `unified_mode true` unconditionally and drop support for releases that predate
|
|
27
|
+
# it, or set `unified_mode false if respond_to?(:unified_mode)` to deliberately opt out of
|
|
28
|
+
# unified mode everywhere and keep writing the resource in the traditional style.
|
|
29
|
+
#
|
|
30
|
+
# Only `unified_mode true` is flagged. `unified_mode false` under a guard is the supported way
|
|
31
|
+
# to opt out and is left alone.
|
|
32
|
+
#
|
|
33
|
+
# @example
|
|
34
|
+
#
|
|
35
|
+
# # bad
|
|
36
|
+
# unified_mode true if respond_to?(:unified_mode)
|
|
37
|
+
#
|
|
38
|
+
# # good
|
|
39
|
+
# unified_mode true
|
|
40
|
+
#
|
|
41
|
+
# # good - deliberately opting out of unified mode
|
|
42
|
+
# unified_mode false if respond_to?(:unified_mode)
|
|
43
|
+
#
|
|
44
|
+
class ConditionalUnifiedModeTrue < Base
|
|
45
|
+
extend AutoCorrector
|
|
46
|
+
extend TargetChefVersion
|
|
47
|
+
|
|
48
|
+
minimum_target_chef_version '15.3'
|
|
49
|
+
|
|
50
|
+
MSG = 'Set `unified_mode true` unconditionally. Making it conditional gives you unified mode on newer Chef Infra Client releases and legacy mode on older ones, so the resource has to be tested and reasoned about both ways.'
|
|
51
|
+
RESTRICT_ON_SEND = [:unified_mode].freeze
|
|
52
|
+
|
|
53
|
+
def_node_matcher :unified_mode_true?, '(send nil? :unified_mode (true))'
|
|
54
|
+
|
|
55
|
+
def on_send(node)
|
|
56
|
+
return unless unified_mode_true?(node)
|
|
57
|
+
|
|
58
|
+
conditional = node.each_ancestor(:if).first
|
|
59
|
+
return unless conditional
|
|
60
|
+
|
|
61
|
+
add_offense(node, severity: :refactor) do |corrector|
|
|
62
|
+
# only unwrap the conditional when it wraps nothing but this property, otherwise
|
|
63
|
+
# we'd delete whatever else the branch was guarding. an elsif is never safe to unwrap:
|
|
64
|
+
# its node covers only the elsif itself, so replacing it drops that branch's condition
|
|
65
|
+
# and folds the property into the preceding branch
|
|
66
|
+
next if conditional.elsif?
|
|
67
|
+
next unless sole_branch_body?(conditional, node)
|
|
68
|
+
|
|
69
|
+
corrector.replace(conditional, node.source)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
private
|
|
74
|
+
|
|
75
|
+
# Is the property the entire body of the conditional? `if` puts the body in the first
|
|
76
|
+
# child after the condition and `unless` puts it in the second, so check both. A branch
|
|
77
|
+
# holding anything else is a `begin` node rather than the property itself, and an `if`
|
|
78
|
+
# with an `else` (or an `elsif` chain) leaves the other branch non-nil.
|
|
79
|
+
#
|
|
80
|
+
# @param [RuboCop::AST::IfNode] conditional
|
|
81
|
+
# @param [RuboCop::AST::SendNode] node the `unified_mode true` property
|
|
82
|
+
#
|
|
83
|
+
# @return [Boolean]
|
|
84
|
+
def sole_branch_body?(conditional, node)
|
|
85
|
+
conditional.children[1..2].compact == [node]
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
@@ -19,13 +19,18 @@ module RuboCop
|
|
|
19
19
|
module Cop
|
|
20
20
|
module Chef
|
|
21
21
|
module Correctness
|
|
22
|
-
# Resource guards (not_if/only_if) should not be empty
|
|
23
|
-
#
|
|
22
|
+
# Resource guards (not_if/only_if) should not be empty. An empty string always evaluates to true
|
|
23
|
+
# and an empty block always evaluates to false, so in either case the guard stops doing the job
|
|
24
|
+
# it was written for and the resource silently runs, or fails to run, on every converge.
|
|
24
25
|
#
|
|
25
26
|
# Empty strings in Ruby are "truthy", which means:
|
|
26
27
|
# - `only_if ''` will ALWAYS execute the resource (guard always passes)
|
|
27
28
|
# - `not_if ''` will NEVER execute the resource (guard always blocks)
|
|
28
29
|
#
|
|
30
|
+
# An empty block behaves the other way around, since a block with no body returns `nil`:
|
|
31
|
+
# - `only_if { }` will NEVER execute the resource
|
|
32
|
+
# - `not_if { }` will ALWAYS execute the resource
|
|
33
|
+
#
|
|
29
34
|
# This behavior is usually unintended and can lead to resources running when they shouldn't
|
|
30
35
|
# or never running when they should.
|
|
31
36
|
#
|
|
@@ -72,6 +77,7 @@ module RuboCop
|
|
|
72
77
|
#
|
|
73
78
|
class EmptyResourceGuard < Base
|
|
74
79
|
MSG = 'Resource guards (not_if/only_if) should not be empty strings as empty strings will always evaluate to true.'
|
|
80
|
+
EMPTY_BLOCK_MSG = 'Resource guards (not_if/only_if) should not be empty blocks as an empty block returns nil, which always evaluates to false.'
|
|
75
81
|
RESTRICT_ON_SEND = [:not_if, :only_if].freeze
|
|
76
82
|
|
|
77
83
|
def_node_matcher :empty_string_guard?, <<-PATTERN
|
|
@@ -82,6 +88,12 @@ module RuboCop
|
|
|
82
88
|
(block (send nil? {:not_if :only_if}) (args) (str #empty_string?))
|
|
83
89
|
PATTERN
|
|
84
90
|
|
|
91
|
+
# A block with no body at all. Such a block returns nil rather than the empty string matched
|
|
92
|
+
# above, so the guard fails rather than passes.
|
|
93
|
+
def_node_matcher :empty_block_guard?, <<-PATTERN
|
|
94
|
+
(block (send nil? {:not_if :only_if}) (args) nil?)
|
|
95
|
+
PATTERN
|
|
96
|
+
|
|
85
97
|
def empty_string?(str)
|
|
86
98
|
str.empty?
|
|
87
99
|
end
|
|
@@ -96,6 +108,10 @@ module RuboCop
|
|
|
96
108
|
empty_string_block_guard?(node) do
|
|
97
109
|
add_offense(node, severity: :refactor)
|
|
98
110
|
end
|
|
111
|
+
|
|
112
|
+
empty_block_guard?(node) do
|
|
113
|
+
add_offense(node, message: EMPTY_BLOCK_MSG, severity: :refactor)
|
|
114
|
+
end
|
|
99
115
|
end
|
|
100
116
|
end
|
|
101
117
|
end
|