Testing Talk

written by ben on February 8th, 2008 @ 11:24 AM

I recently gave a talk on software testing at BarCampTX, the slides are available at slideshare. The talk covered TDD, BDD, rspec, autotest, autometric, and story testing. I going to write more about using story testing to test your views, and where webrat comes in, but in the meantime you can checkout the blerb project to see story testing in action.

Introducing Autometric

written by ben on November 8th, 2007 @ 08:44 PM

Autometric is a tool thats automates the task of running metrics on your ruby code. It’s modeled after autotest, so it behaves the same way. Don’t waist any more time running rcov from the command line or rake task. Here’s a demo:

Or the full screen, if you prefer:

Autometric Demo

And here’s how to install it with images and all:

sudo gem install autometric
mkdir ~/.autometric_images
cd ~/.autometric_images
wget http://upload.wikimedia.org/wikipedia/commons/2/2e/Crystal_Clear_action_button_cancel.png -O coverage_failed.png
wget http://upload.wikimedia.org/wikipedia/commons/d/d6/Crystal_Clear_action_apply.png -O coverage_passed.png
wget http://upload.wikimedia.org/wikipedia/commons/2/2c/Crystal_Clear_action_stop.png -O cyclo_error.png
wget http://upload.wikimedia.org/wikipedia/commons/5/53/Crystal_Clear_app_error.png -O cyclo_warn.png
wget http://upload.wikimedia.org/wikipedia/commons/b/b9/Crystal_Clear_action_edit_remove.png -O decrease.png
wget http://upload.wikimedia.org/wikipedia/commons/e/e2/Crystal_Clear_action_edit_add.png -O increase.png
wget http://upload.wikimedia.org/wikipedia/commons/0/0a/Crystal_Clear_action_1downarrow.png -O flog_decrease.png
wget http://upload.wikimedia.org/wikipedia/commons/2/2c/Crystal_Clear_action_1uparrow.png -O flog_increase.png
wget http://upload.wikimedia.org/wikipedia/commons/0/03/Crystal_Clear_action_bookmark.png -O flog.png

And save the following as “.autometric” in your home directory:

def growl(title, msg, pri=0, stick="", image="")
  image_arg = (!image.empty?) ? "--image #{image}" : ""
  system "growlnotify -n autometric #{image_arg} -p #{pri} -m \"#{msg}\" #{title} #{stick}" 
end

Autocoverage.add_hook :initialize do |at|
  at.threshold = 90.0
end

Autocoverage.add_hook :failed do |at|
  growl("Code Coverage Failed","#{at.coverage}% code coverage", 2, "", "~/.autometric_images/coverage_failed.png")
end

Autocoverage.add_hook :passed do |at|
  growl("Code Coverage Passed", "#{at.coverage}% code coverage", -2, "", "~/.autometric_images/coverage_passed.png") #if at.tainted
end

Autocoverage.add_hook :increased do |at|
  growl("Code Coverage Increased","#{"%.3f" % (at.coverage - at.previous_coverage)}% code coverage increase", 2, "", "~/.autometric_images/increase.png")
end

Autocoverage.add_hook :decreased do |at|
  growl("Code Coverage Decreased", "#{"%.3f" % (at.previous_coverage - at.coverage)}% code coverage decrease", -2, "", "~/.autometric_images/decrease.png") #if at.tainted
end

Autocyclo.add_hook :erred do |at|
  lines = at.errors.collect {|e| "#{e[:rating]} #{e[:id]}" }
  message = lines.length > 5 ? lines[0...5].join("\n") + "\n..." : lines.join("\n")
  growl("Cyclomatic Complexity Errors", message, 1, "", "~/.autometric_images/cyclo_error.png")
end

Autocyclo.add_hook :warned do |at|
  lines = at.warnings.collect {|w| "#{w[:rating]} #{w[:id]}" }
  message = lines.length > 5 ? lines[0...5].join("\n") + "\n..." : lines.join("\n")
  growl("Cyclomatic Complexity Warnings", "#{message}", 2, "", "~/.autometric_images/cyclo_warn.png")
end

Autotoken.add_hook :erred do |at|
  lines = at.errors.collect {|e| "#{e[:rating]} #{e[:id]}" }
  message = lines.length > 5 ? lines[0...5].join("\n") + "\n..." : lines.join("\n")
  growl("Token Complexity Errors", message, 1, "", "~/.autometric_images/cyclo_error.png")
end

Autotoken.add_hook :warned do |at|
  lines = at.warnings.collect {|w| "#{w[:rating]} #{w[:id]}" }
  message = lines.length > 5 ? lines[0...5].join("\n") + "\n..." : lines.join("\n")
  growl("Token Complexity Warnings", "#{message}", 2, "", "~/.autometric_images/cyclo_warn.png")
end

Autoflog.add_hook :flogged do |at|
  growl("Flog Score", "#{"%.3f" % at.score}", 2, "", "~/.autometric_images/flog.png")
end

Autoflog.add_hook :increased do |at|
  growl("Flog Score Increased", "#{"%.3f" % (at.score - at.previous_score)}", -2, "", "~/.autometric_images/flog_increase.png")
end

Autoflog.add_hook :decreased do |at|
  growl("Flog Score Decreased", "#{"%.3f" % (at.previous_score - at.score)}", -2, "", "~/.autometric_images/flog_decrease.png")
end

Enjoy.

Fun with Superators

written by ben on October 25th, 2007 @ 11:43 AM

Jay Phillips wrote a wicked little gem called Superators this summer, and I’ve been looking for an excuse to use it. Here’s a superator to change the protection of a method:

require 'rubygems'
require 'superators'

class Class
  superator "<-" do |method_id|
    private method_id
  end

  superator "<+" do |method_id|
    public method_id
  end

  superator "<~" do |method_id|
    protected method_id
  end
end

This allows you to quickly access private methods:

puts [].rand rescue "rand is private!"
Array <+ "rand"
puts [].rand

Dangerous, but useful if you need to keep a method private but you want to test it as if it was public (think: testing your rails models).

describe Foo, "#bar" do

  before(:each) do
    # Foo#bar is private
    Foo <+ "bar"
  end

  after(:each) do
    Foo <- "bar"
  end

  it "should do something amazing!" do
    bar.should be_amazing
  end
end

Enjoy.

Trim Your 'if' Trees

written by ben on September 21st, 2007 @ 12:46 PM

Here’s a helpful little hack for keeping those if trees under control. Ever written code like this:

def parse_line
  if line_is_parsable? 
    if line_is_nested? 
      if line_is_attribute?
        parse_attribute
      else
        # do parsing
      end
    else
      @line_stack.pop
      return
    end
  else
    skip_line
  end
end

What started out as a single if statement has grown into an unmaintainable mess of if*’s *elsif’s, and unless’. Well, thats fine and all, until you have to make changes. What if you need to check for attributes before nested? Or insert a condition check between parsable and nested? Every change requires stepping through the code to figure out whats going on. It’s a drain on productivity, but there is a cure.

First, add the following snippet to your project. (I like to put it in a file called ‘util.rb’ that gets loaded first.)

# Stolen without regard from http://svn.rubyonrails.org/rails/trunk/activesupport/lib/active_support/core_ext/module/aliasing.rb
# Inspired by http://errtheblog.com/post/1109
class Module

  def alias_method_chain(target, feature)
    # Strip out punctuation on predicates or bang methods since
    # e.g. target?_without_feature is not a valid method name.
    aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
    yield(aliased_target, punctuation) if block_given?

    with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"

    alias_method without_method, target
    alias_method target, with_method

    case
      when public_method_defined?(without_method)
        public target
      when protected_method_defined?(without_method)
        protected target
      when private_method_defined?(without_method)
        private target
    end
  end
end

That little bit of magic allows you to break up your if statements into multiple methods. Not so special by itself, until you start chaining the methods:

def parse_line
  #do parsing
end

def parse_line_with_attribute_check
  if line_is_attribute?
    parse_attribute
  else
    parse_line_without_attribute_check
  end
end

alias_method_chain :parse_line, :attribute_check

def parse_line_with_nested_check
  if line_is_nested?
    parse_line_without_nested_check
  else
    @line_stack.pop
  end
end

alias_method_chain :parse_line, :nested_check

def parse_line_with_parsable_check
  if line_is_parsable?
    parse_line_without_parsable_check
  else
    skip_line
  end
end

alias_method_chain :parse_line, :parsable_check

So now the behavior is determined by the code in the methods and the order of methods. This makes changes to the condition checks as easy as inserting, deleting, or reordering your methods. So if I wanted to check attributes before nested, I simply move the attributes check after the nested check:

def parse_line
  #do parsing
end

def parse_line_with_nested_check
  if line_is_nested?
    parse_line_without_nested_check
  else
    @line_stack.pop
  end
end

alias_method_chain :parse_line, :nested_check

def parse_line_with_attribute_check
  if line_is_attribute?
    parse_attribute
  else
    parse_line_without_attribute_check
  end
end

alias_method_chain :parse_line, :attribute_check

def parse_line_with_parsable_check
  if line_is_parsable?
    parse_line_without_parsable_check
  else
    skip_line
  end
end

alias_method_chain :parse_line, :parsable_check

This leaves your code damp, but much easier to extend and maintain. It works great when BDD‘ing.

Ruby/Rails on sy*.cs.tamu.edu

written by ben on September 12th, 2007 @ 10:58 PM

I was asked to install Ruby on Rails on a windows server, to be used for class projects. I went with the mongrel via apache approach. I was originally going to install it on IIS runing under FastCGI, but decided against it for a few reasons:

  • FastCGI is really only appropriate for a production environment. It’s hard to debug, and this machine is probably going to be used for development, testing, and production.
  • Its relatively unstable. FastCGI on IIS is still an awkward combination, and was bound to cause headaches.
  • The apache/mongrel approach keeps the two very separate, so it’s easier to find problems.
  • To use IIS with Mongrel, you need ISAPI_rewrite, which costs money.

I used this guide, so be sure to look it over if you have any problems.

Step 0: Downloads

Remote desktop into the install machine, and download the following:

Step 1: Install Apache & Ruby

Run the apache installer. Use ”sy.cs.tamu.edu” as the network domain and server name. Put whatever you want for the administrators email.

On the next page, click “Custom Install”, and change the installation to “c:\apache”, then choose next and let the installer finish.

Now open the Ruby installer. The default install should be fine.

Step 2: Start the Apache Daemon

Enter the following into a command shell:

cd \
cd apache\apache2\bin
apache –k install

The apache service icon should appear in the task bar.

Step 3: Install Rails & Mongrel

Install the rails, redcloth, and mongrel gems:

gem install rails mongrel mongrel_service redcloth -y

Make sure to choose the win32 option every time it asks.

Step 4: Configure Apache

From the start menu, navigate to “All Programs”->”Apache HTTP Server X”->”Configure Apache Server”->”Edit The Apache http.conf Configuration file”.

Change the line with:

Listen 8080

to:

Listen 80

Note: Binding to port 80 will only work if you don’t have another webserver running on the machine. If you think you might have IIS or something else running on port 80, run:

netstat -a

If you see something like the following:

TCP    sy02:http              sy02.cs.tamu.edu:0     LISTENING

You may need to change to an open port number. I suggest 81.

Save the file, then start apache using the icon on the bottom right. Enter the following command(change ‘http’ to your port number):

netstat -a | find "http"

It should print out something like:

TCP    sy02:http              sy02.cs.tamu.edu:0     LISTENING

If you don’t see anything, you have a problem somewhere.

Step 5: Create the Rails App Folder

cd \
mkdir rails_apps
cd rails_apps
rails test_site
cd test_site
ruby script/server –e production

Point your browser to “http://localhost:3000/” ( replace 3000 with whatever port mongrel is running on.), if every thing’s working you should be at the default rails page.

Step 6: Configure Apache to Proxy to Mongrel

Run the following from the command shell:

cd \
cd apache\apache2\conf
(
echo LoadModule proxy_module
modules/mod_proxy.so
echo LoadModule proxy_http_module
modules/mod_proxy_http.so
echo ProxyRequests Off
echo ^<Proxy *^>
echo   Order deny,allow
echo   Allow from all
echo ^</Proxy^>
) >> http-proxy.conf
(
echo .
echo Include conf/http-proxy.conf
) >> httpd.conf

this should create a http-proxy.conf file in apache’s conf directory. Next run:

set APP_NAME=test_site
set PORT=3000
(
echo .
echo Alias /%APP_NAME% "c:/rails_apps/%APP_NAME%/public"
echo ^<Directory "c:/rails_apps/%APP_NAME%/public"^>
echo   Options Indexes FollowSymLinks
echo   AllowOverride none
echo   Order allow,deny
echo   Allow from all
echo ^</Directory^>
echo .
echo ProxyPass /%APP_NAME%/images !
echo ProxyPass /%APP_NAME%/stylesheets !
echo ProxyPass /%APP_NAME%/javascripts !
echo ProxyPass /%APP_NAME%/ http://127.0.0.1:%PORT%/
echo ProxyPass /%APP_NAME% http://127.0.0.1:%PORT%/
echo ProxyPassReverse /%APP_NAME%/ http://127.0.0.1:%PORT%/
) >> http-proxy.conf

Restart apache, browse to “http://localhost/test_site/”, and you should be at your default page.

Step 7: Install Proxy Plugin

Apparently rails needs some help rewriting URL’s to work with the proxy server, so you need to install a rails plugin:

cd \
cd rails_apps\test_site
ruby script/plugin install http://svn.napcsweb.com/public/plugins/reverse_proxy_fix/branches/rails123
http://localhost/test_site
cd vendor\plugins\rails123\lib
echo BASE_URL="http://localhost/test_site" >> config.rb

Step 8: Start mongrel as a service

This will start a mongrel server as a windows service:

mongrel_rails service::install -N test_site
net start test_site

And that should do it. If everything went right, you should get the default rails page again.

Lone Star Ruby Conf

written by ben on September 11th, 2007 @ 06:23 PM

Wow. Lone Star Ruby Conference. Through an unexpected series of events, I ended up volunteering this weekend and had a great time. It was great to see the ruby community in action.

There were some fantastic presentations. H D Moor’s presentation about security exploits, an area I could care less about, was an entertaining story about using ruby to create Metasploit 3. Evan Short’s DSL presentation opened my eye’s to a lot of stuff I was missing about DSL’s. Zed’s (anti) keynote was better than expected. So was Jay Phillips talk on Adhearsion. And the Rails Envy guys gave us a sneak peak at their latest Rails video.

It still amazes me how many movers and shakers in the ruby community call Texas their home. Without them, LSRC wouldn’t have been the huge success that it was.

Ruby/Rails on unix.cs.tamu.edu

written by ben on September 1st, 2007 @ 01:42 PM

I wanted to host a ruby on rails website on my csnet account, but there were some problems. The server is running an older version of ruby without gems installed. Also, it looks like apache is not compiled with fastcgi. To get around this, I had to install ruby and gems locally, and proxy the requests to a mongrel server. There are a bunch of how-to’s on installing ruby/rails, but I ran into a few hiccups due csnet’s configuration. This guide is mostly gathered from here.

Step 0: Environment Setup

First off, ssh into your unix account. There’s information on how to do this over at cs helpdesk.

I’m used to the bash shell, but the default is tcsh. If you have no idea what difference is, I recommend switching to bash:

echo "exec bash --login" >> ~/.login

Log out and log back in; the prompt should have changed to something like bash-3.00. I recommend changing the prompt to something more usefull:

echo "PS1=\"\[\033[0;31m\][\u@\h \W]#\[\033[0m\] \"" >> ~/.bash_profile

Log in/out or source the .bash_profile file and the prompt should be updated.

Because we are building and executing everything locally, we need to add a few folders:

cd ~/
mkdir -pv soft run www log
cd ~/run
mkdir -pv bin etc include lib man share

Next, we need to set the RUN variable and update PATH:

cat >>${HOME}/.bashrc <<eof
export RUN="\${HOME}/run"

export PATH="\${RUN}/bin:\${PATH}"

export LD_LIBRARY_PATH=\${RUN}/lib:\${LD_LIBRARY_PATH}
export LD_RUN_PATH=\${RUN}/lib:\${LD_RUN_PATH}
eof

echo "source ~/.bashrc" >> ${HOME}/.bash_profile

Step 1: Install Readline

Ruby requires Readline, so we need to install it locally before compiling ruby:

cd ${HOME}/soft
wget ftp://ftp.cwru.edu/pub/bash/readline-5.2.tar.gz
tar xvzf readline-5.2.tar.gz
cd readline-5.2

./configure --prefix=${RUN}
make && make install

Step 2: Install Ruby

Download, compile, and install ruby locally:

cd ${HOME}/soft
wget ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.6.tar.gz
tar xvzf ruby-1.8.6.tar.gz
cd ruby-1.8.6
./configure --prefix=${RUN} --with-readline-dir=${RUN}
make && make install

This will tell ruby to use use the local libs folders:

cat >> ${HOME}/.bashrc <<eof
# ruby library search path
export RUBYLIB=\${RUN}/lib/ruby:\${RUN}/lib/rubyext
eof

Logout/in then make sure ruby is pointing to ~/run/bin/ruby and is the version you installed (1.8.6):

which ruby && ruby -v

It should print something like:

/user/brb1763/run/bin/ruby
ruby 1.8.6 (2007-03-13 patchlevel 0) [sparc-solaris2.8]

Step 3: Install RubyGems

Download and install rubygems:

cd ${HOME}/soft
wget http://rubyforge.org/frs/download.php/20989/rubygems-0.9.4.tgz
tar xzvf rubygems-0.9.4.tgz
cd rubygems-0.9.4
ruby setup.rb config --prefix=$RUN
ruby setup.rb setup
ruby setup.rb install

Verify rubygems is installed:

which gem && gem -v

You should see:

/user/brb1763/run/bin/gem
0.9.4

Step 4: Install Rails

Install rails and mongrel:

gem install rails mongrel --no-rdoc --no-ri -y
If you are prompted to choose a gem, select the ones that end with (ruby).

Verify rails is installed:

which rails && rails -v

That should print:

/user/brb1763/run/bin/rails
Rails 1.2.3

Step 5: Create a Test Site

Create a test rails site in your www dir:

cd ~/www
rails test_site

Start a mongrel server to server your site:

cd ~/www/test_site
mongrel_rails start -d -p 3042 -l ~/log/mongrel.log

The port number (3042 in this case) should be a random but unique port number. If you are local or have a vpn connection, browse to http://unix.cs.tamu.edu:<your port number>/, it should serve the ruby on rails placeholder page.

Step 6: Proxy Requests to Your Mongrel Server

Set the mod_proxy stuff in your .htaccess file:

mkdir -pv ${HOME}/web_home/test_site
cd ${HOME}/web_home/test_site
cat >> .htaccess <<eof
RewriteEngine   on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) http://unix.cs.tamu.edu:<your port number>/$1 [P]
eof
chmod 644 .htaccess

Don’t forget to set the port number. Browse to http://students.cs.tamu.edu/<your username>/test_site/, if you see the placeholder page, you’re good to go.

skittlish (Haml)

written by ben on August 31st, 2007 @ 01:38 PM

I’m using the skittlish theme by Cristi Balan. I use it for my trac theme as well.

The theme uses liquid, but I’m a big fan of Haml, so I converted it( sorry, no Sass). Warning: I personalized for my site, so it not an exact copy of the liquid theme. You can find it here.

mic check

written by ben on August 30th, 2007 @ 06:39 PM

check. check 1 – 2. check.

italicize check.

bold check.

#ruby mic-check
3.times do |count|
  puts "check #{count}"
end

rock & roll