Skip to content

Instantly share code, notes, and snippets.

@yaauie
Created August 8, 2012 20:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaauie/3298674 to your computer and use it in GitHub Desktop.
Save yaauie/3298674 to your computer and use it in GitHub Desktop.

Given: this rspec file is in PROJECT_ROOT/spec

describe 'codebase' do
  let(:lines_with_more_than_80_characters) do
    Dir.glob( File.expand_path('../../lib/**/*.rb', __FILE__ ) ).map do |path|
      File.open( path ) do |file|
        idx = 0
        file.lines.map do |line|
          idx += 1
          next nil if line.length < 80
          "#{file.path}:#{idx}"
        end
      end
    end.compact.reduce([],:<<).flatten.compact
  end

  it 'should not have lines longer than 80 characters.' do
    lines_with_more_than_80_characters.should be_empty, 
      %Q(Expected no entries, got: \n#{lines_with_more_than_80_characters.join("\n")})
  end
end

yields the following result on failure:

..........F

Failures:

  1) codebase should not have lines longer than 80 characters.
     Failure/Error: lines_with_more_than_80_characters.should be_empty,
       Expected no entries, got: 
       /Users/yaauie/projects/project-name/lib/a-source-file.rb:32
       /Users/yaauie/projects/project-name/lib/a-source-file.rb:41
       /Users/yaauie/projects/project-name/lib/another-source-file.rb:12
       /Users/yaauie/projects/project-name/lib/another-source-file.rb:48
     # ./spec/codebase.spec.rb:16

Finished in 0.01717 seconds
11 examples, 1 failure
@topfunky
Copy link

topfunky commented Aug 8, 2012

Useful! Thanks!

@myronmarston
Copy link

@topfunky -- FWIW, I used to have a spec like this but have switched to using cane. I'm a huge fan of Cane.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment