Last active
October 23, 2024 07:46
-
-
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
This file contains hidden or 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
# NOTE As seen at https://gist.github.com/costa/1f7a77d0949a1ee1f612fcd35b4fc23e | |
# | |
# Enables proper manual (human operator / QA) testing (with https://RSpec.info) | |
# | |
# NOTE It is recommended to tag manual specs with `, manual: true`, | |
# -- so they can be skipped with `$ rspec -t ~manual` when human is not present. | |
# | |
# DSL: | |
# - #explain "<MESSAGE-TO-OPERATOR>" | |
# - #demand "<OPERATOR-TO-CHECK>" | |
require 'highline' | |
module ManualHelper | |
# NOTE set this to 'yes' when using non-TTY UI (through std I/O) | |
NON_TTY_MANUAL_OPERATOR_PRESENT = | |
ENV['NON_TTY_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 | |
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 | |
NON_TTY_MANUAL_OPERATOR_PRESENT | |
example.run | |
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 "\n#{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