IOError: closed stream on Ruby/Rails Test

rails
#1

Trying to read a PDF in an integration test. This was working fine using the circleci/ruby:2.4.1-node image, but upon upgrading to ruby 2.6.0 and the circleci/ruby:2.6.0-node-browsers image, I get the following error:

IOError: closed stream

This points to a method in my test_helper file:

def read_pdf_from_response(response)
  temp_pdf = Tempfile.new('pdf') << response.body.force_encoding('UTF-8')
  reader = PDF::Reader.new(temp_pdf)
  reader.pages.map(&:text).join('') # this is line that throws the closed stream error
end

Any ideas here on how to troubleshoot? This all works fine locally, just not on circleci. And it worked fine before I upgraded to ruby 2.6.

Thanks

1 Like

#2

Here’s how I fixed it. I’m assuming this can only be because of the difference in how macOS and Ubuntu handle temp files. Anyway, this works:

def read_pdf_from_response(response)
  tfile = Rails.root.join 'tmp', 'testpdf.pdf'
  File.write tfile, response.body.force_encoding('UTF-8')

  begin
    reader = PDF::Reader.new(tfile)
    reader.pages.map(&:text).join('').squeeze("\n")
  ensure
    File.delete(tfile)
  end
end
1 Like

closed #3

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.

0 Likes