Module: TestSilencer
- Defined in:
- lib/test_silencer.rb
Overview
Module to provide test-aware warning functionality
Class Method Summary collapse
-
.abort_unless_testing(message) ⇒ Object
Silently abort during tests by raising SystemExit without printing the message.
-
.running_tests? ⇒ Boolean
Helper method to detect if we’re running in a test environment.
-
.setup_warning_suppression ⇒ Object
Suppress Ruby warnings during tests.
-
.warn_unless_testing(message) ⇒ Object
Generic warn method that silences output during tests.
Class Method Details
.abort_unless_testing(message) ⇒ Object
Silently abort during tests by raising SystemExit without printing the message
22 23 24 25 26 27 28 29 30 |
# File 'lib/test_silencer.rb', line 22 def self.abort_unless_testing() if running_tests? # During tests, raise SystemExit without printing the error message raise SystemExit.new(1) else # In production, use normal abort which prints the message and exits abort end end |
.running_tests? ⇒ Boolean
Helper method to detect if we’re running in a test environment
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/test_silencer.rb', line 4 def self.running_tests? # Check for common test environment indicators @running_tests ||= begin caller.any? { |line| line.include?('/test/') || line.include?('minitest') || line.include?('rspec') } || defined?(Minitest) || ENV['RAILS_ENV'] == 'test' || ENV['RACK_ENV'] == 'test' || ($PROGRAM_NAME.include?('rake') && ARGV.include?('test')) || $PROGRAM_NAME.include?('rake_test_loader') end end |
.setup_warning_suppression ⇒ Object
Suppress Ruby warnings during tests
33 34 35 36 37 38 39 40 |
# File 'lib/test_silencer.rb', line 33 def self.setup_warning_suppression if running_tests? # Temporarily reduce verbosity during tests original_verbose = $VERBOSE $VERBOSE = nil at_exit { $VERBOSE = original_verbose } end end |
.warn_unless_testing(message) ⇒ Object
Generic warn method that silences output during tests
17 18 19 |
# File 'lib/test_silencer.rb', line 17 def self.warn_unless_testing() warn unless running_tests? end |