Saturday, September 20, 2008

My first Rails bug

UPDATE January 14, 2009 -> The patch for this bug was accepted!

I'm pretty new to Rails and my first project included uploading videos. Shouldn't be too difficult I thought, after a little Google searching, I came up with the perfect example by Jim Neath: Converting Videos with Rails: Converting the Video Wanting to practice new skills with RSpec and Cucumber I wrote my first feature spec:

  1. Feature: Upload videos  
  2.   In order to provide videos to users after hours  
  3.   As a videographer  
  4.   I want to upload videos  
  5.  
  6.   Scenario: A valid filename is provided  
  7.     Given I go to the new video page  
  8.     And I browse to the file "Movie_0001.avi"  
  9.   
  10.     When I submit the upload  
  11.   
  12.     Then I should see "success"  
  13.     And the file should be uploaded  
and the supporting steps file, upload_steps.rb:
  1. require 'ftools'  
  2. require 'mime/types'  
  3.   
  4. When /I browse to the file \"(.+)\"do |path|  
  5.   @original_filepath = File.join('features/fixtures/', path)  
  6.   mime_types = MIME::Types.of(@original_filepath)  
  7.   
  8.   attach_file 'video[source]'@original_filepath, mime_types[0].content_type  
  9. end  
  10.   
  11. When 'I submit the upload' do  
  12.   click_button 'Create'  
  13. end  
  14.   
  15. def uploaded_filepath  
  16.   uploaded_basename = File.basename(@original_filepath)  
  17.   File.join(RAILS_ROOT, "public/videos/1", uploaded_basename)  
  18. end  
  19.   
  20. Then /the file should be uploaded/ do  
  21.   assert File.compare(@original_filepath, uploaded_filepath)  
  22. end  
what I got was:
...
    And the file should be uploaded
       is not true. (Test::Unit::AssertionFailedError)
      c:/ruby/lib/ruby/1.8/test/unit/assertions.rb:48:in `assert_block'
      c:/ruby/lib/ruby/1.8/test/unit/assertions.rb:500:in `_wrap_assertion'
      c:/ruby/lib/ruby/1.8/test/unit/assertions.rb:46:in `assert_block'
      c:/ruby/lib/ruby/1.8/test/unit/assertions.rb:63:in `assert'
      c:/ruby/lib/ruby/1.8/test/unit/assertions.rb:495:in `_wrap_assertion'
      c:/ruby/lib/ruby/1.8/test/unit/assertions.rb:61:in `assert'
      ./features/upload/steps/upload_steps.rb:22:in `And /the file should be uploaded/'
      features/upload/upload.feature:14:in `And the file should be uploaded'
...
On closer inspection, the test was failing because the uploaded file was truncated in some bizarre way. However, if I ran the application and manually upload a file from the browser, everything worked fine. After much hunting I ended up in rails/actionpack/lib/action_controller/integration.rb where in multipart_body, the mode is not specified in the call to File.open, so it defaults to "r". This is all well and good on anything but Windows, which I happen to be using! Windows requires that the mode be specified as "rb" to ensure the file is read as binary. I submitted a patch but I'm not holding my breath for it to be pulled in anytime soon.

Ruby PCAN DLL Wrapper

Inspired by System Testing In Ruby (Systir), I have published a small Ruby PCAN USB DLL Wrapper. Now with a small device available for less than $300 and some free software, I can write automated system test scripts of the form:
send tftp_rrq("autoexec.bat").with(blksize(2036)) verify_target_sends oack(blksize(2036) send ack(0) verify_target_sends autoexec_bat send ack(1)
If you have rubygems installed, PCAN DLL Wrapper is easily obtained:
set http_proxy=http://my_proxy_host.com:80 gem install pcanusb
and then use:
require 'pcan_usb' PCAN_USB.init(PCAN_USB::BAUD_1M) PCAN_USB.write(0x0E100501, "Hello World!") PCAN_USB.close

Collection o quotes

Ricky Hustler, a friend of mine, used to use this one on a regular basis:
"You never see a luggage rack on a hearse"
I came across this on James Grenning's blog:
"It's easier to act your way into thinking differently than to think your way into acting differently"
From a friend's coffee cup:
"Lead, follow, or get out of my way"
Susan Jeffers:
"Feel the fear and do it anyway"