Skip to content

Commit

Permalink
add interpolation for ruby code
Browse files Browse the repository at this point in the history
  • Loading branch information
tphoney committed Sep 4, 2017
1 parent 0e7646c commit 31d78c3
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/puppet/parser/functions/mysql_deepmerge.rb
Expand Up @@ -19,15 +19,15 @@ module Puppet::Parser::Functions
ENDHEREDOC

if args.length < 2
raise Puppet::ParseError, "mysql_deepmerge(): wrong number of arguments (#{args.length}; must be at least 2)"
raise Puppet::ParseError, _('mysql_deepmerge(): wrong number of arguments (%{args_length}; must be at least 2)') % { args_length: args.length }
end

result = {}
args.each do |arg|
next if arg.is_a?(String) && arg.empty? # empty string is synonym for puppet's undef
# If the argument was not a hash, skip it.
unless arg.is_a?(Hash)
raise Puppet::ParseError, "mysql_deepmerge: unexpected argument type #{arg.class}, only expects hash arguments"
raise Puppet::ParseError, _('mysql_deepmerge: unexpected argument type %{arg_class}, only expects hash arguments') % { args_class: args.class }
end

# Now we have to traverse our hash assigning our non-hash values
Expand Down
5 changes: 1 addition & 4 deletions lib/puppet/parser/functions/mysql_dirname.rb
Expand Up @@ -6,13 +6,10 @@ module Puppet::Parser::Functions
) do |arguments|

if arguments.empty?
raise(Puppet::ParseError, 'mysql_dirname(): Wrong number of arguments ' \
"given (#{arguments.size} for 1)")
raise Puppet::ParseError, _('mysql_dirname(): Wrong number of arguments given (%{args_length} for 1)') % { args_length: args.length }
end

path = arguments[0]
return File.dirname(path)
end
end

# vim: set ts=2 sw=2 et :
3 changes: 1 addition & 2 deletions lib/puppet/parser/functions/mysql_password.rb
Expand Up @@ -8,8 +8,7 @@ module Puppet::Parser::Functions
) do |args|

if args.size != 1
raise(Puppet::ParseError, 'mysql_password(): Wrong number of arguments ' \
"given (#{args.size} for 1)")
raise Puppet::ParseError, _('mysql_password(): Wrong number of arguments given (%{args_length} for 1)') % { args_length: args.length }
end

return '' if args[0].empty?
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/provider/mysql_datadir/mysql.rb
Expand Up @@ -21,7 +21,7 @@ def create
# rubocop:enable Lint/UselessAssignment
unless defaults_extra_file.nil?
unless File.exist?(defaults_extra_file)
raise ArgumentError, "Defaults-extra-file #{defaults_extra_file} is missing"
raise ArgumentError, _('Defaults-extra-file %{file} is missing') % { file: defaults_extra_file }
end
defaults_extra_file = "--defaults-extra-file=#{defaults_extra_file}"
end
Expand Down
6 changes: 2 additions & 4 deletions lib/puppet/provider/mysql_grant/mysql.rb
Expand Up @@ -16,16 +16,14 @@ def self.instances
# of myhost.mydomain.my: root@myhost.mydomain.my, when MySQL is started
# with --skip-name-resolve.
next if e.inspect =~ %r{There is no such grant defined for user}
raise Puppet::Error, "#mysql had an error -> #{e.inspect}"
raise Puppet::Error, _('#mysql had an error -> %{inspect}') % { inspect: e.inspect }
end
# Once we have the list of grants generate entries for each.
grants.each_line do |grant|
# Match the munges we do in the type.
munged_grant = grant.delete("'").delete('`').delete('"')
# Matching: GRANT (SELECT, UPDATE) PRIVILEGES ON (*.*) TO ('root')@('127.0.0.1') (WITH GRANT OPTION)
# rubocop:disable Lint/AssignmentInCondition
next unless match = munged_grant.match(%r{^GRANT\s(.+)\sON\s(.+)\sTO\s(.*)@(.*?)(\s.*)?$})
# rubocop:enable Lint/AssignmentInCondition
next unless match = munged_grant.match(%r{^GRANT\s(.+)\sON\s(.+)\sTO\s(.*)@(.*?)(\s.*)?$}) # rubocop:disable Lint/AssignmentInCondition
privileges, table, user, host, rest = match.captures
table.gsub!('\\\\', '\\')

Expand Down
4 changes: 2 additions & 2 deletions lib/puppet/type/mysql_grant.rb
Expand Up @@ -54,7 +54,7 @@ def initialize(*args)
validate do |value|
mysql_version = Facter.value(:mysql_version)
if value =~ %r{proxy}i && Puppet::Util::Package.versioncmp(mysql_version, '5.5.0') < 0
raise(ArgumentError, "PROXY user not supported on mysql versions < 5.5.0. Current version #{mysql_version}")
raise(ArgumentError, _('PROXY user not supported on mysql versions < 5.5.0. Current version %{version}') % { version: mysql_version })
end
end
end
Expand Down Expand Up @@ -93,7 +93,7 @@ def initialize(*args)
user_part = matches[1]
host_part = matches[2]
else
raise(ArgumentError, "Invalid database user #{value}")
raise(ArgumentError, _('Invalid database user %{user}') % { user: value })
end
# rubocop:enable Lint/AssignmentInCondition
# rubocop:enable Lint/UselessAssignment
Expand Down
4 changes: 2 additions & 2 deletions lib/puppet/type/mysql_user.rb
Expand Up @@ -26,7 +26,7 @@
user_part = matches[1]
host_part = matches[2]
else
raise(ArgumentError, "Invalid database user #{value}")
raise ArgumentError, _('Invalid database user %{user}') % { user: value }
end
# rubocop:enable Lint/AssignmentInCondition
# rubocop:enable Lint/UselessAssignment
Expand Down Expand Up @@ -84,7 +84,7 @@
else
value.each do |opt|
o = opt.match(%r{^(CIPHER|ISSUER|SUBJECT)}i)
raise(ArgumentError, "Invalid tls option #{o}") unless o
raise(ArgumentError, _('Invalid tls option %{option}') % { option: o }) unless o
end
end
end
Expand Down
18 changes: 16 additions & 2 deletions spec/acceptance/locales_spec.rb
@@ -1,7 +1,7 @@
require 'spec_helper_acceptance'
require 'beaker/i18n_helper'

describe 'mysql localization', unless: fact('operatingsystem') == 'Debian' do
describe 'mysql localization', if: fact('osfamily') == 'Debian' do
before :all do
hosts.each do |host|
on(host, "sed -i \"96i FastGettext.locale='ja'\" /opt/puppetlabs/puppet/lib/ruby/vendor_ruby/puppet.rb")
Expand Down Expand Up @@ -37,7 +37,7 @@ class { 'mysql::server':
end
end

context 'when triggering ruby string warning' do
context 'when triggering ruby simple string warning' do
let(:pp) do
<<-EOS
mysql::db { 'mydb':
Expand All @@ -57,6 +57,20 @@ class { 'mysql::server':
end
end

context 'when triggering ruby interpolated string warning' do
let(:pp) do
<<-EOS
$password_hash = mysql_password('new_password', 'should not have second parameter')
EOS
end

it 'displays Japanese error' do
apply_manifest(pp, expect_failures: true) do |r|
expect(r.stderr).not_to match(%r{mysql_password(): Wrong number of arguments given (2 for 1)}i)
end
end
end

after :all do
hosts.each do |host|
on(host, 'sed -i "96d" /opt/puppetlabs/puppet/lib/ruby/vendor_ruby/puppet.rb')
Expand Down

0 comments on commit 31d78c3

Please sign in to comment.