From a9517accd9627da2174828a14e52d4fae7ba8140 Mon Sep 17 00:00:00 2001 From: mishina Date: Mon, 30 May 2022 23:26:06 +0900 Subject: [PATCH] Fix some simple cops --- .rubocop.yml | 3 +++ Gemfile | 10 +++++----- benchmark/allocations.rb | 2 +- lib/mysql2.rb | 1 + lib/mysql2/client.rb | 4 ++++ spec/mysql2/client_spec.rb | 2 ++ spec/mysql2/result_spec.rb | 18 +++++++++--------- spec/mysql2/statement_spec.rb | 24 ++++++++++++------------ spec/spec_helper.rb | 3 ++- support/mysql_enc_to_ruby.rb | 2 +- 10 files changed, 40 insertions(+), 29 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index a9478b791..74224c6b4 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -18,6 +18,9 @@ Layout/FirstHashElementIndentation: Layout/EndAlignment: EnforcedStyleAlignWith: variable +Layout/HashAlignment: + EnforcedHashRocketStyle: table + Style/TrailingCommaInArguments: EnforcedStyleForMultiline: consistent_comma diff --git a/Gemfile b/Gemfile index e6575bc1d..96429bf8a 100644 --- a/Gemfile +++ b/Gemfile @@ -2,11 +2,11 @@ source 'https://rubygems.org' gemspec -gem 'rake', if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.2") - '~> 13.0.1' - else - '< 13' - end +if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.2") + gem 'rake', '~> 13.0.1' +else + gem 'rake', '< 13' +end gem 'rake-compiler', '~> 1.1.0' # For local debugging, irb is Gemified since Ruby 2.6 diff --git a/benchmark/allocations.rb b/benchmark/allocations.rb index 7926a837d..cc6df4d18 100644 --- a/benchmark/allocations.rb +++ b/benchmark/allocations.rb @@ -16,7 +16,7 @@ def bench_allocations(feature, iterations = 10, batch_size = 1000) GC::Profiler.clear GC::Profiler.enable iterations.times { yield batch_size } - GC::Profiler.report(STDOUT) + GC::Profiler.report($stdout) GC::Profiler.disable end diff --git a/lib/mysql2.rb b/lib/mysql2.rb index e49060208..9461846e9 100644 --- a/lib/mysql2.rb +++ b/lib/mysql2.rb @@ -65,6 +65,7 @@ module Util # def self.key_hash_as_symbols(hash) return nil unless hash + Hash[hash.map { |k, v| [k.to_sym, v] }] end diff --git a/lib/mysql2/client.rb b/lib/mysql2/client.rb index 8a4a3e11b..582b6e305 100644 --- a/lib/mysql2/client.rb +++ b/lib/mysql2/client.rb @@ -20,6 +20,7 @@ def self.default_query_options def initialize(opts = {}) raise Mysql2::Error, "Options parameter must be a Hash" unless opts.is_a? Hash + opts = Mysql2::Util.key_hash_as_symbols(opts) @read_timeout = nil @query_options = self.class.default_query_options.dup @@ -33,6 +34,7 @@ def initialize(opts = {}) # TODO: stricter validation rather than silent massaging %i[reconnect connect_timeout local_infile read_timeout write_timeout default_file default_group secure_auth init_command automatic_close enable_cleartext_plugin default_auth].each do |key| next unless opts.key?(key) + case key when :reconnect, :local_infile, :secure_auth, :automatic_close, :enable_cleartext_plugin send(:"#{key}=", !!opts[key]) # rubocop:disable Style/DoubleNegation @@ -136,6 +138,7 @@ def find_default_ca_path # and performance_schema.session_account_connect_attrs def parse_connect_attrs(conn_attrs) return {} if Mysql2::Client::CONNECT_ATTRS.zero? + conn_attrs ||= {} conn_attrs[:program_name] ||= $PROGRAM_NAME conn_attrs.each_with_object({}) do |(key, value), hash| @@ -152,6 +155,7 @@ def query(sql, options = {}) def query_info info = query_info_string return {} unless info + info_hash = {} info.split.each_slice(2) { |s| info_hash[s[0].downcase.delete(':').to_sym] = s[1].to_i } info_hash diff --git a/spec/mysql2/client_spec.rb b/spec/mysql2/client_spec.rb index 5861882c6..2f757815e 100644 --- a/spec/mysql2/client_spec.rb +++ b/spec/mysql2/client_spec.rb @@ -54,6 +54,7 @@ Klient = Class.new(Mysql2::Client) do attr_reader :connect_args + def connect(*args) @connect_args ||= [] @connect_args << args @@ -212,6 +213,7 @@ def run_gc 10.times do closed = @client.query("SHOW PROCESSLIST").none? { |row| row['Id'] == connection_id } break if closed + sleep(0.1) end expect(closed).to eq(true) diff --git a/spec/mysql2/result_spec.rb b/spec/mysql2/result_spec.rb index 614cafc1a..ed3e9d262 100644 --- a/spec/mysql2/result_spec.rb +++ b/spec/mysql2/result_spec.rb @@ -497,17 +497,17 @@ end { - 'char_test' => 'CHAR', - 'varchar_test' => 'VARCHAR', - 'varbinary_test' => 'VARBINARY', - 'tiny_blob_test' => 'TINYBLOB', - 'tiny_text_test' => 'TINYTEXT', - 'blob_test' => 'BLOB', - 'text_test' => 'TEXT', + 'char_test' => 'CHAR', + 'varchar_test' => 'VARCHAR', + 'varbinary_test' => 'VARBINARY', + 'tiny_blob_test' => 'TINYBLOB', + 'tiny_text_test' => 'TINYTEXT', + 'blob_test' => 'BLOB', + 'text_test' => 'TEXT', 'medium_blob_test' => 'MEDIUMBLOB', 'medium_text_test' => 'MEDIUMTEXT', - 'long_blob_test' => 'LONGBLOB', - 'long_text_test' => 'LONGTEXT', + 'long_blob_test' => 'LONGBLOB', + 'long_text_test' => 'LONGTEXT', }.each do |field, type| it "should return a String for #{type}" do expect(test_result[field]).to be_an_instance_of(String) diff --git a/spec/mysql2/statement_spec.rb b/spec/mysql2/statement_spec.rb index 3c00d0ffc..57e590804 100644 --- a/spec/mysql2/statement_spec.rb +++ b/spec/mysql2/statement_spec.rb @@ -1,4 +1,4 @@ -require './spec/spec_helper.rb' +require './spec/spec_helper' RSpec.describe Mysql2::Statement do before(:example) do @@ -277,7 +277,7 @@ def stmt_count end context "#each" do - # note: The current impl. of prepared statement requires results to be cached on #execute except for streaming queries + # NOTE: The current impl. of prepared statement requires results to be cached on #execute except for streaming queries # The drawback of this is that args of Result#each is ignored... it "should yield rows as hash's" do @@ -320,7 +320,7 @@ def stmt_count result = @client.prepare("SELECT 1 UNION SELECT 2").execute(stream: true, cache_rows: false) expect do result.each {} - result.each {} + result.each {} # rubocop:disable Style/CombinableLoops end.to raise_exception(Mysql2::Error) end end @@ -573,17 +573,17 @@ def stmt_count end { - 'char_test' => 'CHAR', - 'varchar_test' => 'VARCHAR', - 'varbinary_test' => 'VARBINARY', - 'tiny_blob_test' => 'TINYBLOB', - 'tiny_text_test' => 'TINYTEXT', - 'blob_test' => 'BLOB', - 'text_test' => 'TEXT', + 'char_test' => 'CHAR', + 'varchar_test' => 'VARCHAR', + 'varbinary_test' => 'VARBINARY', + 'tiny_blob_test' => 'TINYBLOB', + 'tiny_text_test' => 'TINYTEXT', + 'blob_test' => 'BLOB', + 'text_test' => 'TEXT', 'medium_blob_test' => 'MEDIUMBLOB', 'medium_text_test' => 'MEDIUMTEXT', - 'long_blob_test' => 'LONGBLOB', - 'long_text_test' => 'LONGTEXT', + 'long_blob_test' => 'LONGBLOB', + 'long_text_test' => 'LONGTEXT', }.each do |field, type| it "should return a String for #{type}" do expect(test_result[field]).to be_an_instance_of(String) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2e86e112c..594e7d339 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -32,6 +32,7 @@ def new_client(option_overrides = {}) @clients ||= [] @clients << client return client unless block_given? + begin yield client ensure @@ -42,7 +43,7 @@ def new_client(option_overrides = {}) def num_classes # rubocop:disable Lint/UnifiedInteger - 0.class == Integer ? [Integer] : [Fixnum, Bignum] + 0.instance_of?(Integer) ? [Integer] : [Fixnum, Bignum] # rubocop:enable Lint/UnifiedInteger end diff --git a/support/mysql_enc_to_ruby.rb b/support/mysql_enc_to_ruby.rb index 33c878885..4db703409 100644 --- a/support/mysql_enc_to_ruby.rb +++ b/support/mysql_enc_to_ruby.rb @@ -55,7 +55,7 @@ collations.each do |collation| mysql_col_idx = collation[2].to_i rb_enc = mysql_to_rb.fetch(collation[1]) do |mysql_enc| - $stderr.puts "WARNING: Missing mapping for collation \"#{collation[0]}\" with encoding \"#{mysql_enc}\" and id #{mysql_col_idx}, assuming NULL" + warn "WARNING: Missing mapping for collation \"#{collation[0]}\" with encoding \"#{mysql_enc}\" and id #{mysql_col_idx}, assuming NULL" "NULL" end encodings[mysql_col_idx - 1] = [mysql_col_idx, rb_enc]