-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Importable/exportable Minitest assertions, ShoesSpec runner (#399)
* Importable/exportable Minitest assertions, ShoesSpec runner. Refactor Changelog logic in constants.rb to work when not run from Scarpe root dir * Remove Rubocop from default local test run * Update lacci/lib/shoes/app.rb Co-authored-by: Nick Schwaderer <[email protected]> --------- Co-authored-by: Nick Schwaderer <[email protected]>
- Loading branch information
Showing
13 changed files
with
297 additions
and
33 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module Shoes | ||
module Spec | ||
def self.instance | ||
@instance | ||
end | ||
|
||
def self.instance=(spec_inst) | ||
if @instance && @instance != spec_inst | ||
raise "Lacci can only use a single ShoesSpec implementation at one time!" | ||
end | ||
@instance = spec_inst | ||
end | ||
end | ||
end |
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
require "minitest" | ||
require "scarpe/cats_cradle" # Currently needed for CCHelpers | ||
|
||
# Test framework code to allow Scarpe to execute Shoes-Spec test code. | ||
# This will run inside the exe/scarpe child process, then send | ||
# results back to the parent Minitest process. | ||
|
||
module Scarpe::Test | ||
# Cut down from Rails camelize | ||
def self.camelize(string) | ||
string = string.sub(/^[a-z\d]*/) { |match| match.capitalize } | ||
string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" } | ||
end | ||
|
||
# Is it at all reasonable to define more than one test to run in the same Shoes run? Probably not. | ||
# They'll leave in-memory residue. | ||
def self.run_shoes_spec_test_code(code, class_name: "TestShoesSpecCode", test_name: "test_shoes_spec") | ||
if @shoes_spec_init | ||
raise MultipleShoesSpecRunsError, "Scarpe-Webview can only run a single Shoes spec per process!" | ||
end | ||
@shoes_spec_init = true | ||
|
||
require_relative "cats_cradle" | ||
|
||
# We want Minitest assertions available in the test code. | ||
# But this will normally run in a subprocess. So we need | ||
# to run Minitest tests and then export the results. | ||
|
||
test_obj = Object.new | ||
class << test_obj | ||
include Scarpe::Test::CatsCradle | ||
end | ||
test_obj.instance_eval do | ||
event_init | ||
|
||
on_heartbeat do | ||
Minitest.run ARGV | ||
|
||
test_finished_no_results | ||
end | ||
end | ||
|
||
test_class = Class.new(Scarpe::ShoesSpecTest) | ||
Object.const_set(camelize(class_name), test_class) | ||
test_name = "test_" + test_name unless test_name.start_with?("test_") | ||
test_class.define_method(test_name) do | ||
eval(code) | ||
#test_obj.instance_variable_get(:@cc_instance).instance_eval(code) | ||
end | ||
end | ||
end | ||
|
||
# When running ShoesSpec tests, we create a parent class for all of them | ||
# with the appropriate convenience methods and accessors. | ||
class Scarpe::ShoesSpecTest < Minitest::Test | ||
include Scarpe::Test::CCHelpers | ||
end |
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
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
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
71 changes: 71 additions & 0 deletions
71
scarpe-components/lib/scarpe/components/minitest_export_reporter.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# frozen_string_literal: true | ||
|
||
# Have to require this to get DefaultReporter and the Minitest::Reporters namespace. | ||
ENV["MINITEST_REPORTER"] = "ShoesExportReporter" | ||
require "minitest/reporters" | ||
require "json" | ||
require "json/add/exception" | ||
|
||
module Minitest | ||
module Reporters | ||
# To use this Scarpe component, you'll need minitest-reporters in your Gemfile, | ||
# probably in the "test" group. You'll need to require and activate it to | ||
# register it as Minitest's reporter: | ||
# | ||
# require "scarpe/components/minitest_export_reporter" | ||
# Minitest::Reporters::ShoesExportReporter.activate! | ||
# | ||
# Select a destination to export JSON test results to: | ||
# | ||
# export SHOES_MINITEST_EXPORT_FILE=/tmp/shoes_test_export.json | ||
# | ||
# This class overrides the MINITEST_REPORTER environment variable when you call activate. | ||
# If MINITEST_REPORTER isn't set then when you run via Vim, TextMate, RubyMine, etc, | ||
# the reporter will be automatically overridden and print to console instead. | ||
# | ||
# Based on https://gist.github.com/davidwessman/09a13840a8a80080e3842ac3051714c7 | ||
class ShoesExportReporter < DefaultReporter | ||
def self.activate! | ||
unless ENV["SHOES_MINITEST_EXPORT_FILE"] | ||
raise "ShoesExportReporter is available, but no export file was specified! Set SHOES_MINITEST_EXPORT_FILE!" | ||
end | ||
|
||
Minitest::Reporters.use! | ||
end | ||
|
||
def serialize_failures(failures) | ||
failures.map do |fail| | ||
case fail | ||
when Minitest::UnexpectedError | ||
["unexpected", fail.to_json, fail.error.to_json] | ||
when Exception | ||
["exception", fail.to_json] | ||
else | ||
raise "Not sure how to serialize failure object! #{fail.inspect}" | ||
end | ||
end | ||
end | ||
|
||
def report | ||
super | ||
|
||
results = tests.map do |result| | ||
failures = serialize_failures result.failures | ||
{ | ||
name: result.name, | ||
klass: test_class(result), | ||
assertions: result.assertions, | ||
failures: failures, | ||
time: result.time, | ||
metadata: result.metadata, | ||
source_location: (result.source_location rescue ["unknown", -1]), | ||
} | ||
end | ||
|
||
out_file = ENV["SHOES_MINITEST_EXPORT_FILE"] | ||
puts "Writing Minitest results to #{out_file.inspect}." | ||
File.write(out_file, JSON.dump(results)) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.