Skip to content

Commit

Permalink
chore: Add rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtha committed Oct 31, 2023
1 parent f04b71c commit 14a4dad
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
AllCops:
NewCops: enable
Gemspec/RequiredRubyVersion:
Enabled: false
Layout/LineLength:
Max: 120
Layout/MultilineMethodCallIndentation:
Expand All @@ -12,3 +16,5 @@ Style/GuardClause:
Enabled: false
Style/NumericPredicate:
Enabled: false
Style/OptionalBooleanParameter:
Enabled: false
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ gemspec

gem 'rake', '~> 13.1'
gem 'rspec', '~> 3.12'
gem 'rubocop', '~> 1.57'
6 changes: 3 additions & 3 deletions lib/simple_flag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,19 @@ 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

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
5 changes: 3 additions & 2 deletions lib/simple_flag/overrides.rb
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -42,6 +44,5 @@ def reset_all_overrides
def overridden?(name)
@overrides.key?(name)
end

end
end
7 changes: 2 additions & 5 deletions simple_flag.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
56 changes: 24 additions & 32 deletions spec/simple_flag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -155,21 +147,21 @@
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

context 'when flag is falsy' do
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
Expand All @@ -181,21 +173,21 @@
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

context 'when flag is falsy' do
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
Expand All @@ -206,15 +198,15 @@
it 'returns true' do
subject.define(:flag) { true }

expect(subject.presence(:flag)).to eq true
expect(subject.presence(:flag)).to be true
end
end

context 'when flag is falsy' do
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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -405,15 +397,15 @@
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
features = described_class.new do |f|
f.define(:flag) { false }
end

expect(features.overridden?(:flag)).to eq false
expect(features.overridden?(:flag)).to be false
end
end
end

0 comments on commit 14a4dad

Please sign in to comment.