[Pkg-puppet-devel] [facter] 203/352: (maint) Emit warnings when log methods called without string

Stig Sandbeck Mathisen ssm at debian.org
Sun Apr 6 22:21:46 UTC 2014


This is an automated email from the git hooks/post-receive script.

ssm pushed a commit to branch master
in repository facter.

commit 3e5db044513b2549e28df6a45ffabecd74df741f
Author: Adrien Thebo <git at somethingsinistral.net>
Date:   Mon Feb 3 11:59:23 2014 -0800

    (maint) Emit warnings when log methods called without string
---
 lib/facter/core/logging.rb     | 19 +++++++++-----
 spec/unit/core/logging_spec.rb | 59 ++++++++++++++++++++++++++++++------------
 2 files changed, 54 insertions(+), 24 deletions(-)

diff --git a/lib/facter/core/logging.rb b/lib/facter/core/logging.rb
index c7e8674..9f213a7 100644
--- a/lib/facter/core/logging.rb
+++ b/lib/facter/core/logging.rb
@@ -20,14 +20,16 @@ module Facter::Core::Logging
 
   # Prints a debug message if debugging is turned on
   #
-  # @param string [String] the debug message
+  # @param msg [String] the debug message
   # @return [void]
-  def debug(string)
-    if string.nil?
-      return
-    end
+  def debug(msg)
     if self.debugging?
-      puts GREEN + string + RESET
+      if msg.nil? or msg.empty?
+        invoker = caller[0].slice(/.*:\d+/)
+        self.warn "#{self.class}#debug invoked with invalid message #{msg.inspect}:#{msg.class} at #{invoker}"
+      else
+        puts GREEN + msg + RESET
+      end
     end
   end
 
@@ -52,7 +54,10 @@ module Facter::Core::Logging
   #
   # @return [void]
   def warn(msg)
-    if msg and not msg.empty?
+    if msg.nil? or msg.empty?
+      invoker = caller[0].slice(/.*:\d+/)
+      Kernel.warn "#{self.class}#debug invoked with invalid message #{msg.inspect}:#{msg.class} at #{invoker}"
+    else
       Kernel.warn msg
     end
   end
diff --git a/spec/unit/core/logging_spec.rb b/spec/unit/core/logging_spec.rb
index 7182e82..8bcf9e3 100644
--- a/spec/unit/core/logging_spec.rb
+++ b/spec/unit/core/logging_spec.rb
@@ -5,33 +5,58 @@ describe Facter::Core::Logging do
 
   subject { described_class }
 
+  after(:all) do
+    subject.debugging(false)
+    subject.timing(false)
+  end
+
+  describe "emitting debug messages" do
+    it "doesn't log a message when debugging is disabled" do
+      subject.debugging(false)
+      subject.expects(:puts).never
+      subject.debug("foo")
+    end
+
+    describe "and debugging is enabled" do
+      before { subject.debugging(true) }
+      it "emits a warning when called with nil" do
+        subject.expects(:warn).with { |msg| expect(msg).to match /invalid message nil:NilClass/ }
+        subject.debug(nil)
+      end
+
+      it "emits a warning when called with an empty string" do
+        subject.expects(:warn).with { |msg| expect(msg).to match /invalid message "":String/ }
+        subject.debug("")
+      end
+
+      it "prints the message when logging is enabled" do
+        subject.expects(:puts).with { |msg| expect(msg).to match /foo/ }
+        subject.debug("foo")
+      end
+    end
+  end
+
   describe "when warning" do
-    it "should warn if debugging is enabled" do
+    it "emits a warning when given a string" do
       subject.debugging(true)
-      Kernel.stubs(:warn)
       Kernel.expects(:warn).with('foo')
       subject.warn('foo')
     end
 
-    it "should not warn if debugging is enabled but nil is passed" do
-      subject.debugging(true)
-      Kernel.stubs(:warn)
-      Kernel.expects(:warn).never
-      subject.warn(nil)
+    it "emits a warning regardless of log level" do
+      subject.debugging(false)
+      Kernel.expects(:warn).with "foo"
+      subject.warn "foo"
     end
 
-    it "should not warn if debugging is enabled but an empyt string is passed" do
-      subject.debugging(true)
-      Kernel.stubs(:warn)
-      Kernel.expects(:warn).never
-      subject.warn('')
+    it "emits a warning if nil is passed" do
+      Kernel.expects(:warn).with { |msg| expect(msg).to match /invalid message nil:NilClass/ }
+      subject.warn(nil)
     end
 
-    it "should not warn if debugging is disabled" do
-      subject.debugging(false)
-      Kernel.stubs(:warn)
-      Kernel.expects(:warn).never
-      subject.warn('foo')
+    it "emits a warning if an empty string is passed" do
+      Kernel.expects(:warn).with { |msg| expect(msg).to match /invalid message "":String/ }
+      subject.warn('')
     end
   end
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git



More information about the Pkg-puppet-devel mailing list