Development Quality for Ruby and Chef

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.

Preparation

I have a WIP Git repository which install all the plugins and configures Atom. Please check README.md for more details.

Unit Testing

In first place we have unit testing, I’m a big fan of TDD and in Ruby there I’m using Rspec and following practices from Better Spec.

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

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.

As for Chef there is Serverspec available or more recently Inspec

Acceptance Testing

For this I’m running Cucumber and Capybara so I can test end user behaviour either it is a website or an API.

Static Code Analysis

Editor tools

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.

As well as Linter-Ruby/Linter-ERB.

And if you feel a bit dangerous you can Rubocop-auto-correct to automatically apply the changes.

If you work with Chef there is also Foodcritic for Chef specific style guide and on Atom there is Linter-Foodcritic (I think you see where this is going).

Atom running the previously named plugins

Other languages

Linter-JSON— Useful for Cloudformation or any JSON… Uses JSONLint

Linter-Shellcheck — Linter for Shell scripts (which you’ll almost certainly have around)

Linter-Docker / Language-Docker— Linting and Syntax Highlighting.

Linter-YAML— Using it for Test-kitchen and docker YAML.

Background tools

Rubycritic

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:

Reek Code smell detector

Flag — Structural similarities (code duplication, variable names, …)

Flog— Code complexity

I also run Linter-Reek for Atom to more quickly address issues before waiting for Rubycritic and find line numbers.

Simplecov

Simplecov is a code coverage generator, and it does what it says in the tin.

image from Rubyinsider

Debride

Debride looks for methods which it thinks are not in use. You can also install Debride-ERB which is useful for Chef config files.

Debride

Fasterer

Next we have Fasterer which will analyse the code based on performance best practices from Fast-Ruby

Fasterer

Atom ‘Nice to Have’ / Workflow

Version Control (Git)

Git-Plus — Hit Cmd+Shift+H for the Git Command Pallete

Merge-Conflicts

Git-Time-Machine— Hit Alt+T and visualise previous commits and compare on the fly.

Others

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.

Conclusions

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.