Installing RCov by script

Code coverage analysis with RCov, next on my TODO :>
Another install script for your leisure…

OBSOLETE: gem install rcov works again!

wget http://rubyforge.org/frs/download.php/28270/rcov-0.8.1.2.tar.gz
tar -xf rcov-0.8.1.2.tar.gz
sudo ruby rcov-0.8.1.2/setup.rb
rm -rf rcov-0.8.1.2

svn cleanup
svn update
piston import http://svn.codahale.com/rails_rcov vendor/plugins/rails_rcov

Now you can get started with rake test:units:rcov

Unit test results

  • Show only selected parts: SHOW_ONLY=m,l,c,v //model,lib,controller,view
  • Run single tests: rcov FILE -exclude “/var/|/usr/” –rails (irgnore /var and /usr folder)
  • Add RCov parameters: RCOV_PARAMS=””
  • exclude RCOV_PARAM=”–exclude ‘var/*,gems/*'”
  • Sort by coverage: COV_PARAMS=”–sort=coverage”
  • Hide fully covered: RCOV_PARAMS=” –only-uncovered”
  • If you see something like (eval):580:in `attribute’: wrong number of arguments (2 for 0) (ArgumentError) it means one of your tests is uncoverable, this can happen while using SQS gem

Add the RCOV_PARAMS to spec/rcov.opts (or test/rcov.opts)

Autoupdate jQuery with Rake

After installing jQuery on Rails (jRails) i noticed that the update task just kept overwriting my jquery.js with an older version. Effectively downgrading it…

So here is a real update task, execute it with rake update:jquery

  #lib/tasks/jquery.rake
  namespace :update do
    desc "Download recent jQuery javascripts to public/javascripts"
    task :jquery => :environment do
      puts "Downloading files..."
      files = {
        'jquery.js'=>'http://code.jquery.com/jquery-latest.min.js',
      }

      require 'open-uri'
      files.each do |local,remote|
        f = File.open "#{js_dir}/#{local}", 'w' do |f|
          f.write open(remote).read
        end
        puts "downloaded #{local} successfully."
      end
      puts "all files downloaded successfully."
    end

    def js_dir
      ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR
    end
  end

PS: If the bleeding edge breaks anything you can get your old version back by jrails:update:javascripts

The other jquery libaries(fx,ui) are missing atm, since i am just using jquery on its own. If you know an update site, please drop a comment.

Beware of Dir.mkdir Permission Handling

Took me 1 hours to figure out Dir.mkdir’s permission handling is broken ?

micha@ubuntu:/home/data/test$ ruby -e "Dir.mkdir 'wtf',0777"
micha@ubuntu:/home/data/test$ ls -al
drwxr-xr-x 2 micha   micha 4096 2008-02-24 13:07 wtf
micha@ubuntu:/home/data/test$ ruby -e "File.chmod 0777,'wtf'"
micha@ubuntu:/home/data/test$ ls -al
drwxrwxrwx 2 micha   micha 4096 2008-02-24 13:07 wtf

Dir: 0777 = drwxr-xr-x
File: 0777 = drwxrwxrwx
WTF?

And to not make it a short post, i add some recursive directory creating 😀

  #simple recursive dir creation
  FileUtils.mkdir_p dir

  #create a directory recursive and set rights/group
  def self.mkdir_r path, permission=0770,group_id=false
    path.split('/').inject(path =~ /^\// ? '/':'') do |root,part|
      path = root+'/'+part
      unless File.exist? path
        Dir.mkdir(path, permission)
        #hack, mkdir does not set the permisson right !?
        File.chmod permission,path
        File.chown(-1,group_id,path) if group_id
      end
      path
    end
  end

The Simpelest Mongrel Server (Without Rails)

I tried to make my own small server and mongrel came in handy!
Some things i covered:

  • making the server stop on ctr+c
  • ignoring favicon requests
  • using the local ip (192.168.X.X) or localhost
  • parsing the request parameters (?aaa=bbb&…) to a hash

The rest is up to you!

Stick your code into process, then run ‘ruby server.rb’ and your done 🙂

#server.rb
require 'mongrel'
require 'yaml'

module SimpleServer
  class SimpleHandler < Mongrel::HttpHandler
    def process(request, response)
      return if request.params['REQUEST_URI']=='/favicon.ico'

      params = extract_params request
      puts "Called with:"
      puts params.to_yaml

      response.start(200) do |head,out|
        head["Content-Type"] = "text/plain"
        out.write("HELLO WORLD!")
      end
    end

    def extract_params request
      params = request.params['QUERY_STRING']
      return {} if !params

      arr = params.split('&')

      hash = {}
      arr.each do |value|
        break if !value
        temp = value.split('=')
        hash[temp[0]]= temp[1]
      end 

      hash
    end
  end

  #SERVER
  def self.start
    require 'socket'
    #or use 'localhost' if u do not want to be callable from outside
    host = IPSocket.getaddress(Socket.gethostname)
    config = Mongrel::Configurator.new :host => host, :port => 3000 do
      listener {uri "/", :handler => SimpleHandler.new}
      trap("INT") { stop }
      run
    end

    #show what we are running
    puts "Running at"
    puts config.defaults.to_yaml
    config.join
  end
end

SimpleServer::start