Ruby: “the best” language for general automation

Earlier than you say “there is no such thing as a higher programming language, every one has its goal”, I do know it. That is why “the most effective” is in quotes within the title.

However on this textual content I wish to present 3 conditions during which Ruby was the only option, though I did not wish to use it in two. With Ruby, I did in hours what I could not do in days (2 to three) in one other language (Shell Script, on this case).

The three conditions that I’ll discuss listed below are:

  1. Steady compliance
  2. GitHub Automations
  3. CLI Take a look at Automation (Command Line Instruments)

However earlier than stepping into these matters,



Why Ruby?

Its creator, Yukihiro “Matz” Matsumoto, mixed components of his favourite languages (Perl, Smalltalk, Eiffel, Ada, and Lisp) to type a brand new language that balanced practical programming with crucial programming. He stated, “I needed a scripting language that was extra highly effective than Perl and extra object-oriented than Python.” (From https://www.ruby-lang.org/en/about/)

Ruby is very transportable: it’s primarily developed on GNU/Linux, however works on many kinds of UNIX, macOS, Home windows, DOS, BeOS, OS/2, and many others. It comes by default on many Linux distributions and on macOS.

Ruby isn’t solely fully free, but in addition free to make use of, copy, modify and distribute.

It’s thought-about among the finest languages for check automation and one of many pioneers in some frameworks, resembling RSpec and Cucumber. However sadly its recognition is lowering.



The place is Ruby used?

Beneath, solely 3 examples the place Ruby is used:

  • GitHub
  • homebrew
  • cocoapods



My expertise with Ruby

Principally my complete profession as a QA was automating in Ruby. Solely lately with the popularization of Cypress for internet testing, I migrated to JavaScript as the principle language.

Now let’s get to the principle topic, which is the place I am utilizing Ruby though it wasn’t my first alternative.



Steady compliance

I did not even comprehend it existed till I watched TAU’s Continuous Compliance course. This course opened my thoughts to a sea of ​​prospects for automating checks.

The course makes use of Chef Inspec, an open source Ruby DSL. I made a POC with this instrument to mechanically verify repositories on GitHub, checks like if it accommodates a gitignore according to the language used, if node_modules isn’t current, and many others.

The issue is that Chef Inspec have to be put in each time a workflow runs, along with having to run a selected command, the repository being in a selected folder, having to look within the documentation for the command to carry out a verify, which is typically restricted …

After studying the documentation and realizing the chances to be used, I ended up abandoning the instrument to make use of pure Ruby. If the instrument made in Ruby does it, clearly pure Ruby will do it too, and rather more, as a result of there can be actions not carried out within the DSL (this is among the causes I am a bit in opposition to DSLs, however that is for an additional textual content).



GitHub Automations

In all probability everybody right here has already created a repository on GitHub, however I am positive in case you do not use GitHub for work, you’ve got by no means bothered to safe a department, for instance.

Some settings are important when working with GitHub, resembling defending the develop and important branches with some guidelines:

  • Don’t settle for direct push
  • Merge with pull request solely
  • Require signed commits and many others

These settings are just a little boring to make. When making a repository, it will probably take a while clicking on (checking) checkboxes. This time could be diminished by scripts for automating the creation of repositories by the GitHub API.

As I needed these scripts to have the ability to run on each Linux and macOS (sorry Home windows. Hello WSL), I began to develop them in Shell Script, since it’s a native language of each programs.

However when it got here time to do some checks earlier than an motion, Shell Script grew to become extraordinarily sophisticated. And what’s the different language that comes with Linux and macOS? That is proper, Ruby.

I used Ruby to do some checks like if the repository exists, checklist repositories and name current sh information.

I considered utilizing JavaScript, as we’ve got extra individuals who develop in that language on the staff, however Ruby was accepted as the only option.

And to make it much more “system suitable”, that is inside a VSCode devcontainer, with the Ruby Docker picture, making the whole lot configured with one click on.



CLI Take a look at Automation

We have been creating a command line software. I had by no means labored with this and had no thought the way to automate CLI checks. Clearly I went to ask Google.

One of many outcomes was the way to check CLI developed in Rust, simply what I wanted! Sadly, for lower-level testing, the code would should be refactored to be testable. And I haven’t got the information in Rust for that.

Luckily this and a number of other different outcomes informed the way to check, which is solely typing a command and checking what comes out within the terminal. So I might simply do e2e for now.

The instrument that was most indicated was Bats, made in Shell Script. However I could not make some easy issues work like assigning variables to make use of them inside the checks. The documentation itself stated that it was just a little sophisticated to separate the checks into completely different information. Think about the mess that will be because the checks grew.

One other instrument I discovered was Aruba, written in Ruby, however the documentation is virtually non-existent and it recommends utilizing it with Cucumber. Even the examples inside are in a options folder. I refuse to make use of Cucumber…

Then got here the identical thought from Chef Inspec: if it is achieved in Ruby, I can do it in Ruby. And it is working fantastically with Ruby, RSpec and the Open3 module for dealing with terminal instructions.



Conclusion

At any time when I am going searching for a technique to automate one thing, if there is not a broadly used framework like Cypress, Robotic Framework, Skuli and many others, and there’s some point out of a framework made in Ruby, I will not even have a look at the others. Ruby is the selection!



The place to study Ruby?

I realized from Codecademy and, in fact, from Google too. I like to recommend Codecademy for any language. On-line fingers on and straight to the purpose! Fundamental programs are free.

RubyGuides additionally has loads of materials and helped me lots. It additionally has a e-newsletter that you simply obtain content material about Ruby immediately in your electronic mail. That is nice for me!

Within the language official documentation, there are different hyperlinks to study Ruby.

Add a Comment

Your email address will not be published. Required fields are marked *