Skip to content

Commit

Permalink
Improve highlighting
Browse files Browse the repository at this point in the history
- Changed dark blue in reporter to light blue
- Highlight errors headline in error list reporter
- Highlight test method in backtraces
  • Loading branch information
Ragmaanir committed Oct 18, 2023
1 parent f27ddb4 commit 28de06f
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 29 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

### 1.2.7

- Changed dark blue in reporter to light blue
- Highlight errors headline in error list reporter
- Highlight test method in backtraces

### 1.2.6

- Crystal 1.10.0-dev (no fixes needed)
Expand Down
4 changes: 2 additions & 2 deletions spec/backtrace_printer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe Microtest::BacktracePrinter do

printer = Microtest::BacktracePrinter.new

pretty_trace = printer.call(raw_trace, false)
pretty_trace = printer.call(raw_trace, colorize: false)

assert pretty_trace == <<-BACKTRACE
┏ CRY: /crystal/main.cr:119 main
Expand All @@ -69,6 +69,6 @@ describe Microtest::BacktracePrinter do
test "raise when path in backtrace could not be classified" do
e = generate_exception
printer = Microtest::BacktracePrinter.new
printer.call(e.backtrace, false)
printer.call(e.backtrace, colorize: false)
end
end
2 changes: 1 addition & 1 deletion spec/reporters_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe Microtest::Reporter do
output = uncolor(result.stdout)

assert output.matches?(%r{skip: skip this one in SPEC: spec/test.cr:16})
assert output.matches?(%r{# 1 MicrotestTest#error \nunexpected\n┏})
assert output.matches?(%r{# 1 MicrotestTest#error SPEC: spec/test.cr:12\nunexpected\n┏})
assert output.matches?(%r{# 2 MicrotestTest#failure SPEC: spec/test.cr:\d+\n◆ assert 3 > 5})
end

Expand Down
9 changes: 6 additions & 3 deletions src/microtest/backtrace_printer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module Microtest
{kind, simple_path}
end

def call(backtrace : Array(String), colorize : Bool) : String
def call(backtrace : Array(String), highlight : String? = nil, colorize : Bool = true) : String
entries = simplify(backtrace)

list = entries.map do |entry|
Expand All @@ -65,9 +65,12 @@ module Microtest

t.w(":", entry.line.to_s, " ", fg: :dark_gray)

m = Colorize::Mode::None
m = Colorize::Mode::Bold if highlight && entry.func.includes?(highlight)

case entry.kind
when :app, :spec then t.w(entry.func, fg: :yellow)
else t.w(entry.func, fg: :dark_gray)
when :app, :spec then t.w(entry.func, fg: :yellow, m: m)
else t.w(entry.func, fg: :dark_gray, m: m)
end
}
end
Expand Down
25 changes: 16 additions & 9 deletions src/microtest/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,16 @@ class ReleaseCommand
def call
run("crystal", ["spec"])

run("./bin/ameba")
# TODO: make it pass
# run("./bin/ameba")
run("./cli", ["readme"])
run("git", ["add", "README.md"])
run("git", ["add", "-A", "./assets"])

run("git", ["status"])
print "🔬 Does commit look ok? [y/n/yes/no]: "
confirm("Did you update CHANGLEOG.md?")

while a = gets.not_nil!.strip.downcase
case a
when "y", "yes" then break
when "n", "no" then exit
end
end
run("git", ["status"])
confirm("Does commit look ok?")

version_name = "v#{Microtest::VERSION}"

Expand All @@ -75,6 +71,17 @@ class ReleaseCommand
run("git", ["push", "gh", version_name])
end

def self.confirm(msg : String)
print "#{msg} [y/n/yes/no]: "

while a = gets.not_nil!.strip.downcase
case a
when "y", "yes" then break
when "n", "no" then exit
end
end
end

def run(cmd, args = [] of String, msg = "Command failed: #{cmd} #{args.join(" ")}")
puts "Running: #{cmd} #{args.join(" ")}"

Expand Down
16 changes: 12 additions & 4 deletions src/microtest/dsl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,17 @@ module Microtest

macro test(name = nil, *args, __filename = __FILE__, __line_number = __LINE__, **options, &block)
{%
raise "Test name cant be empty in #{__filename.id}:#{__line_number}" if name == ""
file = __filename
line = __line_number

name = "unnamed_in_line_#{__line_number}" if name == nil
if block
file = block.filename
line = block.line_number
end

raise "Test name cant be empty in #{file}:#{line}" if name == ""

name = "unnamed_in_line_#{line}" if name == nil

sanitized_name = name.gsub(/[^a-zA-Z0-9_]/, "_")
method_name = "test__#{sanitized_name.id}"
Expand All @@ -58,8 +66,8 @@ module Microtest
method_name: {{method_name}},
focus: {{focus}},
skip: {{skip}},
# __filename: {{__filename}},
# __line_number: {{__line_number}},
filename: {{file}},
line_number: {{line}},
) do |m, ctx|
test = new(ctx)
test.run_test(m) { test.{{ method_name.id }} }
Expand Down
25 changes: 16 additions & 9 deletions src/microtest/reporters.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ module Microtest
}
end

private def exception_to_string(ex : Exception) : String
private def exception_to_string(ex : Exception, highlight : String? = nil) : String
String.build { |io|
io << ex.message.colorize(:red)
io << "\n"

if b = ex.backtrace?
io << BacktracePrinter.new.call(b, true)
io << BacktracePrinter.new.call(b, highlight)
else
io << "(no backtrace)"
end
Expand Down Expand Up @@ -140,17 +140,22 @@ module Microtest

private def print_failure(number : Int32, failure : TestFailure)
ex = failure.exception
test = failure.test
bold = Colorize::Mode::Bold

write("# %-3d" % (number + 1), failure.test.full_name, " ", fg: :red)
write("# %-3d" % (number + 1), test.full_name, " ", fg: :red, m: bold)

case ex
when AssertionFailure
write(BacktracePrinter.simplify_path(ex.file)[1], ":", ex.line, fg: :dark_gray)
path = BacktracePrinter.simplify_path(ex.file)[1]
write(path, ":", ex.line, fg: :light_gray, m: bold)
br
writeln(ex.message)
when UnexpectedError
path = BacktracePrinter.simplify_path(test.filename)[1]
write(path, ":", test.line_number, fg: :light_gray, m: bold)
br
writeln(exception_to_string(ex.exception))
writeln(exception_to_string(ex.exception, test.method_name))
else Microtest.bug("Invalid Exception")
end

Expand All @@ -174,9 +179,11 @@ module Microtest
write(" ")
end

write("Executed", fg: :blue)
write(" #{ctx.executed_tests}/#{ctx.total_tests} ", fg: (ctx.executed_tests < ctx.total_tests) ? :red : :blue)
write("tests in #{total}#{unit} with seed #{ctx.random_seed}", fg: :blue)
fg = :light_blue

write("Executed", fg: fg)
write(" #{ctx.executed_tests}/#{ctx.total_tests} ", fg: (ctx.executed_tests < ctx.total_tests) ? :red : fg)
write("tests in #{total}#{unit} with seed #{ctx.random_seed}", fg: fg)
br

write("Success: ", ctx.total_success, fg: (:green if ctx.total_success > 0))
Expand Down Expand Up @@ -221,7 +228,7 @@ module Microtest
num, unit = Formatter.format_duration(threshold)
writeln("No slow tests (threshold: #{num}#{unit})", fg: :dark_gray)
else
writeln("Slowest #{res.size} tests", fg: :blue)
writeln("Slowest #{res.size} tests", fg: :light_blue)
br

res.each do |r|
Expand Down
4 changes: 3 additions & 1 deletion src/microtest/test_method.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ module Microtest
getter method_name : String
getter focus : Bool | String
getter skip : Bool
getter filename : String
getter line_number : Int32
getter block : (TestMethod, ExecutionContext) ->

def initialize(@suite, @name, @sanitized_name, @method_name, @focus, @skip, &@block : (TestMethod, ExecutionContext) ->)
def initialize(@suite, @name, @sanitized_name, @method_name, @focus, @skip, @filename, @line_number, &@block : (TestMethod, ExecutionContext) ->)
end

def focus?
Expand Down

0 comments on commit 28de06f

Please sign in to comment.