Skip to content

Commit 7999ff2

Browse files
david22swanHelen
authored and
Helen
committed
Rubocop Implementation (#838)
* Rubocop Implemented * Update spec_helper.rb * Update spec_helper.rb
1 parent ae7ed0d commit 7999ff2

File tree

460 files changed

+6497
-6974
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

460 files changed

+6497
-6974
lines changed

.rubocop.yml

Lines changed: 89 additions & 497 deletions
Large diffs are not rendered by default.

.rubocop_todo.yml

Whitespace-only changes.

.sync.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ spec/spec_helper.rb:
1818
  - rvm: 2.1.9
1919
    env: PUPPET_GEM_VERSION="~> 4.7.0"
2020
    bundler_args: --without system_tests
21+
- rvm: 2.1.9
22+
script: bundle exec rake rubocop

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ matrix:
2828
- rvm: 2.1.9
2929
bundler_args: --without system_tests
3030
env: PUPPET_GEM_VERSION="~> 4.0"
31+
- rvm: 2.1.9
32+
script: bundle exec rake rubocop
3133
notifications:
3234
email: false

lib/facter/facter_dot_d.rb

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,18 @@
1111
# The cache is stored in $libdir/facts_dot_d.cache as a mode
1212
# 600 file and will have the end result of not calling your
1313
# fact scripts more often than is needed
14-
1514
class Facter::Util::DotD
1615
require 'yaml'
1716

18-
def initialize(dir="/etc/facts.d", cache_file=File.join(Puppet[:libdir], "facts_dot_d.cache"))
17+
def initialize(dir = '/etc/facts.d', cache_file = File.join(Puppet[:libdir], 'facts_dot_d.cache'))
1918
@dir = dir
2019
@cache_file = cache_file
2120
@cache = nil
22-
@types = {".txt" => :txt, ".json" => :json, ".yaml" => :yaml}
21+
@types = { '.txt' => :txt, '.json' => :json, '.yaml' => :yaml }
2322
end
2423

2524
def entries
26-
Dir.entries(@dir).reject { |f| f =~ /^\.|\.ttl$/ }.sort.map { |f| File.join(@dir, f) }
25+
Dir.entries(@dir).reject { |f| f =~ %r{^\.|\.ttl$} }.sort.map { |f| File.join(@dir, f) }
2726
rescue
2827
[]
2928
end
@@ -35,17 +34,17 @@ def fact_type(file)
3534

3635
type = :script if type == :unknown && File.executable?(file)
3736

38-
return type
37+
type
3938
end
4039

4140
def txt_parser(file)
4241
File.readlines(file).each do |line|
43-
if line =~ /^([^=]+)=(.+)$/
44-
var = $1; val = $2
42+
next unless line =~ %r{^([^=]+)=(.+)$}
43+
var = Regexp.last_match(1)
44+
val = Regexp.last_match(2)
4545

46-
Facter.add(var) do
47-
setcode { val }
48-
end
46+
Facter.add(var) do
47+
setcode { val }
4948
end
5049
end
5150
rescue StandardError => e
@@ -60,7 +59,7 @@ def json_parser(file)
6059
raise
6160
end
6261

63-
JSON.load(File.read(file)).each_pair do |f, v|
62+
JSON.parse(File.read(file)).each_pair do |f, v|
6463
Facter.add(f) do
6564
setcode { v }
6665
end
@@ -85,25 +84,25 @@ def script_parser(file)
8584
result = cache_lookup(file)
8685
ttl = cache_time(file)
8786

88-
unless result
87+
if result
88+
Facter.debug("Using cached data for #{file}")
89+
else
8990
result = Facter::Util::Resolution.exec(file)
9091

9192
if ttl > 0
9293
Facter.debug("Updating cache for #{file}")
9394
cache_store(file, result)
9495
cache_save!
9596
end
96-
else
97-
Facter.debug("Using cached data for #{file}")
9897
end
9998

10099
result.split("\n").each do |line|
101-
if line =~ /^(.+)=(.+)$/
102-
var = $1; val = $2
100+
next unless line =~ %r{^(.+)=(.+)$}
101+
var = Regexp.last_match(1)
102+
val = Regexp.last_match(2)
103103

104-
Facter.add(var) do
105-
setcode { val }
106-
end
104+
Facter.add(var) do
105+
setcode { val }
107106
end
108107
end
109108
rescue StandardError => e
@@ -113,15 +112,15 @@ def script_parser(file)
113112

114113
def cache_save!
115114
cache = load_cache
116-
File.open(@cache_file, "w", 0600) { |f| f.write(YAML.dump(cache)) }
117-
rescue
115+
File.open(@cache_file, 'w', 0o600) { |f| f.write(YAML.dump(cache)) }
116+
rescue # rubocop:disable Lint/HandleExceptions - Is meant to be suppressed
118117
end
119118

120119
def cache_store(file, data)
121120
load_cache
122121

123-
@cache[file] = {:data => data, :stored => Time.now.to_i}
124-
rescue
122+
@cache[file] = { data: data, stored: Time.now.to_i }
123+
rescue # rubocop:disable Lint/HandleExceptions - Is meant to be suppressed
125124
end
126125

127126
def cache_lookup(file)
@@ -131,21 +130,18 @@ def cache_lookup(file)
131130

132131
ttl = cache_time(file)
133132

134-
if cache[file]
135-
now = Time.now.to_i
133+
return nil unless cache[file]
134+
now = Time.now.to_i
136135

137-
return cache[file][:data] if ttl == -1
138-
return cache[file][:data] if (now - cache[file][:stored]) <= ttl
139-
return nil
140-
else
141-
return nil
142-
end
136+
return cache[file][:data] if ttl == -1
137+
return cache[file][:data] if (now - cache[file][:stored]) <= ttl
138+
return nil
143139
rescue
144140
return nil
145141
end
146142

147143
def cache_time(file)
148-
meta = file + ".ttl"
144+
meta = file + '.ttl'
149145

150146
return File.read(meta).chomp.to_i
151147
rescue
@@ -154,11 +150,11 @@ def cache_time(file)
154150

155151
def load_cache
156152
unless @cache
157-
if File.exist?(@cache_file)
158-
@cache = YAML.load_file(@cache_file)
159-
else
160-
@cache = {}
161-
end
153+
@cache = if File.exist?(@cache_file)
154+
YAML.load_file(@cache_file)
155+
else
156+
{}
157+
end
162158
end
163159

164160
return @cache
@@ -172,28 +168,26 @@ def create
172168
type = fact_type(fact)
173169
parser = "#{type}_parser"
174170

175-
if respond_to?("#{type}_parser")
176-
Facter.debug("Parsing #{fact} using #{parser}")
171+
next unless respond_to?("#{type}_parser")
172+
Facter.debug("Parsing #{fact} using #{parser}")
177173

178-
send(parser, fact)
179-
end
174+
send(parser, fact)
180175
end
181176
end
182177
end
183178

184-
185-
mdata = Facter.version.match(/(\d+)\.(\d+)\.(\d+)/)
179+
mdata = Facter.version.match(%r{(\d+)\.(\d+)\.(\d+)})
186180
if mdata
187-
(major, minor, patch) = mdata.captures.map { |v| v.to_i }
181+
(major, minor, _patch) = mdata.captures.map { |v| v.to_i }
188182
if major < 2
189183
# Facter 1.7 introduced external facts support directly
190-
unless major == 1 and minor > 6
191-
Facter::Util::DotD.new("/etc/facter/facts.d").create
192-
Facter::Util::DotD.new("/etc/puppetlabs/facter/facts.d").create
184+
unless major == 1 && minor > 6
185+
Facter::Util::DotD.new('/etc/facter/facts.d').create
186+
Facter::Util::DotD.new('/etc/puppetlabs/facter/facts.d').create
193187

194188
# Windows has a different configuration directory that defaults to a vendor
195189
# specific sub directory of the %COMMON_APPDATA% directory.
196-
if Dir.const_defined? 'COMMON_APPDATA' then
190+
if Dir.const_defined? 'COMMON_APPDATA' # rubocop:disable Metrics/BlockNesting : Any attempt to alter this breaks it
197191
windows_facts_dot_d = File.join(Dir::COMMON_APPDATA, 'PuppetLabs', 'facter', 'facts.d')
198192
Facter::Util::DotD.new(windows_facts_dot_d).create
199193
end

lib/facter/package_provider.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
Facter.add(:package_provider) do
1414
setcode do
15-
if defined? Gem and Gem::Version.new(Facter.value(:puppetversion).split(' ')[0]) >= Gem::Version.new('3.6')
16-
Puppet::Type.type(:package).newpackage(:name => 'dummy', :allow_virtual => 'true')[:provider].to_s
15+
if defined? Gem && Gem::Version.new(Facter.value(:puppetversion).split(' ')[0]) >= Gem::Version.new('3.6')
16+
Puppet::Type.type(:package).newpackage(name: 'dummy', allow_virtual: 'true')[:provider].to_s
1717
else
18-
Puppet::Type.type(:package).newpackage(:name => 'dummy')[:provider].to_s
18+
Puppet::Type.type(:package).newpackage(name: 'dummy')[:provider].to_s
1919
end
2020
end
2121
end

lib/facter/pe_version.rb

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,53 @@
88
#
99
# Caveats:
1010
#
11-
Facter.add("pe_version") do
11+
Facter.add('pe_version') do
1212
setcode do
13-
puppet_ver = Facter.value("puppetversion")
14-
if puppet_ver != nil
15-
pe_ver = puppet_ver.match(/Puppet Enterprise (\d+\.\d+\.\d+)/)
13+
puppet_ver = Facter.value('puppetversion')
14+
if !puppet_ver.nil?
15+
pe_ver = puppet_ver.match(%r{Puppet Enterprise (\d+\.\d+\.\d+)})
1616
pe_ver[1] if pe_ver
1717
else
1818
nil
1919
end
2020
end
2121
end
2222

23-
Facter.add("is_pe") do
23+
Facter.add('is_pe') do
2424
setcode do
25-
if Facter.value(:pe_version).to_s.empty? then
25+
if Facter.value(:pe_version).to_s.empty?
2626
false
2727
else
2828
true
2929
end
3030
end
3131
end
3232

33-
Facter.add("pe_major_version") do
34-
confine :is_pe => true
33+
Facter.add('pe_major_version') do
34+
confine is_pe: true
3535
setcode do
36-
if pe_version = Facter.value(:pe_version)
36+
pe_version = Facter.value(:pe_version)
37+
if pe_version
3738
pe_version.to_s.split('.')[0]
3839
end
3940
end
4041
end
4142

42-
Facter.add("pe_minor_version") do
43-
confine :is_pe => true
43+
Facter.add('pe_minor_version') do
44+
confine is_pe: true
4445
setcode do
45-
if pe_version = Facter.value(:pe_version)
46+
pe_version = Facter.value(:pe_version)
47+
if pe_version
4648
pe_version.to_s.split('.')[1]
4749
end
4850
end
4951
end
5052

51-
Facter.add("pe_patch_version") do
52-
confine :is_pe => true
53+
Facter.add('pe_patch_version') do
54+
confine is_pe: true
5355
setcode do
54-
if pe_version = Facter.value(:pe_version)
56+
pe_version = Facter.value(:pe_version)
57+
if pe_version
5558
pe_version.to_s.split('.')[2]
5659
end
5760
end

lib/facter/puppet_settings.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
# #4248). It should (in the future) but for the time being we need to be
1414
# defensive which is what this rescue block is doing.
1515
rb_file = File.join(File.dirname(__FILE__), 'util', 'puppet_settings.rb')
16-
load rb_file if File.exists?(rb_file) or raise e
16+
load rb_file if File.exist?(rb_file) || raise(e)
1717
end
1818

19-
2019
# These will be nil if Puppet is not available.
2120
Facter.add(:puppet_vardir) do
2221
setcode do

lib/facter/root_home.rb

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
11
# A facter fact to determine the root home directory.
22
# This varies on PE supported platforms and may be
33
# reconfigured by the end user.
4-
54
module Facter::Util::RootHome
65
class << self
7-
def get_root_home
8-
root_ent = Facter::Util::Resolution.exec("getent passwd root")
6+
def returnt_root_home
7+
root_ent = Facter::Util::Resolution.exec('getent passwd root')
98
# The home directory is the sixth element in the passwd entry
109
# If the platform doesn't have getent, root_ent will be nil and we should
1110
# return it straight away.
12-
root_ent && root_ent.split(":")[5]
11+
root_ent && root_ent.split(':')[5]
1312
end
1413
end
1514
end
1615

1716
Facter.add(:root_home) do
18-
setcode { Facter::Util::RootHome.get_root_home }
17+
setcode { Facter::Util::RootHome.returnt_root_home }
1918
end
2019

2120
Facter.add(:root_home) do
22-
confine :kernel => :darwin
21+
confine kernel: :darwin
2322
setcode do
24-
str = Facter::Util::Resolution.exec("dscacheutil -q user -a name root")
23+
str = Facter::Util::Resolution.exec('dscacheutil -q user -a name root')
2524
hash = {}
2625
str.split("\n").each do |pair|
27-
key,value = pair.split(/:/)
26+
key, value = pair.split(%r{:})
2827
hash[key] = value
2928
end
3029
hash['dir'].strip
3130
end
3231
end
3332

3433
Facter.add(:root_home) do
35-
confine :kernel => :aix
34+
confine kernel: :aix
3635
root_home = nil
3736
setcode do
38-
str = Facter::Util::Resolution.exec("lsuser -c -a home root")
37+
str = Facter::Util::Resolution.exec('lsuser -c -a home root')
3938
str && str.split("\n").each do |line|
40-
next if line =~ /^#/
41-
root_home = line.split(/:/)[1]
39+
next if line =~ %r{^#}
40+
root_home = line.split(%r{:})[1]
4241
end
4342
root_home
4443
end

lib/facter/service_provider.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212

1313
Facter.add(:service_provider) do
1414
setcode do
15-
Puppet::Type.type(:service).newservice(:name => 'dummy')[:provider].to_s
15+
Puppet::Type.type(:service).newservice(name: 'dummy')[:provider].to_s
1616
end
1717
end

0 commit comments

Comments
 (0)