Skip to content

Commit

Permalink
Merge pull request #32 from raszi/add-rubocop-rspec
Browse files Browse the repository at this point in the history
chore: add rubocop-rspec
  • Loading branch information
raszi authored Dec 8, 2020
2 parents 2725490 + 3b9dc6c commit be19233
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 21 deletions.
20 changes: 13 additions & 7 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
require:
- rubocop-rspec

AllCops:
TargetRubyVersion: 2.5
NewCops: enable
Exclude:
- 'bin/**/*'
- 'vendor/**/*'
- 'vendor.test/**/*'
- "bin/**/*"
- "vendor/**/*"
- "vendor.test/**/*"

Layout/LineLength:
Exclude:
- 'exe/cleanup_vendor'
- "exe/cleanup_vendor"
Max: 128

Metrics/BlockLength:
Exclude:
- 'exe/cleanup_vendor'
- 'spec/**/*'
- "exe/cleanup_vendor"
- "spec/**/*"

Style/AccessModifierDeclarations:
Enabled: false
Expand All @@ -28,4 +31,7 @@ Style/HashTransformValues:
Enabled: true
Style/StderrPuts:
Exclude:
- 'lib/cleanup_vendor.rb'
- "lib/cleanup_vendor.rb"

RSpec/MultipleMemoizedHelpers:
Enabled: false
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ GEM
unicode-display_width (>= 1.4.0, < 2.0)
rubocop-ast (1.3.0)
parser (>= 2.7.1.5)
rubocop-rspec (2.0.1)
rubocop (~> 1.0)
rubocop-ast (>= 1.1.0)
ruby-progressbar (1.10.1)
simplecov (0.17.1)
docile (~> 1.1)
Expand All @@ -73,6 +76,7 @@ DEPENDENCIES
pry-doc (~> 1.0)
rspec (~> 3.0)
rubocop (~> 1.5.2)
rubocop-rspec (~> 2.0, >= 2.0.1)
simplecov (= 0.17.1)

BUNDLED WITH
Expand Down
1 change: 1 addition & 0 deletions cleanup_vendor.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'pry-doc', '~> 1.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rubocop', '~> 1.5.2'
spec.add_development_dependency 'rubocop-rspec', '~> 2.0', '>= 2.0.1'
spec.add_development_dependency 'simplecov', '= 0.17.1'
end
2 changes: 1 addition & 1 deletion lib/cleanup_vendor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def filter(dir, opts = {})
yield(path)
end
end
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

def get_options(options)
options.values_at(:files, :directories, :filtered, :exclude).map do |v|
Expand Down
33 changes: 20 additions & 13 deletions spec/cleanup_vendor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,58 +48,64 @@
let(:all_files_count) { files.count + 2 * dirs.count + extensions.count }

before { FileUtils.touch(File.join(dir, 'kept.gemspec')) }

after { FileUtils.remove_entry(dir) }

context 'without dry-run' do
it 'should remove all matching entries' do
# rubocop:disable RSpec/MultipleExpectations
it 'removes all matching entries' do
described_class.run(dir)

files = dir.glob('**/*', File::FNM_DOTMATCH).reject { |p| p == dir }
expect(files).to all(satisfy { |p| p.relative_path_from(dir).to_s.start_with?('kept') })
expect(files).to include(*non_top_level_directories)
end
# rubocop:enable RSpec/MultipleExpectations
end

context 'with dry run' do
it 'should keep all the entries' do
# rubocop:disable RSpec/MultipleExpectations
it 'keeps all the entries' do
described_class.run(dir, dry_run: true)

left_files = dir.glob('**/*', File::FNM_DOTMATCH)
expect(left_files).to include(*files)
expect(left_files).to include(*dirs)
expect(left_files).to include(*extensions)
end
# rubocop:enable RSpec/MultipleExpectations
end

context 'with verbose' do
it 'should display what it removes' do
it 'displays what it removes' do
expect { described_class.run(dir, verbose: true) }.to output(/Removing/).to_stderr
end
end

context 'with print0' do
it 'should display all the removed files separated by 0' do
it 'displays all the removed files separated by 0' do
expect { described_class.run(dir, print0: true) }.to output(/\0/).to_stdout
end
end

context 'with summary' do
it 'should display a nice summary' do
it 'displays a nice summary' do
expected_count = all_files_count
expect { described_class.run(dir, summary: true) }.to output(/Summary:\s+Removed files:\s+#{expected_count}/).to_stderr
end
end
end

describe '.filter' do
it 'should throw an error when called with a nil' do
it 'throws an error when called with a nil' do
expect { described_class.filter(nil) }.to raise_error(CleanupVendor::Error)
end

it 'should throw an error when called with a file' do
it 'throws an error when called with a file' do
expect { described_class.filter(__FILE__) }.to raise_error(CleanupVendor::Error)
end

# rubocop:disable RSpec/LetSetup
context 'when called with a real directory' do
let!(:rb_file) { Tempfile.create(['test', '.rb'], dir) }
let!(:spec) { File.join(dir, 'spec').tap { |dir| Dir.mkdir(dir) } }
Expand All @@ -110,25 +116,26 @@
let!(:exclude_file) { Tempfile.create('common.txt', dir) }

it 'without options it should return with an empty list' do
expect { described_class.filter(dir).to be_empty }
expect { |b| described_class.filter(dir, &b) }.not_to yield_control
end

it 'should filter for extensions' do
it 'filters for extensions' do
expect { |b| described_class.filter(dir, files: ['**/*.{rb}'], &b) }.to yield_with_args(Pathname.new(rb_file))
end

it 'should filter for directories' do
it 'filters for directories' do
expect { |b| described_class.filter(dir, directories: %w[spec], &b) }.to yield_with_args(Pathname.new(spec))
end

it 'should filter for filenames' do
it 'filters for filenames' do
expect { |b| described_class.filter(dir, files: [fix_filename], &b) }.to yield_with_args(Pathname.new(tmp_file))
end

it 'should filter for exclusions' do
it 'filters for exclusions' do
exclude_path = Pathname.new(exclude_file)
expect { |b| described_class.filter(dir, exclude: [exclude_file.path], &b) }.to_not yield_with_args(exclude_path)
expect { |b| described_class.filter(dir, exclude: [exclude_file.path], &b) }.not_to yield_with_args(exclude_path)
end
end
# rubocop:enable RSpec/LetSetup
end
end

0 comments on commit be19233

Please sign in to comment.