rubytest 0.9.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,144 @@
1
+ # encoding: UTF-8
2
+
3
+ module Test::Reporters
4
+
5
+ # HTML Test Reporter
6
+ #
7
+ # Writes the run as one self-contained HTML document, once the run has
8
+ # finished: a tally, then every result under the heading of its case.
9
+ # Test output is captured and shown with the test that wrote it.
10
+ #
11
+ class Html < Abstract
12
+
13
+ LABELS = {
14
+ :pass => 'PASS',
15
+ :fail => 'FAIL',
16
+ :error => 'ERROR',
17
+ :todo => 'TODO',
18
+ :skip => 'SKIP'
19
+ }
20
+
21
+ ESCAPES = {
22
+ '&' => '&amp;', '<' => '&lt;', '>' => '&gt;', '"' => '&quot;', "'" => '&#39;'
23
+ }
24
+
25
+ STYLE = <<-CSS
26
+ :root { color-scheme: light dark; --pass: #1a7f37; --fail: #cf222e; --todo: #9a6700; --skip: #57606a; --line: #d0d7de; }
27
+ @media (prefers-color-scheme: dark) { :root { --pass: #3fb950; --fail: #f85149; --todo: #d29922; --skip: #8b949e; --line: #30363d; } }
28
+ body { font-family: system-ui, sans-serif; line-height: 1.5; max-width: 60rem; margin: 2rem auto; padding: 0 1rem; }
29
+ h1 { margin-bottom: 0.25rem; }
30
+ .tally { font-weight: bold; }
31
+ .tally.passed { color: var(--pass); }
32
+ .tally.failed { color: var(--fail); }
33
+ section { border-left: 2px solid var(--line); padding-left: 1rem; margin: 1rem 0; }
34
+ .result { margin: 0.25rem 0; }
35
+ .status { display: inline-block; min-width: 3.5rem; font: bold 0.85em ui-monospace, monospace; }
36
+ .pass .status { color: var(--pass); }
37
+ .fail .status, .error .status { color: var(--fail); }
38
+ .todo .status { color: var(--todo); }
39
+ .skip .status { color: var(--skip); }
40
+ .time, .reason, footer { color: var(--skip); font-size: 0.85em; }
41
+ pre { margin: 0.25rem 0 0.5rem 3.5rem; padding: 0.5rem; border: 1px solid var(--line); overflow-x: auto; font-size: 0.85em; }
42
+ CSS
43
+
44
+ #
45
+ def initialize(runner, output: $stdout)
46
+ super(runner)
47
+ @output = output
48
+ @body = []
49
+ @depth = 0
50
+ end
51
+
52
+ # Test output would otherwise land in the middle of the document.
53
+ def capture_output?
54
+ true
55
+ end
56
+
57
+ #
58
+ def begin_suite(suite)
59
+ @body = []
60
+ @depth = 0
61
+ end
62
+
63
+ #
64
+ def begin_case(test_case)
65
+ title, *rest = test_case.to_s.strip.split("\n")
66
+ level = [@depth + 2, 6].min
67
+ @body << %[<section class="case">]
68
+ @body << %[<h#{level}>#{h title}</h#{level}>]
69
+ @body << %[<p>#{rest.map{ |line| h(line.strip) }.join('<br>')}</p>] unless rest.empty?
70
+ @depth += 1
71
+ end
72
+
73
+ #
74
+ def end_case(test_case)
75
+ @depth -= 1
76
+ @body << %[</section>]
77
+ end
78
+
79
+ # Add a completed result to the document.
80
+ def record(result)
81
+ label = result.test.to_s.strip.split("\n").first.to_s
82
+ label = "#{label} (case)" if result.kind == :case
83
+
84
+ line = %[<span class="status">#{LABELS.fetch(result.status)}</span> #{h label}]
85
+ line << %[ <span class="reason">#{h result.reason}</span>] if result.status == :skip && result.reason
86
+ line << %[ <span class="reason">#{h result.exception.message}</span>] if result.status == :todo && result.exception
87
+ line << %[ <span class="time">#{'%.5fs' % result.elapsed}</span>]
88
+
89
+ @body << %[<div class="result #{result.status}">#{line}]
90
+ if [:fail, :error].include?(result.status)
91
+ result.exceptions.each{ |exception| @body << %[<pre>#{h detail(exception, result.status == :error)}</pre>] }
92
+ end
93
+ @body << %[<pre class="stdout">#{h result.stdout}</pre>] unless result.stdout.empty?
94
+ @body << %[<pre class="stderr">#{h result.stderr}</pre>] unless result.stderr.empty?
95
+ @body << %[</div>]
96
+ end
97
+
98
+ # Write the document.
99
+ def finish(summary)
100
+ outcome = summary.success? ? 'passed' : 'failed'
101
+ tally = if summary.total.zero?
102
+ 'No tests were run.'
103
+ else
104
+ counts = ::Test::Result::STATUSES.map{ |status| "#{summary.counts[status]} #{TITLES[status]}" }
105
+ "#{summary.total} tests: #{counts.join(', ')}."
106
+ end
107
+
108
+ @output.puts '<!DOCTYPE html>'
109
+ @output.puts '<html lang="en">'
110
+ @output.puts '<head>'
111
+ @output.puts '<meta charset="utf-8">'
112
+ @output.puts '<meta name="viewport" content="width=device-width, initial-scale=1">'
113
+ @output.puts '<title>Test Report</title>'
114
+ @output.puts "<style>\n#{STYLE.gsub(/^ /, '')}</style>"
115
+ @output.puts '</head>'
116
+ @output.puts '<body>'
117
+ @output.puts '<h1>Test Report</h1>'
118
+ @output.puts %[<p class="tally #{outcome}">#{h tally}</p>]
119
+ @output.puts @body.join("\n") unless @body.empty?
120
+ @output.puts %[<footer>Finished in #{'%.5fs' % summary.elapsed}. Generated by Rubytest #{h ::Test::VERSION} on #{h Time.now.strftime('%Y-%m-%d %H:%M:%S')}.</footer>]
121
+ @output.puts '</body>'
122
+ @output.puts '</html>'
123
+ end
124
+
125
+ private
126
+
127
+ # Escape text for use in HTML.
128
+ def h(text)
129
+ text.to_s.gsub(/[&<>"']/, ESCAPES)
130
+ end
131
+
132
+ # An exception's class, message and where it was raised, with the rest
133
+ # of its backtrace if asked.
134
+ def detail(exception, backtrace=false)
135
+ lines = ["#{exception.class}: #{exception.message}"]
136
+ trace = exception.backtrace ? clean_backtrace(exception) : []
137
+ trace = trace.first(1) unless backtrace
138
+ lines.concat(trace.map{ |line| " #{line}" })
139
+ lines.join("\n")
140
+ end
141
+
142
+ end
143
+
144
+ end
@@ -0,0 +1,44 @@
1
+ require_relative 'dotprogress'
2
+
3
+ module Test::Reporters
4
+ # Nest case headings and show each result at its place in the suite.
5
+ class Outline < Dotprogress
6
+ SYMBOLS = {
7
+ pass: '.', fail: 'F', error: 'E', todo: 'P', skip: 'S'
8
+ }.freeze
9
+
10
+ def begin_suite(suite)
11
+ @depth = 0
12
+ end
13
+
14
+ def begin_case(test_case)
15
+ @output.puts "#{' ' * @depth}#{test_case.to_s.lines.first.to_s.strip}"
16
+ @depth += 1
17
+ end
18
+
19
+ def end_case(test_case)
20
+ @depth -= 1
21
+ end
22
+
23
+ def record(result)
24
+ depth = result.kind == :case && @depth.positive? ? @depth - 1 : @depth
25
+ indent = ' ' * depth
26
+ @output.puts "#{indent}#{SYMBOLS.fetch(result.status)} #{result.test.to_s.lines.first.to_s.strip}"
27
+ if [:fail, :error].include?(result.status)
28
+ result.exceptions.each do |exception|
29
+ @output.puts "#{indent} #{exception.class}: #{exception.message}"
30
+ location = file_and_line(exception)
31
+ @output.puts "#{indent} #{location}" unless location.empty?
32
+ end
33
+ end
34
+ end
35
+
36
+ def end_suite(suite)
37
+ summary = @summary
38
+ @output.puts
39
+ rate = summary.elapsed.zero? ? 0.0 : summary.total / summary.elapsed
40
+ @output.puts "Finished in %.5fs, %.2f tests/s." % [summary.elapsed, rate]
41
+ @output.puts summary.total.zero? ? 'No tests were run.' : tally_for(summary)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'dotprogress'
2
+
3
+ module Test::Reporters
4
+ # Show completed results and timing without enumerating the suite in advance.
5
+ class Progress < Dotprogress
6
+ SYMBOLS = {
7
+ pass: '.', fail: 'F', error: 'E', todo: 'P', skip: 'S'
8
+ }.freeze
9
+
10
+ def begin_suite(suite)
11
+ @completed = 0
12
+ @started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
13
+ @depth = 0
14
+ end
15
+
16
+ def begin_case(test_case)
17
+ @output.puts "#{' ' * @depth}#{test_case.to_s.lines.first.to_s.strip}"
18
+ @depth += 1
19
+ end
20
+
21
+ def end_case(test_case)
22
+ @depth -= 1
23
+ end
24
+
25
+ def record(result)
26
+ @completed += 1
27
+ elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - @started_at
28
+ depth = result.kind == :case && @depth.positive? ? @depth - 1 : @depth
29
+ label = "#{' ' * depth}#{result.test.to_s.lines.first.to_s.strip}"
30
+ @output.puts "%4d %8.3fs %8.5fs %s %s" % [
31
+ @completed, elapsed, result.elapsed, SYMBOLS.fetch(result.status), label
32
+ ]
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,31 @@
1
+ require_relative 'dotprogress'
2
+
3
+ module Test::Reporters
4
+ # One status line per result, followed by failure details and totals.
5
+ class Summary < Dotprogress
6
+ LABELS = {
7
+ pass: 'PASS', fail: 'FAIL', error: 'ERROR',
8
+ todo: 'TODO', skip: 'SKIP'
9
+ }.freeze
10
+
11
+ def begin_suite(suite)
12
+ @case_path = []
13
+ end
14
+
15
+ def begin_case(test_case)
16
+ @case_path << test_case.to_s.lines.first.to_s.strip
17
+ end
18
+
19
+ def end_case(test_case)
20
+ @case_path.pop
21
+ end
22
+
23
+ def record(result)
24
+ label = result.test.to_s.lines.first.to_s.strip
25
+ path = @case_path.dup
26
+ path.pop if result.kind == :case && path.last == label
27
+ path << label unless label.empty?
28
+ @output.puts "#{LABELS.fetch(result.status).ljust(5)} #{path.join(' ')}"
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,54 @@
1
+ module Test::Reporters
2
+ # TAP version 13 stream. The plan follows execution so case outcomes count.
3
+ class Tap < Abstract
4
+ def initialize(runner, output: $stdout)
5
+ super(runner)
6
+ @output = output
7
+ end
8
+
9
+ def capture_output?
10
+ true
11
+ end
12
+
13
+ def begin_suite(suite)
14
+ @index = 0
15
+ @output.puts 'TAP version 13'
16
+ end
17
+
18
+ def record(result)
19
+ @index += 1
20
+ label = single_line(result.test.to_s)
21
+ line = "#{[:pass, :skip].include?(result.status) ? 'ok' : 'not ok'} #{@index} - #{label}"
22
+ case result.status
23
+ when :skip
24
+ line << " # SKIP #{single_line(result.reason)}"
25
+ when :todo
26
+ line << " # TODO #{single_line(result.exception&.message)}"
27
+ end
28
+ @output.puts line.rstrip
29
+
30
+ if [:fail, :error].include?(result.status)
31
+ result.exceptions.each do |exception|
32
+ diagnostic("#{exception.class}: #{exception.message}")
33
+ diagnostic(exception.backtrace&.first) if exception.backtrace&.first
34
+ end
35
+ end
36
+ result.stdout.each_line { |line| diagnostic("stdout: #{line}") }
37
+ result.stderr.each_line { |line| diagnostic("stderr: #{line}") }
38
+ end
39
+
40
+ def finish(summary)
41
+ @output.puts summary.total.zero? ? '1..0 # SKIP No tests were run' : "1..#{summary.total}"
42
+ end
43
+
44
+ private
45
+
46
+ def single_line(value)
47
+ value.to_s.gsub(/\s+/, ' ').strip.gsub('#', '#')
48
+ end
49
+
50
+ def diagnostic(message)
51
+ message.to_s.each_line { |line| @output.puts "# #{line.chomp}" }
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,76 @@
1
+ # encoding: UTF-8
2
+
3
+ module Test::Reporters
4
+
5
+ # TAP-J Reporter
6
+ #
7
+ # Writes the run as a TAP-J stream (revision 5): a line of JSON for
8
+ # the suite, each case, each test and the final tally.
9
+ #
10
+ class Tapj < AbstractHash
11
+
12
+ REVISION = 5
13
+
14
+ #
15
+ def initialize(runner, output: $stdout)
16
+ require 'json'
17
+ super(runner)
18
+ @output = output
19
+ end
20
+
21
+ #
22
+ def begin_suite(suite)
23
+ emit super(suite).merge('rev' => REVISION)
24
+ end
25
+
26
+ #
27
+ def begin_case(test_case)
28
+ emit super(test_case)
29
+ end
30
+
31
+ #
32
+ def pass(test)
33
+ emit super(test)
34
+ end
35
+
36
+ #
37
+ def fail(test, exception)
38
+ emit super(test, exception)
39
+ end
40
+
41
+ #
42
+ def error(test, exception)
43
+ emit super(test, exception)
44
+ end
45
+
46
+ #
47
+ def todo(test, exception)
48
+ emit super(test, exception)
49
+ end
50
+
51
+ #
52
+ def skip_test(test, reason=nil)
53
+ emit super(test, reason)
54
+ end
55
+
56
+ #
57
+ def skip_case(test_case, reason=nil)
58
+ emit super(test_case, reason)
59
+ end
60
+
61
+ #
62
+ def end_suite(suite)
63
+ emit super(suite)
64
+ end
65
+
66
+ private
67
+
68
+ # Write one record to the stream.
69
+ def emit(hash)
70
+ @output.puts hash.to_json
71
+ hash
72
+ end
73
+
74
+ end
75
+
76
+ end
@@ -0,0 +1,77 @@
1
+ # encoding: UTF-8
2
+
3
+ module Test::Reporters
4
+
5
+ # TAP-Y Reporter
6
+ #
7
+ # Writes the run as a TAP-Y stream (revision 5): a YAML document for
8
+ # the suite, each case, each test and the final tally.
9
+ #
10
+ class Tapy < AbstractHash
11
+
12
+ REVISION = 5
13
+
14
+ #
15
+ def initialize(runner, output: $stdout)
16
+ require 'yaml'
17
+ super(runner)
18
+ @output = output
19
+ end
20
+
21
+ #
22
+ def begin_suite(suite)
23
+ emit super(suite).merge('rev' => REVISION)
24
+ end
25
+
26
+ #
27
+ def begin_case(test_case)
28
+ emit super(test_case)
29
+ end
30
+
31
+ #
32
+ def pass(test)
33
+ emit super(test)
34
+ end
35
+
36
+ #
37
+ def fail(test, exception)
38
+ emit super(test, exception)
39
+ end
40
+
41
+ #
42
+ def error(test, exception)
43
+ emit super(test, exception)
44
+ end
45
+
46
+ #
47
+ def todo(test, exception)
48
+ emit super(test, exception)
49
+ end
50
+
51
+ #
52
+ def skip_test(test, reason=nil)
53
+ emit super(test, reason)
54
+ end
55
+
56
+ #
57
+ def skip_case(test_case, reason=nil)
58
+ emit super(test_case, reason)
59
+ end
60
+
61
+ #
62
+ def end_suite(suite)
63
+ emit super(suite)
64
+ @output.puts '...'
65
+ end
66
+
67
+ private
68
+
69
+ # Write one record to the stream.
70
+ def emit(hash)
71
+ @output.puts hash.to_yaml
72
+ hash
73
+ end
74
+
75
+ end
76
+
77
+ end
@@ -0,0 +1,128 @@
1
+ require 'rubytest'
2
+ require 'rake'
3
+ require 'rake/tasklib'
4
+
5
+ module Test
6
+
7
+ ##
8
+ # Rake subspace.
9
+ #
10
+ module Rake
11
+
12
+ ##
13
+ # Define a rake task that runs tests.
14
+ #
15
+ # require 'rubytest/rake'
16
+ #
17
+ # Test::Rake::TestTask.new do |run|
18
+ # run.loadpath 'lib'
19
+ # run.files << 'test/*_test.rb'
20
+ # end
21
+ #
22
+ # The block is given the task's Test::Config, the same object that
23
+ # `Test.configure` yields. Without any files, the task runs whichever
24
+ # of DEFAULT_TESTS match, and without a load path it adds `lib`.
25
+ #
26
+ # The `TEST` environment variable selects the tests to run for one
27
+ # invocation, in place of the task's own: `rake test TEST=test/foo_test.rb`.
28
+ # Separate several files or globs with `:` or `;`.
29
+ #
30
+ # Where the platform can fork, tests run in a child process, so that
31
+ # what they load doesn't stay in Rake's. If the run is unsuccessful the
32
+ # task exits with status 1.
33
+ #
34
+ class TestTask < ::Rake::TaskLib
35
+
36
+ # Globs used to find test files when the task is given none.
37
+ DEFAULT_TESTS = [
38
+ 'test/**/case_*.rb',
39
+ 'test/**/*_case.rb',
40
+ 'test/**/test_*.rb',
41
+ 'test/**/*_test.rb'
42
+ ]
43
+
44
+ # Name of the task.
45
+ #
46
+ # @return [String]
47
+ attr :name
48
+
49
+ # Test run configuration.
50
+ #
51
+ # @return [Config]
52
+ attr :config
53
+
54
+ # Initialize new Rake::TestTask instance.
55
+ #
56
+ def initialize(name='test', desc='run tests', &block)
57
+ @name = (name || 'test').to_s
58
+ @desc = desc
59
+
60
+ @config = Test::Config.new
61
+
62
+ block.call(@config) if block
63
+
64
+ define_task
65
+ end
66
+
67
+ # Define rake task for testing.
68
+ #
69
+ # @return nothing
70
+ def define_task
71
+ desc @desc
72
+ task @name do
73
+ run
74
+ end
75
+ end
76
+
77
+ # Run tests, in a child process if possible, otherwise in this one.
78
+ # Exits with status 1 if the run is unsuccessful.
79
+ #
80
+ # @return nothing
81
+ def run
82
+ prepare
83
+ success = Process.respond_to?(:fork) ? run_in_fork : run_here
84
+ exit 1 unless success
85
+ end
86
+
87
+ private
88
+
89
+ # Settle the files and load path the run will use.
90
+ def prepare
91
+ if ENV['TEST']
92
+ config.files = ENV['TEST'].split(/[:;]/)
93
+ elsif config.files.empty?
94
+ config.files = default_tests
95
+ end
96
+ config.loadpath << 'lib' if config.loadpath.empty?
97
+ end
98
+
99
+ # The default globs that match at least one file. The runner fails a
100
+ # glob that matches nothing, so the others are left out.
101
+ #
102
+ # @return [Array<String>]
103
+ def default_tests
104
+ base = config.chdir || Dir.pwd
105
+ globs = DEFAULT_TESTS.reject{ |glob| Dir.glob(glob, base: base).empty? }
106
+ if globs.empty?
107
+ raise ArgumentError, "no test files found; looked for #{DEFAULT_TESTS.join(', ')}"
108
+ end
109
+ globs
110
+ end
111
+
112
+ # @return [Boolean] Whether the run was successful.
113
+ def run_here
114
+ Test::Runner.new(config).run
115
+ end
116
+
117
+ # @return [Boolean] Whether the run was successful.
118
+ def run_in_fork
119
+ pid = fork { exit(run_here ? 0 : 1) }
120
+ Process.wait(pid)
121
+ $?.success?
122
+ end
123
+
124
+ end
125
+
126
+ end
127
+
128
+ end
@@ -1,53 +1,60 @@
1
1
  module Test
2
-
3
- # Recorder class is an observer that tracks all tests
4
- # that are run and categorizes them according to their
5
- # test status.
2
+ # Owns run results and controls when they reach the reporter.
6
3
  class Recorder
4
+ attr_reader :results, :reporter, :summary
7
5
 
8
- def initialize
9
- @table = Hash.new{ |h,k| h[k] = [] }
6
+ def initialize(reporter = nil)
7
+ @reporter = reporter
8
+ @results = []
10
9
  end
11
10
 
12
- def [](key)
13
- @table[key.to_sym]
11
+ def begin_suite(suite)
12
+ @suite = suite
13
+ @started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
14
+ reporter.begin_suite(suite) if reporter&.respond_to?(:begin_suite)
14
15
  end
15
16
 
16
- #
17
- def skip_test(test, reason)
18
- self[:skip] << [test, reason]
17
+ def begin_case(test_case)
18
+ reporter.begin_case(test_case) if reporter&.respond_to?(:begin_case)
19
19
  end
20
20
 
21
- # Add `test` to pass set.
22
- def pass(test)
23
- self[:pass] << test
21
+ def begin_test(test)
22
+ reporter.begin_test(test) if reporter&.respond_to?(:begin_test)
24
23
  end
25
24
 
26
- def fail(test, exception)
27
- self[:fail] << [test, exception]
25
+ def capture_output?
26
+ !!(reporter && reporter.respond_to?(:capture_output?) && reporter.capture_output?)
28
27
  end
29
28
 
30
- def error(test, exception)
31
- self[:error] << [test, exception]
29
+ def record(result)
30
+ @results << result
31
+ reporter.record(result) if reporter
32
+ result
32
33
  end
33
34
 
34
- def todo(test, exception)
35
- self[:todo] << [test, exception]
35
+ def end_test(test)
36
+ reporter.end_test(test) if reporter&.respond_to?(:end_test)
36
37
  end
37
38
 
38
- #def omit(test, exception)
39
- # self[:omit] << [test, exception]
40
- #end
39
+ def end_case(test_case)
40
+ reporter.end_case(test_case) if reporter&.respond_to?(:end_case)
41
+ end
41
42
 
42
- # Returns true if their are no test errors or failures.
43
- def success?
44
- self[:error].size + self[:fail].size > 0 ? false : true
43
+ def end_suite(suite)
44
+ @summary = RunSummary.new(suite: suite, results: results, elapsed: elapsed)
45
+ reporter.finish(@summary) if reporter
46
+ @summary
45
47
  end
46
48
 
47
- # Ignore any other signals.
48
- def method_missing(*a)
49
+ def success?
50
+ (@summary || RunSummary.new(suite: @suite, results: results, elapsed: elapsed)).success?
49
51
  end
50
52
 
51
- end
53
+ private
52
54
 
55
+ def elapsed
56
+ return 0.0 unless @started_at
57
+ Process.clock_gettime(Process::CLOCK_MONOTONIC) - @started_at
58
+ end
59
+ end
53
60
  end