Brew Test Environment: Running cask commands from within brew tests

I’m working on a feature that allows brew commands to reference casks. For example, brew reinstall firefox should work as one would expect.

I’m currently writing tests for this feature and I’m running into (what is seemingly) a test-environment-related issue. From within test/cmd/reinstall_spec.rb, when running Cask::Cmd::Reinstall.new("local-caffeine").run, I’m getting an error:

No such file or directory @ rb_sysopen - /tmp/homebrew-tests-20200626-7988-sgudda/prefix/Caskroom/local-caffeine/.metadata/1.2.3/20200626135133.420/Casks/local-caffeine.rb
Relevant Stacktrace
/usr/local/Homebrew/Library/Homebrew/cask/installer.rb:390:in `save_caskfile'
/usr/local/Homebrew/Library/Homebrew/cask/installer.rb:74:in `stage'
/usr/local/Homebrew/Library/Homebrew/cask/installer.rb:97:in `install'
/usr/local/Homebrew/Library/Homebrew/cask/installer.rb:127:in `reinstall'
/usr/local/Homebrew/Library/Homebrew/cask/cmd/reinstall.rb:14:in `block in run'
/usr/local/Homebrew/Library/Homebrew/cask/cmd/reinstall.rb:7:in `each'
/usr/local/Homebrew/Library/Homebrew/cask/cmd/reinstall.rb:7:in `run'
Test Code in `test/cmd/reinstall_spec.rb`
  it "reinstalls a given Cask" do
    Cask::Installer.new(Cask::CaskLoader.load(cask_path("local-caffeine"))).install
    expect(Cask::CaskLoader.load(cask_path("local-caffeine"))).to be_installed

    expect { brew "reinstall", "local-caffeine", "--debug" }
      .to output("something or other, bleh").to_stdout
      .and not_to_output.to_stderr
      .and be_a_success

    expect(Cask::CaskLoader.load(cask_path("local-caffeine"))).to be_installed
  end

Some notes that I took:

  • What’s most interesting is that the reinstall logic doesn’t throw exceptions in test/cask/cmd/reinstall_spec.rb, which leads me to believe it is a test environment issue.

  • Something else I’ve noticed is that invoking Cask::CaskLoader.load with cask_path("local-caffeine") loads the cask from where I would expect, but when .load is invoked from within brew reinstall, it finds local-caffeine among the installed casks with this line in cask_loader.rb:

    FromPathLoader.new(possible_installed_cask.installed_caskfile) if possible_installed_cask.installed?

    If I replace brew "reinstall", "local-caffeine" with brew "reinstall", cask_path("local-caffeine") in the test case, the test reinstalls Caffeine to my /Applications directory, which is less than satisfactory. The test works, however.

Any thoughts on what I should investigate next, or a pointer to a possible solution?

1 Like