Skip to content
Browse files

Move trace heavy lifting to its own module.

  • Loading branch information...
1 parent 410858b commit 699b78dd3ee94688f688a4519deeaf5dd951d912 @jimweirich committed Nov 22, 2012
Showing with 66 additions and 48 deletions.
  1. +1 −0 lib/rake.rb
  2. +3 −7 lib/rake/application.rb
  3. +19 −0 lib/rake/trace_output.rb
  4. +0 −41 test/test_rake_application.rb
  5. +43 −0 test/test_trace_output.rb
View
1 lib/rake.rb
@@ -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'
View
10 lib/rake/application.rb
@@ -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
@@ -17,6 +18,7 @@ module Rake
#
class Application
include TaskManager
+ include TraceOutput
# The name of the application (typically 'rake')
attr_reader :name
@@ -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)
View
19 lib/rake/trace_output.rb
@@ -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
View
41 test/test_rake_application.rb
@@ -1,5 +1,4 @@
require File.expand_path('../helper', __FILE__)
-require 'stringio'
class TestRakeApplication < Rake::TestCase
@@ -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
View
43 test/test_trace_output.rb
@@ -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.
Something went wrong with that request. Please try again.