Created
July 18, 2024 04:49
-
-
Save costa/1f7a77d0949a1ee1f612fcd35b4fc23e to your computer and use it in GitHub Desktop.
Manual (e.g. human-terminal-based) RSpec helper; requires https://github.com/JEG2/highline/issues/273 -- just `require` it in your spec_helper.rb
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
module ManualHelper | |
MANUAL_OPERATOR_PRESENT = | |
ENV['MANUAL_OPERATOR_PRESENT'].to_s !~ /^(0|no?|f(alse)?)?$/i | |
HighLine.add_to_color_scheme( | |
manual: [:yellow, :on_black], | |
explain: [:grey], | |
demand: [:bold, :white], | |
prompt: [:green] | |
) | |
HL = ::HighLine.new | |
HL.use_color if | |
$stdout.isatty | |
def self.included(c) | |
HighLine.colorize_strings | |
c.around do |example| | |
@manual_test = example.metadata[:manual] | |
expect($stdout.isatty).to be_truthy, "Must be run from a terminal (by a person, manually)" unless | |
MANUAL_OPERATOR_PRESENT | |
manual_say "\n## manual please\n", :manual | |
example.run | |
manual_say "\n## thank you\n", :manual | |
@manual_test = false | |
end | |
end | |
def explain(msg) | |
fail "Cannot explain unless it's manual" unless @manual_test | |
manual_say msg, :explain | |
end | |
def demand(claim) | |
fail "Cannot demand unless it's manual" unless @manual_test | |
manual_say claim, :demand | |
expect(HL.agree "OK?").to be_truthy, "Well let's get this working then (#{claim})" if @manual_test | |
end | |
private | |
def manual_say(what, color) | |
# NOTE to flush highline, you need a tab at the end, apparently | |
HL.say "#{what}\n\t".color(color) if @manual_test | |
end | |
end | |
RSpec.configure do |c| | |
c.include ManualHelper | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment