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 all 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
118 changes: 118 additions & 0 deletions spec/std/spec/list_tags_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
require "./spec_helper"

describe Spec do
describe "list_tags" do
it "lists the count of all tags", tags: %w[slow] do
compile_and_run_source(<<-CRYSTAL, flags: %w(--no-debug), runtime_args: %w(--list-tags))[1].lines.should eq <<-OUT.lines
require "spec"

it "untagged #1" do
end
it "untagged #2" do
end
it "untagged #3" do
end

it "slow #1", tags: "slow" do
end
it "slow #2", tags: "slow" do
end

it "untagged #4" do
end

it "flakey #1", tags: "flakey" do
end
it "flakey #2, slow #3", tags: ["flakey", "slow"] do
end

it "untagged #5" do
end

pending "untagged #6"

pending "untagged #7" do
end

pending "slow #5", tags: "slow"

describe "describe specs", tags: "describe" do
it "describe #1" do
end
it "describe #2" do
end
it "describe #3, slow #4", tags: "slow" do
end
it "describe #4, flakey #3", tags: "flakey" do
end
end
CRYSTAL
untagged: 7
slow: 5
describe: 4
flakey: 3
OUT
end

it "lists the count of slow tags", tags: %w[slow] do
compile_and_run_source(<<-CRYSTAL, flags: %w(--no-debug), runtime_args: %w(--list-tags --tag slow))[1].lines.should eq <<-OUT.lines
require "spec"

it "untagged #1" do
end
it "untagged #2" do
end
it "untagged #3" do
end

it "slow #1", tags: "slow" do
end
it "slow #2", tags: "slow" do
end

it "untagged #4" do
end

it "flakey #1", tags: "flakey" do
end
it "flakey #2, slow #3", tags: ["flakey", "slow"] do
end

it "untagged #5" do
end

pending "untagged #6"

pending "untagged #7" do
end

pending "slow #5", tags: "slow"

describe "describe specs", tags: "describe" do
it "describe #1" do
end
it "describe #2" do
end
it "describe #3, slow #4", tags: "slow" do
end
it "describe #4, flakey #3", tags: "flakey" do
end
end
CRYSTAL
slow: 5
describe: 1
flakey: 1
OUT
end

it "does nothing if there are no examples", tags: %w[slow] do
compile_and_run_source(<<-CRYSTAL, flags: %w(--no-debug), runtime_args: %w(--list-tags))[1].lines.should eq <<-OUT.lines
require "spec"

describe "describe specs", tags: "describe" do
end
CRYSTAL
OUT
end
end
end
8 changes: 4 additions & 4 deletions spec/std/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,19 @@ def compile_source(source, flags = %w(), file = __FILE__, &)
end
end

def compile_and_run_file(source_file, flags = %w(), file = __FILE__)
def compile_and_run_file(source_file, flags = %w(), runtime_args = %w(), file = __FILE__)
compile_file(source_file, flags: flags, file: file) do |executable_file|
output, error = IO::Memory.new, IO::Memory.new
status = Process.run executable_file, output: output, error: error
status = Process.run executable_file, args: runtime_args, output: output, error: error

{status, output.to_s, error.to_s}
end
end

def compile_and_run_source(source, flags = %w(), file = __FILE__)
def compile_and_run_source(source, flags = %w(), runtime_args = %w(), file = __FILE__)
with_tempfile("source_file", file: file) do |source_file|
File.write(source_file, source)
compile_and_run_file(source_file, flags, file: file)
compile_and_run_file(source_file, flags, runtime_args, file: file)
end
end

Expand Down
6 changes: 6 additions & 0 deletions src/spec/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ module Spec
# :nodoc:
class_property? dry_run = false

# :nodoc:
class_property? list_tags = false

# :nodoc:
def self.add_location(file, line)
locations = @@locations ||= {} of String => Array(Int32)
Expand Down Expand Up @@ -86,6 +89,9 @@ 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
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
60 changes: 55 additions & 5 deletions src/spec/dsl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,71 @@ module Spec
next unless status == 0

begin
log_setup
maybe_randomize
run_filters
root_context.run
if Spec.list_tags?
execute_list_tags
else
execute_examples
end
rescue ex
STDERR.print "Unhandled exception: "
ex.inspect_with_backtrace(STDERR)
STDERR.flush
@@aborted = true
ensure
finish_run
finish_run unless Spec.list_tags?
end
end
end

# :nodoc:
def self.execute_examples
log_setup
maybe_randomize
run_filters
root_context.run
end

# :nodoc:
def self.execute_list_tags
run_filters
tag_counts = collect_tags(root_context)
print_list_tags(tag_counts)
end

private def self.collect_tags(context) : Hash(String, Int32)
tag_counts = Hash(String, Int32).new(0)
collect_tags(tag_counts, context, Set(String).new)
tag_counts
end

private def self.collect_tags(tag_counts, context : Context, tags)
if context.responds_to?(:tags) && (context_tags = context.tags)
tags += context_tags
end
context.children.each do |child|
collect_tags(tag_counts, child, tags)
end
end

private def self.collect_tags(tag_counts, example : Example, tags)
if example_tags = example.tags
tags += example_tags
end
if tags.empty?
tag_counts.update("untagged") { |count| count + 1 }
else
tags.tally(tag_counts)
end
end

private def self.print_list_tags(tag_counts : Hash(String, Int32)) : Nil
return if tag_counts.empty?
longest_name_size = tag_counts.keys.max_of(&.size)
tag_counts.to_a.sort_by! { |k, v| {-v, k} }.each do |tag_name, count|
puts "#{tag_name.rjust(longest_name_size)}: #{count}"
end
end

# :nodoc:
#
# Workaround for #8914
Expand Down