Skip to content

Commit

Permalink
Quality spec, enforcing clean/well-formed files
Browse files Browse the repository at this point in the history
  • Loading branch information
sgonyea committed Dec 28, 2010
1 parent b7a18d8 commit 9fcf148
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
11 changes: 11 additions & 0 deletions spec/quality_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "spec_helper"

IGNORE = /\.(png$|gz$|rbc$|ico$|jpg$|gif$|eps$|logo$|pdf$)/

describe "The application itself" do
it "has no malformed whitespace" do
files = `git ls-files`.split("\n").select {|fn| fn !~ IGNORE}

files.should be_well_formed
end
end
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rspec'

Dir[ Bundler.root.join("spec/support/**/*.rb") ].each{|f| require f}

RSpec.configure do |c|
c.include CustomMatchers
end
49 changes: 49 additions & 0 deletions spec/support/customer_matchers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module CustomMatchers
class BeWellFormed
def matches?(files)
@errors = files.map {|filename|
begin
[
check_for_tabs(filename),
excessive_spacing(filename),
newline_precedes_eof(filename)
]
rescue ArgumentError => e
"File #{filename} likely contains binary data. If so, please exclude it via quality_spec.IGNORE" <<
"\n Exception => #{e.inspect}"
end
}.flatten.compact

@errors.empty?
end

def failure_message_for_should
@errors.join("\n")
end

private
def check_for_tabs(filename)
bad_lines = File.readlines(filename).each_with_index.map do |line, line_no|
line_no + 1 if line["\t"] and line !~ /^\s+#.*\s+\n$/
end.flatten.compact

"#{filename} has tab characters on lines #{bad_lines.join(', ')}" if bad_lines.any?
end

def excessive_spacing(filename)
bad_lines = File.readlines(filename).each_with_index.map do |line, line_no|
line_no + 1 if line =~ /\s+\n$/ and line !~ /^\s+#.*\s+\n$/
end.flatten.compact

"#{filename} has spaces on the EOL on lines #{bad_lines.join(', ')}" if bad_lines.any?
end

def newline_precedes_eof(filename)
"#{filename} does not have a newline (\\n) before EOF" if File.read(filename) !~ /\n$/
end
end

def be_well_formed
BeWellFormed.new
end
end

0 comments on commit 9fcf148

Please sign in to comment.