提交 870310db 编写于 作者: A Andrew White

Eliminate dependency on Rails::VERSION::STRING

To facilitate the use of ActiveSupport::Testing::Performance outside
of a Rails application conditionally check for the presence of
Rails::VERSION::STRING before including it in the environment string.
上级 9b742fd6
require 'fileutils'
require 'rails/version'
require 'active_support/concern'
require 'active_support/core_ext/class/delegating_attributes'
require 'active_support/core_ext/string/inflections'
......@@ -149,26 +148,20 @@ def record
end
def environment
unless defined? @env
app = "#{$1}.#{$2}" if File.directory?('.git') && `git branch -v` =~ /^\* (\S+)\s+(\S+)/
rails = Rails::VERSION::STRING
if File.directory?('vendor/rails/.git')
Dir.chdir('vendor/rails') do
rails += ".#{$1}.#{$2}" if `git branch -v` =~ /^\* (\S+)\s+(\S+)/
end
end
ruby = "#{RUBY_ENGINE}-#{RUBY_VERSION}.#{RUBY_PATCHLEVEL}"
@env = [app, rails, ruby, RUBY_PLATFORM] * ','
end
@env
@env ||= [].tap do |env|
env << "#{$1}.#{$2}" if File.directory?('.git') && `git branch -v` =~ /^\* (\S+)\s+(\S+)/
env << rails_version if defined?(Rails::VERSION::STRING)
env << "#{RUBY_ENGINE}-#{RUBY_VERSION}.#{RUBY_PATCHLEVEL}"
env << RUBY_PLATFORM
end.join(',')
end
protected
HEADER = 'measurement,created_at,app,rails,ruby,platform'
if defined?(Rails::VERSION::STRING)
HEADER = 'measurement,created_at,app,rails,ruby,platform'
else
HEADER = 'measurement,created_at,app,ruby,platform'
end
def with_output_file
fname = output_filename
......@@ -186,6 +179,18 @@ def with_output_file
def output_filename
"#{super}.csv"
end
def rails_version
"rails-#{Rails::VERSION::STRING}#{rails_branch}"
end
def rails_branch
if File.directory?('vendor/rails/.git')
Dir.chdir('vendor/rails') do
".#{$1}.#{$2}" if `git branch -v` =~ /^\* (\S+)\s+(\S+)/
end
end
end
end
module Metrics
......
......@@ -96,7 +96,6 @@ def test_debugger_not_available_message_to_stderr
end
def test_debugger_not_available_message_to_rails_logger
Object.send(:remove_const, :Rails) if Object.const_defined?(:Rails)
rails = Class.new do
def self.logger
@logger ||= MockStdErr.new
......
......@@ -11,7 +11,7 @@ def test_amount_format
assert_equal "1", amount_metric.format(1.23)
assert_equal "40,000,000", amount_metric.format(40000000)
end
def test_time_format
time_metric = ActiveSupport::Testing::Performance::Metrics[:time].new
assert_equal "0 ms", time_metric.format(0)
......@@ -21,7 +21,7 @@ def test_time_format
assert_equal "40000.00 sec", time_metric.format(40000)
assert_equal "-5000 ms", time_metric.format(-5)
end
def test_space_format
space_metric = ActiveSupport::Testing::Performance::Metrics[:digital_information_unit].new
assert_equal "0 Bytes", space_metric.format(0)
......@@ -35,6 +35,25 @@ def test_space_format
assert_equal "91 TB", space_metric.format(10**14)
assert_equal "910000 TB", space_metric.format(10**18)
end
def test_environment_format_without_rails
metric = ActiveSupport::Testing::Performance::Metrics[:time].new
benchmarker = ActiveSupport::Testing::Performance::Benchmarker.new(self, metric)
assert_equal "#{RUBY_ENGINE}-#{RUBY_VERSION}.#{RUBY_PATCHLEVEL},#{RUBY_PLATFORM}", benchmarker.environment
end
def test_environment_format_with_rails
rails, version = Module.new, Module.new
version.const_set :STRING, "4.0.0"
rails.const_set :VERSION, version
Object.const_set :Rails, rails
metric = ActiveSupport::Testing::Performance::Metrics[:time].new
benchmarker = ActiveSupport::Testing::Performance::Benchmarker.new(self, metric)
assert_equal "rails-4.0.0,#{RUBY_ENGINE}-#{RUBY_VERSION}.#{RUBY_PATCHLEVEL},#{RUBY_PLATFORM}", benchmarker.environment
ensure
Object.send :remove_const, :Rails
end
end
end
end
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册