[Pkg-puppet-devel] [facter] 198/352: (maint) Extract Facter logging functionality
Stig Sandbeck Mathisen
ssm at debian.org
Sun Apr 6 22:21:45 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 fc93a4facba224af45dc574fdbd5dccb335e5f70
Author: Adrien Thebo <git at somethingsinistral.net>
Date: Mon Feb 3 09:55:38 2014 -0800
(maint) Extract Facter logging functionality
---
lib/facter.rb | 153 +--------------------------------------
lib/facter/core/logging.rb | 159 +++++++++++++++++++++++++++++++++++++++++
spec/unit/core/logging_spec.rb | 113 +++++++++++++++++++++++++++++
spec/unit/facter_spec.rb | 121 -------------------------------
4 files changed, 274 insertions(+), 272 deletions(-)
diff --git a/lib/facter.rb b/lib/facter.rb
index da433d0..2244912 100644
--- a/lib/facter.rb
+++ b/lib/facter.rb
@@ -39,18 +39,8 @@ module Facter
include Comparable
include Enumerable
- # @api private
- GREEN = "[0;32m"
- # @api private
- RESET = "[0m"
- # @api private
- @@debug = 0
- # @api private
- @@timing = 0
- # @api private
- @@messages = {}
- # @api private
- @@debug_messages = {}
+ require 'facter/core/logging'
+ extend Facter::Core::Logging
# module methods
@@ -67,55 +57,6 @@ module Facter
@collection
end
- # Prints a debug message if debugging is turned on
- #
- # @param string [String] the debug message
- # @return [void]
- def self.debug(string)
- if string.nil?
- return
- end
- if self.debugging?
- puts GREEN + string + RESET
- end
- end
-
- # Prints a debug message only once.
- #
- # @note Uniqueness is based on the string, not the specific location
- # of the method call.
- #
- # @param msg [String] the debug message
- # @return [void]
- def self.debugonce(msg)
- if msg and not msg.empty? and @@debug_messages[msg].nil?
- @@debug_messages[msg] = true
- debug(msg)
- end
- end
-
- # Returns whether debugging output is turned on
- def self.debugging?
- @@debug != 0
- end
-
- # Prints a timing
- #
- # @param string [String] the time to print
- # @return [void]
- #
- # @api private
- def self.show_time(string)
- puts "#{GREEN}#{string}#{RESET}" if string and Facter.timing?
- end
-
- # Returns whether timing output is turned on
- #
- # @api private
- def self.timing?
- @@timing != 0
- end
-
# Returns whether the JSON "feature" is available.
#
# @api private
@@ -268,96 +209,6 @@ module Facter
Facter.reset
end
- # Clears the seen state of warning messages. See {warnonce}.
- #
- # @return [void]
- #
- # @api private
- def self.clear_messages
- @@messages.clear
- end
-
- # Sets debugging on or off.
- #
- # @return [void]
- #
- # @api private
- def self.debugging(bit)
- if bit
- case bit
- when TrueClass; @@debug = 1
- when FalseClass; @@debug = 0
- when Fixnum
- if bit > 0
- @@debug = 1
- else
- @@debug = 0
- end
- when String;
- if bit.downcase == 'off'
- @@debug = 0
- else
- @@debug = 1
- end
- else
- @@debug = 0
- end
- else
- @@debug = 0
- end
- end
-
- # Sets whether timing messages are displayed.
- #
- # @return [void]
- #
- # @api private
- def self.timing(bit)
- if bit
- case bit
- when TrueClass; @@timing = 1
- when Fixnum
- if bit > 0
- @@timing = 1
- else
- @@timing = 0
- end
- end
- else
- @@timing = 0
- end
- end
-
- # Prints a warning message. The message is only printed if debugging
- # is enabled.
- #
- # @param msg [String] the warning message to be printed
- #
- # @return [void]
- def self.warn(msg)
- if Facter.debugging? and msg and not msg.empty?
- msg = [msg] unless msg.respond_to? :each
- msg.each { |line| Kernel.warn line }
- end
- end
-
- # Prints a warning message only once per process. Each unique string
- # is printed once.
- #
- # @note Unlike {warn} the message will be printed even if debugging is
- # not turned on. This behavior is likely to change and should not be
- # relied on.
- #
- # @param msg [String] the warning message to be printed
- #
- # @return [void]
- def self.warnonce(msg)
- if msg and not msg.empty? and @@messages[msg].nil?
- @@messages[msg] = true
- Kernel.warn(msg)
- end
- end
-
# Removes all facts from memory. Use this when the fact code has
# changed on disk and needs to be reloaded.
#
diff --git a/lib/facter/core/logging.rb b/lib/facter/core/logging.rb
new file mode 100644
index 0000000..c466b0c
--- /dev/null
+++ b/lib/facter/core/logging.rb
@@ -0,0 +1,159 @@
+require 'facter'
+
+module Facter::Core::Logging
+
+ extend self
+
+ # @api private
+ GREEN = "[0;32m"
+ # @api private
+ RESET = "[0m"
+
+ # @api private
+ @@debug = 0
+ # @api private
+ @@timing = 0
+ # @api private
+ @@warn_messages = {}
+ # @api private
+ @@debug_messages = {}
+
+ # Prints a debug message if debugging is turned on
+ #
+ # @param string [String] the debug message
+ # @return [void]
+ def debug(string)
+ if string.nil?
+ return
+ end
+ if self.debugging?
+ puts GREEN + string + RESET
+ end
+ end
+
+ # Prints a debug message only once.
+ #
+ # @note Uniqueness is based on the string, not the specific location
+ # of the method call.
+ #
+ # @param msg [String] the debug message
+ # @return [void]
+ def debugonce(msg)
+ if msg and not msg.empty? and @@debug_messages[msg].nil?
+ @@debug_messages[msg] = true
+ debug(msg)
+ end
+ end
+
+ # Prints a warning message. The message is only printed if debugging
+ # is enabled.
+ #
+ # @param msg [String] the warning message to be printed
+ #
+ # @return [void]
+ def warn(msg)
+ if self.debugging? and msg and not msg.empty?
+ msg = [msg] unless msg.respond_to? :each
+ msg.each { |line| Kernel.warn line }
+ end
+ end
+
+ # Prints a warning message only once per process. Each unique string
+ # is printed once.
+ #
+ # @note Unlike {warn} the message will be printed even if debugging is
+ # not turned on. This behavior is likely to change and should not be
+ # relied on.
+ #
+ # @param msg [String] the warning message to be printed
+ #
+ # @return [void]
+ def warnonce(msg)
+ if msg and not msg.empty? and @@warn_messages[msg].nil?
+ @@warn_messages[msg] = true
+ Kernel.warn(msg)
+ end
+ end
+
+ # Print timing information
+ #
+ # @param string [String] the time to print
+ # @return [void]
+ #
+ # @api private
+ def show_time(string)
+ puts "#{GREEN}#{string}#{RESET}" if string and self.timing?
+ end
+
+ # Sets debugging on or off.
+ #
+ # @return [void]
+ #
+ # @api private
+ def debugging(bit)
+ if bit
+ case bit
+ when TrueClass; @@debug = 1
+ when FalseClass; @@debug = 0
+ when Fixnum
+ if bit > 0
+ @@debug = 1
+ else
+ @@debug = 0
+ end
+ when String;
+ if bit.downcase == 'off'
+ @@debug = 0
+ else
+ @@debug = 1
+ end
+ else
+ @@debug = 0
+ end
+ else
+ @@debug = 0
+ end
+ end
+
+ # Returns whether debugging output is turned on
+ def debugging?
+ @@debug != 0
+ end
+
+ # Sets whether timing messages are displayed.
+ #
+ # @return [void]
+ #
+ # @api private
+ def timing(bit)
+ if bit
+ case bit
+ when TrueClass; @@timing = 1
+ when Fixnum
+ if bit > 0
+ @@timing = 1
+ else
+ @@timing = 0
+ end
+ end
+ else
+ @@timing = 0
+ end
+ end
+
+ # Returns whether timing output is turned on
+ #
+ # @api private
+ def timing?
+ @@timing != 0
+ end
+
+ # Clears the seen state of warning messages. See {warnonce}.
+ #
+ # @return [void]
+ #
+ # @api private
+ def clear_messages
+ @@warn_messages.clear
+ end
+end
diff --git a/spec/unit/core/logging_spec.rb b/spec/unit/core/logging_spec.rb
new file mode 100644
index 0000000..14e09c4
--- /dev/null
+++ b/spec/unit/core/logging_spec.rb
@@ -0,0 +1,113 @@
+require 'spec_helper'
+require 'facter/core/logging'
+
+describe Facter::Core::Logging do
+
+ subject { described_class }
+
+ describe "when warning" do
+ it "should warn if debugging is enabled" 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)
+ 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('')
+ end
+
+ it "should not warn if debugging is disabled" do
+ subject.debugging(false)
+ Kernel.stubs(:warn)
+ Kernel.expects(:warn).never
+ subject.warn('foo')
+ end
+
+ it "should warn for any given element for an array if debugging is enabled" do
+ subject.debugging(true)
+ Kernel.stubs(:warn)
+ Kernel.expects(:warn).with('foo')
+ Kernel.expects(:warn).with('bar')
+ subject.warn( ['foo','bar'])
+ end
+ end
+
+ describe "when warning once" do
+ it "should only warn once" do
+ Kernel.stubs(:warnonce)
+ Kernel.expects(:warn).with('foo').once
+ subject.warnonce('foo')
+ subject.warnonce('foo')
+ end
+
+ it "should not warnonce if nil is passed" do
+ Kernel.stubs(:warn)
+ Kernel.expects(:warnonce).never
+ subject.warnonce(nil)
+ end
+
+ it "should not warnonce if an empty string is passed" do
+ Kernel.stubs(:warn)
+ Kernel.expects(:warnonce).never
+ subject.warnonce('')
+ end
+ end
+
+ describe "when setting debugging mode" do
+ it "should have debugging enabled using 1" do
+ subject.debugging(1)
+ subject.should be_debugging
+ end
+ it "should have debugging enabled using true" do
+ subject.debugging(true)
+ subject.should be_debugging
+ end
+ it "should have debugging enabled using any string except off" do
+ subject.debugging('aaaaa')
+ subject.should be_debugging
+ end
+ it "should have debugging disabled using 0" do
+ subject.debugging(0)
+ subject.should_not be_debugging
+ end
+ it "should have debugging disabled using false" do
+ subject.debugging(false)
+ subject.should_not be_debugging
+ end
+ it "should have debugging disabled using the string 'off'" do
+ subject.debugging('off')
+ subject.should_not be_debugging
+ end
+ end
+
+ describe "when setting timing mode" do
+ it "should have timing enabled using 1" do
+ subject.timing(1)
+ subject.should be_timing
+ end
+ it "should have timing enabled using true" do
+ subject.timing(true)
+ subject.should be_timing
+ end
+ it "should have timing disabled using 0" do
+ subject.timing(0)
+ subject.should_not be_timing
+ end
+ it "should have timing disabled using false" do
+ subject.timing(false)
+ subject.should_not be_timing
+ end
+ end
+
+end
diff --git a/spec/unit/facter_spec.rb b/spec/unit/facter_spec.rb
index 2279c27..42204dd 100755
--- a/spec/unit/facter_spec.rb
+++ b/spec/unit/facter_spec.rb
@@ -124,127 +124,6 @@ describe Facter do
Facter.should respond_to(:search_external_path)
end
- it "should have a method to query debugging mode" do
- Facter.should respond_to(:debugging?)
- end
-
- it "should have a method to query timing mode" do
- Facter.should respond_to(:timing?)
- end
-
- it "should have a method to show timing information" do
- Facter.should respond_to(:show_time)
- end
-
- it "should have a method to warn" do
- Facter.should respond_to(:warn)
- end
-
- describe "when warning" do
- it "should warn if debugging is enabled" do
- Facter.debugging(true)
- Kernel.stubs(:warn)
- Kernel.expects(:warn).with('foo')
- Facter.warn('foo')
- end
-
- it "should not warn if debugging is enabled but nil is passed" do
- Facter.debugging(true)
- Kernel.stubs(:warn)
- Kernel.expects(:warn).never
- Facter.warn(nil)
- end
-
- it "should not warn if debugging is enabled but an empyt string is passed" do
- Facter.debugging(true)
- Kernel.stubs(:warn)
- Kernel.expects(:warn).never
- Facter.warn('')
- end
-
- it "should not warn if debugging is disabled" do
- Facter.debugging(false)
- Kernel.stubs(:warn)
- Kernel.expects(:warn).never
- Facter.warn('foo')
- end
-
- it "should warn for any given element for an array if debugging is enabled" do
- Facter.debugging(true)
- Kernel.stubs(:warn)
- Kernel.expects(:warn).with('foo')
- Kernel.expects(:warn).with('bar')
- Facter.warn( ['foo','bar'])
- end
- end
-
- describe "when warning once" do
- it "should only warn once" do
- Kernel.stubs(:warnonce)
- Kernel.expects(:warn).with('foo').once
- Facter.warnonce('foo')
- Facter.warnonce('foo')
- end
-
- it "should not warnonce if nil is passed" do
- Kernel.stubs(:warn)
- Kernel.expects(:warnonce).never
- Facter.warnonce(nil)
- end
-
- it "should not warnonce if an empty string is passed" do
- Kernel.stubs(:warn)
- Kernel.expects(:warnonce).never
- Facter.warnonce('')
- end
- end
-
- describe "when setting debugging mode" do
- it "should have debugging enabled using 1" do
- Facter.debugging(1)
- Facter.should be_debugging
- end
- it "should have debugging enabled using true" do
- Facter.debugging(true)
- Facter.should be_debugging
- end
- it "should have debugging enabled using any string except off" do
- Facter.debugging('aaaaa')
- Facter.should be_debugging
- end
- it "should have debugging disabled using 0" do
- Facter.debugging(0)
- Facter.should_not be_debugging
- end
- it "should have debugging disabled using false" do
- Facter.debugging(false)
- Facter.should_not be_debugging
- end
- it "should have debugging disabled using the string 'off'" do
- Facter.debugging('off')
- Facter.should_not be_debugging
- end
- end
-
- describe "when setting timing mode" do
- it "should have timing enabled using 1" do
- Facter.timing(1)
- Facter.should be_timing
- end
- it "should have timing enabled using true" do
- Facter.timing(true)
- Facter.should be_timing
- end
- it "should have timing disabled using 0" do
- Facter.timing(0)
- Facter.should_not be_timing
- end
- it "should have timing disabled using false" do
- Facter.timing(false)
- Facter.should_not be_timing
- end
- end
-
describe "when registering directories to search" do
after { Facter.instance_variable_set("@search_path", []) }
--
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