Skip to content

Commit

Permalink
Upgrade Rubocop and move it to separate CI job
Browse files Browse the repository at this point in the history
  • Loading branch information
Envek committed Jul 27, 2023
1 parent 717b515 commit 5456049
Show file tree
Hide file tree
Showing 21 changed files with 98 additions and 61 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Lint Ruby

on:
push:
branches:
- master
paths:
- "gemfiles/*"
- "Gemfile"
- "**/*.rb"
- "**/*.gemspec"
- ".github/workflows/lint.yml"
pull_request:
paths:
- "gemfiles/*"
- "Gemfile"
- "**/*.rb"
- "**/*.gemspec"
- ".github/workflows/lint.yml"

jobs:
rubocop:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.2
bundler-cache: true
- name: Lint Ruby code with RuboCop
run: |
bundle exec rubocop
2 changes: 0 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,5 @@ jobs:
bundle config path vendor/bundle
bundle install
bundle update
- name: Run Rubocop
run: bundle exec rubocop
- name: Run RSpec
run: bundle exec rspec
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Metrics/BlockLength:
- "Gemfile"
- "spec/**/*"

Metrics/AbcSize:
Max: 25

Layout/LineLength:
Max: 120

Expand All @@ -29,6 +32,9 @@ RSpec/DescribeClass:
RSpec/NestedGroups:
Max: 4

RSpec/MultipleMemoizedHelpers:
Enabled: false

Bundler/OrderedGems:
Enabled: false

Expand Down
7 changes: 6 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
gemspec

group :development, :test do
gem "rake", "~> 12.0"
gem "rspec", "~> 3.0"
gem "yard"
gem "yard-dry-initializer"

gem "pry"
gem "pry-byebug", platform: :mri

gem "rubocop", "~> 0.80.0"
gem "rubocop", "~> 1.0"
gem "rubocop-rspec"
end
6 changes: 2 additions & 4 deletions lib/yabeda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def configured?

# Perform configuration: registration of metrics and collector blocks
# @return [void]
# rubocop: disable Metrics/MethodLength, Metrics/AbcSize
# rubocop: disable Metrics/MethodLength
def configure!
raise(AlreadyConfiguredError, @configured_by) if already_configured?

Expand Down Expand Up @@ -129,12 +129,11 @@ def debug!

true
end
# rubocop: enable Metrics/MethodLength, Metrics/AbcSize
# rubocop: enable Metrics/MethodLength

# Forget all the configuration.
# For testing purposes as it doesn't rollback changes in adapters.
# @api private
# rubocop: disable Metrics/AbcSize
def reset!
default_tags.clear
adapters.clear
Expand All @@ -147,6 +146,5 @@ def reset!
instance_variable_set(:@configured_by, nil)
instance_variable_set(:@debug_was_enabled_by, nil)
end
# rubocop: enable Metrics/AbcSize
end
end
2 changes: 1 addition & 1 deletion lib/yabeda/global_group.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require "forwardable"
require_relative "./group"
require_relative "group"

module Yabeda
# Represents implicit global group
Expand Down
10 changes: 5 additions & 5 deletions lib/yabeda/rspec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# frozen_string_literal: true

require_relative "./testing"
require_relative "testing"

module Yabeda
# RSpec integration for Yabeda: custom matchers, etc
module RSpec
end
end

require_relative "./rspec/increment_yabeda_counter"
require_relative "./rspec/update_yabeda_gauge"
require_relative "./rspec/measure_yabeda_histogram"
require_relative "rspec/increment_yabeda_counter"
require_relative "rspec/update_yabeda_gauge"
require_relative "rspec/measure_yabeda_histogram"

::RSpec.configure do |config|
RSpec.configure do |config|
config.before(:suite) do
Yabeda.configure! unless Yabeda.already_configured?
end
Expand Down
1 change: 1 addition & 0 deletions lib/yabeda/rspec/base_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def with_tags(tags)
end

def initialize(expected)
super
@expected = @metric = resolve_metric(expected)
rescue KeyError
raise ArgumentError, <<~MSG
Expand Down
14 changes: 7 additions & 7 deletions lib/yabeda/rspec/increment_yabeda_counter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative "./base_matcher"
require_relative "base_matcher"

module Yabeda
module RSpec
Expand Down Expand Up @@ -54,16 +54,16 @@ def match_when_negated(metric, block)

def failure_message
"expected #{expected_formatted} " \
"to be incremented #{"by #{description_of(expected_increment)} " unless expected_increment.nil?}" \
"#{("with tags #{::RSpec::Support::ObjectFormatter.format(tags)} " if tags)}" \
"but #{actual_increments_message}"
"to be incremented #{"by #{description_of(expected_increment)} " unless expected_increment.nil?}" \
"#{"with tags #{::RSpec::Support::ObjectFormatter.format(tags)} " if tags}" \
"but #{actual_increments_message}"
end

def failure_message_when_negated
"expected #{expected_formatted} " \
"not to be incremented " \
"#{("with tags #{::RSpec::Support::ObjectFormatter.format(tags)} " if tags)}" \
"but #{actual_increments_message}"
"not to be incremented " \
"#{"with tags #{::RSpec::Support::ObjectFormatter.format(tags)} " if tags}" \
"but #{actual_increments_message}"
end

def actual_increments_message
Expand Down
14 changes: 7 additions & 7 deletions lib/yabeda/rspec/measure_yabeda_histogram.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative "./base_matcher"
require_relative "base_matcher"

module Yabeda
module RSpec
Expand Down Expand Up @@ -52,16 +52,16 @@ def match_when_negated(metric, block)

def failure_message
"expected #{expected_formatted} " \
"to be changed #{"to #{expected} " unless expected_value.nil?}" \
"#{("with tags #{::RSpec::Support::ObjectFormatter.format(tags)} " if tags)}" \
"but #{actual_changes_message}"
"to be changed #{"to #{expected} " unless expected_value.nil?}" \
"#{"with tags #{::RSpec::Support::ObjectFormatter.format(tags)} " if tags}" \
"but #{actual_changes_message}"
end

def failure_message_when_negated
"expected #{expected_formatted} " \
"not to be incremented " \
"#{("with tags #{::RSpec::Support::ObjectFormatter.format(tags)} " if tags)}" \
"but #{actual_changes_message}"
"not to be incremented " \
"#{"with tags #{::RSpec::Support::ObjectFormatter.format(tags)} " if tags}" \
"but #{actual_changes_message}"
end

def actual_changes_message
Expand Down
14 changes: 7 additions & 7 deletions lib/yabeda/rspec/update_yabeda_gauge.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative "./base_matcher"
require_relative "base_matcher"

module Yabeda
module RSpec
Expand Down Expand Up @@ -52,16 +52,16 @@ def match_when_negated(metric, block)

def failure_message
"expected #{expected_formatted} " \
"to be changed #{"to #{expected_value} " unless expected_value.nil?}" \
"#{("with tags #{::RSpec::Support::ObjectFormatter.format(tags)} " if tags)}" \
"but #{actual_changes_message}"
"to be changed #{"to #{expected_value} " unless expected_value.nil?}" \
"#{"with tags #{::RSpec::Support::ObjectFormatter.format(tags)} " if tags}" \
"but #{actual_changes_message}"
end

def failure_message_when_negated
"expected #{expected_formatted} " \
"not to be changed " \
"#{("with tags #{::RSpec::Support::ObjectFormatter.format(tags)} " if tags)}" \
"but #{actual_changes_message}"
"not to be changed " \
"#{"with tags #{::RSpec::Support::ObjectFormatter.format(tags)} " if tags}" \
"but #{actual_changes_message}"
end

def actual_changes_message
Expand Down
3 changes: 2 additions & 1 deletion lib/yabeda/test_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "singleton"

require_relative "./base_adapter"
require_relative "base_adapter"

module Yabeda
# Fake monitoring system adapter that collects latest metric values for later inspection
Expand All @@ -12,6 +12,7 @@ class TestAdapter < BaseAdapter
attr_reader :counters, :gauges, :histograms

def initialize
super
@counters = Hash.new { |ch, ck| ch[ck] = Hash.new { |th, tk| th[tk] = 0 } }
@gauges = Hash.new { |gh, gk| gh[gk] = Hash.new { |th, tk| th[tk] = nil } }
@histograms = Hash.new { |hh, hk| hh[hk] = Hash.new { |th, tk| th[tk] = nil } }
Expand Down
2 changes: 1 addition & 1 deletion lib/yabeda/testing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

# Include this file to get things prepared for testing

require_relative "./test_adapter"
require_relative "test_adapter"

Yabeda.register_adapter(:test, Yabeda::TestAdapter.instance)
8 changes: 4 additions & 4 deletions spec/yabeda/counter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

let(:tags) { { foo: "bar" } }
let(:metric_value) { 10 }
let(:counter) { ::Yabeda.test_counter }
let(:counter) { Yabeda.test_counter }
let(:built_tags) { { built_foo: "built_bar" } }
let(:adapter) { instance_double("Yabeda::BaseAdapter", perform_counter_increment!: true, register!: true) }
let(:adapter) { instance_double(Yabeda::BaseAdapter, perform_counter_increment!: true, register!: true) }

before do
::Yabeda.configure do
Yabeda.configure do
counter :test_counter
end
Yabeda.configure! unless Yabeda.already_configured?
allow(Yabeda::Tags).to receive(:build).with(tags, anything).and_return(built_tags)
::Yabeda.register_adapter(:test_adapter, adapter)
Yabeda.register_adapter(:test_adapter, adapter)
end

it { expect(increment_counter).to eq(metric_value) }
Expand Down
8 changes: 4 additions & 4 deletions spec/yabeda/gauge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

let(:tags) { { foo: "bar" } }
let(:metric_value) { 10 }
let(:gauge) { ::Yabeda.test_gauge }
let(:gauge) { Yabeda.test_gauge }
let(:built_tags) { { built_foo: "built_bar" } }
let(:adapter) { instance_double("Yabeda::BaseAdapter", perform_gauge_set!: true, register!: true) }
let(:adapter) { instance_double(Yabeda::BaseAdapter, perform_gauge_set!: true, register!: true) }

before do
::Yabeda.configure do
Yabeda.configure do
gauge :test_gauge
end
Yabeda.configure! unless Yabeda.already_configured?
allow(Yabeda::Tags).to receive(:build).with(tags, anything).and_return(built_tags)
::Yabeda.register_adapter(:test_adapter, adapter)
Yabeda.register_adapter(:test_adapter, adapter)
end

it { expect(set_gauge).to eq(metric_value) }
Expand Down
8 changes: 4 additions & 4 deletions spec/yabeda/histogram_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
let(:tags) { { foo: "bar" } }
let(:metric_value) { 10 }
let(:block) { proc { 1 + 1 } }
let(:histogram) { ::Yabeda.test_histogram }
let(:histogram) { Yabeda.test_histogram }
let(:built_tags) { { built_foo: "built_bar" } }
let(:adapter) { instance_double("Yabeda::BaseAdapter", perform_histogram_measure!: true, register!: true) }
let(:adapter) { instance_double(Yabeda::BaseAdapter, perform_histogram_measure!: true, register!: true) }

before do
::Yabeda.configure do
Yabeda.configure do
histogram :test_histogram, buckets: [1, 10, 100]
end
Yabeda.configure! unless Yabeda.already_configured?
allow(Yabeda::Tags).to receive(:build).with(tags, anything).and_return(built_tags)
::Yabeda.register_adapter(:test_adapter, adapter)
Yabeda.register_adapter(:test_adapter, adapter)
end

context "with value given" do
Expand Down
2 changes: 1 addition & 1 deletion spec/yabeda/rspec/increment_yabeda_counter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
RSpec.describe "Yabeda RSpec matchers" do
before do
Yabeda.reset!
::Yabeda.configure do
Yabeda.configure do
counter :test_counter
counter :other_counter
end
Expand Down
2 changes: 1 addition & 1 deletion spec/yabeda/rspec/measure_yabeda_histogram_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
RSpec.describe "Yabeda RSpec matchers" do
before do
Yabeda.reset!
::Yabeda.configure do
Yabeda.configure do
histogram :test_histogram, buckets: [1, 10, 100]
histogram :other_histogram, buckets: [1, 10, 100]
end
Expand Down
2 changes: 1 addition & 1 deletion spec/yabeda/rspec/update_yabeda_gauge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
RSpec.describe "Yabeda RSpec matchers" do
before do
Yabeda.reset!
::Yabeda.configure do
Yabeda.configure do
gauge :test_gauge
gauge :other_gauge
end
Expand Down
Loading

0 comments on commit 5456049

Please sign in to comment.