From 31d78c3e440f843af979f12b5e32af383fe86a6a Mon Sep 17 00:00:00 2001 From: tphoney Date: Mon, 4 Sep 2017 13:00:39 +0100 Subject: [PATCH] add interpolation for ruby code --- lib/puppet/parser/functions/mysql_deepmerge.rb | 4 ++-- lib/puppet/parser/functions/mysql_dirname.rb | 5 +---- lib/puppet/parser/functions/mysql_password.rb | 3 +-- lib/puppet/provider/mysql_datadir/mysql.rb | 2 +- lib/puppet/provider/mysql_grant/mysql.rb | 6 ++---- lib/puppet/type/mysql_grant.rb | 4 ++-- lib/puppet/type/mysql_user.rb | 4 ++-- spec/acceptance/locales_spec.rb | 18 ++++++++++++++++-- 8 files changed, 27 insertions(+), 19 deletions(-) diff --git a/lib/puppet/parser/functions/mysql_deepmerge.rb b/lib/puppet/parser/functions/mysql_deepmerge.rb index 68aae8f3a..5959a0fde 100644 --- a/lib/puppet/parser/functions/mysql_deepmerge.rb +++ b/lib/puppet/parser/functions/mysql_deepmerge.rb @@ -19,7 +19,7 @@ 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 = {} @@ -27,7 +27,7 @@ module Puppet::Parser::Functions 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 diff --git a/lib/puppet/parser/functions/mysql_dirname.rb b/lib/puppet/parser/functions/mysql_dirname.rb index 33680a7dd..e1a21202d 100644 --- a/lib/puppet/parser/functions/mysql_dirname.rb +++ b/lib/puppet/parser/functions/mysql_dirname.rb @@ -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 : diff --git a/lib/puppet/parser/functions/mysql_password.rb b/lib/puppet/parser/functions/mysql_password.rb index 3d3f4f841..4169bf433 100644 --- a/lib/puppet/parser/functions/mysql_password.rb +++ b/lib/puppet/parser/functions/mysql_password.rb @@ -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? diff --git a/lib/puppet/provider/mysql_datadir/mysql.rb b/lib/puppet/provider/mysql_datadir/mysql.rb index 64ac460ae..55740c30f 100644 --- a/lib/puppet/provider/mysql_datadir/mysql.rb +++ b/lib/puppet/provider/mysql_datadir/mysql.rb @@ -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 diff --git a/lib/puppet/provider/mysql_grant/mysql.rb b/lib/puppet/provider/mysql_grant/mysql.rb index c54d2b6f5..b32404fca 100644 --- a/lib/puppet/provider/mysql_grant/mysql.rb +++ b/lib/puppet/provider/mysql_grant/mysql.rb @@ -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!('\\\\', '\\') diff --git a/lib/puppet/type/mysql_grant.rb b/lib/puppet/type/mysql_grant.rb index b544b24d9..587db5ac1 100644 --- a/lib/puppet/type/mysql_grant.rb +++ b/lib/puppet/type/mysql_grant.rb @@ -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 @@ -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 diff --git a/lib/puppet/type/mysql_user.rb b/lib/puppet/type/mysql_user.rb index 075a144e2..ae38c383f 100644 --- a/lib/puppet/type/mysql_user.rb +++ b/lib/puppet/type/mysql_user.rb @@ -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 @@ -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 diff --git a/spec/acceptance/locales_spec.rb b/spec/acceptance/locales_spec.rb index 50db50865..45c4cd0c1 100644 --- a/spec/acceptance/locales_spec.rb +++ b/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") @@ -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': @@ -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')