class Guard::JestRunner::Runner
This class runs `jest` command, retrieves result and notifies. An instance of this class is intended to invoke `jest` only once in its lifetime.
Attributes
check_stderr[RW]
check_stdout[RW]
options[R]
Public Class Methods
new(options)
click to toggle source
# File lib/guard/jest_runner/runner.rb, line 11 def initialize(options) @options = options end
Public Instance Methods
failed_paths()
click to toggle source
# File lib/guard/jest_runner/runner.rb, line 31 def failed_paths result[:testResults].select { |f| f[:status] == "failed" }.map { |f| f[:name] }.uniq end
run(paths)
click to toggle source
# File lib/guard/jest_runner/runner.rb, line 17 def run(paths) paths = options[:default_paths] unless paths passed = run_for_check(paths) case options[:notification] when :failed notify(passed) unless passed when true notify(passed) end passed end
Private Instance Methods
args_specified_by_user()
click to toggle source
# File lib/guard/jest_runner/runner.rb, line 57 def args_specified_by_user @args_specified_by_user ||= begin args = options[:cli] case args when Array then args when String then args.shellsplit when NilClass then [] else fail ':cli option must be either an array or string' end end end
command_for_check(paths)
click to toggle source
# File lib/guard/jest_runner/runner.rb, line 49 def command_for_check(paths) command = [options[:command]] command.concat(args_specified_by_user) command.concat(['--json', "--outputFile=#{json_file_path}"]) command.concat(paths) end
json_file()
click to toggle source
Keep the Tempfile instance around so it isn't garbage-collected and therefore deleted.
# File lib/guard/jest_runner/runner.rb, line 78 def json_file @json_file ||= begin # Just generate random tempfile path. basename = self.class.name.downcase.gsub('::', '_') Tempfile.new(basename) end end
json_file_path()
click to toggle source
# File lib/guard/jest_runner/runner.rb, line 69 def json_file_path @json_file_path ||= begin json_file.close json_file.path end end
notify(passed)
click to toggle source
# File lib/guard/jest_runner/runner.rb, line 97 def notify(passed) image = passed ? :success : :failed Notifier.notify(summary_text, title: 'Jest results', image: image) end
pluralize(number, thing, options = {})
click to toggle source
# File lib/guard/jest_runner/runner.rb, line 125 def pluralize(number, thing, options = {}) text = String.new if number == 0 && options[:no_for_zero] text = 'no' else text << number.to_s end text << " #{thing}" text << 's' unless number == 1 text end
result()
click to toggle source
# File lib/guard/jest_runner/runner.rb, line 86 def result @result ||= begin File.open(json_file_path) do |file| # Rubinius 2.0.0.rc1 does not support `JSON.load` with 3 args. JSON.parse(file.read, symbolize_names: true) end end rescue JSON::ParserError fail "jest JSON output could not be parsed. Output from jest was:\n#{check_stderr}\n#{check_stdout}" end
run_for_check(paths)
click to toggle source
# File lib/guard/jest_runner/runner.rb, line 39 def run_for_check(paths) command = command_for_check(paths) (stdout, stderr, status) = Open3.capture3(*command) self.check_stdout = stdout self.check_stderr = stderr status rescue SystemCallError => e fail "The jest command failed with #{e.message}: `#{command}`" end
summary_text()
click to toggle source
# File lib/guard/jest_runner/runner.rb, line 102 def summary_text summary = { tests_run: result[:numTotalTests], passed: result[:numPassedTests], pending: result[:numPendingTests], failed: result[:numFailedTests], } String.new.tap do |text| if summary[:failed] > 0 text << pluralize(summary[:failed], 'example') text << " failed" text << " (#{summary[:passed]} passed" text << ", #{summary[:pending]} pending" if summary[:pending] > 0 text << ")." else text << "#{summary[:passed]} passed" text << " (#{summary[:pending]} pending)" if summary[:pending] > 0 text << "." end end end