mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Remove runtime compilation tests
These may be readded later. Right now they're failing because the GitHub issue 44 spec changes the behavior of Process.run. The changes made by that spec shouldn't leak, but to fix correctly requires substantial changes. These runtime tests provide little value right now and slow down testing.
This commit is contained in:
parent
11e227b29f
commit
bc0a9c03c9
8 changed files with 1 additions and 262 deletions
0
spec/helpers/.gitkeep
Normal file
0
spec/helpers/.gitkeep
Normal file
|
@ -1,71 +0,0 @@
|
|||
require "ecr"
|
||||
require "json"
|
||||
require "./result"
|
||||
|
||||
module Spectator::SpecHelpers
|
||||
# Wrapper for compiling and running an example at runtime and getting a result.
|
||||
class Example
|
||||
# Creates the example.
|
||||
# The *spec_helper_path* is the path to spec_helper.cr file.
|
||||
# The name or ID of the example is given by *example_id*.
|
||||
# Lastly, the source code for the example is given by *example_code*.
|
||||
def initialize(@spec_helper_path : String, @example_id : String, @example_code : String)
|
||||
end
|
||||
|
||||
# Instructs the Crystal compiler to compile the test.
|
||||
# Returns an instance of `JSON::Any`.
|
||||
# This will be the outcome and information about the test.
|
||||
# Output will be suppressed for the test.
|
||||
# If an error occurs while attempting to compile and run the test, an error will be raised.
|
||||
def compile
|
||||
# Create a temporary file containing the test.
|
||||
with_tempfile do |source_file|
|
||||
args = ["run", "--no-color", source_file, "--", "--json"]
|
||||
Process.run(crystal_executable, args) do |process|
|
||||
JSON.parse(process.output)
|
||||
rescue JSON::ParseException
|
||||
raise "Compilation of example #{@example_id} failed\n\n#{process.error.gets_to_end}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Same as `#compile`, but returns the result of the first example in the test.
|
||||
# Returns a `SpectatorHelpers::Result` instance.
|
||||
def result
|
||||
output = compile
|
||||
example = output["examples"][0]
|
||||
Result.from_json_any(example)
|
||||
end
|
||||
|
||||
# Constructs the string representation of the example.
|
||||
# This produces the Crystal source code.
|
||||
# *io* is the file handle to write to.
|
||||
# The *dir* is the directory of the file being written to.
|
||||
# This is needed to resolve the relative path to the spec_helper.cr file.
|
||||
private def write(io, dir)
|
||||
spec_helper_path = Path[@spec_helper_path].relative_to(dir) # ameba:disable Lint/UselessAssign
|
||||
ECR.embed(__DIR__ + "/example.ecr", io)
|
||||
end
|
||||
|
||||
# Creates a temporary file containing the compilable example code.
|
||||
# Yields the path of the temporary file.
|
||||
# Ensures the file is deleted after it is done being used.
|
||||
private def with_tempfile
|
||||
tempfile = File.tempfile("_#{@example_id}_spec.cr") do |file|
|
||||
dir = File.dirname(file.path)
|
||||
write(file, dir)
|
||||
end
|
||||
|
||||
begin
|
||||
yield tempfile.path
|
||||
ensure
|
||||
tempfile.delete
|
||||
end
|
||||
end
|
||||
|
||||
# Attempts to find the Crystal compiler on the system or raises an error.
|
||||
private def crystal_executable
|
||||
Process.find_executable("crystal") || raise("Could not find Crystal compiler")
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,5 +0,0 @@
|
|||
require "<%= spec_helper_path %>"
|
||||
|
||||
Spectator.describe "<%= @example_id %>" do
|
||||
<%= @example_code %>
|
||||
end
|
|
@ -1,28 +0,0 @@
|
|||
module Spectator::SpecHelpers
|
||||
# Information about an `expect` call in an example.
|
||||
struct Expectation
|
||||
# Indicates whether the expectation passed or failed.
|
||||
getter? satisfied : Bool
|
||||
|
||||
# Message when the expectation failed.
|
||||
# Only available when `#satisfied?` is false.
|
||||
getter! message : String
|
||||
|
||||
# Additional information about the expectation.
|
||||
# Only available when `#satisfied?` is false.
|
||||
getter! values : Hash(String, String)
|
||||
|
||||
# Creates the expectation outcome.
|
||||
def initialize(@satisfied, @message, @values)
|
||||
end
|
||||
|
||||
# Extracts the expectation information from a `JSON::Any` object.
|
||||
def self.from_json_any(object : JSON::Any)
|
||||
satisfied = object["satisfied"].as_bool
|
||||
message = object["failure"]?.try(&.as_s?)
|
||||
values = object["values"]?.try(&.as_h?)
|
||||
values = values.transform_values(&.as_s) if values
|
||||
new(satisfied, message, values)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,67 +0,0 @@
|
|||
module Spectator::SpecHelpers
|
||||
# Information about an example compiled and run at runtime.
|
||||
struct Result
|
||||
# Status of the example after running.
|
||||
enum Outcome
|
||||
Success
|
||||
Failure
|
||||
Error
|
||||
Unknown
|
||||
end
|
||||
|
||||
# Full name and description of the example.
|
||||
getter name : String
|
||||
|
||||
# Status of the example after running.
|
||||
getter outcome : Outcome
|
||||
|
||||
# List of expectations ran in the example.
|
||||
getter expectations : Array(Expectation)
|
||||
|
||||
# Creates the result.
|
||||
def initialize(@name, @outcome, @expectations)
|
||||
end
|
||||
|
||||
# Checks if the example was successful.
|
||||
def success?
|
||||
outcome.success?
|
||||
end
|
||||
|
||||
# :ditto:
|
||||
def successful?
|
||||
outcome.success?
|
||||
end
|
||||
|
||||
# Checks if the example failed, but did not error.
|
||||
def failure?
|
||||
outcome.failure?
|
||||
end
|
||||
|
||||
# Checks if the example encountered an error.
|
||||
def error?
|
||||
outcome.error?
|
||||
end
|
||||
|
||||
# Extracts the result information from a `JSON::Any` object.
|
||||
def self.from_json_any(object : JSON::Any)
|
||||
name = object["description"].as_s
|
||||
outcome = parse_outcome_string(object["status"].as_s)
|
||||
expectations = if (list = object["expectations"].as_a?)
|
||||
list.map { |e| Expectation.from_json_any(e) }
|
||||
else
|
||||
[] of Expectation
|
||||
end
|
||||
new(name, outcome, expectations)
|
||||
end
|
||||
|
||||
# Converts a result string, such as "fail" to an enum value.
|
||||
private def self.parse_outcome_string(string)
|
||||
case string
|
||||
when /pass/i then Outcome::Success
|
||||
when /fail/i then Outcome::Failure
|
||||
when /error/i then Outcome::Error
|
||||
else Outcome::Unknown
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue