From 14a4dad5b594d322ae53883c0bfa392e0ed5bb1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Kus=C3=BD?= Date: Tue, 31 Oct 2023 23:42:47 +0100 Subject: [PATCH] chore: Add rubocop --- .github/workflows/ci.yml | 2 ++ .rubocop.yml | 6 ++++ Gemfile | 1 + lib/simple_flag.rb | 6 ++-- lib/simple_flag/overrides.rb | 5 ++-- simple_flag.gemspec | 7 ++--- spec/simple_flag_spec.rb | 56 ++++++++++++++++-------------------- 7 files changed, 41 insertions(+), 42 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f7944b6..b565e45 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,5 +24,7 @@ jobs: with: ruby-version: ${{ matrix.ruby-version }} bundler-cache: true # runs 'bundle install' and caches installed gems automatically + - name: Run linter + run: bundle exec rubocop - name: Run tests run: bundle exec rspec diff --git a/.rubocop.yml b/.rubocop.yml index 2d48a7c..e64a60f 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,3 +1,7 @@ +AllCops: + NewCops: enable +Gemspec/RequiredRubyVersion: + Enabled: false Layout/LineLength: Max: 120 Layout/MultilineMethodCallIndentation: @@ -12,3 +16,5 @@ Style/GuardClause: Enabled: false Style/NumericPredicate: Enabled: false +Style/OptionalBooleanParameter: + Enabled: false diff --git a/Gemfile b/Gemfile index 014bb61..f4f723a 100644 --- a/Gemfile +++ b/Gemfile @@ -7,3 +7,4 @@ gemspec gem 'rake', '~> 13.1' gem 'rspec', '~> 3.12' +gem 'rubocop', '~> 1.57' diff --git a/lib/simple_flag.rb b/lib/simple_flag.rb index 21a4b40..9d0fec1 100644 --- a/lib/simple_flag.rb +++ b/lib/simple_flag.rb @@ -127,7 +127,7 @@ def validate_flag_arity(flag_name, flag_arity, override_arity) if original_arity != override_arity raise FlagArgumentsMismatch, "Flag '#{flag_name}' expects #{flag_arity} arguments, " \ - "but #{override_arity} arguments were given" + "but #{override_arity} arguments were given" end end @@ -135,11 +135,11 @@ def validate_flag_arguments(flag_name, flag_arity, args_size) if flag_arity < 0 && (flag_arity.abs - 1) > args_size # Contains variable -n-1 arguments raise FlagArgumentsMismatch, "Flag '#{flag_name}' expects #{flag_arity.abs - 1} or more arguments, " \ - "but #{args_size} arguments were given" + "but #{args_size} arguments were given" elsif flag_arity >= 0 && flag_arity != args_size # Contains zero or fixed number of arguments raise FlagArgumentsMismatch, "Flag '#{flag_name}' expects #{flag_arity} arguments, " \ - "but #{args_size} arguments were given" + "but #{args_size} arguments were given" end end end diff --git a/lib/simple_flag/overrides.rb b/lib/simple_flag/overrides.rb index a08c0f3..0aff1fa 100644 --- a/lib/simple_flag/overrides.rb +++ b/lib/simple_flag/overrides.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + class SimpleFlag + # Overrides module provides methods for overriding feature flags during testing. module Overrides - def override(name, result = true, &block) raise FlagNotDefined, "Feature flag `#{name}` is not defined" unless flag?(name) @@ -42,6 +44,5 @@ def reset_all_overrides def overridden?(name) @overrides.key?(name) end - end end diff --git a/simple_flag.gemspec b/simple_flag.gemspec index 0256368..6ba3546 100644 --- a/simple_flag.gemspec +++ b/simple_flag.gemspec @@ -18,8 +18,9 @@ Gem::Specification.new do |spec| spec.metadata['changelog_uri'] = 'https://github.com/wojtha/simple_flag/blob/master/CHANGELOG.md' spec.metadata['source_code_uri'] = 'https://github.com/wojtha/simple_flag' spec.metadata['bug_tracker_uri'] = 'https://github.com/wojtha/simple_flag/issues' + spec.metadata['rubygems_mfa_required'] = 'true' - spec.required_ruby_version = '>= 2.4.0' + spec.required_ruby_version = '>= 2.7.0' # Specify which files should be added to the gem when it is released. # The `git ls-files -z` loads the files in the RubyGem that have been added into git. @@ -29,8 +30,4 @@ Gem::Specification.new do |spec| spec.bindir = 'bin' spec.executables = [] spec.require_paths = ['lib'] - - spec.add_development_dependency 'bundler' - spec.add_development_dependency 'rake' - spec.add_development_dependency 'rspec' end diff --git a/spec/simple_flag_spec.rb b/spec/simple_flag_spec.rb index afdaaeb..693e395 100644 --- a/spec/simple_flag_spec.rb +++ b/spec/simple_flag_spec.rb @@ -40,17 +40,13 @@ end it 'cannot define feature flag as proc' do - expect { - subject.define(:flag, proc { flag_result }) - } - .to raise_error(ArgumentError, 'wrong number of arguments (given 2, expected 1)') + expect { subject.define(:flag, proc { flag_result }) } + .to raise_error(ArgumentError, 'wrong number of arguments (given 2, expected 1)') end it 'cannot define feature flag as lambda' do - expect { - subject.define(:flag, -> { flag_result }) - } - .to raise_error(ArgumentError, 'wrong number of arguments (given 2, expected 1)') + expect { subject.define(:flag, -> { flag_result }) } + .to raise_error(ArgumentError, 'wrong number of arguments (given 2, expected 1)') end end @@ -75,17 +71,13 @@ end it 'cannot redefine feature flag as proc' do - expect { - subject.redefine(:flag, proc { flag_result }) - } - .to raise_error(ArgumentError, 'wrong number of arguments (given 2, expected 1)') + expect { subject.redefine(:flag, proc { flag_result }) } + .to raise_error(ArgumentError, 'wrong number of arguments (given 2, expected 1)') end it 'cannot redefine feature flag as lambda' do - expect { - subject.redefine(:flag, -> { flag_result }) - } - .to raise_error(ArgumentError, 'wrong number of arguments (given 2, expected 1)') + expect { subject.redefine(:flag, -> { flag_result }) } + .to raise_error(ArgumentError, 'wrong number of arguments (given 2, expected 1)') end end @@ -155,7 +147,7 @@ it 'returns true' do subject.define(:flag) { true } - expect(subject.public_send(enabled_method, :flag)).to eq true + expect(subject.public_send(enabled_method, :flag)).to be true end end @@ -163,13 +155,13 @@ it 'returns false' do subject.define(:flag) { false } - expect(subject.public_send(enabled_method, :flag)).to eq false + expect(subject.public_send(enabled_method, :flag)).to be false end end context 'when flag is not defined' do it 'returns false' do - expect(subject.public_send(enabled_method, :flag)).to eq false + expect(subject.public_send(enabled_method, :flag)).to be false end end end @@ -181,7 +173,7 @@ it 'returns false' do subject.define(:flag) { true } - expect(subject.public_send(enabled_method, :flag)).to eq false + expect(subject.public_send(enabled_method, :flag)).to be false end end @@ -189,13 +181,13 @@ it 'returns true' do subject.define(:flag) { false } - expect(subject.public_send(enabled_method, :flag)).to eq true + expect(subject.public_send(enabled_method, :flag)).to be true end end context 'when flag is not defined' do it 'returns true' do - expect(subject.public_send(enabled_method, :flag)).to eq true + expect(subject.public_send(enabled_method, :flag)).to be true end end end @@ -206,7 +198,7 @@ it 'returns true' do subject.define(:flag) { true } - expect(subject.presence(:flag)).to eq true + expect(subject.presence(:flag)).to be true end end @@ -214,7 +206,7 @@ it 'returns nil' do subject.define(:flag) { false } - expect(subject.presence(:flag)).to eq nil + expect(subject.presence(:flag)).to be_nil end end end @@ -285,14 +277,14 @@ f.define(:flag) { false } end - expect(features.flag?(:flag)).to eq true + expect(features.flag?(:flag)).to be true end it 'returns false if flag wasn\'t defined' do - features = described_class.new do |f| + features = described_class.new do |f| # rubocop:disable Lint/EmptyBlock end - expect(features.flag?(:flag)).to eq false + expect(features.flag?(:flag)).to be false end end @@ -326,7 +318,7 @@ end it 'raises exception when trying to override non-existing flag' do - features = described_class.new do |f| + features = described_class.new do |f| # rubocop:disable Lint/EmptyBlock end expect { features.override(:flag, true) }.to raise_error(SimpleFlag::FlagNotDefined) @@ -340,10 +332,10 @@ features.override(:flag, 'override 1') features.override(:flag, 'override 2') - expect( features.enabled?(:flag) ).to eq('override 2') + expect(features.enabled?(:flag)).to eq('override 2') features.reset_override(:flag) - expect( features.enabled?(:flag) ).to eq('original') + expect(features.enabled?(:flag)).to eq('original') end end @@ -405,7 +397,7 @@ f.override(:flag, true) end - expect(features.overridden?(:flag)).to eq true + expect(features.overridden?(:flag)).to be true end it 'returns false if flag wasn\'t overridden' do @@ -413,7 +405,7 @@ f.define(:flag) { false } end - expect(features.overridden?(:flag)).to eq false + expect(features.overridden?(:flag)).to be false end end end