Skip to content

Commit c2bc038

Browse files
committed
Added test for the missing ::Async::Reactor.run.
1 parent 0720377 commit c2bc038

File tree

13 files changed

+29
-37
lines changed

13 files changed

+29
-37
lines changed

.rubocop.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
AllCops:
22
SuggestExtensions: false
33
TargetRubyVersion: 2.6
4+
NewCops: enable
45

56
Metrics:
67
Enabled: false

.rubocop_todo.yml

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2023-03-02 20:56:34 UTC using RuboCop version 1.47.0.
3+
# on 2023-07-26 19:57:49 UTC using RuboCop version 1.47.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -77,16 +77,6 @@ Style/GlobalStdStream:
7777
- 'spec/database_adapters/activerecord/activerecord.rb'
7878
- 'tasks/db.rake'
7979

80-
# Offense count: 18
81-
# This cop supports safe autocorrection (--autocorrect).
82-
# Configuration parameters: EnforcedStyle, EnforcedShorthandSyntax, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
83-
# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
84-
# SupportedShorthandSyntax: always, never, either, consistent
85-
Style/HashSyntax:
86-
Exclude:
87-
- 'lib/slack-ruby-bot-server/api/endpoints/teams_endpoint.rb'
88-
- 'lib/slack-ruby-bot-server/models/team/methods.rb'
89-
9080
# Offense count: 1
9181
# This cop supports unsafe autocorrection (--autocorrect-all).
9282
Style/HashTransformKeys:
@@ -99,6 +89,12 @@ Style/HashTransformValues:
9989
Exclude:
10090
- 'lib/slack-ruby-bot-server/api/helpers/error_helpers.rb'
10191

92+
# Offense count: 2
93+
# This cop supports unsafe autocorrection (--autocorrect-all).
94+
Style/MapToHash:
95+
Exclude:
96+
- 'spec/support/api/endpoints/it_behaves_like_a_cursor_api.rb'
97+
10298
# Offense count: 1
10399
# This cop supports unsafe autocorrection (--autocorrect-all).
104100
# Configuration parameters: EnforcedStyle, Autocorrect.

Gemfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
source 'https://rubygems.org'
22

3-
case ENV['DATABASE_ADAPTER']
3+
case ENV.fetch('DATABASE_ADAPTER', nil)
44
when 'mongoid' then
55
gem 'kaminari-mongoid'
66
gem 'mongoid', ENV['MONGOID_VERSION'] || '~> 7.3.0'
@@ -14,7 +14,7 @@ when 'activerecord' then
1414
when nil
1515
warn "Missing ENV['DATABASE_ADAPTER']."
1616
else
17-
warn "Invalid ENV['DATABASE_ADAPTER']: #{ENV['DATABASE_ADAPTER']}."
17+
warn "Invalid ENV['DATABASE_ADAPTER']: #{ENV.fetch('DATABASE_ADAPTER', nil)}."
1818
end
1919

2020
gemspec

Rakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'rspec/core'
55
require 'rspec/core/rake_task'
66

77
RSpec::Core::RakeTask.new(:spec) do |spec|
8-
spec.pattern = FileList['spec/**/*_spec.rb'].exclude(%r{ext\/(?!#{ENV['DATABASE_ADAPTER']})})
8+
spec.pattern = FileList['spec/**/*_spec.rb'].exclude(%r{ext\/(?!#{ENV.fetch('DATABASE_ADAPTER', nil)})})
99
end
1010

1111
require 'rubocop/rake_task'

lib/slack-ruby-bot-server/api/endpoints/teams_endpoint.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ class TeamsEndpoint < Grape::API
4141
raise 'Missing SLACK_CLIENT_ID or SLACK_CLIENT_SECRET.' unless ENV.key?('SLACK_CLIENT_ID') && ENV.key?('SLACK_CLIENT_SECRET')
4242

4343
options = {
44-
client_id: ENV['SLACK_CLIENT_ID'],
45-
client_secret: ENV['SLACK_CLIENT_SECRET'],
44+
client_id: ENV.fetch('SLACK_CLIENT_ID', nil),
45+
client_secret: ENV.fetch('SLACK_CLIENT_SECRET', nil),
4646
code: params[:code]
4747
}
4848

lib/slack-ruby-bot-server/api/middleware.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'rack/cors'
22
require 'rack-rewrite'
33
require 'rack-server-pages'
4-
require 'otr-activerecord' if SlackRubyBotServer::Config.activerecord? && !defined?(::Rails)
4+
require 'otr-activerecord' if SlackRubyBotServer::Config.activerecord? && !defined?(Rails)
55

66
module SlackRubyBotServer
77
module Api

lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ def self.init!
3333
end
3434
end
3535

36-
::Boolean = Grape::API::Boolean
36+
Boolean = Grape::API::Boolean

lib/slack-ruby-bot-server/service.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ def start_from_database!
7878
start_intervals!
7979
end
8080

81-
def start_intervals!
82-
::Async::Reactor.run do
81+
def start_intervals!(&_block)
82+
::Async::Reactor.run do |task|
8383
@intervals.each_pair do |period, calls_with_options|
8484
calls_with_options.each do |call_with_options|
8585
call, options = *call_with_options
@@ -88,6 +88,7 @@ def start_intervals!
8888
end
8989
end
9090
end
91+
yield task if block_given?
9192
end
9293
end
9394

slack-ruby-bot-server.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
2424
spec.add_dependency 'rack-rewrite'
2525
spec.add_dependency 'rack-server-pages'
2626
spec.add_dependency 'slack-ruby-client'
27+
spec.metadata['rubygems_mfa_required'] = 'true'
2728
end

spec/database_adapters/activerecord/activerecord.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
yml = ERB.new(File.read(File.expand_path('config/postgresql.yml', __dir__))).result
22
db_config = if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0.pre1')
3-
::YAML.safe_load(yml, aliases: true)[ENV['RACK_ENV']]
3+
YAML.safe_load(yml, aliases: true)[ENV.fetch('RACK_ENV', nil)]
44
else
5-
::YAML.safe_load(yml, [], [], true)[ENV['RACK_ENV']]
5+
YAML.safe_load(yml, [], [], true)[ENV.fetch('RACK_ENV', nil)]
66
end
77
ActiveRecord::Tasks::DatabaseTasks.create(db_config)
88
ActiveRecord::Base.establish_connection(db_config)
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Mongo::Logger.logger.level = Logger::INFO
2-
Mongoid.load!(File.expand_path('config/mongoid.yml', __dir__), ENV['RACK_ENV'])
2+
Mongoid.load!(File.expand_path('config/mongoid.yml', __dir__), ENV.fetch('RACK_ENV', nil))

spec/slack-ruby-bot-server/service_spec.rb

+5-12
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@
5656
let(:instance) { SlackRubyBotServer::Service.instance }
5757
context 'without timers' do
5858
it 'noop' do
59-
Async::Reactor.run do |task|
60-
instance.start_intervals!
61-
task.stop
62-
end
59+
instance.start_intervals!(&:stop)
6360
expect(instance.instance_variable_get(:@intervals).keys).to eq []
6461
end
6562
end
@@ -94,8 +91,7 @@
9491
end
9592
end
9693
it 'sets up timers' do
97-
Async::Reactor.run do |task|
98-
instance.start_intervals!
94+
instance.start_intervals! do |task|
9995
task.sleep 3
10096
task.stop
10197
end
@@ -115,8 +111,7 @@
115111
end
116112
end
117113
it 'does not abort all timers on failure of the first one' do
118-
Async::Reactor.run do |task|
119-
instance.start_intervals!
114+
instance.start_intervals! do |task|
120115
task.sleep 3
121116
task.stop
122117
end
@@ -132,8 +127,7 @@
132127
end
133128
end
134129
it 'runs the timer once within 3 seconds' do
135-
Async::Reactor.run do |task|
136-
instance.start_intervals!
130+
instance.start_intervals! do |task|
137131
task.sleep 3
138132
task.stop
139133
end
@@ -148,8 +142,7 @@
148142
end
149143
end
150144
it 'runs the timer exactly twice within 3 seconds' do
151-
Async::Reactor.run do |task|
152-
instance.start_intervals!
145+
instance.start_intervals! do |task|
153146
task.sleep 3
154147
task.stop
155148
end

spec/support/api/endpoints/it_behaves_like_a_cursor_api.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
models_ids.concat(response.map { |instance| instance._links.self._url.gsub("http://example.org/api/#{model_ps}/", '') })
3636
break unless response._links[:next]
3737

38-
next_cursor = Hash[CGI.parse(URI.parse(response._links.next._url).query).map { |a| [a[0], a[1][0]] }]
38+
next_cursor = CGI.parse(URI.parse(response._links.next._url).query).map { |a| [a[0], a[1][0]] }.to_h
3939
end
4040
expect(models_ids.uniq.count).to eq model.all.count
4141
end
@@ -48,7 +48,7 @@
4848
models_ids.concat(response.map { |instance| instance._links.self._url.gsub("http://example.org/api/#{model_ps}/", '') })
4949
break unless response._links[:next]
5050

51-
next_cursor = Hash[CGI.parse(URI.parse(response._links.next._url).query).map { |a| [a[0], a[1][0]] }]
51+
next_cursor = CGI.parse(URI.parse(response._links.next._url).query).map { |a| [a[0], a[1][0]] }.to_h
5252
end
5353
expect(models_ids.uniq.count).to eq model.all.count - 3
5454
end

0 commit comments

Comments
 (0)