Skip to content

Commit

Permalink
Test runner improvements. (#673)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdotdesign authored Sep 21, 2023
1 parent eccd15c commit cda6761
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 28 deletions.
26 changes: 21 additions & 5 deletions src/commands/test.cr
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,30 @@ module Mint
description: "Will use supplied runtime path instead of the default distribution",
required: false

define_argument test : String
define_flag watch : Bool,
description: "Watch files for changes and rerun tests",
required: false

define_argument test : String,
description: "The path to the test file to run"

def run
succeeded = nil
execute "Running Tests" do
succeeded = TestRunner.new(flags, arguments).run
MintJson.parse_current.check_dependencies!

runner =
TestRunner.new(flags, arguments)

if flags.watch
runner.watch
else
succeeded = nil

execute "Running Tests" do
succeeded = runner.run
end

exit(1) unless succeeded
end
exit(1) unless succeeded
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions src/render/terminal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ module Mint
print contents.to_s
end

def reset
print "\ec"
end

def puts(contents = nil)
print contents if contents
print "\n"
Expand Down
72 changes: 51 additions & 21 deletions src/test_runner.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Mint
class Message
include JSON::Serializable

property id : String
property type : String
property name : String?
property suite : String?
Expand All @@ -30,9 +31,10 @@ module Mint
@artifacts : TypeChecker::Artifacts?
@reporter : Reporter
@browser_path : String?
@script : String?
@browser : {Process, String}?

@failed = [] of Message
@test_id = Random::Secure.hex
@succeeded = 0

def initialize(@flags : Cli::Test::Flags, @arguments : Cli::Test::Arguments)
Expand All @@ -46,15 +48,38 @@ module Mint
@succeeded = 0
end

def run : Bool
MintJson.parse_current.check_dependencies!
def watch
workspace = Workspace.current
workspace.on "change" { run_tests }
workspace.watch

ast = terminal.measure "#{COG} Compiling tests..." do
compile_ast.tap do |a|
@script = compile_script(a)
end
setup_kemal
run_tests

Server.run "Test", @flags.host, @flags.port, false
end

def run_tests
@test_id = Random::Secure.hex
cleanup_browser
terminal.reset

begin
type_checker = TypeChecker.new(ast)
type_checker.check

@reporter.reset
open_page
rescue error : Error
terminal.reset
terminal.puts error.to_terminal
rescue error
terminal.reset
terminal.puts error.to_s
end
end

def run : Bool
if ast.try(&.suites.empty?)
terminal.puts
terminal.puts "There are no tests to run!"
Expand All @@ -74,7 +99,7 @@ module Mint
@failed.empty?
end

def compile_ast
def ast
file_argument =
@arguments.test

Expand All @@ -96,7 +121,7 @@ module Mint
end
end

def compile_script(ast)
def script
type_checker = TypeChecker.new(ast)
type_checker.check

Expand Down Expand Up @@ -186,15 +211,21 @@ module Mint

begin
process = open_process(browser_path, profile_directory)
at_exit do
process.signal(:kill) rescue nil
FileUtils.rm_rf(profile_directory)
end
@browser = {process, profile_directory}
at_exit { cleanup_browser }
@channel.receive
ensure
process.try &.signal(:kill) rescue nil
cleanup_browser
end
end

def cleanup_browser
@browser.try do |(process, profile_directory)|
process.signal(:kill) rescue nil
FileUtils.rm_rf(profile_directory)
end

@browser = nil
end

def open_page
Expand All @@ -206,9 +237,6 @@ module Mint
ws_url =
"ws://#{@flags.browser_host}:#{@flags.browser_port}/"

page_source =
ECR.render("#{__DIR__}/test_runner.ecr")

runtime =
if runtime_path = @flags.runtime
Cli.runtime_file_not_found(runtime_path) unless File.exists?(runtime_path)
Expand All @@ -219,7 +247,7 @@ module Mint

get "/" do
reset
page_source
ECR.render("#{__DIR__}/test_runner.ecr")
end

get "/external-javascripts.js" do |env|
Expand Down Expand Up @@ -258,7 +286,7 @@ module Mint
end

get "/tests" do
@script
script
end

ws "/" do |socket|
Expand All @@ -277,6 +305,8 @@ module Mint
end

def handle_message(data : Message) : Nil
return unless data.id == @test_id

case data.type
when "LOG"
terminal.puts data.result
Expand All @@ -296,7 +326,7 @@ module Mint
@failed << data

@reporter.done
stop_server
stop_server unless @flags.watch
end
end

Expand Down Expand Up @@ -335,7 +365,7 @@ module Mint
end
end

stop_server
stop_server unless @flags.watch
end

def stop_server
Expand Down
3 changes: 3 additions & 0 deletions src/test_runner.ecr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<script src="/external-javascripts.js"></script>
<script src="/runtime.js"></script>
<script>
window.TEST_ID = "<%= @test_id %>";
class TestRunner {
constructor () {
this.socket = new WebSocket("<%= ws_url %>")
Expand Down Expand Up @@ -66,6 +68,7 @@
message = message.toString()
}
this.socket.send(JSON.stringify({
id: window.TEST_ID,
type: type,
suite: suite,
name: name,
Expand Down
3 changes: 3 additions & 0 deletions src/test_runner/documentation_reporter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ module Mint
def done
end

def reset
end

def crashed(message)
puts "❗ An internal error occurred while executing a test: #{message}".colorize(:red)
end
Expand Down
4 changes: 4 additions & 0 deletions src/test_runner/dot_reporter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ module Mint
puts
end

def reset
@count = 0
end

def crashed(message)
puts
puts "❗ An internal error occurred while executing a test: #{message}".colorize(:red)
Expand Down
1 change: 1 addition & 0 deletions src/test_runner/reporter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Mint
abstract def errored(name, error)
abstract def suite(name)
abstract def done
abstract def reset
abstract def crashed(message)
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/utils/server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ module Mint
true
end

def run(name, host = "127.0.0.1", port = 3000)
def run(name, host = "127.0.0.1", port = 3000, verbose = true)
config = Kemal.config
config.logging = false
config.setup

if port_open?(host, port)
server = HTTP::Server.new(config.handlers)
terminal.puts "#{COG} #{name} server started on http://#{host}:#{port}/"
terminal.puts "#{COG} #{name} server started on http://#{host}:#{port}/" if verbose
elsif STDIN.tty?
new_port = config.port + 1
until port_open?(host, new_port)
Expand Down

0 comments on commit cda6761

Please sign in to comment.