diff options
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG.md | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/callbacks.rb | 8 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/module/delegation.rb | 38 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/uri.rb | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/log_subscriber.rb | 6 | ||||
-rw-r--r-- | activesupport/lib/active_support/logger.rb | 35 | ||||
-rw-r--r-- | activesupport/lib/active_support/ordered_options.rb | 5 | ||||
-rw-r--r-- | activesupport/lib/active_support/tagged_logging.rb | 78 | ||||
-rw-r--r-- | activesupport/lib/active_support/test_case.rb | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/testing/isolation.rb | 10 | ||||
-rw-r--r-- | activesupport/lib/active_support/testing/pending.rb | 20 | ||||
-rw-r--r-- | activesupport/test/broadcast_logger_test.rb | 82 | ||||
-rw-r--r-- | activesupport/test/callbacks_test.rb | 56 | ||||
-rw-r--r-- | activesupport/test/core_ext/uri_ext_test.rb | 8 | ||||
-rw-r--r-- | activesupport/test/log_subscriber_test.rb | 4 |
15 files changed, 246 insertions, 110 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index c339e93808..02a989db22 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,5 +1,7 @@ ## Rails 4.0.0 (unreleased) ## +* Remove ActiveSupport::TestCase#pending method, use `skip` instead. *Carlos Antonio da Silva* + * Deprecates the compatibility method Module#local_constant_names, use Module#local_constants instead (which returns symbols). *fxn* diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index d4a9b93ff2..5eaeac2cb3 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -357,9 +357,9 @@ module ActiveSupport # calculating which callbacks can be omitted because of per_key conditions. # def __run_callbacks(key, kind, object, &blk) #:nodoc: - name = __callback_runner_name(key, kind) + name = __callback_runner_name(kind) unless object.respond_to?(name) - str = send("_#{kind}_callbacks").compile(key, object) + str = object.send("_#{kind}_callbacks").compile(key, object) class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def #{name}() #{str} end protected :#{name} @@ -369,11 +369,11 @@ module ActiveSupport end def __reset_runner(symbol) - name = __callback_runner_name(nil, symbol) + name = __callback_runner_name(symbol) undef_method(name) if method_defined?(name) end - def __callback_runner_name(key, kind) + def __callback_runner_name(kind) "_run__#{self.name.hash.abs}__#{kind}__callbacks" end diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index 7de824a77f..ac2a63d3a1 100644 --- a/activesupport/lib/active_support/core_ext/module/delegation.rb +++ b/activesupport/lib/active_support/core_ext/module/delegation.rb @@ -81,25 +81,25 @@ class Module # no matter whether +nil+ responds to the delegated method. You can get a # +nil+ instead with the +:allow_nil+ option. # - # class Foo - # attr_accessor :bar - # def initialize(bar = nil) - # @bar = bar - # end - # delegate :zoo, :to => :bar - # end - # - # Foo.new.zoo # raises NoMethodError exception (you called nil.zoo) - # - # class Foo - # attr_accessor :bar - # def initialize(bar = nil) - # @bar = bar - # end - # delegate :zoo, :to => :bar, :allow_nil => true - # end - # - # Foo.new.zoo # returns nil + # class Foo + # attr_accessor :bar + # def initialize(bar = nil) + # @bar = bar + # end + # delegate :zoo, :to => :bar + # end + # + # Foo.new.zoo # raises NoMethodError exception (you called nil.zoo) + # + # class Foo + # attr_accessor :bar + # def initialize(bar = nil) + # @bar = bar + # end + # delegate :zoo, :to => :bar, :allow_nil => true + # end + # + # Foo.new.zoo # returns nil # def delegate(*methods) options = methods.pop diff --git a/activesupport/lib/active_support/core_ext/uri.rb b/activesupport/lib/active_support/core_ext/uri.rb index 0b219ce44a..bfe0832b37 100644 --- a/activesupport/lib/active_support/core_ext/uri.rb +++ b/activesupport/lib/active_support/core_ext/uri.rb @@ -20,7 +20,7 @@ end module URI class << self def parser - @parser ||= URI.const_defined?(:Parser) ? URI::Parser.new : URI + @parser ||= URI::Parser.new end end end diff --git a/activesupport/lib/active_support/log_subscriber.rb b/activesupport/lib/active_support/log_subscriber.rb index 6296c1d4b8..58938cdc3d 100644 --- a/activesupport/lib/active_support/log_subscriber.rb +++ b/activesupport/lib/active_support/log_subscriber.rb @@ -92,7 +92,7 @@ module ActiveSupport begin send(method, ActiveSupport::Notifications::Event.new(message, *args)) rescue Exception => e - logger.error "Could not log #{message.inspect} event. #{e.class}: #{e.message}" + logger.error "Could not log #{message.inspect} event. #{e.class}: #{e.message} #{e.backtrace}" end end @@ -100,9 +100,9 @@ module ActiveSupport %w(info debug warn error fatal unknown).each do |level| class_eval <<-METHOD, __FILE__, __LINE__ + 1 - def #{level}(*args, &block) + def #{level}(progname = nil, &block) return unless logger - logger.#{level}(*args, &block) + logger.#{level}(progname, &block) end METHOD end diff --git a/activesupport/lib/active_support/logger.rb b/activesupport/lib/active_support/logger.rb index 66e8fcadb4..d055767eab 100644 --- a/activesupport/lib/active_support/logger.rb +++ b/activesupport/lib/active_support/logger.rb @@ -2,6 +2,41 @@ require 'logger' module ActiveSupport class Logger < ::Logger + # Broadcasts logs to multiple loggers + def self.broadcast(logger) # :nodoc: + Module.new do + define_method(:add) do |*args, &block| + logger.add(*args, &block) + super(*args, &block) + end + + define_method(:<<) do |x| + logger << x + super(x) + end + + define_method(:close) do + logger.close + super() + end + + define_method(:progname=) do |name| + logger.progname = name + super(name) + end + + define_method(:formatter=) do |formatter| + logger.formatter = formatter + super(formatter) + end + + define_method(:level=) do |level| + logger.level = level + super(level) + end + end + end + def initialize(*args) super @formatter = SimpleFormatter.new diff --git a/activesupport/lib/active_support/ordered_options.rb b/activesupport/lib/active_support/ordered_options.rb index bf81567d22..9e5a5d0246 100644 --- a/activesupport/lib/active_support/ordered_options.rb +++ b/activesupport/lib/active_support/ordered_options.rb @@ -30,8 +30,9 @@ module ActiveSupport #:nodoc: end def method_missing(name, *args) - if name.to_s =~ /(.*)=$/ - self[$1] = args.first + name_string = name.to_s + if name_string.chomp!('=') + self[name_string] = args.first else self[name] end diff --git a/activesupport/lib/active_support/tagged_logging.rb b/activesupport/lib/active_support/tagged_logging.rb index b60bc94db4..f6ad861353 100644 --- a/activesupport/lib/active_support/tagged_logging.rb +++ b/activesupport/lib/active_support/tagged_logging.rb @@ -1,62 +1,58 @@ require 'active_support/core_ext/object/blank' require 'logger' +require 'active_support/logger' module ActiveSupport - # Wraps any standard Logger class to provide tagging capabilities. Examples: + # Wraps any standard Logger object to provide tagging capabilities. Examples: # - # Logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT)) - # Logger.tagged("BCX") { Logger.info "Stuff" } # Logs "[BCX] Stuff" - # Logger.tagged("BCX", "Jason") { Logger.info "Stuff" } # Logs "[BCX] [Jason] Stuff" - # Logger.tagged("BCX") { Logger.tagged("Jason") { Logger.info "Stuff" } } # Logs "[BCX] [Jason] Stuff" + # logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT)) + # logger.tagged("BCX") { logger.info "Stuff" } # Logs "[BCX] Stuff" + # logger.tagged("BCX", "Jason") { logger.info "Stuff" } # Logs "[BCX] [Jason] Stuff" + # logger.tagged("BCX") { logger.tagged("Jason") { logger.info "Stuff" } } # Logs "[BCX] [Jason] Stuff" # # This is used by the default Rails.logger as configured by Railties to make it easy to stamp log lines # with subdomains, request ids, and anything else to aid debugging of multi-user production applications. - class TaggedLogging - def initialize(logger) - @logger = logger + module TaggedLogging + class Formatter < ActiveSupport::Logger::SimpleFormatter # :nodoc: + # This method is invoked when a log event occurs + def call(severity, timestamp, progname, msg) + super(severity, timestamp, progname, "#{tags_text}#{msg}") + end + + def clear! + current_tags.clear + end + + def current_tags + Thread.current[:activesupport_tagged_logging_tags] ||= [] + end + + private + def tags_text + tags = current_tags + if tags.any? + tags.collect { |tag| "[#{tag}] " }.join + end + end + end + + def self.new(logger) + logger.formatter = Formatter.new + logger.extend(self) end def tagged(*new_tags) - tags = current_tags - new_tags = Array(new_tags).flatten.reject(&:blank?) + tags = formatter.current_tags + new_tags = new_tags.flatten.reject(&:blank?) tags.concat new_tags yield ensure tags.pop(new_tags.size) end - def add(severity, message = nil, progname = nil, &block) - @logger.add(severity, "#{tags_text}#{message}", progname, &block) - end - - %w( fatal error warn info debug unknown ).each do |severity| - eval <<-EOM, nil, __FILE__, __LINE__ + 1 - def #{severity}(progname = nil, &block) # def warn(progname = nil, &block) - add(Logger::#{severity.upcase}, progname, &block) # add(Logger::WARN, progname, &block) - end # end - EOM - end - def flush - current_tags.clear - @logger.flush if @logger.respond_to?(:flush) - end - - def method_missing(method, *args) - @logger.send(method, *args) - end - - protected - - def tags_text - tags = current_tags - if tags.any? - tags.collect { |tag| "[#{tag}] " }.join - end - end - - def current_tags - Thread.current[:activesupport_tagged_logging_tags] ||= [] + formatter.clear! + super if defined?(super) end end end diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb index 4169557286..9a52c916ec 100644 --- a/activesupport/lib/active_support/test_case.rb +++ b/activesupport/lib/active_support/test_case.rb @@ -3,7 +3,6 @@ require 'active_support/testing/setup_and_teardown' require 'active_support/testing/assertions' require 'active_support/testing/deprecation' require 'active_support/testing/declarative' -require 'active_support/testing/pending' require 'active_support/testing/isolation' require 'active_support/testing/mochaing' require 'active_support/core_ext/kernel/reporting' @@ -40,7 +39,6 @@ module ActiveSupport include ActiveSupport::Testing::SetupAndTeardown include ActiveSupport::Testing::Assertions include ActiveSupport::Testing::Deprecation - include ActiveSupport::Testing::Pending extend ActiveSupport::Testing::Declarative # test/unit backwards compatibility methods diff --git a/activesupport/lib/active_support/testing/isolation.rb b/activesupport/lib/active_support/testing/isolation.rb index c896b955fb..5d5de4798d 100644 --- a/activesupport/lib/active_support/testing/isolation.rb +++ b/activesupport/lib/active_support/testing/isolation.rb @@ -117,13 +117,3 @@ module ActiveSupport end end end - -# Only in subprocess for windows / jruby. -if ENV['ISOLATION_TEST'] - require "test/unit/collector/objectspace" - class Test::Unit::Collector::ObjectSpace - def include?(test) - super && test.method_name == ENV['ISOLATION_TEST'] - end - end -end diff --git a/activesupport/lib/active_support/testing/pending.rb b/activesupport/lib/active_support/testing/pending.rb deleted file mode 100644 index 510f80f32c..0000000000 --- a/activesupport/lib/active_support/testing/pending.rb +++ /dev/null @@ -1,20 +0,0 @@ -# Some code from jeremymcanally's "pending" -# https://github.com/jeremymcanally/pending/tree/master - -module ActiveSupport - module Testing - module Pending - - unless defined?(Spec) - - @@pending_cases = [] - @@at_exit = false - - def pending(description = "", &block) - skip(description.blank? ? nil : description) - end - end - - end - end -end diff --git a/activesupport/test/broadcast_logger_test.rb b/activesupport/test/broadcast_logger_test.rb new file mode 100644 index 0000000000..6d4e3b74f7 --- /dev/null +++ b/activesupport/test/broadcast_logger_test.rb @@ -0,0 +1,82 @@ +require 'abstract_unit' + +module ActiveSupport + class BroadcastLoggerTest < TestCase + attr_reader :logger, :log1, :log2 + def setup + @log1 = FakeLogger.new + @log2 = FakeLogger.new + @log1.extend Logger.broadcast @log2 + @logger = @log1 + end + + def test_debug + logger.debug "foo" + assert_equal 'foo', log1.adds.first[2] + assert_equal 'foo', log2.adds.first[2] + end + + def test_close + logger.close + assert log1.closed, 'should be closed' + assert log2.closed, 'should be closed' + end + + def test_chevrons + logger << "foo" + assert_equal %w{ foo }, log1.chevrons + assert_equal %w{ foo }, log2.chevrons + end + + def test_level + assert_nil logger.level + logger.level = 10 + assert_equal 10, log1.level + assert_equal 10, log2.level + end + + def test_progname + assert_nil logger.progname + logger.progname = 10 + assert_equal 10, log1.progname + assert_equal 10, log2.progname + end + + def test_formatter + assert_nil logger.formatter + logger.formatter = 10 + assert_equal 10, log1.formatter + assert_equal 10, log2.formatter + end + + class FakeLogger + attr_reader :adds, :closed, :chevrons + attr_accessor :level, :progname, :formatter + + def initialize + @adds = [] + @closed = false + @chevrons = [] + @level = nil + @progname = nil + @formatter = nil + end + + def debug msg, &block + add(:omg, nil, msg, &block) + end + + def << x + @chevrons << x + end + + def add(*args) + @adds << args + end + + def close + @closed = true + end + end + end +end diff --git a/activesupport/test/callbacks_test.rb b/activesupport/test/callbacks_test.rb index 2c2a420619..032787f0f4 100644 --- a/activesupport/test/callbacks_test.rb +++ b/activesupport/test/callbacks_test.rb @@ -344,6 +344,54 @@ module CallbacksTest end end + module ExtendModule + def self.extended(base) + base.class_eval do + set_callback :save, :before, :record3 + end + end + def record3 + @recorder << 3 + end + end + + module IncludeModule + def self.included(base) + base.class_eval do + set_callback :save, :before, :record2 + end + end + def record2 + @recorder << 2 + end + end + + class ExtendCallbacks + + include ActiveSupport::Callbacks + + define_callbacks :save + set_callback :save, :before, :record1 + + include IncludeModule + + def save + run_callbacks :save + end + + attr_reader :recorder + + def initialize + @recorder = [] + end + + private + + def record1 + @recorder << 1 + end + end + class AroundCallbacksTest < ActiveSupport::TestCase def test_save_around around = AroundPerson.new @@ -645,4 +693,12 @@ module CallbacksTest end end + class ExtendCallbacksTest < ActiveSupport::TestCase + def test_save + model = ExtendCallbacks.new.extend ExtendModule + model.save + assert_equal [1, 2, 3], model.recorder + end + end + end diff --git a/activesupport/test/core_ext/uri_ext_test.rb b/activesupport/test/core_ext/uri_ext_test.rb index 4a6cbb8801..03e388dd7a 100644 --- a/activesupport/test/core_ext/uri_ext_test.rb +++ b/activesupport/test/core_ext/uri_ext_test.rb @@ -7,11 +7,7 @@ class URIExtTest < ActiveSupport::TestCase def test_uri_decode_handle_multibyte str = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E" # Ni-ho-nn-go in UTF-8, means Japanese. - if URI.const_defined?(:Parser) - parser = URI::Parser.new - assert_equal str, parser.unescape(parser.escape(str)) - else - assert_equal str, URI.unescape(URI.escape(str)) - end + parser = URI::Parser.new + assert_equal str, parser.unescape(parser.escape(str)) end end diff --git a/activesupport/test/log_subscriber_test.rb b/activesupport/test/log_subscriber_test.rb index 0c1f3c51ed..8e160714b1 100644 --- a/activesupport/test/log_subscriber_test.rb +++ b/activesupport/test/log_subscriber_test.rb @@ -118,6 +118,6 @@ class SyncLogSubscriberTest < ActiveSupport::TestCase assert_equal 'some_event.my_log_subscriber', @logger.logged(:info).last assert_equal 1, @logger.logged(:error).size - assert_equal 'Could not log "puke.my_log_subscriber" event. RuntimeError: puke', @logger.logged(:error).last + assert_match 'Could not log "puke.my_log_subscriber" event. RuntimeError: puke', @logger.logged(:error).last end -end
\ No newline at end of file +end |