June gloom

june gloom The view from home, on a late spring morning and on a late spring afternoon

Nothing much to report from the past couple weeks other than that the California primary election happened, as did yet another horrible mass shooting in Florida. And May Gray passed the torch to June Gloom. Today brought the first cloudless morning in a long time! I’d somewhat fallen off the exercise wagon since beginning this internship, but this week I’ve attempted to climb back aboard (huffing and puffing). A lot of hours have been spent indoors, hunched over and peering at code and occasionally typing code.

This post and the next few posts will give an overview of the first sets of Homebrew integration tests I’ve worked on so far. I figured I’d start with some relatively low-hanging fruit and work on testing commands that are primarily used to output text to the shell (or, whose functions as writ in code are similarly easy for me to grasp).

First, brew home.

My first pull request for the main Homebrew repository updated the test for the home command, which opens Homebrew’s homepage – or a formula’s homepage – in a web browser. There was already test coverage in place to ensure that brew home would open Homebrew’s homepage (i.e., lines 9-10, in green, in the cmd/home.rb screenshot below). So I added some lines that would ensure the same function for a formula’s homepage, or for multiple formula homepages (line 12 in the screenshot below). For example, brew home aften opens the homepage for the aften formula, and brew home aften sox opens the homepages for both aften and sox.


The test coverage for brew home – at 80%, until recently.

The updated test creates a test formula file in the homebrew-core tap and in the assertion it does something amazing – amazing to me, at least.

formula_file = CoreTap.new.formula_dir/"testball.rb"
formula_file.write <<-EOS.undent
  class Testball < Formula
    desc "Some test"
    homepage "https://example.com/testball"
    url "https://example.com/testball-0.1.tar.gz"

By way of a helper method, cmd (which runs a brew command in a separate process and retrieves its output) this line not only runs brew home testball, it also sets the HOMEBREW_BROWSER environment variable equal to echo:

cmd("home", "testball", {"HOMEBREW_BROWSER" => "echo"})

Which is to say that instead of opening a web browser and navigating to the given homepage, this test temporarily fools your system into thinking that your browser is the equivalent of the /bin/echo command. echo simply parrots or echoes the string of text it’s fed as an argument, which is “https://example.com/testball” in the test.

Why is the HOMEBREW_BROWSER environment variable is involved here? home.rb employs the exec_browser method to determine which application to use as a web browser, and exec_browser first checks if HOMEBREW_BROWSER has been set to point to a particular application. If it has, then brew home uses that application. If it hasn’t, then exec_browser keeps checking other default settings until it finds a valid application (if it finds one at all).

In the end, the test asserts that both Formula["testball"].homepage and cmd("home", "testball", {"HOMEBREW_BROWSER" => "echo"}) output the same string.

assert_equal Formula["testball"].homepage,
             cmd("home", "testball", {"HOMEBREW_BROWSER" => "echo"})

As mentioned in my previous post, I forgot to include the crucial require "formula" statement at the beginning of the file when I updated my pull request; this was the first of several mistakes I’ve made so far. The formula.rb file is needed because this line makes use of a Formula class method, which is defined only in that file. This mistake was soon fixed by one of the Homebrew maintainers, Martin.

I’ve since submitted pull requests to add test coverage for brew desc, brew log, and brew search, plus one more to refactor the integration tests file so that the creation of test formula files is consolidated in one or two methods. More later!