-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from sleepingkingstudios/feature/sandbox
Feature/Implement RSpec::SleepingKingStudios::Sandbox.
- Loading branch information
Showing
39 changed files
with
479 additions
and
101 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
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,81 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'stringio' | ||
|
||
require 'rspec/core/sandbox' | ||
|
||
require 'rspec/sleeping_king_studios' | ||
|
||
module RSpec::SleepingKingStudios | ||
# Helper for running RSpec files in isolation. | ||
# | ||
# Sandboxed files can be used to test enhancements to RSpec itself, such as | ||
# custom matchers or shared or deferred example groups. | ||
module Sandbox | ||
# Value class for the result of calling a sandboxed spec file. | ||
Result = Struct.new(:output, :errors, :json, :status, keyword_init: true) do | ||
# @return [Array<String>] the full description for each run example. | ||
def example_descriptions | ||
json['examples'].map { |hsh| hsh['full_description'] } | ||
end | ||
|
||
# @return [String] the summary of the sandboxed spec run. | ||
def summary | ||
json['summary_line'] | ||
end | ||
end | ||
|
||
class << self | ||
# Runs the specified spec files in a sandbox. | ||
# | ||
# The examples and other RSpec code in the files will *not* be added to | ||
# the current RSpec process. | ||
# | ||
# @param files [Array<String>] the file names or patterns for the spec | ||
# files to run. | ||
# | ||
# @return [RSpec::SleepingKingStudios::Result] the status and output of | ||
# the spec run. | ||
def run(*files) # rubocop:disable Metrics/MethodLength | ||
if files.empty? | ||
raise ArgumentError, 'must specify at least one file or pattern' | ||
end | ||
|
||
err = StringIO.new | ||
out = StringIO.new | ||
status = nil | ||
args = format_args(*files) | ||
|
||
RSpec::Core::Sandbox.sandboxed do |config| | ||
config.filter_run_when_matching :focus | ||
|
||
status = RSpec::Core::Runner.run(args, err, out) | ||
end | ||
|
||
build_result(err:, out:, status:) | ||
end | ||
|
||
private | ||
|
||
def build_result(err:, out:, status:) | ||
*output, raw_json = out.string.lines | ||
|
||
Result.new( | ||
output: output.join, | ||
errors: err.string, | ||
json: JSON.parse(raw_json), | ||
status: | ||
) | ||
end | ||
|
||
def format_args(*files) | ||
[ | ||
'--format=json', | ||
'--format=doc', | ||
'--order=defined', | ||
"--pattern=#{files.join(',')}" | ||
] | ||
end | ||
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
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
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
Oops, something went wrong.