Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Monday, August 5, 2013

When RVM can't fetch from http://ftp.ruby-lang.org/

I wanted to install Ruby 1.9.2.  Easy, I thought, that's as simple as rvm install 1.9.2.  Unfortunately, it looks like http://ftp.ruby-lang.org/ is having a bad day.

http://rvm.io/rvm/offline provides helpful instructions assuming you are starting with nothing.  I already have several Ruby versions installed, so my process was reduced to:

$ curl -L http://www.mirrorservice.org/sites/ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p320.tar.bz2 -o ruby-1.9.2-p320.tar.bz2
$ mv ruby-1.9.2-p320.tar.bz2 ~/.rvm/archives/
$ rvm install 1.9.2-p320 --disable-binary
$ rvm use ruby-1.9.2 --default

Not too shabby! Many thanks to the RVM team.

Saturday, February 14, 2009

BDD WxRuby applications with Cucumber and Nobbie

For a long time now, I've been wanting to write some Cucumber features to describe a WxRuby application I have. A couple of weeks ago, I found a library called wx-nobbie on Rubyforge that seems to be almost exactly what I was looking for.  It provides a simple, high level interface for driving WxRuby applications through commands like "click", "choose" and "type".  I had a little trouble getting the tests to pass to start with, but with a few minor tweaks I got there.  I contacted the original author and got his permission to take it forward.

I created a GitHub repository for Wx-Nobbie and have made a few updates over the last week.  Currently, I've got 4 feature scenarios with 10 steps:

C:\dev\wx-nobbie>rake features
(in C:/dev/wx-nobbie)
In order to test drive a WxRuby application  # features/acceptance_test.feature
As a developer
I want Nobbie to provide acceptance test access to the application
  Scenario: Choosing a radio button  # features/acceptance_test.feature:5
    Then "radio_button" is not chosen# features/step_definitions/acceptance_test_steps.rb:21
    When I choose "radio_button"     # features/step_definitions/acceptance_test_steps.rb:1
    Then "radio_button" is chosen    # features/step_definitions/acceptance_test_steps.rb:17

  Scenario: Choosing a check box  # features/acceptance_test.feature:10
    Then "check_box" is not chosen# features/step_definitions/acceptance_test_steps.rb:21
    When I choose "check_box"     # features/step_definitions/acceptance_test_steps.rb:1
    Then "check_box" is chosen    # features/step_definitions/acceptance_test_steps.rb:17

  Scenario: Type into a text control        # features/acceptance_test.feature:15
    When I type "123" into "text_ctrl"      # features/step_definitions/acceptance_test_steps.rb:5
    Then I should see "123" in "text_ctrl"  # features/step_definitions/acceptance_test_steps.rb:9

  Scenario: Type into a combo box           # features/acceptance_test.feature:19
    When I type "456" into "combo_box"      # features/step_definitions/acceptance_test_steps.rb:5
    Then I should see "456" in "combo_box"  # features/step_definitions/acceptance_test_steps.rb:9


4 scenarios
10 steps passed

With just these 4 scenarios, RCov tells me I have 62.9% coverage.

Hurrah!

Sunday, January 25, 2009

Building a Ruby Extension With Visual C++ Express 2008

Edit March 29, 2009: While the instructions below describe how to build the Win32::GuiTest extension, I didn't get very far with using it before I found Wx::Nobbie which I much prefer and have taken up the maintenance of at Github.

I'd like to be able to use Cucumber to drive the development of Windows applications so I went looking for something like Webrat for desktop GUI's. I found Win32::GuiTest.  There is a project of the same name on RubyForge that contains the same source but that's as far as that went. 

Win32::GuiTest is a C extension that was compiled with Cygwin, so I wanted to rebuild a native version.  I found some instructions on Kevin Kleinfelter's blog that look much like below, but using VC++ 2008 I got to skip a couple of steps:

  1. Download and install Visual C++ Express Edition (this is free, I got the 2008 version)
  2. Edit $RUBY_HOME/lib/ruby/1.8/i386-mswin32/config.h and delete the “#error MSC version unmatch” line
  3. Open a Visual Studio 2008 Command Prompt
    • Start
    • All Programs
    • Microsoft Visual C++ 2008 Express Edition
    • Visual Studio Tools
  4. cd C:\temp\guitest020218\ext\cguitest
    • I'm building the guitest extension
  5. ruby extconf.rb
  6. nmake
  7. mt.exe -manifest cguitest.so.manifest -outputresource:cguitest.so;2
    • Copy and paste the command above. You need it all from the 'mt' to the ';2'
  8. nmake install
  9. cd \temp\guitest020218
  10. ruby install.rb config
  11. ruby install.rb install
  12. To test:
    • irb
    • require 'win32/guitest'
    • check response is '=> true'

Wednesday, January 14, 2009

My first Rails bug – accepted!

At the time that I posted about My first Rails bug the tests that existed for Action Pack contained failures and the only test in integration_upload_test.rb was:

assert_equal(:multipart_form, SessionUploadTest.last_request_type)

I felt that my change could be submitted with no tests because:

  • The change was trivial (6 characters)
  • The change was for a well known Windows file issue
  • The tests that existed didn’t pass
  • There were no example tests for me to build on

Three months after I submitted the ticket, Pratik dismissed it by changing its state to incomplete with the comment “Missing a test case.”  Are these guys CRAZY?  No, no they are not.  Just because there were broken windows in the neighborhood did not give me the right to break any more.

So, I sucked it up, cloned the latest rails repository again and to my surprise … all the Action Pack tests passed! … and there was a nice simple example in multipart_params_parsing_test.rb:

  test "uploads and reads file" do
    with_test_routing do
      post '/read', :uploaded_data => fixture_file_upload(FIXTURE_PATH + "/hello.txt", "text/plain")
      assert_equal "File: Hello", response.body
    end
  end

This is too easy!  I added a test for my issue:

  test "uploads and reads a binary file in windows" do
    with_test_routing do
      fixture_file = FIXTURE_PATH + "/mona_lisa.jpg"
      post '/read', :uploaded_data => fixture_file_upload(fixture_file, "image/jpg")
      assert_equal 'File: '.length + File.size(fixture_file), response.content_length
    end
  end

Watched it fail.  Added the magic 6 characters (“, ‘rb’”).  Watched it pass.  Wrapped it up and shipped it off.  The following morning I pick up an email letting me know that Pratik has reopened the ticket and assigned it to Joshua Peek for review.  The change was committed before the day was out!

Many thanks to Pratik and Josh for attending to this so promptly.

Sunday, October 5, 2008

Where to put sqlite3.dll on Windows

When I first started using SQLite3 on Windows, most people suggested downloading sqlite3.dll from www.sqlite.org and storing it in ruby/bin so Windows could find it. For a while, this strategy worked fine but I really didn't like the application requiring users to put that DLL into an acceptable place. I don't know why it didn't occur to me earlier, but, given that this is only intended to be a Windows application, why not just modify the PATH environment variable within the application. So that's what I've done. In config/boot.rb:
# Make sqlite3.dll available
ENV['PATH'] += ";#{RAILS_ROOT}/vendor/bin/sqlite3"

Thursday, October 2, 2008

class << self

I've come across this construct many times while browsing a number of open source projects:
class MyClass
  class << self
    def a_class_method
      ...
    end
  end

  def an_instance_method
    ...
  end
end
I finally got it today ... there's no black magic ... all "class << self" means is:
add the following class method definitions to the class MyClass.
Or,
open the class MyClass, and add the following class methods.
Now that I'm in the gang and understand this incantation, I'm not sure I like it. It seems like a nice way to group all your class methods together, but if you define several class methods then it becomes harder to recognize whether a method belongs to the class or an instance. I think it's cleaner to explicitly state the ownership of each method:
class MyClass
  def self.a_class_method
    ...
  end

  def an_instance_method
    ...
  end
end

Saturday, September 20, 2008

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

Tuesday, June 10, 2008

ActiveRecord requires RAILS_ROOT for relative Sqlite path

I'm writing a little time tracking tool for Windows in Ruby. The data is stored in a database so I figured I'd use ActiveRecord and maybe learn something about Rails along the way. Everything I found about ActiveRecord tells me that it "can be used independently outside of Rails". One minor detail that I just figured out:
If you want to use a relative path for a sqlite3 database in your database.yml, you have to define RAILS_ROOT.
For example, if config/database.yml =>
production:
  adapter: sqlite3
  database: db/production.sqlite
ActiveRecord initialization (mine's in config/boot.rb) looks like:
RAILS_ROOT = "#{File.dirname(File.expand_path(__FILE__))}/.."
RAILS_ENV  = ENV['RAILS_ENV'] || 'production'

$LOAD_PATH.unshift "#{RAILS_ROOT}/vendor/sqlite3"
config = YAML::load(IO.read("#{RAILS_ROOT}/config/database.yml"))
ActiveRecord::Base.configurations = config
ActiveRecord::Base.establish_connection(config[RAILS_ENV])