The Devver Blog

A Boulder startup improving the way developers work.

Archive for May 2009

Spellcheck your files with Aspell and Rake

We recently redid our website. The new site included a new design and much more content explaining what we do. We wanted a quick way to check over everything and make sure we didn’t miss any spelling errors or typos. First I started looking for a web service that could scan the site for spelling errors. I found spellr.us, which is nice but would only catch errors once they were live. It also can’t scan all of the pages which require being logged in.

I was pairing with Avdi who thought we should just run Aspell, which worked out great. We were originally trying to just create a simple Emacs macro to go through all our HTML files and check them but in the end created simple Rake tasks, which makes it really easy to integrate spellcheck into CI. After Avdi figured out the commands we needed to use on each file to get the information we needed from Aspell, it was easy to just wrap the command using Rake’s FileList. To keep everyone on the same setup, we created a local dictionary of words to ignore or accept and keep that checked into source control as well.

The final solution grabs all the files you want to spell check, then runs them through Aspell with HTML filtering. We have two tasks: one that runs in interactive mode the the user can fix mistakes and one mode for CI that just fails if it finds any errors.

def run_spellcheck(file,interactive=false)
  if interactive
    cmd = "aspell -p ./config/devver_dictionary -H check #{file}"
    puts cmd
    system(cmd)
    [true,""]
  else
    cmd = "aspell -p ./config/devver_dictionary -H list  'spellcheck:interactive'

namespace :spellcheck do
  files = FileList['app/views/**/*.html.erb']

  desc "Spellcheck interactive"
  task :interactive do
    files.each do |file|
      run_spellcheck(file,true)
    end
    puts "spelling check complete"
  end

  desc "Spellcheck for ci"
  task :ci do
    files.each do |file|
      success, results = run_spellcheck(file)
      unless success
        puts results
        exit 1
      end
    end
    puts "no spelling errors"
    exit 0
  end
end

view this gist

Advertisements

Written by DanM

May 26, 2009 at 8:33 am

Devver.net has a new look!

Tonight we just launched the brand new version of Devver at http://devver.net. It’s not perfect (yeah, yeah, we know the blog doesn’t match – that should be fixed in the next week or so), but we’re trying to “release early, release often.” Let us know how you like the new look and how we can improve it!

Written by Ben

May 14, 2009 at 8:40 pm

Posted in Uncategorized

Tagged with ,

Single-file Sinatra apps with specs baked-in

It’s so easy to create little single-file apps in Sinatra that it almost seems a shame to start a second file just for tests.  The other day Dan and I decided to see if we could create a Sinatra app with everything – including the tests – baked right in.  Here’s what we came up with.

#!/usr/bin/env ruby
require 'rubygems'
gem 'rack', '=0.9.1'
gem 'thin', '=1.0.0'
require 'sinatra'

get '/' do
  content_type 'text/plain'
  "Hello, world"
end

# Run me with 'spec' executable to run my specs!
if $0 =~ /spec$/
  set :environment, :test
  set :run,         false       # Don't autostart server

  require 'spec/interop/test'
  require 'sinatra/test'

  describe "Example App" do
    include Sinatra::Test

    it "should serve a greeting" do
      get '/'
      response.should be_ok
      response.body.should == "Hello, world"
    end

    it "should serve content as text/plain" do
      get '/'
      response.headers['Content-Type'].should == 'text/plain'
    end

  end
end

view this gist
The code switches modes on the name of the executable used to run the file. If we run it with the

spec

command, we get a test run:

$ spec -fs sinatra-tests-baked-in.rb

Example App
- should serve a greeting
- should serve content as text/plain

Finished in 0.007221 seconds

2 examples, 0 failures

Otherwise, if we call it as a Ruby program, it runs the Sinatra server as we would expect:

$ ruby sinatra-tests-baked-in.rb
== Sinatra/0.9.1.1 has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.0.0 codename That's What She Said)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:4567, CTRL+C to stop

And there you have it: a true single-file application, specs and all.

Written by avdi

May 13, 2009 at 9:00 am

Posted in Development, Hacking, Ruby

Tagged with , ,