How to use a resource in a test?

#1

I’m trying to download a resource in a test, but I’m not sure how to do it. So far I’ve got this:

test do
    resource "my_tarball" do
      url "https://example.com/foo.tgz"
      sha256 "12345...."
    end

    resource("my_tarball").stage("#{testpath}")
    ...
  end

But when I do brew test my_formula I get this:

undefined method `name' for nil:NilClass
/usr/local/Homebrew/Library/Homebrew/resource.rb:70:in `download_name'
/usr/local/Homebrew/Library/Homebrew/resource.rb:59:in `downloader'
/usr/local/Homebrew/Library/Homebrew/resource.rb:137:in `fetch'
/usr/local/Homebrew/Library/Homebrew/resource.rb:90:in `stage'
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/my_formula.rb:31:in `block in <class:My_Formula>'
/usr/local/Homebrew/Library/Homebrew/formula.rb:1636:in `block (3 levels) in run_test'
/usr/local/Homebrew/Library/Homebrew/utils.rb:552:in `with_env'
/usr/local/Homebrew/Library/Homebrew/formula.rb:1635:in `block (2 levels) in run_test'
/usr/local/Homebrew/Library/Homebrew/formula.rb:835:in `with_logging'
/usr/local/Homebrew/Library/Homebrew/formula.rb:1634:in `block in run_test'
/usr/local/Homebrew/Library/Homebrew/extend/fileutils.rb:14:in `block in mktemp'
/usr/local/Homebrew/Library/Homebrew/extend/fileutils.rb:74:in `block in run'
/usr/local/Homebrew/Library/Homebrew/extend/fileutils.rb:74:in `chdir'
/usr/local/Homebrew/Library/Homebrew/extend/fileutils.rb:74:in `run'
/usr/local/Homebrew/Library/Homebrew/extend/fileutils.rb:13:in `mktemp'
/usr/local/Homebrew/Library/Homebrew/formula.rb:1628:in `run_test'
/usr/local/Homebrew/Library/Homebrew/test.rb:28:in `block in <main>'
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/timeout.rb:91:in `block in timeout'
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/timeout.rb:33:in `block in catch'
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/timeout.rb:33:in `catch'
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/timeout.rb:33:in `catch'
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/timeout.rb:106:in `timeout'
/usr/local/Homebrew/Library/Homebrew/test.rb:27:in `<main>'

I’m not sure how one is supposed to use external resources in a test. I’m also having a tough time grepping for this kind of thing in other formulas. Anyone have advice for such a plight?

0 Likes

(Amar Paul) #2

Bit late, but i did this:

  resource "testfile" do
    url ".../request.txt"
  end

  # other stuff

  test do
    resource("testfile").stage(testpath)
    shell_output("#{prefix}/bin/mycommand < #{testpath}/request.txt", 0)
  end
0 Likes

#3

Thanks for implementing this. However, I don’t use Homebrew too much these days, since I’ve moved to MacPorts.

0 Likes