Rails, tests and code coverage

“If it’s not tested assume it’s broken.” This is one of the mantras in my company. Sometimes it’s easier said then done. If I always had to test everything I would be doing nothing but testing. So I employ different techniques to keep me programming and the computer to test the code I write.

One of the nice things about Rails is that you have a whole array of tools to automate testing and at least one very nice tool to let you know what’s tested and what’s not. Yes I am talking about Rcov.

Rcov has been “the bee’s knees” for code coverage awareness. Recently I have noticed Rcov has been doing more work then expected. When I ran the rcov command in a rails project it tested everything. I mean everything, including gems installed on the system.

Since everything is just a bit too much for my project I started looking for a way to limit the output. It appears I had to use -x option to stop the code coverage tool from traversing the gems. Here is the whole task:

desc 'Measures test coverage using rcov'
Rcov::RcovTask.new(:rcov) do |rcov|
  rcov.pattern    = "test/**/*_test.rb"
  rcov.rcov_opts  = [
    "-x 'gems/.*,rubygems/.*,rcov/.*'",
    "-T",
    "--rails" ]
  rcov.output_dir = "rcov/unified"
end

I hope this helps you.

iChessClock in the Apple AppStore

Brent and I finally were able to submit our first iPhone application. It’s a simple yet elegant chess clock that can satisfy most of the chess matches out there.

Eidetic Software’s Website:
http://www.eideticsoftware.com

The iChessClock is available at the Apple AppStore

Removing git-svn remote branch

I could not find a reference how to remove a remote branch that is connected to an SVN repository so I have tried this:

$git branch -r -d my-remote-branch

This seemed to work well.

Unix prompt (PS1) with git branch name

Working with Git I noticed that I change branches quite a lot. This is a good thing but sometimes I am in the incorrect branch when I check things in. I needed a warning system especially when I check things into remote SVN repository. Since most of my check-ins are done from the command line it would be nice to know what branch I am on just by looking at the screen. Fortunately there is neat way to add the branch information to the Unix prompt. You can do that by overriding the PS1 variable for example:

export PS1="[\D{%Y-%m-%d} \t] \w \n$"

would generate

[2008-07-12 12:00:00] /foo/bar
$

if I am in the /foo/bar directory.

So at this point I would like the prompt to look like:

[2008-07-12 12:00:00] /foo/bar [master]
$

when I am in a master branch and prompt:

[2008-07-12 12:00:00] /foo/bar [experimental]
$

when I am in any other branch.

Here are the steps to get this working:
Prerequisites:
- ruby interpreter
- git

Steps:

  1. Create a git-ps.rb file
    green = "\033[0;32m"
    white = "\033[0;37m"
    red   = "\033[0;31m"
    current_branch = `git branch 2>/dev/null`.grep(/^\*/).first
    if current_branch
      branch_name = current_branch.gsub(/^\*\s*/,'').strip
      color = branch_name  =~ /master/ ? green : red
      unless current_branch.empty?
        puts "  #{color}[#{branch_name}]#{white}"
      end
    end
    
  2. create .ps file in your home directory:
    parse_git_branch() {
      ruby ~/git-ps.rb 2>/dev/null
    }
    export PS1="[\D{%Y-%m-%d} \t] \w\$(parse_git_branch) \n$ "
    
  3. Include the .ps file in your .bashrc and .bash_profile by inserting following line:
    . ~/.ps
    

To activate the new prompt in your current execute command:
. ~/.ps

I hope this helps.


References

Ref: Prompt Creation
Ref: Git and Bash prompt
Ref: __git_ps1


Downloads

colored_branch_name

Chess quote of the day

“I would do just fine if I could get you to keep your pieces still.”
- Scott

Gaussian Rounding in Ruby

While working on a research tool for my work I have noticed that my ruby solution rounds differently then the original c# .net code. It was not in every but about 10 percent of my tests failed due to the different rounding schemes.

Ruby uses the Symmetric Arithmetic Rounding while C# .net uses the Gaussian rounding otherwise known as the Banker’s routing.

I have looked for a quick solution for Ruby but Google did not return a result I could immediately use. I have found a javascript solution which I translated to Ruby.

Additionally I have added a “decimal” parameter to specify the precision of rounding.

Here is the code:

class Numeric
  def _round(val)
    sign = val < 0 ? -1 : 1
    return (val.abs.round * sign) if (val.abs - val.abs.floor) != 0.5
    return (val.abs.ceil  * sign) if val.abs.floor % 2 == 1
    return (val.abs.floor * sign)
  end

  def gaussian_round(decimals = 0)
    return (_round(self * (10**decimals))/((10**decimals).to_f))
  end
end

Ref: Javascript Gaussian Rounding Example
Ref: Wikipedia article on rounding

Using MacRuby to set Xcode project version from git

I have been looking into automating some of my development tasks in Cocoa/Xcode environment.  For the longest time I was using Subversion which would integrate quite well with Xcode. Recently, however, I have been exploring other version control systems.  Especially git.  Since it’s a much newer (D)VCS it does not have as much integration into systems as subversion.

I have managed to find some interesting solutions to include Git version number in Xcode project however the only actual solutions I have found are written in languages I don’t care for anymore.  Yes I can program in them but why would I want to.

So here is a quick solution written in MacRuby.

#!/usr/local/bin/macruby

git_output = `git show --abbrev-commit`
commit_version = git_output.split("\n").grep(/^commit/).first
version = commit_version.gsub(/^commit\s+(.*)\.{3}/, "\\1")
if version
  list = NSMutableDictionary.dictionaryWithContentsOfFile("Info.plist")
  list["CFBundleVersion"] = version
  list.writeToFile('Info.plist', :atomically => true)
end

In my final solution I intend to have an output in following format:

major.minor.revision (build)

where the “build” is going to correspond with the git commit version
and the other parts will reflect the marketing version numbers.


Ref: Shiny Frog Article Python solution
Ref: Cocoa is my Girlfriend Article Perl solution

Motto of the Day

“If you don’t know where to start, start from writing tests.”

Quick rename of menu items in XCode

Sometimes a little bit of automation goes a long way and other times it may be just wasted time. I have run into one of these situations recently when dealing with Xcode and trying to rename menu items from NewApplication to the Application Name.

For Example:

Quit NewApplication -> Quit TestApp

My first approach was to use Ruby open the MainMenu.xib and do a replacement. This technique may prove to be useful in the future when I have more command line tools for other tasks, but currently there is a much simpler way of dealing with renaming menu items.

1. Right click on MainMenu.nib and select “Open As” -> “Source Code File”

2. Press: “Command-F” to open find/replace dialog

3. Replace all NewApplication strings with desired application name.

That’s it

Webkit-appearance property.

I have spend some time wondering why would Apple decide to style their buttons in a non-standard way and then not respect the css style set by the designer. I just found the answer how to get rid of the Apple predefined style and allow the buttons to look like in every other browser. To do this you have to set the webkit-appearance property to none:


input { --webkit-appearance: none; }

That’s all.