Skip to content

Commit

Permalink
Add some colors to text formatter output
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 20, 2013
1 parent db6d77f commit 010c01c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 75 deletions.
5 changes: 2 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Bundler.require
require 'opal/rspec/rake_task'
Opal::RSpec::RakeTask.new(:default)

desc "Build opal-rspec/rspec.js"
desc "Build opal/opal/rspec/rspec.js"
task :build do
Opal::Processor.dynamic_require_severity = :warning
Opal.append_path 'app'
Expand All @@ -15,7 +15,7 @@ task :build do
code = Opal.process('rspec-builder')
min = uglify code

puts "\nDev: #{code.size}, min: #{min.size}"
puts "\ndevelopment: #{code.size}, minified: #{min.size}"

File.open('opal/opal/rspec/rspec.js', 'w+') do |out|
out << code
Expand All @@ -32,4 +32,3 @@ rescue Errno::ENOENT
$stderr.puts '"uglifyjs" command not found (install with: "npm install -g uglify-js")'
nil
end

1 change: 0 additions & 1 deletion opal/opal-rspec.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
require 'opal/rspec'

4 changes: 1 addition & 3 deletions opal/opal/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
require 'dir'
require 'thread'

# production
# vendor a pre-built rspec
require 'opal/rspec/rspec'
# development
# require 'rspec-builder'

require 'opal/rspec/fixes'
require 'opal/rspec/text_formatter'
Expand Down
14 changes: 0 additions & 14 deletions opal/opal/rspec/fixes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,3 @@ def underscore(camel_cased_word)
word
end
end

# These two methods break because of instance_variables(). That method should ignore
# private variables added by opal. This breaks as we copy ._klass which makes these
# collections think they are arrays as we copy the _klass property from an array
#
# OR:
#
# it breaks because we copy all methods from array, and dont have our real send,
# __send__ and class methods. This is more likely
class RSpec::Core::Hooks::HookCollection
`def.$send = Opal.Kernel.$send`
`def.$__send__ = Opal.Kernel.$__send__`
`def.$class = Opal.Kernel.$class`
end
74 changes: 38 additions & 36 deletions opal/opal/rspec/runner.rb
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
module Opal; module RSpec
class Runner
module Opal
module RSpec
class Runner

class << self
def browser?
`typeof(document) !== "undefined"`
end
class << self
def browser?
`typeof(document) !== "undefined"`
end

def phantom?
`typeof(phantom) !== 'undefined' || typeof(OPAL_SPEC_PHANTOM) !== 'undefined'`
end
def phantom?
`typeof(phantom) !== 'undefined' || typeof(OPAL_SPEC_PHANTOM) !== 'undefined'`
end

def default_formatter
if phantom?
TextFormatter
else # browser
BrowserFormatter
def default_formatter
if phantom?
TextFormatter
else # browser
BrowserFormatter
end
end
end

def autorun
if browser?
`setTimeout(function() { #{Runner.new.run} }, 0)`
else # phantom
Runner.new.run
def autorun
if browser?
`setTimeout(function() { #{Runner.new.run} }, 0)`
else # phantom
Runner.new.run
end
end
end
end

def initialize(options={}, configuration=::RSpec::configuration, world=::RSpec::world)
@options = options
@configuration = configuration
@world = world
end
def initialize(options={}, configuration=::RSpec::configuration, world=::RSpec::world)
@options = options
@configuration = configuration
@world = world
end

def run(err=$stdout, out=$stdout)
@configuration.error_stream = err
@configuration.output_stream ||= out
def run(err=$stdout, out=$stdout)
@configuration.error_stream = err
@configuration.output_stream ||= out

@configuration.reporter.report(@world.example_count) do |reporter|
begin
@configuration.run_hook(:before, :suite)
@world.example_groups.map {|g| g.run(reporter) }.all? ? 0 : @configuration.failure_exit_code
ensure
@configuration.run_hook(:after, :suite)
@configuration.reporter.report(@world.example_count) do |reporter|
begin
@configuration.run_hook(:before, :suite)
@world.example_groups.map {|g| g.run(reporter) }.all? ? 0 : @configuration.failure_exit_code
ensure
@configuration.run_hook(:after, :suite)
end
end
end
end
end
end; end
end
47 changes: 29 additions & 18 deletions opal/opal/rspec/text_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ module RSpec
class TextFormatter < ::RSpec::Core::Formatters::BaseFormatter

def dump_failures
return if failed_examples.empty?
puts "\n"
puts "Failures:"
failed_examples.each_with_index do |example, index|
puts "\n"
dump_failure(example, index)
if failed_examples.empty?
puts "\nFinished"
else
puts "\nFailures:"
failed_examples.each_with_index do |example, index|
puts "\n"
dump_failure(example, index)
end
end
end

Expand All @@ -20,16 +22,8 @@ def dump_failure(example, index)
def dump_failure_info(example)
exception = example.execution_result[:exception]
exception_class_name = exception.class.name.to_s
puts "#{long_padding}#{exception_class_name}:"
exception.message.to_s.split("\n").each { |line| puts "#{long_padding} #{line}" }
end

def short_padding
' '
end

def long_padding
' '
red "#{long_padding}#{exception_class_name}:"
exception.message.to_s.split("\n").each { |line| red "#{long_padding} #{line}" }
end

def dump_summary(duration, example_count, failure_count, pending_count)
Expand All @@ -38,12 +32,13 @@ def dump_summary(duration, example_count, failure_count, pending_count)
@failure_count = failure_count
@pending_count = pending_count

puts "\nFinished\n"
puts "#{example_count} examples, #{failure_count} failures (time taken: #{duration})"
msg = "\n\n#{example_count} examples, #{failure_count} failures (time taken: #{duration})"

if failure_count == 0
green msg
finish_with_code(0)
else
red msg
finish_with_code(1)
end
end
Expand All @@ -58,6 +53,22 @@ def finish_with_code(code)
}
}
end

def green(str)
`console.log('\\033[32m' + str + '\\033[0m')`
end

def red(str)
`console.log('\\033[31m' + str + '\\033[0m')`
end

def short_padding
' '
end

def long_padding
' '
end
end
end
end

0 comments on commit 010c01c

Please sign in to comment.