chef / cookstyle

A linting tool that helps you to write better Chef Infra cookbooks and InSpec profiles by detecting and automatically correcting style, syntax, and logic mistakes in your code.
Apache License 2.0
107 stars 54 forks source link

Flag Chef/Modernize/CronDFileOrTemplate offenses with string interpolation #941

Closed yacn closed 2 years ago

yacn commented 2 years ago

Updates node matcher pattern for Chef/Modernize/CronDFileOrTempalte to capture everything in the file/template/cookbook_file resource declaration to catch offenses where the resource name is both a regular string and an interpolated string.

Description

The existing matcher does not flag offenses if the file/cookbook_file/template resource is named using string interpolation

Related Issue

https://github.com/chef/cookstyle/issues/940

Types of changes

Checklist:

since i don't use ruby outside of ChefDK, I need to put together a local dev env to run the tests in; doing so currently and will update the Checklist after I've done so and run the tests on my local machine. This has since been completed.

tas50 commented 2 years ago

@yacn This fails with this kind of code:

template config_file do
  source 'opt/docker-compose/app/athenz/docker-compose.yml'
  owner 'root'
  group 'root'
  mode '0644'
end
tas50 commented 2 years ago

I added a spec that will catch that scenario

yacn commented 2 years ago

@tas50 great catch! I originally modified the node matcher pattern to specifically look for an interpolated string but thought it might be more complicated and so used the larger capture (thinking it'd be simpler) thus which introduced the bug you added a test for.

tas50 commented 2 years ago

This found 26 new offenses on Supermarket which is more than the previous cop found. Nice improvement