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

Extracts checks into common helper #232

Merged
merged 1 commit into from
Aug 1, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 30 additions & 23 deletions lib/rspec/support/spec/library_wide_checks.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
require 'rspec/support/spec/shell_out'

module RSpec
module Support
module WhitespaceChecks
# This malformed whitespace detection logic has been borrowed from bundler:
# https://github.com/bundler/bundler/blob/v1.8.0/spec/quality_spec.rb
def check_for_tab_characters(filename)
failing_lines = []
File.readlines(filename).each_with_index do |line, number|
failing_lines << number + 1 if line =~ /\t/
end

return if failing_lines.empty?
"#{filename} has tab characters on lines #{failing_lines.join(', ')}"
end

def check_for_extra_spaces(filename)
failing_lines = []
File.readlines(filename).each_with_index do |line, number|
next if line =~ /^\s+#.*\s+\n$/
failing_lines << number + 1 if line =~ /\s+\n$/
end

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

RSpec.shared_examples_for "library wide checks" do |lib, options|
consider_a_test_env_file = options.fetch(:consider_a_test_env_file, /MATCHES NOTHING/)
allowed_loaded_feature_regexps = options.fetch(:allowed_loaded_feature_regexps, [])
Expand All @@ -8,6 +37,7 @@
skip_spec_files = options.fetch(:skip_spec_files, /MATCHES NOTHING/)

include RSpec::Support::ShellOut
include RSpec::Support::WhitespaceChecks

define_method :files_to_require_for do |sub_dir|
slash = File::SEPARATOR
Expand Down Expand Up @@ -101,29 +131,6 @@ def have_successful_no_warnings_output
expect(loaded_features).to eq([])
end

# This malformed whitespace detection logic has been borrowed from bundler:
# https://github.com/bundler/bundler/blob/v1.8.0/spec/quality_spec.rb
def check_for_tab_characters(filename)
failing_lines = []
File.readlines(filename).each_with_index do |line, number|
failing_lines << number + 1 if line =~ /\t/
end

return if failing_lines.empty?
"#{filename} has tab characters on lines #{failing_lines.join(', ')}"
end

def check_for_extra_spaces(filename)
failing_lines = []
File.readlines(filename).each_with_index do |line, number|
next if line =~ /^\s+#.*\s+\n$/
failing_lines << number + 1 if line =~ /\s+\n$/
end

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

RSpec::Matchers.define :be_well_formed do
match do |actual|
actual.empty?
Expand Down