Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add crystal spec --list-tags #13616

Merged
merged 17 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def Spec.configure_formatter(formatter, output_path = nil)
when "junit"
junit_formatter = Spec::JUnitFormatter.file(Path.new(output_path.not_nil!))
Spec.add_formatter(junit_formatter)
when "list_tags"
Spec.override_default_formatter(Spec::ListTagsFormatter.new)
when "verbose"
Spec.override_default_formatter(Spec::VerboseFormatter.new)
when "tap"
Expand Down
7 changes: 7 additions & 0 deletions src/spec/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ module Spec
# :nodoc:
class_property? focus = false

# :nodoc:
class_property? list_tags = false

# :nodoc:
def self.add_location(file, line)
locations = @@locations ||= {} of String => Array(Int32)
Expand Down Expand Up @@ -84,6 +87,10 @@ module Spec
opts.on("--tag TAG", "run examples with the specified TAG, or exclude examples by adding ~ before the TAG.") do |tag|
Spec.add_tag tag
end
opts.on("--list-tags", "lists all the tags used.") do
Spec.list_tags = true
configure_formatter("list_tags")
end
opts.on("--order MODE", "run examples in random order by passing MODE as 'random' or to a specific seed by passing MODE as the seed value") do |mode|
if mode.in?("default", "random")
Spec.order = mode
Expand Down
9 changes: 8 additions & 1 deletion src/spec/context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Spec
abstract class Context
# All the children, which can be `describe`/`context` or `it`
getter children = [] of ExampleGroup | Example
getter tag_counts = Hash(String, Int32).new(0)

def randomize(randomizer)
children.each do |child|
Expand Down Expand Up @@ -180,8 +181,14 @@ module Spec
end

def print_results(elapsed_time, aborted = false)
if Spec.list_tags?
longest_name = tag_counts.keys.map { |tag_name| tag_name.size }.max
straight-shoota marked this conversation as resolved.
Show resolved Hide resolved
tag_counts.to_a.sort_by { |k, v| -v }.each do |tag_name, count|
straight-shoota marked this conversation as resolved.
Show resolved Hide resolved
puts "#{tag_name.rjust(longest_name)}: #{count}"
end
end
pendings = results_for(:pending)
unless pendings.empty?
unless pendings.empty? || Spec.list_tags?
puts
puts "Pending:"
pendings.each do |pending|
Expand Down
18 changes: 16 additions & 2 deletions src/spec/example.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,22 @@ module Spec
non_nil_block = block
start = Time.monotonic

ran = @parent.run_around_each_hooks(Example::Procsy.new(self) { internal_run(start, non_nil_block) })
ran || internal_run(start, non_nil_block)
if Spec.list_tags?
tags = all_tags
tags << "untagged" if tags.empty?
context = self.parent
while !context.nil?
straight-shoota marked this conversation as resolved.
Show resolved Hide resolved
tags.each do |tag|
context.tag_counts[tag] += 1
end
break if !context.responds_to?(:parent)
context = context.parent
end
@parent.report(:success, description, file, line, Time.monotonic - start)
else
ran = @parent.run_around_each_hooks(Example::Procsy.new(self) { internal_run(start, non_nil_block) })
ran || internal_run(start, non_nil_block)
end

# We do this to give a chance for signals (like CTRL+C) to be handled,
# which currently are only handled when there's a fiber switch
Expand Down
6 changes: 6 additions & 0 deletions src/spec/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ module Spec
end
end

# :nodoc:
class ListTagsFormatter < DotFormatter
def report(result)
end
end

@@formatters = [Spec::DotFormatter.new] of Spec::Formatter

# :nodoc:
Expand Down