提交 bd84b820 编写于 作者: J Jeremy Kemper

Eliminate Pathname extensions

上级 727e9dc1
require 'active_support/core_ext/pathname'
module ActiveSupport module ActiveSupport
FrozenObjectError = RUBY_VERSION < '1.9' ? TypeError : RuntimeError FrozenObjectError = RUBY_VERSION < '1.9' ? TypeError : RuntimeError
end end
# TODO: Turn all this into using the BacktraceCleaner. # TODO: Turn all this into using the BacktraceCleaner.
class Exception # :nodoc: class Exception # :nodoc:
# Clean the paths contained in the message.
def self.clean_paths(string)
require 'pathname' unless defined? Pathname
string.gsub(%r{[\w. ]+(/[\w. ]+)+(\.rb)?(\b|$)}) do |path|
Pathname.new(path).cleanpath
end
end
def clean_message def clean_message
Pathname.clean_within message Exception.clean_paths(message)
end end
TraceSubstitutions = [] TraceSubstitutions = []
...@@ -16,9 +22,10 @@ def clean_message ...@@ -16,9 +22,10 @@ def clean_message
def clean_backtrace def clean_backtrace
backtrace.collect do |line| backtrace.collect do |line|
Pathname.clean_within(TraceSubstitutions.inject(line) do |result, (regexp, sub)| substituted = TraceSubstitutions.inject(line) do |result, (regexp, sub)|
result.gsub regexp, sub result.gsub regexp, sub
end) end
Exception.clean_paths(substituted)
end end
end end
......
if defined? Pathname
require 'active_support/core_ext/pathname/clean_within'
else
autoload :Pathname, 'active_support/core_ext/pathname/clean_within'
end
require 'pathname'
class Pathname
# Clean the paths contained in the provided string.
def self.clean_within(string)
string.gsub(%r{[\w. ]+(/[\w. ]+)+(\.rb)?(\b|$)}) do |path|
new(path).cleanpath
end
end
end
require 'abstract_unit'
require 'active_support/core_ext/pathname'
class TestPathname < Test::Unit::TestCase
def test_clean_within
assert_equal "Hi", Pathname.clean_within("Hi")
assert_equal "Hi", Pathname.clean_within("Hi/a/b/../..")
assert_equal "Hello\nWorld", Pathname.clean_within("Hello/a/b/../..\na/b/../../World/c/..")
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册