Skip to content

Commit ba95021

Browse files
tonytactran
authored andcommitted
Redirect error messages to current standard error stream (ctran#528)
1 parent 7c7fef3 commit ba95021

File tree

5 files changed

+38
-38
lines changed

5 files changed

+38
-38
lines changed

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ require 'mg'
2828
begin
2929
MG.new('annotate.gemspec')
3030
rescue Exception
31-
STDERR.puts("WARNING: Couldn't read gemspec. As such, a number of tasks may be unavailable to you until you run 'rake gem:gemspec' to correct the issue.")
31+
$stderr.puts("WARNING: Couldn't read gemspec. As such, a number of tasks may be unavailable to you until you run 'rake gem:gemspec' to correct the issue.")
3232
# Gemspec is probably in a broken state, so let's give ourselves a chance to
3333
# build a new one...
3434
end

lib/annotate.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def self.bootstrap_rake
169169
require 'rake/dsl_definition'
170170
rescue StandardError => e
171171
# We might just be on an old version of Rake...
172-
puts e.message
172+
$stderr.puts e.message
173173
exit e.status_code
174174
end
175175
require 'rake'

lib/annotate/annotate_models.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,8 @@ def annotate(klass, file, header, options = {})
640640
end
641641
end
642642
rescue StandardError => e
643-
puts "Unable to annotate #{file}: #{e.message}"
644-
puts "\t" + e.backtrace.join("\n\t") if options[:trace]
643+
$stderr.puts "Unable to annotate #{file}: #{e.message}"
644+
$stderr.puts "\t" + e.backtrace.join("\n\t") if options[:trace]
645645
end
646646

647647
annotated
@@ -676,9 +676,9 @@ def get_model_files(options)
676676

677677
model_files
678678
rescue SystemCallError
679-
puts "No models found in directory '#{model_dir.join("', '")}'."
680-
puts "Either specify models on the command line, or use the --model-dir option."
681-
puts "Call 'annotate --help' for more info."
679+
$stderr.puts "No models found in directory '#{model_dir.join("', '")}'."
680+
$stderr.puts "Either specify models on the command line, or use the --model-dir option."
681+
$stderr.puts "Call 'annotate --help' for more info."
682682
exit 1
683683
end
684684

@@ -786,12 +786,12 @@ def annotate_model_file(annotated, file, header, options)
786786
annotated.concat(annotate(klass, file, header, options)) if do_annotate
787787
rescue BadModelFileError => e
788788
unless options[:ignore_unknown_models]
789-
puts "Unable to annotate #{file}: #{e.message}"
790-
puts "\t" + e.backtrace.join("\n\t") if options[:trace]
789+
$stderr.puts "Unable to annotate #{file}: #{e.message}"
790+
$stderr.puts "\t" + e.backtrace.join("\n\t") if options[:trace]
791791
end
792792
rescue StandardError => e
793-
puts "Unable to annotate #{file}: #{e.message}"
794-
puts "\t" + e.backtrace.join("\n\t") if options[:trace]
793+
$stderr.puts "Unable to annotate #{file}: #{e.message}"
794+
$stderr.puts "\t" + e.backtrace.join("\n\t") if options[:trace]
795795
end
796796
end
797797

@@ -821,8 +821,8 @@ def remove_annotations(options = {})
821821
end
822822
deannotated << klass if deannotated_klass
823823
rescue StandardError => e
824-
puts "Unable to deannotate #{File.join(file)}: #{e.message}"
825-
puts "\t" + e.backtrace.join("\n\t") if options[:trace]
824+
$stderr.puts "Unable to deannotate #{File.join(file)}: #{e.message}"
825+
$stderr.puts "\t" + e.backtrace.join("\n\t") if options[:trace]
826826
end
827827
end
828828
puts "Removed annotations from: #{deannotated.join(', ')}"

spec/annotate/annotate_models_spec.rb

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,22 +1676,22 @@ class User < ActiveRecord::Base
16761676
EOS
16771677
end
16781678

1679-
it 'displays an error message' do
1680-
expect(capturing(:stdout) do
1679+
it 'displays just the error message with trace disabled (default)' do
1680+
error_output = capturing(:stderr) do
16811681
AnnotateModels.do_annotations model_dir: @model_dir, is_rake: true
1682-
end).to include("Unable to annotate #{@model_dir}/user.rb: oops")
1683-
end
1682+
end
16841683

1685-
it 'displays the full stack trace with --trace' do
1686-
expect(capturing(:stdout) do
1687-
AnnotateModels.do_annotations model_dir: @model_dir, trace: true, is_rake: true
1688-
end).to include('/spec/annotate/annotate_models_spec.rb:')
1684+
expect(error_output).to include("Unable to annotate #{@model_dir}/user.rb: oops")
1685+
expect(error_output).not_to include('/spec/annotate/annotate_models_spec.rb:')
16891686
end
16901687

1691-
it 'omits the full stack trace without --trace' do
1692-
expect(capturing(:stdout) do
1693-
AnnotateModels.do_annotations model_dir: @model_dir, trace: false, is_rake: true
1694-
end).not_to include('/spec/annotate/annotate_models_spec.rb:')
1688+
it 'displays the error message and stacktrace with trace enabled' do
1689+
error_output = capturing(:stderr) do
1690+
AnnotateModels.do_annotations model_dir: @model_dir, is_rake: true, trace: true
1691+
end
1692+
1693+
expect(error_output).to include("Unable to annotate #{@model_dir}/user.rb: oops")
1694+
expect(error_output).to include('/spec/annotate/annotate_models_spec.rb:')
16951695
end
16961696
end
16971697

@@ -1706,22 +1706,22 @@ class User < ActiveRecord::Base
17061706
EOS
17071707
end
17081708

1709-
it 'displays an error message' do
1710-
expect(capturing(:stdout) do
1709+
it 'displays just the error message with trace disabled (default)' do
1710+
error_output = capturing(:stderr) do
17111711
AnnotateModels.remove_annotations model_dir: @model_dir, is_rake: true
1712-
end).to include("Unable to deannotate #{@model_dir}/user.rb: oops")
1713-
end
1712+
end
17141713

1715-
it 'displays the full stack trace' do
1716-
expect(capturing(:stdout) do
1717-
AnnotateModels.remove_annotations model_dir: @model_dir, trace: true, is_rake: true
1718-
end).to include("/user.rb:2:in `<class:User>'")
1714+
expect(error_output).to include("Unable to deannotate #{@model_dir}/user.rb: oops")
1715+
expect(error_output).not_to include("/user.rb:2:in `<class:User>'")
17191716
end
17201717

1721-
it 'omits the full stack trace without --trace' do
1722-
expect(capturing(:stdout) do
1723-
AnnotateModels.remove_annotations model_dir: @model_dir, trace: false, is_rake: true
1724-
end).not_to include("/user.rb:2:in `<class:User>'")
1718+
it 'displays the error message and stacktrace with trace enabled' do
1719+
error_output = capturing(:stderr) do
1720+
AnnotateModels.remove_annotations model_dir: @model_dir, is_rake: true, trace: true
1721+
end
1722+
1723+
expect(error_output).to include("Unable to deannotate #{@model_dir}/user.rb: oops")
1724+
expect(error_output).to include("/user.rb:2:in `<class:User>'")
17251725
end
17261726
end
17271727
end

spec/integration/rails_2.3_with_bundler/config/boot.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def load_rails_gem
6363
end
6464
rescue Gem::LoadError => load_error
6565
if load_error.message =~ /Could not find RubyGem rails/
66-
STDERR.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
66+
$stderr.puts "Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed."
6767
exit 1
6868
else
6969
raise

0 commit comments

Comments
 (0)