Skip to content

Commit

Permalink
Merge pull request #763 from rodjek/issue-759
Browse files Browse the repository at this point in the history
Restore Ruby 1.8.7 support
  • Loading branch information
rodjek committed Sep 28, 2017
2 parents 03c5e3d + f1efeba commit 0799152
Show file tree
Hide file tree
Showing 50 changed files with 114 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Expand Up @@ -62,7 +62,7 @@ Style/MethodCalledOnDoEndBlock:
Style/RegexpLiteral:
EnforcedStyle: percent_r
Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: comma
EnforcedStyleForMultiline: no_comma
Style/TrailingCommaInLiteral:
EnforcedStyleForMultiline: comma
Style/FormatStringToken:
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -11,6 +11,7 @@ script:
env:
- CHECK=ci
rvm:
- 1.8.7
- 1.9.3
- 2.0.0
- 2.1.10
Expand Down
1 change: 1 addition & 0 deletions lib/puppet-lint.rb
Expand Up @@ -8,6 +8,7 @@
require 'puppet-lint/data'
require 'puppet-lint/checks'
require 'puppet-lint/bin'
require 'puppet-lint/monkeypatches'

class PuppetLint::NoCodeError < StandardError; end
class PuppetLint::NoFix < StandardError; end
Expand Down
6 changes: 3 additions & 3 deletions lib/puppet-lint/checks.rb
Expand Up @@ -68,7 +68,7 @@ def run(fileinfo, data)

@problems
rescue => e
puts <<-END.gsub(%r{^ {6}}, '')
$stdout.puts <<-END.gsub(%r{^ {6}}, '')
Whoops! It looks like puppet-lint has encountered an error that it doesn't
know how to handle. Please open an issue at https://github.com/rodjek/puppet-lint
and paste the following output into the issue description.
Expand All @@ -80,15 +80,15 @@ def run(fileinfo, data)
END

if File.readable?(fileinfo)
puts [
$stdout.puts [
'file contents:',
'```',
File.read(fileinfo),
'```',
].join("\n")
end

puts [
$stdout.puts [
'error:',
'```',
"#{e.class}: #{e.message}",
Expand Down
4 changes: 2 additions & 2 deletions lib/puppet-lint/configuration.rb
Expand Up @@ -50,9 +50,9 @@ def self.add_check(check)
#
# <option>=(value)
def method_missing(method, *args, &_block)
super unless method.to_s =~ %r{^(?<option>\w+)=?$}
super unless method.to_s =~ %r{^(\w+)=?$}

option = Regexp.last_match(:option)
option = Regexp.last_match(1)
add_option(option.to_s) if settings[option].nil?
settings[option] = args[0] unless args.empty?
end
Expand Down
12 changes: 6 additions & 6 deletions lib/puppet-lint/lexer.rb
Expand Up @@ -170,7 +170,7 @@ def heredoc_queue
# \v == vertical tab
# \f == form feed
# \p{Zs} == ASCII + Unicode non-linebreaking whitespace
WHITESPACE_RE = %r{[\t\v\f\p{Zs}]}
WHITESPACE_RE = RUBY_VERSION == '1.8.7' ? %r{[\t\v\f ]} : %r{[\t\v\f\p{Zs}]}

# Internal: Access the internal token storage.
#
Expand Down Expand Up @@ -266,7 +266,7 @@ def tokenise(code)
else
heredoc_tag = heredoc_queue.shift
heredoc_name = heredoc_tag[%r{\A"?(.+?)"?(:.+?)?(/.*)?\Z}, 1]
str_contents = StringScanner.new(code[i + length..-1]).scan_until(%r{\|?\s*-?\s*#{heredoc_name}})
str_contents = StringScanner.new(code[(i + length)..-1]).scan_until(%r{\|?\s*-?\s*#{heredoc_name}})
interpolate_heredoc(str_contents, heredoc_tag)
length += str_contents.size
end
Expand All @@ -282,8 +282,8 @@ def tokenise(code)
unless heredoc_queue.empty?
heredoc_tag = heredoc_queue.shift
heredoc_name = heredoc_tag[%r{\A"?(.+?)"?(:.+?)?(/.*)?\Z}, 1]
str_contents = StringScanner.new(code[i + length..-1]).scan_until(%r{\|?\s*-?\s*#{heredoc_name}})
_ = code[0..i + length].split("\n")
str_contents = StringScanner.new(code[(i + length)..-1]).scan_until(%r{\|?\s*-?\s*#{heredoc_name}})
_ = code[0..(i + length)].split("\n")
interpolate_heredoc(str_contents, heredoc_tag)
length += str_contents.size
end
Expand Down Expand Up @@ -557,8 +557,8 @@ def get_heredoc_segment(string, eos_text, interpolate = true)

str = string.scan_until(regexp)
begin
str =~ %r{\A(?<value>.*?)(?<terminator>[$]+|\|?\s*-?#{Regexp.escape(eos_text)})\Z}m
[Regexp.last_match(:value), Regexp.last_match(:terminator)]
str =~ %r{\A(.*?)([$]+|\|?\s*-?#{Regexp.escape(eos_text)})\Z}m
[Regexp.last_match(1), Regexp.last_match(2)]
rescue
[nil, nil]
end
Expand Down
51 changes: 51 additions & 0 deletions lib/puppet-lint/monkeypatches.rb
@@ -0,0 +1,51 @@
begin
'%{test}' % { :test => 'replaced' } == 'replaced' # rubocop:disable Style/FormatString
rescue
# monkeypatch String#% into Ruby 1.8.7
class String
Percent = instance_method('%') unless defined?(Percent)

def %(*a, &b)
a.flatten!

string = case a.last
when Hash
expand(a.pop)
else
self
end

if a.empty?
string
else
Percent.bind(string).call(a, &b)
end
end

def expand!(vars = {})
loop do
changed = false
vars.each do |var, value|
var = var.to_s
var.gsub!(%r{[^a-zA-Z0-9_]}, '')
changed = gsub!(%r{\%\{#{var}\}}, value.to_s)
end
break unless changed
end
self
end

def expand(opts = {})
dup.expand!(opts)
end
end
end

unless String.respond_to?(:prepend)
# monkeypatch String#prepend into Ruby 1.8.7
class String
def prepend(lead)
replace("#{lead}#{self}")
end
end
end
2 changes: 1 addition & 1 deletion lib/puppet-lint/optparser.rb
Expand Up @@ -44,7 +44,7 @@ def self.build
opts.on(
'--error-level LEVEL',
[:all, :warning, :error],
'The level of error to return (warning, error or all).',
'The level of error to return (warning, error or all).'
) do |el|
PuppetLint.configuration.error_level = el
end
Expand Down
Expand Up @@ -12,7 +12,7 @@ def check
:message => 'arrow should be on the right operand\'s line',
:line => token.line,
:column => token.column,
:token => token,
:token => token
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet-lint/plugins/check_classes/autoloader_layout.rb
Expand Up @@ -27,7 +27,7 @@ def check
:error,
:message => "#{title_token.value} not in autoload module layout",
:line => title_token.line,
:column => title_token.column,
:column => title_token.column
)
end
end
Expand Down
Expand Up @@ -11,7 +11,7 @@ def check
:warning,
:message => 'class inheriting from params class',
:line => class_idx[:inherited_token].line,
:column => class_idx[:inherited_token].column,
:column => class_idx[:inherited_token].column
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet-lint/plugins/check_classes/code_on_top_scope.rb
Expand Up @@ -13,7 +13,7 @@ def check
:warning,
:message => "code outside of class or define block - #{token.value}",
:line => token.line,
:column => token.column,
:column => token.column
)
end
end
Expand Down
Expand Up @@ -16,7 +16,7 @@ def check
:warning,
:message => 'class inherits across module namespaces',
:line => class_idx[:inherited_token].line,
:column => class_idx[:inherited_token].column,
:column => class_idx[:inherited_token].column
)
end
end
Expand Down
Expand Up @@ -17,7 +17,7 @@ def check
:error,
:message => "#{obj_type} name containing a dash",
:line => class_idx[:name_token].line,
:column => class_idx[:name_token].column,
:column => class_idx[:name_token].column
)
end
end
Expand Down
Expand Up @@ -18,7 +18,7 @@ def check
:message => "#{obj_type} '#{class_idx[:name_token].value}' contains illegal uppercase",
:line => class_idx[:name_token].line,
:column => class_idx[:name_token].column,
:token => class_idx[:name_token],
:token => class_idx[:name_token]
)
end
end
Expand Down
Expand Up @@ -19,7 +19,7 @@ def check
:warning,
:message => "#{type} defined inside a class",
:line => token.line,
:column => token.column,
:column => token.column
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet-lint/plugins/check_classes/parameter_order.rb
Expand Up @@ -32,7 +32,7 @@ def check
:warning,
:message => msg,
:line => token.line,
:column => token.column,
:column => token.column
)
end
end
Expand Down
Expand Up @@ -9,7 +9,7 @@ def check
:warning,
:message => 'right-to-left (<-) relationship',
:line => token.line,
:column => token.column,
:column => token.column
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet-lint/plugins/check_classes/variable_scope.rb
Expand Up @@ -132,7 +132,7 @@ def check
:warning,
:message => msg,
:line => token.line,
:column => token.column,
:column => token.column
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet-lint/plugins/check_comments/slash_comments.rb
Expand Up @@ -12,7 +12,7 @@ def check
:message => '// comment found',
:line => token.line,
:column => token.column,
:token => token,
:token => token
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet-lint/plugins/check_comments/star_comments.rb
Expand Up @@ -12,7 +12,7 @@ def check
:message => '/* */ comment found',
:line => token.line,
:column => token.column,
:token => token,
:token => token
)
end
end
Expand Down
Expand Up @@ -37,7 +37,7 @@ def check
:warning,
:message => 'case statement without a default case',
:line => case_tokens.first.line,
:column => case_tokens.first.column,
:column => case_tokens.first.column
)
end
end
Expand Down
Expand Up @@ -14,7 +14,7 @@ def check
:warning,
:message => 'selector inside resource block',
:line => token.line,
:column => token.column,
:column => token.column
)
end
end
Expand Down
Expand Up @@ -24,7 +24,7 @@ def check
:warning,
:message => "#{type} not documented",
:line => first_token.line,
:column => first_token.column,
:column => first_token.column
)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/puppet-lint/plugins/check_nodes/unquoted_node_name.rb
Expand Up @@ -14,7 +14,7 @@ def check
:check => :syntax,
:message => 'Syntax error (try running `puppet parser validate <file>`)',
:line => node.line,
:column => node.column,
:column => node.column
)
next
end
Expand All @@ -29,7 +29,7 @@ def check
:message => 'unquoted node name found',
:line => token.line,
:column => token.column,
:token => token,
:token => token
)
end
end
Expand Down
Expand Up @@ -26,7 +26,7 @@ def check
:error,
:message => 'duplicate parameter found in resource',
:line => prev_token.line,
:column => prev_token.column,
:column => prev_token.column
)
else
seen_params[level] << prev_token.value
Expand Down
Expand Up @@ -20,7 +20,7 @@ def check
:message => "ensure found on line but it's not the first attribute",
:line => ensure_token.line,
:column => ensure_token.column,
:resource => resource,
:resource => resource
)
end
end
Expand Down
Expand Up @@ -20,7 +20,7 @@ def check
:line => value_token.line,
:column => value_token.column,
:param_token => ensure_token,
:value_token => value_token,
:value_token => value_token
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet-lint/plugins/check_resources/file_mode.rb
Expand Up @@ -26,7 +26,7 @@ def check
:message => MSG,
:line => value_token.line,
:column => value_token.column,
:token => value_token,
:token => value_token
)
end
end
Expand Down
Expand Up @@ -20,7 +20,7 @@ def check
:message => 'unquoted file mode',
:line => value_token.line,
:column => value_token.column,
:token => value_token,
:token => value_token
)
end
end
Expand Down
Expand Up @@ -12,7 +12,7 @@ def check
:message => 'unquoted resource title',
:line => token.line,
:column => token.column,
:token => token,
:token => token
)
end
end
Expand Down
Expand Up @@ -16,7 +16,7 @@ def check
:message => 'double quoted string containing no variables',
:line => token.line,
:column => token.column,
:token => token,
:token => token
)
end
end
Expand Down
Expand Up @@ -30,7 +30,7 @@ def check
:column => var_token.column,
:start_token => start_token,
:var_token => var_token,
:end_token => eos_token,
:end_token => eos_token
)
end
break
Expand Down

0 comments on commit 0799152

Please sign in to comment.