Skip to content

Instantly share code, notes, and snippets.

@costa
Last active August 30, 2024 16:30
Show Gist options
  • Save costa/1f7a77d0949a1ee1f612fcd35b4fc23e to your computer and use it in GitHub Desktop.
Save costa/1f7a77d0949a1ee1f612fcd35b4fc23e to your computer and use it in GitHub Desktop.
Manual (e.g. human-terminal-based) RSpec helper -- just `require` it in your spec_helper.rb
require 'highline'
module ManualHelper
MANUAL_OPERATOR_PRESENT =
ENV['MANUAL_OPERATOR_PRESENT'].to_s !~ /^(0|no?|f(alse)?)?$/i
FORCE_COLOR =
ENV['FORCE_COLOR'].to_s !~ /^(0|no?|f(alse)?)?$/i
HighLine.color_scheme = HighLine.add_to_color_scheme(
# TODO? , :on_black etc
manual: [:yellow],
explain: [:bold, :yellow],
demand: [:bold, :white],
prompt: [:bold, :green]
)
HighLine.use_color = false unless
$stdout.isatty || FORCE_COLOR
def self.included(c)
HighLine.colorize_strings
c.around do |example|
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
end
end
def explain(msg)
manual_say msg, :explain
end
def demand(claim)
manual_say claim, :demand
expect(HighLine.agree "OK?".color(:prompt)).to be_truthy, "Well let's get this working then, shall we?!"
end
private
def manual_say(what, color)
# NOTE to flush HL, you need a space at the end, apparently
HighLine.say "#{what}\n ".color(color)
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