not sure why download csv file test fails on windows

I have this sinatra app test where I downloaded a file by hand from
the app and stored it in the test area. I then have my test program
download the same file, save it and compare the two.
The test seems to fail because the one downloaded by hand has
newlines of the form
"\r\n" whereas the one that the test program downloads using rack/test
has newlines of the form
"\r\r\n"

Any obvious insights from my test code below ?

Thanks ..

require 'fileutils'

require 'test/unit'
require 'rack/test'

class MyAppTest < Test::Unit::TestCase
  include Rack::Test::Methods

  def app
    Sinatra::Application
  end

  def test_recreate_csv
    csv_path = File.dirname(__FILE__) + '/csv_files/web_test_file.csv'
    csf = File.open(csv_path,'w'){|f| f.write(last_response.body)}

    downloaded_csv_path = File.dirname(__FILE__) + '/csv_files/
downloaded_file.csv'

    assert FileUtils.identical?(csv_path, downloaded_csv_path)
  end

end

Hello,

The test seems to fail because the one downloaded by hand has

newlines of the form

“\r\n” whereas the one that the test program downloads using rack/test

has newlines of the form

“\r\r\n”

If you want identical files then you should tell Ruby to open your file in a binary mode.

Any obvious insights from my test code below ?

Thanks …

csf = File.open(csv_path,‘w’){|f| f.write(last_response.body)}

Change the mode from ‘w’ to ‘wb’ (write binary)

Also, don’t forget to close the file (csf.close) as you keep that open in your test, leaking file handlers.