rb: The deprecation stuff should skip thrift library code when showing caller [THRIFT-56]

Author: Kevin Ballard <kevin@rapleaf.com>


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@671984 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/rb/lib/thrift/deprecation.rb b/lib/rb/lib/thrift/deprecation.rb
index 3dd1a57..6a422a8 100644
--- a/lib/rb/lib/thrift/deprecation.rb
+++ b/lib/rb/lib/thrift/deprecation.rb
@@ -62,7 +62,7 @@
           obj, name, warned = CLASS_MAPPING[klass_id]
           unless warned
             STDERR.puts "Warning: class #{name} is deprecated"
-            STDERR.puts "  from #{caller.first}"
+            STDERR.puts "  from #{Thrift::DeprecationProxy.process_caller(caller)}"
             CLASS_MAPPING[klass_id][2] = true
           end
           if klass.__id__ == self.__id__
@@ -92,7 +92,7 @@
         obj, name, warned = MODULE_MAPPING[mod_id]
         unless warned
           STDERR.puts "Warning: module #{name} is deprecated"
-          STDERR.puts "  from #{caller.first}"
+          STDERR.puts "  from #{Thrift::DeprecationProxy.process_caller(caller)}"
           MODULE_MAPPING[mod_id][2] = true
         end
         obj.instance_method(sym).bind(self).call(*args, &block)
@@ -109,7 +109,7 @@
           obj, name, warned = MODULE_MAPPING[mod_id]
           unless warned
             STDERR.puts "Warning: module #{name} is deprecated"
-            STDERR.puts "  from #{caller.first}"
+            STDERR.puts "  from #{Thrift::DeprecationProxy.process_caller(caller)}"
             MODULE_MAPPING[mod_id][2] = true
           end
           obj.send sym, *args, &block
@@ -119,6 +119,10 @@
     MODULE_MAPPING[mod_id][2] = false
     mod
   end
+  def self.process_caller(stack)
+    dir = File.dirname(__FILE__)
+    stack.find { |frame| frame[0,dir.size] != dir }
+  end
 end
 
 module Kernel
diff --git a/lib/rb/spec/deprecation_spec.rb b/lib/rb/spec/deprecation_spec.rb
index b0cc527..4ca4ee3 100644
--- a/lib/rb/spec/deprecation_spec.rb
+++ b/lib/rb/spec/deprecation_spec.rb
@@ -311,12 +311,14 @@
 
   def stub_stderr(mod, offset=1, called=nil)
     STDERR.should_receive(:puts).with("Warning: module #{mod} is deprecated")
+    source = Regexp.escape(__FILE__) + ":"
     if offset
       line = (called || caller.first)[/\d+$/].to_i + offset
-      STDERR.should_receive(:puts).with("  from #{__FILE__}:#{line}")
+      source += Regexp.escape(line.to_s)
     else
-      STDERR.should_receive(:puts).with(/^  from #{Regexp.escape(__FILE__)}:/)
+      source += "\d+"
     end
+    STDERR.should_receive(:puts).with(/^  from #{source}(?::in `[^']+')?$/)
   end
 
   it "should create a new global constant that points to the old one" do
@@ -423,4 +425,15 @@
       end
     end
   end
+
+  it "should skip thrift library code when printing caller" do
+    klass = Class.new do
+      include ThriftStruct
+      FIELDS = {
+        1 => {:name => "foo", :type => Thrift::Types::STRING}
+      }
+    end
+    stub_stderr('ThriftStruct')
+    klass.new(:foo => "foo")
+  end
 end