Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: f1998de803bc
Choose a base ref
...
head repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 8387828f0dfe
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Mar 3, 2017

  1. Change spec junit formatter to use XML::Builder

    RX14 authored and Ary Borenszweig committed Mar 3, 2017
    Copy the full SHA
    5957f02 View commit details
  2. Add junit_output option to Makefile

    RX14 authored and Ary Borenszweig committed Mar 3, 2017
    Copy the full SHA
    8387828 View commit details
Showing with 73 additions and 67 deletions.
  1. +10 −9 Makefile
  2. +26 −25 spec/std/spec/junit_formatter_spec.cr
  3. +37 −33 src/spec/junit_formatter.cr
19 changes: 10 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -13,17 +13,18 @@

LLVM_CONFIG ?= ## llvm-config command path to use

release ?= ## Compile in release mode
stats ?= ## Enable statistics output
threads ?= ## Maximum number of threads to use
debug ?= ## Add symbolic debug info
verbose ?= ## Run specs in verbose mode
release ?= ## Compile in release mode
stats ?= ## Enable statistics output
threads ?= ## Maximum number of threads to use
debug ?= ## Add symbolic debug info
verbose ?= ## Run specs in verbose mode
junit_output ?= ## Directory to output junit results

O := .build
SOURCES := $(shell find src -name '*.cr')
SPEC_SOURCES := $(shell find spec -name '*.cr')
FLAGS := $(if $(release),--release )$(if $(stats),--stats )$(if $(threads),--threads $(threads) )$(if $(debug),-d )
VERBOSE := $(if $(verbose),-v )
SPEC_FLAGS := $(if $(verbose),-v )$(if $(junit_output),--junit_output $(junit_output) )
EXPORTS := $(if $(release),,CRYSTAL_CONFIG_PATH=`pwd`/src)
SHELL = bash
LLVM_CONFIG_FINDER := \
@@ -73,15 +74,15 @@ help: ## Show this help

.PHONY: spec
spec: $(O)/all_spec ## Run all specs
$(O)/all_spec $(VERBOSE)
$(O)/all_spec $(SPEC_FLAGS)

.PHONY: std_spec
std_spec: $(O)/std_spec ## Run standard library specs
$(O)/std_spec $(VERBOSE)
$(O)/std_spec $(SPEC_FLAGS)

.PHONY: compiler_spec
compiler_spec: $(O)/compiler_spec ## Run compiler specs
$(O)/compiler_spec $(VERBOSE)
$(O)/compiler_spec $(SPEC_FLAGS)

.PHONY: doc
doc: ## Generate standard library documentation
51 changes: 26 additions & 25 deletions spec/std/spec/junit_formatter_spec.cr
Original file line number Diff line number Diff line change
@@ -8,15 +8,14 @@ describe "JUnit Formatter" do
end

expected = <<-XML
<?xml version="1.0"?>
<testsuite tests="2" errors="0" failed="0">
<testcase file=\"spec/some_spec.cr\" classname=\"spec.some_spec\" name="should do something">
</testcase>
<testcase file=\"spec/some_spec.cr\" classname=\"spec.some_spec\" name="should do something else">
</testcase>
<testcase file=\"spec/some_spec.cr\" classname=\"spec.some_spec\" name="should do something"/>
<testcase file=\"spec/some_spec.cr\" classname=\"spec.some_spec\" name="should do something else"/>
</testsuite>
XML

output.should eq(expected)
output.chomp.should eq(expected)
end

it "reports failures" do
@@ -25,14 +24,15 @@ describe "JUnit Formatter" do
end

expected = <<-XML
<?xml version="1.0"?>
<testsuite tests="1" errors="0" failed="1">
<testcase file=\"spec/some_spec.cr\" classname=\"spec.some_spec\" name="should do something">
<failure />
</testcase>
<testcase file=\"spec/some_spec.cr\" classname=\"spec.some_spec\" name="should do something">
<failure/>
</testcase>
</testsuite>
XML

output.should eq(expected)
output.chomp.should eq(expected)
end

it "reports errors" do
@@ -41,14 +41,15 @@ describe "JUnit Formatter" do
end

expected = <<-XML
<?xml version="1.0"?>
<testsuite tests="1" errors="1" failed="0">
<testcase file=\"spec/some_spec.cr\" classname=\"spec.some_spec\" name="should do something">
<error />
</testcase>
<testcase file=\"spec/some_spec.cr\" classname=\"spec.some_spec\" name="should do something">
<error/>
</testcase>
</testsuite>
XML

output.should eq(expected)
output.chomp.should eq(expected)
end

it "reports mixed results" do
@@ -60,22 +61,22 @@ describe "JUnit Formatter" do
end

expected = <<-XML
<?xml version="1.0"?>
<testsuite tests="4" errors="2" failed="1">
<testcase file=\"spec/some_spec.cr\" classname=\"spec.some_spec\" name="should do something1">
</testcase>
<testcase file=\"spec/some_spec.cr\" classname=\"spec.some_spec\" name="should do something2">
<failure />
</testcase>
<testcase file=\"spec/some_spec.cr\" classname=\"spec.some_spec\" name="should do something3">
<error />
</testcase>
<testcase file=\"spec/some_spec.cr\" classname=\"spec.some_spec\" name="should do something4">
<error />
</testcase>
<testcase file=\"spec/some_spec.cr\" classname=\"spec.some_spec\" name="should do something1"/>
<testcase file=\"spec/some_spec.cr\" classname=\"spec.some_spec\" name="should do something2">
<failure/>
</testcase>
<testcase file=\"spec/some_spec.cr\" classname=\"spec.some_spec\" name="should do something3">
<error/>
</testcase>
<testcase file=\"spec/some_spec.cr\" classname=\"spec.some_spec\" name="should do something4">
<error/>
</testcase>
</testsuite>
XML

output.should eq(expected)
output.chomp.should eq(expected)
end

it "escapes spec names" do
70 changes: 37 additions & 33 deletions src/spec/junit_formatter.cr
Original file line number Diff line number Diff line change
@@ -27,14 +27,20 @@ module Spec

def finish
io = @output
io << "<testsuite tests=\"#{@results.size}\" \
errors=\"#{@summary[:error]? || 0}\" \
failed=\"#{@summary[:fail]? || 0}\">\n"

@results.each { |r| write_report(r, io) }
XML.build(io, indent: 2) do |xml|
attributes = {
tests: @results.size,
errors: @summary[:error]? || 0,
failed: @summary[:fail]? || 0,
}

io << "</testsuite>"
io.close
xml.element("testsuite", attributes) do
@results.each { |r| write_report(r, xml) }
end
end
ensure
io.try &.close
end

def self.file(output_dir)
@@ -44,46 +50,44 @@ module Spec
JUnitFormatter.new(file)
end

# -------- private utility methods
private def write_report(result, io)
io << "<testcase file=\"#{result.file}\" classname=\"#{classname(result)}\" name=\"#{XML.escape(result.description)}\">\n"

if (has_inner_content(result.kind))
tag = inner_content_tag(result.kind)
ex = result.exception
if ex
write_inner_content(tag, ex, io)
else
io << "<#{tag} />\n"
private def write_report(result, xml)
attributes = {
file: result.file,
classname: classname(result),
name: result.description,
}

xml.element("testcase", attributes) do
if tag = inner_content_tag(result.kind)
if ex = result.exception
write_inner_content(tag, ex, xml)
else
xml.element(tag)
end
end
end

io << "</testcase>\n"
end

private def has_inner_content(kind)
kind == :fail || kind == :error
end

private def inner_content_tag(kind)
case kind
when :error
:error
"error"
when :fail
:failure
"failure"
end
end

private def write_inner_content(tag, exception, io)
m = exception.message
if m
io << "<#{tag} message=\"#{XML.escape(m)}\">"
private def write_inner_content(tag, exception, xml)
if message = exception.message
attributes = {message: message}
else
io << "<#{tag}>"
attributes = NamedTuple.new
end

xml.element(tag, attributes) do
backtrace = exception.backtrace? || Array(String).new
xml.text backtrace.join('\n')
end
backtrace = exception.backtrace? || ([] of String)
io << XML.escape(backtrace.join("\n"))
io << "</#{tag}>\n"
end

private def classname(result)