Last active
April 15, 2021 22:04
-
-
Save brand-it/962a0ae81b4e56ea09b059e258fc6b35 to your computer and use it in GitHub Desktop.
A tool to test in a effect way by only testing the min amount. .git/hooks/pre-commit.d/rspec
This file contains 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
#!/usr/bin/env ruby | |
class ExampleStatusParser | |
def self.parse(string) | |
new(string).parse | |
end | |
def initialize(string) | |
@header_line, _, *@row_lines = string.lines.to_a | |
end | |
def parse | |
@row_lines.map { |line| parse_row(line) } | |
end | |
private | |
def parse_row(line) | |
headers.zip(split_line(line)).to_h | |
end | |
def headers | |
@headers ||= split_line(@header_line).grep(/\S/).map(&:to_sym) | |
end | |
def split_line(line) | |
line.split(/\s+\|\s+?/, -1) | |
end | |
end | |
@ignore = [] | |
@file_ends_with = 'spec.rb' | |
def git_files_changed | |
`git diff --staged --name-only --cached`.split("\n").reject do |file| | |
!file.end_with?(@file_ends_with) || @ignore.include?(file) || !File.exist?(file) | |
end | |
end | |
def spec_failures | |
ExampleStatusParser.parse(File.read('./spec/examples.txt')).map do |example| | |
next if example[:status] != 'failed' | |
example[:example_id] | |
end.compact | |
end | |
def success(message) | |
puts message | |
exit 0 | |
end | |
def rspec(files) | |
return true if files.size.zero? | |
system("bundle exec rspec --format documentation #{files.join(' ')}") | |
end | |
success('no file to check') if (spec_failures + git_files_changed).size.zero? | |
success('no errors found') if rspec(spec_failures) && rspec(git_files_changed) | |
# Open input from keyboard usin IO.new(0) normal standard in is closed | |
fd = IO.sysopen '/dev/tty', 'r' | |
ios = IO.new(fd, 'r') | |
print 'Do you want to commit anyway?(Y) ' | |
exit ios.gets.chomp == 'Y' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment