Created
August 12, 2011 05:18
-
-
Save dtsato/1141506 to your computer and use it in GitHub Desktop.
Spec to check for whitespace issues in your codebase. Tweak/contribute your patches!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec::Matchers.define :have_standardized_whitespaces do | |
failure_message_for_should do |actual| | |
whitespace_errors_for(actual).join("\n") | |
end | |
match do |actual| | |
whitespace_errors_for(actual).empty? | |
end | |
class WhitespaceCheck < Struct.new(:pattern, :message) | |
def lines | |
@lines ||= [] | |
end | |
def check(line, number) | |
lines << number if line =~ pattern | |
end | |
def error_for(file) | |
"#{file} #{message} on lines #{lines.join(',')}" unless lines.empty? | |
end | |
end | |
def whitespace_errors_for(files) | |
@whitespace_errors ||= files.inject([]) do |errors, filename| | |
whitespace_checks = [ | |
WhitespaceCheck.new(/\s+\n$/, "has EOL spaces"), | |
WhitespaceCheck.new(/\r/, "has DOS EOL characters"), | |
WhitespaceCheck.new(/\t/, "has tab characters") | |
] | |
File.readlines(filename).each_with_index do |line, number| | |
whitespace_checks.each {|w| w.check(line, number+1)} | |
end | |
errors += whitespace_checks.map {|m| m.error_for(filename)}.compact | |
end | |
end | |
end | |
describe "The codebase" do | |
around(:each) do |example| | |
Dir.chdir(File.expand_path("../..", __FILE__)) { example.run } | |
end | |
let(:filter) { /^(app|lib|spec).*\.(rb|rake)$/ } | |
subject { `git ls-files -c -m -o`.split("\n").select {|f| f =~ filter} } | |
it { should have_standardized_whitespaces } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment