originally posted medium
A few years ago I wrote a small blog post about how to setup a local development environment for Chef using Vagrant and Test-Kitchen (you can see it here).
Lately I’ve been questioned about how to maintain a codebase through the years and how to work on/find quality metrics.
A lot of people come from previous background where just vi/nano is required to lash out some code and moving to Ruby/Chef it seems only the language changed but there wasn’t much investment on better software development practices.
I’m now describing my Ruby/Chef local setup hoping it will help people get out of the rut or get some feedback for myself about tools/processes I might be missing out on.
I’m assuming the previous article was read or people are aware of its contents so I won’t re-explain Bundler, Gemfiles, RbEnv, etc.
I’m also using the Atom editor so a lot of the integration will go through its plugins.
For Chef the option is Chefspec (which also includes Berkshelf support)
I’m also running Guard/Guard-Rspec in the background which will re-run tests based on file changes so I don’t need to be reminded — I can set up alerts in case tests fail.
Guard running for the unit tests
Another useful library is Timecop which allows me to time travel on time specific code — ensuring no external dependencies in that aspect and testing edge cases like timeouts.
If you’re still using ERB for local Ruby debugging then I’d suggest moving to Pry, it has too many features to mention but just the debugging is a lifesaver.
Pry binding example from the Pry website
Integration testing (between different layers, services, but only testing one relation at a time) is also done in Rspec. You can split the folder structure in a way that integration runs separately on guard (or not).
I also use Fakeweb which will allow me to stub out any HTTP calls outside the scope of the test.
Static Code Analysis
I’m using Rubocop for analysing code based on the community ruby style guide.
Also on Atom I’m using the Linter-Rubocop to have per line information about the issues.
And if you feel a bit dangerous you can Rubocop-auto-correctto automatically apply the changes.
Atom running the previously named plugins
Linter-Shellcheck — Linter for Shell scripts (which you’ll almost certainly have around)
Linter-YAML****— Using it for Test-kitchen and docker YAML.
We can use Rubycritic and Guard-Rubycritic to report ruby code smells. I like to run Rubycritic once in a while to get a page with a report.
Images from the official Rubycritic page
Rubycritic runs with three highly influential gems:
Flag — Structural similarities (code duplication, variable names, …)
I also run Linter-Reek for Atom to more quickly address issues before waiting for Rubycritic and find line numbers.
Simplecov is a code coverage generator, and it does what it says in the tin.
image from Rubyinsider
Debride looks for methods which it thinks are not in use. You can also install Debride-ERB which is useful for Chef config files.
Atom ‘Nice to Have’ / Workflow
Version Control (Git)
Git-Plus — Hit Cmd+Shift+H for the Git Command Pallete
Git-Time-Machine****— Hit Alt+T and visualise previous commits and compare on the fly.
Ctrl+Shift+M (Default install) — Open Markdown preview
Shift+Cmd+P (Default install) — Open the Command Palette in case you don’t know a command
Atom-Beautify (Default install) — Hit Ctrl+Alt+B to beautify a number of formats
Fuzzy-Grep — Ctrl+Alt+G to find variables, methods or strings on any file
Ror-Refactor****— Hit Ctrl+Alt+Cmd+R to extract line, method or class (if multiple methods)
TODO-Show****— Hit Ctrl+Shift+T to show list of TODO, XXX, FIX-MEs and so on.
Linter-Write-Good****— Write. Better. English.
I hope you found this post useful to maybe finally upgrade your barebones editor install or to give you some ideas on ways to improve your day to day code analysis.
I’ll be waiting for feedback on tools I might have missed which should be on this post and might help me on my day to day work.
I’ll also be adding a blog post on workflow to improve updating chef cookbook CI/CD through several environments.