Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuboCop - enable new cops, address new issues #1220

Merged
merged 15 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ require:
- rubocop-performance

AllCops:
# Prevent RuboCop from exploding when it finds an older-than-2.4 .ruby-version
TargetRubyVersion: 2.7
Exclude: []
NewCops: enable

Bundler/DuplicatedGem:
Enabled: true
Expand Down Expand Up @@ -33,7 +33,7 @@ Bundler/InsecureProtocolSource:
Bundler/OrderedGems:
Enabled: false

Gemspec/DateAssignment:
Gemspec/DeprecatedAttributeAssignment:
Enabled: true

Gemspec/DuplicatedAssignment:
Expand All @@ -44,6 +44,9 @@ Gemspec/DuplicatedAssignment:
Gemspec/OrderedDependencies:
Enabled: false

Gemspec/RequireMFA:
Enabled: false

Gemspec/RequiredRubyVersion:
Enabled: false

Expand Down Expand Up @@ -238,7 +241,7 @@ Layout/IndentationStyle:
Layout/IndentationWidth:
Enabled: true
Width: 2
IgnoredPatterns: []
AllowedPatterns: []

Layout/InitialIndentation:
Enabled: true
Expand Down Expand Up @@ -1333,6 +1336,11 @@ Style/ExplicitBlockArgument:
Style/ExponentialNotation:
Enabled: false

# TODO: MAJOR VERSION - Re-enalbe FetchEnvVar after dropping support for Ruby 2.2
fallwith marked this conversation as resolved.
Show resolved Hide resolved
# Ruby 2.3+ allows for ENV.fetch('KEY') { default }
Style/FetchEnvVar:
Enabled: false

Style/FloatDivision:
Enabled: false

Expand Down Expand Up @@ -1589,6 +1597,10 @@ Style/NumericPredicate:
Style/OneLineConditional:
Enabled: true

# TODO: refactor the use of OpenStruct out of the unit tests and enable
kaylareopelle marked this conversation as resolved.
Show resolved Hide resolved
Style/OpenStructUse:
Enabled: false

Style/OptionHash:
Enabled: false

Expand Down
4 changes: 2 additions & 2 deletions infinite_tracing/lib/infinite_tracing/transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ def safe_param_name value
end

def hash_to_attributes values
values.map do |key, value|
values.to_h do |key, value|
fallwith marked this conversation as resolved.
Show resolved Hide resolved
begin
[key, AttributeValue.new(safe_param_name(value) => value)]
rescue => e
NewRelic::Agent.logger.debug("Infinite tracing transformer error: #{e.inspect}")
nil
end
end.to_h
end
end
end
end
Expand Down
1 change: 0 additions & 1 deletion infinite_tracing/newrelic-infinite_tracing.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ Gem or plugin, hosted on https://github.com/newrelic/newrelic-ruby-agent/

s.homepage = "https://github.com/newrelic/newrelic-ruby-agent/tree/main/infinite_tracing"
s.require_paths = ["lib", "infinite_tracing"]
s.rubygems_version = Gem::VERSION
s.summary = "New Relic Infinite Tracing for the Ruby agent"

s.add_dependency 'newrelic_rpm', NewRelic::VERSION::STRING
Expand Down
16 changes: 8 additions & 8 deletions lib/new_relic/agent/configuration/default_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ def self.config_search_paths
paths << File.join(NewRelic::Control.instance.root, "newrelic.yml.erb")
end

if ENV["HOME"]
paths << File.join(ENV["HOME"], ".newrelic", "newrelic.yml")
paths << File.join(ENV["HOME"], "newrelic.yml")
paths << File.join(ENV["HOME"], ".newrelic", "newrelic.yml.erb")
paths << File.join(ENV["HOME"], "newrelic.yml.erb")
if Dir.home
paths << File.join(Dir.home, ".newrelic", "newrelic.yml")
paths << File.join(Dir.home, "newrelic.yml")
paths << File.join(Dir.home, ".newrelic", "newrelic.yml.erb")
paths << File.join(Dir.home, "newrelic.yml.erb")
end

# If we're packaged for warbler, we can tell from GEM_HOME
Expand Down Expand Up @@ -517,9 +517,9 @@ def self.enforce_fallback(allowed_values: nil, fallback: nil)
:allowed_from_server => false,
:transform => DefaultSource.method(:convert_to_regexp_list),
:description => 'Specify an Array of Rake tasks to automatically instrument. ' \
'This configuration option converts the Array to a RegEx list. If you\'d like '\
'to allow all tasks by default, use `rake.tasks: [.+]`. No rake tasks will be '\
'instrumented unless they\'re added to this list. For more information, '\
'This configuration option converts the Array to a RegEx list. If you\'d like ' \
'to allow all tasks by default, use `rake.tasks: [.+]`. No rake tasks will be ' \
'instrumented unless they\'re added to this list. For more information, ' \
'visit the (New Relic Rake Instrumentation docs)[/docs/apm/agents/ruby-agent/background-jobs/rake-instrumentation].'
},
:'rake.connect_timeout' => {
Expand Down
4 changes: 2 additions & 2 deletions lib/new_relic/agent/instrumentation/sunspot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

executes do
::NewRelic::Agent.logger.info 'Installing Rails Sunspot instrumentation'
deprecation_msg = 'The instrumentation for Sunspot is deprecated.' \
' It will be removed in version 9.0.0.' \
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is that this trailing slash would cause issues should it ever get executed. Perhaps we've broken support for Sunspot earlier than expected?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! I guess that makes it two public-facing changes this release 😉

deprecation_msg = 'The instrumentation for Sunspot is deprecated. ' \
'It will be removed in version 9.0.0.'

::NewRelic::Agent.logger.log_once(
:warn,
Expand Down
4 changes: 2 additions & 2 deletions lib/new_relic/agent/threading/thread_profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def to_collector_array(encoder)
end

def to_log_description
"#<ThreadProfile:#{object_id} "\
"@profile_id: #{profile_id} "\
"#<ThreadProfile:#{object_id} " \
"@profile_id: #{profile_id} " \
"@command_arguments=#{@command_arguments.inspect}>"
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/new_relic/dependency_detection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ def deprecated_disabled_configured?

::NewRelic::Agent.logger.debug("Not installing #{self.name} instrumentation because of configuration #{key}")
::NewRelic::Agent.logger.debug \
"[DEPRECATED] configuration #{key} for #{self.name} will be removed in the next major release." \
" Use `#{config_key}` with one of `#{VALID_CONFIG_VALUES.map(&:to_s).inspect}`"
"[DEPRECATED] configuration #{key} for #{self.name} will be removed in the next major release. " \
"Use `#{config_key}` with one of `#{VALID_CONFIG_VALUES.map(&:to_s).inspect}`"

return true
end
Expand Down
2 changes: 1 addition & 1 deletion lib/new_relic/metric_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def metric_spec= new_spec
end

def hash
metric_spec.hash ^ stats.hash
[metric_spec, stats].hash
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end

def to_json(*a)
Expand Down
2 changes: 1 addition & 1 deletion lib/new_relic/metric_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def eql? o
end

def hash
@name.hash ^ @scope.hash
[@name, @scope].hash
end

def to_s
Expand Down
1 change: 0 additions & 1 deletion newrelic_rpm.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ https://github.com/newrelic/newrelic-ruby-agent/

s.homepage = "https://github.com/newrelic/rpm"
s.require_paths = ["lib"]
s.rubygems_version = Gem::VERSION
s.summary = "New Relic Ruby Agent"
s.add_development_dependency 'rake', '12.3.3'
s.add_development_dependency 'rb-inotify', '0.9.10' # locked to support < Ruby 2.3 (and listen 3.0.8)
Expand Down
7 changes: 5 additions & 2 deletions test/multiverse/lib/multiverse/suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ def filter_file

def clean_gemfiles(env_index)
gemfiles = ["Gemfile.#{env_index}", "Gemfile.#{env_index}.lock"]
gemfiles.each { |f| File.delete(f) if File.exist?(f) }
gemfiles.each do |f|
next unless File.exist?(f)
File.delete(f)
end
end

def envfile_path
Expand Down Expand Up @@ -270,7 +273,7 @@ def generate_gemfile(gemfile_text, env_index, local = true)
if verbose?
puts "Ruby: #{RUBY_VERSION} Platform: #{RUBY_PLATFORM} RubyGems: #{Gem::VERSION}"
puts yellow("Gemfile.#{env_index} set to:")
puts File.open(gemfile).read
puts File.read(gemfile)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ def test_config_loads_from_newrelic_yml
end

def test_config_loads_from_home_newrelic_yml
assert_config_read_from(ENV['HOME'] + "/newrelic.yml")
assert_config_read_from(Dir.home + "/newrelic.yml")
end

def test_config_loads_from_home_dot_newrelic_newrelic_yml
assert_config_read_from(ENV['HOME'] + "/.newrelic/newrelic.yml")
assert_config_read_from(Dir.home + "/.newrelic/newrelic.yml")
end

def test_config_loads_from_config_path_option_to_manual_start
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ Gem::Specification.new do |spec|
f.match(%r{^(test|spec|features)/})
end
spec.require_paths = ["lib"]
spec.metadata['rubygems_mfa_required'] = 'true'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious -- why add this here?

Also, have you done any absorbing of this gem? I'm somewhat skeptical about its contemporary relevance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a new cop, Gemspec/RequireMFA, that insists on this line being present in gemspec files. I don't know if we're ready to start using that declaration yet for our 2 main gemspec files, so I have disabled the cop for now and left those files untouched. But for this file that is basically a fixture file, I figured it was ok to leave the rubocop -a driven addition.

end
4 changes: 2 additions & 2 deletions test/new_relic/agent/agent/connect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def test_connect_memoizes_event_harvest_config
}
}

NewRelic::Agent.instance.service.stubs(:connect)\
NewRelic::Agent.instance.service.stubs(:connect) \
# the stub connect service will return this canned response
.returns({
'agent_run_id' => 23,
Expand All @@ -180,7 +180,7 @@ def test_connect_memoizes_event_harvest_config
'log_event_data' => 833
}
}
})\
}) \
# every call to :connect should pass the same expected event_harvest_config payload
.with { |value| value[:event_harvest_config] == expected_event_harvest_config_payload }

Expand Down
2 changes: 1 addition & 1 deletion test/new_relic/agent/span_event_primitive_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_root_span_gets_dt_parent_attributes
payload = nil
external_segment = nil
in_transaction('test_txn') do |txn|
external_segment = NewRelic::Agent::Tracer\
external_segment = NewRelic::Agent::Tracer \
.start_external_request_segment library: "net/http",
uri: "http://docs.newrelic.com",
procedure: "GET"
Expand Down
4 changes: 2 additions & 2 deletions test/new_relic/agent/span_events_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_span_ids_passed_in_payload_when_span_events_enabled
payload = nil
external_segment = nil
transaction = in_transaction('test_txn') do |txn|
external_segment = NewRelic::Agent::Tracer\
external_segment = NewRelic::Agent::Tracer \
.start_external_request_segment library: "net/http",
uri: "http://docs.newrelic.com",
procedure: "GET"
Expand All @@ -49,7 +49,7 @@ def test_parent_span_id_propagated_cross_process
payload = nil
external_segment = nil
in_transaction('test_txn') do |txn|
external_segment = NewRelic::Agent::Tracer\
external_segment = NewRelic::Agent::Tracer \
.start_external_request_segment library: "net/http",
uri: "http://docs.newrelic.com",
procedure: "GET"
Expand Down
4 changes: 1 addition & 3 deletions test/performance/lib/performance/baseline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ def load!
end

def save!
File.open(PERSIST_PATH, "w") do |f|
f.write(JSON.dump(@results.map(&:to_h)))
end
File.write(PERSIST_PATH, JSON.dump(@results.map(&:to_h)))
end
end
end