Skip to content

Commit

Permalink
RuboCop lint rules, linting, TODO config added
Browse files Browse the repository at this point in the history
Signed-off-by: Olle Jonsson <[email protected]>
  • Loading branch information
olleolleolle committed Aug 16, 2017
1 parent 8d7e834 commit b317039
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 39 deletions.
21 changes: 21 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
inherit_from: .rubocop_todo.yml

AllCops:
DisplayCopNames: true
TargetRubyVersion: 2.2
Exclude:
- 'features/**/*'
Metrics/LineLength:
Max: 120
Metrics/BlockLength:
Exclude:
- 'spec/**/*'
Style/Documentation:
Enabled: false
Style/FrozenStringLiteralComment:
Enabled: false
# Use double quotes everywhere by default.
Style/StringLiterals:
EnforcedStyle: double_quotes


52 changes: 52 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-08-16 10:25:30 +0200 using RuboCop version 0.47.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Configuration parameters: AllowSafeAssignment.
Lint/AssignmentInCondition:
Exclude:
- 'lib/mixlib/log.rb'

# Offense count: 2
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 129

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 12

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 109

# Offense count: 2
Style/ClassVars:
Exclude:
- 'lib/mixlib/log/formatter.rb'

# Offense count: 2
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: format, sprintf, percent
Style/FormatString:
Exclude:
- 'lib/mixlib/log/formatter.rb'

# Offense count: 1
# Configuration parameters: AllowedVariables.
Style/GlobalVars:
Exclude:
- 'spec/spec_helper.rb'

# Offense count: 1
Style/MethodMissing:
Exclude:
- 'lib/mixlib/log.rb'
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ source "https://rubygems.org"
gemspec

group :development do
gem "rdoc"
gem "bundler"
gem "rdoc"
end
5 changes: 3 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ end
task default: [:style, :spec, :features]

# For rubygems-test
task :test => :spec
task test: :spec

RDoc::Task.new do |rdoc|
rdoc.rdoc_dir = "rdoc"
Expand Down Expand Up @@ -39,5 +39,6 @@ GitHubChangelogGenerator::RakeTask.new :changelog do |config|
config.future_release = Mixlib::Log::VERSION
config.enhancement_labels = "enhancement,Enhancement,New Feature,Feature".split(",")
config.bug_labels = "bug,Bug,Improvement,Upstream Bug".split(",")
config.exclude_labels = "duplicate,question,invalid,wontfix,no_changelog,Exclude From Changelog,Question,Discussion".split(",")
config.exclude_labels = ["duplicate", "question", "invalid", "wontfix",
"no_changelog", "Exclude From Changelog", "Question", "Discussion"]
end
27 changes: 15 additions & 12 deletions lib/mixlib/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@

module Mixlib
module Log

@logger, @loggers = nil

LEVELS = { :debug => Logger::DEBUG, :info => Logger::INFO, :warn => Logger::WARN, :error => Logger::ERROR, :fatal => Logger::FATAL }.freeze
LEVELS = { debug: Logger::DEBUG, info: Logger::INFO,
warn: Logger::WARN, error: Logger::ERROR, fatal: Logger::FATAL }.freeze
LEVEL_NAMES = LEVELS.invert.freeze

def reset!
close!
@logger, @loggers = nil, nil
@logger = nil
@loggers = nil
end

# An Array of log devices that will be logged to. Defaults to just the default
Expand Down Expand Up @@ -58,13 +59,12 @@ def use_log_devices(other)
if other.respond_to?(:loggers) && other.respond_to?(:logger)
@loggers = other.loggers
@logger = other.logger
elsif other.kind_of?(Array)
elsif other.is_a?(Array)
@loggers = other
@logger = other.first
else
msg = "#use_log_devices takes a Mixlib::Log object or array of log devices. " <<
"You gave: #{other.inspect}"
raise ArgumentError, msg
raise ArgumentError, "#use_log_devices takes a Mixlib::Log object or array of log devices. " \
"You gave: #{other.inspect}"
end
@configured = true
end
Expand All @@ -79,7 +79,7 @@ def use_log_devices(other)
def init(*opts)
reset!
@logger = logger_for(*opts)
@logger.formatter = Mixlib::Log::Formatter.new() if @logger.respond_to?(:formatter=)
@logger.formatter = Mixlib::Log::Formatter.new if @logger.respond_to?(:formatter=)
@logger.level = Logger::WARN
@configured = true
@logger
Expand Down Expand Up @@ -109,7 +109,7 @@ def level(new_level = nil)
if new_level.nil?
LEVEL_NAMES[logger.level]
else
self.level = (new_level)
self.level = new_level
end
end

Expand Down Expand Up @@ -143,7 +143,7 @@ def add(severity, message = nil, progname = nil, &block)
loggers.each { |l| l.add(severity, message, progname, &block) }
end

alias :log :add
alias log add

# Passes any other method calls on directly to the underlying Logger object created with init. If
# this method gets hit before a call to Mixlib::Logger.init has been made, it will call
Expand Down Expand Up @@ -185,9 +185,12 @@ def loggers_to_close
def close!
# try to close all file loggers
loggers_to_close.each do |l|
l.close rescue nil
begin
l.close
rescue
nil
end
end
end

end
end
4 changes: 2 additions & 2 deletions lib/mixlib/log/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def self.show_time=(show = false)

# Prints a log message as '[time] severity: message' if Chef::Log::Formatter.show_time == true.
# Otherwise, doesn't print the time.
def call(severity, time, progname, msg)
def call(severity, time, _progname, msg)
if @@show_time
sprintf("[%s] %s: %s\n", time.iso8601(), severity, msg2str(msg))
sprintf("[%s] %s: %s\n", time.iso8601, severity, msg2str(msg))
else
sprintf("%s: %s\n", severity, msg2str(msg))
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mixlib/log/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Mixlib
module Log
VERSION = "1.7.1"
VERSION = "1.7.1".freeze
end
end
2 changes: 1 addition & 1 deletion mixlib-log.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$:.unshift File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
require "mixlib/log/version"

Gem::Specification.new do |gem|
Expand Down
3 changes: 1 addition & 2 deletions spec/mixlib/log/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
end

it "should format random objects via inspect with msg2str(Object)" do
expect(@formatter.msg2str([ "black thought", "?uestlove" ])).to eq('["black thought", "?uestlove"]')
expect(@formatter.msg2str(["black thought", "?uestlove"])).to eq('["black thought", "?uestlove"]')
end

it "should return a formatted string with call" do
Expand All @@ -47,5 +47,4 @@
Mixlib::Log::Formatter.show_time = false
expect(@formatter.call("monkey", Time.new, "test", "mos def")).to eq("monkey: mos def\n")
end

end
33 changes: 16 additions & 17 deletions spec/mixlib/log_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def #{method_name}(message)
end

describe Mixlib::Log do

# Since we are testing class behaviour for an instance variable
# that gets set once, we need to reset it prior to each example [cb]
before(:each) do
Expand Down Expand Up @@ -69,7 +68,8 @@ def #{method_name}(message)
end

it "should re-initialize the logger if init is called again" do
first_logdev, second_logdev = StringIO.new, StringIO.new
first_logdev = StringIO.new
second_logdev = StringIO.new
Logit.init(first_logdev)
Logit.fatal "FIRST"
expect(first_logdev.string).to match(/FIRST/)
Expand All @@ -86,11 +86,11 @@ def #{method_name}(message)

it "should set the log level using the binding form, with :debug, :info, :warn, :error, or :fatal" do
levels = {
:debug => Logger::DEBUG,
:info => Logger::INFO,
:warn => Logger::WARN,
:error => Logger::ERROR,
:fatal => Logger::FATAL,
debug: Logger::DEBUG,
info: Logger::INFO,
warn: Logger::WARN,
error: Logger::ERROR,
fatal: Logger::FATAL
}
levels.each do |symbol, constant|
Logit.level = symbol
Expand All @@ -108,11 +108,11 @@ def #{method_name}(message)

it "should set the log level using the method form, with :debug, :info, :warn, :error, or :fatal" do
levels = {
:debug => Logger::DEBUG,
:info => Logger::INFO,
:warn => Logger::WARN,
:error => Logger::ERROR,
:fatal => Logger::FATAL,
debug: Logger::DEBUG,
info: Logger::INFO,
warn: Logger::WARN,
error: Logger::ERROR,
fatal: Logger::FATAL
}
levels.each do |symbol, constant|
Logit.level(symbol)
Expand All @@ -121,25 +121,25 @@ def #{method_name}(message)
end

it "should raise an ArgumentError if you try and set the level to something strange using the binding form" do
expect(lambda { Logit.level = :the_roots }).to raise_error(ArgumentError)
expect(-> { Logit.level = :the_roots }).to raise_error(ArgumentError)
end

it "should raise an ArgumentError if you try and set the level to something strange using the method form" do
expect(lambda { Logit.level(:the_roots) }).to raise_error(ArgumentError)
expect(-> { Logit.level(:the_roots) }).to raise_error(ArgumentError)
end

it "should pass other method calls directly to logger" do
Logit.level = :debug
expect(Logit).to be_debug
expect(lambda { Logit.debug("Gimme some sugar!") }).to_not raise_error
expect(-> { Logit.debug("Gimme some sugar!") }).to_not raise_error
end

it "should pass add method calls directly to logger" do
logdev = StringIO.new
Logit.init(logdev)
Logit.level = :debug
expect(Logit).to be_debug
expect(lambda { Logit.add(Logger::DEBUG, "Gimme some sugar!") }).to_not raise_error
expect(-> { Logit.add(Logger::DEBUG, "Gimme some sugar!") }).to_not raise_error
expect(logdev.string).to match(/Gimme some sugar/)
end

Expand Down Expand Up @@ -186,5 +186,4 @@ def #{method_name}(message)
end
expect(opened_files_count_after).to eq(opened_files_count_before + 1)
end

end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#

$TESTING = true
$:.push File.join(File.dirname(__FILE__), "..", "lib")
$LOAD_PATH.push File.join(File.dirname(__FILE__), "..", "lib")

require "rspec"
require "mixlib/log"
Expand Down

0 comments on commit b317039

Please sign in to comment.