not sure why download csv file test fails on windows

16 views
Skip to first unread message

Jedrin

unread,
May 7, 2012, 9:48:35 AM5/7/12
to Ruby on Rails: Talk

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

Luis Lavena

unread,
May 7, 2012, 10:27:21 AM5/7/12
to rubyonra...@googlegroups.com
Hello,

On Monday, May 7, 2012 10:48:35 AM UTC-3, Jedrin wrote:

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.

--
Luis Lavena

Reply all
Reply to author
Forward
0 new messages