Skip to content

Commit

Permalink
Move trace heavy lifting to its own module.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimweirich committed Nov 21, 2012
1 parent 410858b commit 699b78d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 48 deletions.
1 change: 1 addition & 0 deletions lib/rake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
require 'rake/task_argument_error'
require 'rake/rule_recursion_overflow_error'
require 'rake/rake_module'
require 'rake/trace_output'
require 'rake/pseudo_status'
require 'rake/task_arguments'
require 'rake/invocation_chain'
Expand Down
10 changes: 3 additions & 7 deletions lib/rake/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'rake/file_list'
require 'rake/thread_pool'
require 'rake/thread_history_display'
require 'rake/trace_output'
require 'rake/win32'

module Rake
Expand All @@ -17,6 +18,7 @@ module Rake
#
class Application
include TaskManager
include TraceOutput

# The name of the application (typically 'rake')
attr_reader :name
Expand Down Expand Up @@ -316,13 +318,7 @@ def display_prerequisites

def trace(*strings)
options.trace_output ||= $stderr
sep = $\ || "\n"
if strings.empty?
output = sep
else
output = strings.map { |s| s.end_with?(sep) ? s : s + sep }.join
end
options.trace_output.print(output)
trace_on(options.trace_output, *strings)
end

def sort_options(options)
Expand Down
19 changes: 19 additions & 0 deletions lib/rake/trace_output.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Rake
module TraceOutput

# Write trace output to output stream +out+.
#
# The write is done as a single IO call (to print) to lessen the
# chance that the trace output is interrupted by other tasks also
# producing output.
def trace_on(out, *strings)
sep = $\ || "\n"
if strings.empty?
output = sep
else
output = strings.map { |s| s.end_with?(sep) ? s : s + sep }.join
end
out.print(output)
end
end
end
41 changes: 0 additions & 41 deletions test/test_rake_application.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require File.expand_path('../helper', __FILE__)
require 'stringio'

class TestRakeApplication < Rake::TestCase

Expand Down Expand Up @@ -341,46 +340,6 @@ def test_handle_options_trace_does_not_eat_following_task_names
assert @app.options.trace
end

class PrintSpy
attr_reader :result, :calls
def initialize
@result = ""
@calls = 0
end
def print(string)
@result << string
@calls += 1
end
end

def test_trace_issues_single_io_for_args_with_empty_args
spy = PrintSpy.new
@app.options.trace_output = spy
@app.trace
assert_equal "\n", spy.result
assert_equal 1, spy.calls
end

def test_trace_issues_single_io_for_args_multiple_strings
spy = PrintSpy.new
@app.options.trace_output = spy
@app.trace("HI\n", "LO")
assert_equal "HI\nLO\n", spy.result
assert_equal 1, spy.calls
end

def test_trace_issues_single_io_for_args_multiple_strings_and_alternate_sep
old_sep = $\
$\ = "\r"
spy = PrintSpy.new
@app.options.trace_output = spy
@app.trace("HI\r", "LO")
assert_equal "HI\rLO\r", spy.result
assert_equal 1, spy.calls
ensure
$\ = old_sep
end

def test_good_run
ran = false

Expand Down
43 changes: 43 additions & 0 deletions test/test_trace_output.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require File.expand_path('../helper', __FILE__)
require 'stringio'

class TestTraceOutput < Rake::TestCase
include Rake::TraceOutput

class PrintSpy
attr_reader :result, :calls
def initialize
@result = ""
@calls = 0
end
def print(string)
@result << string
@calls += 1
end
end

def test_trace_issues_single_io_for_args_with_empty_args
spy = PrintSpy.new
trace_on(spy)
assert_equal "\n", spy.result
assert_equal 1, spy.calls
end

def test_trace_issues_single_io_for_args_multiple_strings
spy = PrintSpy.new
trace_on(spy, "HI\n", "LO")
assert_equal "HI\nLO\n", spy.result
assert_equal 1, spy.calls
end

def test_trace_issues_single_io_for_args_multiple_strings_and_alternate_sep
old_sep = $\
$\ = "\r"
spy = PrintSpy.new
trace_on(spy, "HI\r", "LO")
assert_equal "HI\rLO\r", spy.result
assert_equal 1, spy.calls
ensure
$\ = old_sep
end
end

0 comments on commit 699b78d

Please sign in to comment.