Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit aefe3aa

Browse files
committed
Add whitespace spec.
1 parent a2d2656 commit aefe3aa

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

lib/rspec/support/spec/library_wide_checks.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,46 @@ def have_successful_no_warnings_output
117117

118118
expect(loaded_features).to eq([])
119119
end
120+
121+
# This malformed whitespace detection logic has been borrowed from bundler:
122+
# https://github.com/bundler/bundler/blob/v1.8.0/spec/quality_spec.rb
123+
def check_for_tab_characters(filename)
124+
failing_lines = []
125+
File.readlines(filename).each_with_index do |line, number|
126+
failing_lines << number + 1 if line =~ /\t/
127+
end
128+
129+
return if failing_lines.empty?
130+
"#{filename} has tab characters on lines #{failing_lines.join(', ')}"
131+
end
132+
133+
def check_for_extra_spaces(filename)
134+
failing_lines = []
135+
File.readlines(filename).each_with_index do |line, number|
136+
next if line =~ /^\s+#.*\s+\n$/
137+
failing_lines << number + 1 if line =~ /\s+\n$/
138+
end
139+
140+
return if failing_lines.empty?
141+
"#{filename} has spaces on the EOL on lines #{failing_lines.join(', ')}"
142+
end
143+
144+
RSpec::Matchers.define :be_well_formed do
145+
match do |actual|
146+
actual.empty?
147+
end
148+
149+
failure_message do |actual|
150+
actual.join("\n")
151+
end
152+
end
153+
154+
it "has no malformed whitespace" do
155+
error_messages = []
156+
`git ls-files -z`.split("\x0").each do |filename|
157+
error_messages << check_for_tab_characters(filename)
158+
error_messages << check_for_extra_spaces(filename)
159+
end
160+
expect(error_messages.compact).to be_well_formed
161+
end
120162
end

0 commit comments

Comments
 (0)