Skip to content
This repository
Browse code

Tests tag the Rails log with the current test class and test case

  • Loading branch information...
commit 86ebe0bd36e03b7b7531d818f65cacdb03766abe 1 parent 107fd87
Jeremy Kemper authored September 26, 2012
7  activesupport/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,12 @@
1 1
 ## Rails 4.0.0 (unreleased) ##
2 2
 
  3
+*   Tests tag the Rails log with the current test class and test case:
  4
+
  5
+        [SessionsControllerTest] [test_0002_sign in] Processing by SessionsController#create as HTML
  6
+        [SessionsControllerTest] [test_0002_sign in] ...
  7
+
  8
+    *Jeremy Kemper*
  9
+
3 10
 *   Add logger.push_tags and .pop_tags to complement logger.tagged:
4 11
 
5 12
         class Job
2  activesupport/lib/active_support/test_case.rb
... ...
@@ -1,5 +1,6 @@
1 1
 gem 'minitest' # make sure we get the gem, not stdlib
2 2
 require 'minitest/spec'
  3
+require 'active_support/testing/tagged_logging'
3 4
 require 'active_support/testing/setup_and_teardown'
4 5
 require 'active_support/testing/assertions'
5 6
 require 'active_support/testing/deprecation'
@@ -33,6 +34,7 @@ def self.test_order # :nodoc:
33 34
       :sorted
34 35
     end
35 36
 
  37
+    include ActiveSupport::Testing::TaggedLogging
36 38
     include ActiveSupport::Testing::SetupAndTeardown
37 39
     include ActiveSupport::Testing::Assertions
38 40
     include ActiveSupport::Testing::Deprecation
30  activesupport/lib/active_support/testing/tagged_logging.rb
... ...
@@ -0,0 +1,30 @@
  1
+require 'active_support/concern'
  2
+
  3
+module ActiveSupport
  4
+  module Testing
  5
+    module TaggedLogging
  6
+      extend ActiveSupport::Concern
  7
+
  8
+      attr_writer :tagged_logger
  9
+
  10
+      def before_setup
  11
+        tagged_logger.push_tags(self.class.name, __name__) if tagged_logging?
  12
+        super
  13
+      end
  14
+
  15
+      def after_teardown
  16
+        super
  17
+        tagged_logger.pop_tags(2) if tagged_logging?
  18
+      end
  19
+
  20
+      private
  21
+        def tagged_logger
  22
+          @tagged_logger ||= (defined?(Rails.logger) && Rails.logger)
  23
+        end
  24
+
  25
+        def tagged_logging?
  26
+          tagged_logger && tagged_logger.respond_to?(:push_tags)
  27
+        end
  28
+    end
  29
+  end
  30
+end
15  activesupport/test/test_test.rb
@@ -171,3 +171,18 @@ def sentinel
171 171
       assert_equal [:foo, :bar, :bar, :foo], @called_back
172 172
     end
173 173
 end
  174
+
  175
+
  176
+class TestCaseTaggedLoggingTest < ActiveSupport::TestCase
  177
+  def before_setup
  178
+    require 'stringio'
  179
+    @out = StringIO.new
  180
+    self.tagged_logger = ActiveSupport::TaggedLogging.new(Logger.new(@out))
  181
+    super
  182
+  end
  183
+
  184
+  def test_logs_tagged_with_current_test_case
  185
+    tagged_logger.info 'test'
  186
+    assert_equal "[#{self.class.name}] [#{__name__}] test\n", @out.string
  187
+  end
  188
+end

0 notes on commit 86ebe0b

Aaron Patterson

Do we need this? (I mean the AS::Concern part)

Please sign in to comment.
Something went wrong with that request. Please try again.