Skip to content

Commit

Permalink
Add RuboCop to specs
Browse files Browse the repository at this point in the history
  • Loading branch information
tagliala authored Sep 9, 2023
1 parent 5e729bb commit d3ac229
Show file tree
Hide file tree
Showing 55 changed files with 1,698 additions and 1,407 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/legacy_ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
TEST_CONFIG: ./spec/config.github.yml

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Database
run: |
psql -c "CREATE ROLE runner SUPERUSER LOGIN CREATEDB;" -U postgres -h localhost
Expand Down
29 changes: 29 additions & 0 deletions .github/workflows/rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: RuboCop

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

permissions:
contents: read

jobs:
test:
name: RuboCop
runs-on: ubuntu-latest

env:
BUNDLE_GEMFILE: gemfiles/rubocop.gemfile

steps:
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.2
rubygems: latest
bundler-cache: true
- name: RuboCop
run: bundle exec rubocop
2 changes: 1 addition & 1 deletion .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
continue-on-error: ${{ matrix.channel != 'stable' }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Database
run: |
psql -c "CREATE ROLE runner SUPERUSER LOGIN CREATEDB;" -U postgres -h localhost
Expand Down
82 changes: 82 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require:
- rubocop-packaging
- rubocop-performance
- rubocop-rails
- rubocop-rake
- rubocop-rspec

AllCops:
NewCops: enable
TargetRailsVersion: 5.0
TargetRubyVersion: 2.2
Exclude:
- 'gemfiles/**/*'
- 'lib/**/*'
- 'vendor/bundle/**/*'
- 'tmp/**/*'

Gemspec/DevelopmentDependencies:
Enabled: false

Layout/LineLength:
Max: 140
Exclude:
- 'spec/**/*'

Lint/MissingSuper:
Exclude:
- 'spec/**/*'

Metrics/AbcSize:
Exclude:
- 'spec/**/*'

Metrics/BlockLength:
Exclude:
- 'chrono_model.gemspec'
- 'spec/**/*'

Metrics/MethodLength:
Exclude:
- 'spec/**/*'

Rails/ApplicationRecord:
Enabled: false

Rails/Date:
Enabled: false

Rails/RakeEnvironment:
Enabled: false

Rails/SkipsModelValidations:
AllowedMethods: ['touch', 'touch_all', 'update_column', 'update_columns']

Rails/TimeZone:
Enabled: false

RSpec/BeforeAfterAll:
Exclude:
- 'spec/support/adapter/helpers.rb'

RSpec/ExampleLength:
Enabled: false

RSpec/MultipleExpectations:
Enabled: false

RSpec/NestedGroups:
Enabled: false

RSpec/SpecFilePathFormat:
Enabled: false

Style/GlobalVars:
Exclude:
- 'spec/**/*'

# NOTE: This cop is enabled by RuboCop Rails, because active support adds this
# method to hash, but `except` is not available in specs
Style/HashExcept:
Exclude:
- 'spec/**/*'
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in chrono_model.gemspec
Expand Down
6 changes: 4 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require "bundler/gem_tasks"
# frozen_string_literal: true

require 'bundler/gem_tasks'

# RSpec
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new do |spec|
spec.rspec_opts = '-f doc'
end
task :default => ['testapp:create', :spec]
task default: ['testapp:create', :spec]

# Create a test Rails app in tmp/railsapp for testing the rake
# tasks and overall Rails integration with Aruba.
Expand Down
8 changes: 4 additions & 4 deletions chrono_model.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ Gem::Specification.new do |gem|
gem.summary = 'Temporal extensions (SCD Type II) for Active Record'
gem.homepage = 'https://github.com/ifad/chronomodel'

gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
gem.files = `git ls-files`.split("\n")
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
gem.files = Dir.glob('{LICENSE,README.md,lib/**/*.rb}', File::FNM_DOTMATCH)
gem.name = 'chrono_model'
gem.license = 'MIT'
gem.require_paths = ['lib']
Expand All @@ -20,7 +18,8 @@ Gem::Specification.new do |gem|
gem.metadata = {
'bug_tracker_uri' => 'https://github.com/ifad/chronomodel/issues',
'homepage_uri' => 'https://github.com/ifad/chronomodel',
'source_code_uri' => 'https://github.com/ifad/chronomodel'
'source_code_uri' => 'https://github.com/ifad/chronomodel',
'rubygems_mfa_required' => 'true'
}

gem.required_ruby_version = '>= 2.2.2'
Expand All @@ -30,6 +29,7 @@ Gem::Specification.new do |gem|
gem.add_dependency 'pg', '> 1.1'

gem.add_development_dependency 'aruba'
gem.add_development_dependency 'bundler'
gem.add_development_dependency 'byebug'
gem.add_development_dependency 'fuubar'
gem.add_development_dependency 'hirb'
Expand Down
8 changes: 8 additions & 0 deletions gemfiles/rubocop.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source "https://rubygems.org"

gem 'rubocop', require: false
gem 'rubocop-packaging', require: false
gem 'rubocop-performance', require: false
gem 'rubocop-rails', require: false
gem 'rubocop-rake', require: false
gem 'rubocop-rspec', require: false
2 changes: 2 additions & 0 deletions spec/aruba/dbconsole_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# The db consle does not work on Rails 5.0
#
unless Bundler.default_gemfile.to_s =~ /rails_5.0/
Expand Down
10 changes: 7 additions & 3 deletions spec/aruba/migrations_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'database migrations', type: :aruba do
before { copy_db_config }

context 'after a migration was generated' do
context 'when a migration is generated' do
before { run_command_and_stop('bundle exec rails g migration CreateModels name:string') }

describe 'bundle exec rake db:migrate' do
Expand All @@ -17,19 +19,21 @@

describe 'rerun bundle exec rake db:drop db:create db:migrate', issue: 56 do
let(:command) { 'bundle exec rake db:drop db:create db:migrate' }
before { copy('%/migrations/56/', 'db/migrate') }

let(:action) { run_command(command) }
let(:regex) { /-- change_table\(:impressions, {:temporal=>true, :copy_data=>true}\)/ }

before { copy('%/migrations/56/', 'db/migrate') }

describe 'once' do
let(:last_command) { action && last_command_started }

it { expect(last_command).to be_successfully_executed }
it { expect(last_command).to have_output(regex) }
end

describe 'twice' do
let(:last_command) { run_command_and_stop(command) && action && last_command_started }

it { expect(last_command).to be_successfully_executed }
it { expect(last_command).to have_output(regex) }
end
Expand Down
52 changes: 29 additions & 23 deletions spec/aruba/rake_task_spec.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
# frozen_string_literal: true

require 'spec_helper'
require 'rake'
include ChronoTest::Aruba

# TODO: Understand why this is needed at root level and fix the RuboCop offense
include ChronoTest::Aruba # rubocop:disable Style/MixinUsage
# add :announce_stdout, :announce_stderr, before the type: aruba tag in order
# to see the commmands' stdout and stderr output.
#

RSpec.describe 'rake tasks', type: :aruba do
describe 'bundle exec rake -T' do
before { run_command_and_stop('bundle exec rake -T') }
subject { last_command_started }

before { run_command_and_stop('bundle exec rake -T') }

it { is_expected.to have_output(load_schema_task(as_regexp: true)) }
end

describe "#{dump_schema_task}" do
before { copy_db_config }
describe dump_schema_task.to_s do
let(:db_file) { 'db/test.sql' }

before { run_command_and_stop("bundle exec rake #{dump_schema_task} SCHEMA=db/test.sql") }
before do
copy_db_config
run_command_and_stop("bundle exec rake #{dump_schema_task} SCHEMA=db/test.sql")
end

it { expect(last_command_started).to be_successfully_executed }
it { expect('db/test.sql').to be_an_existing_file }
it { expect('db/test.sql').not_to have_file_content(/\A--/) }
it { expect(db_file).to be_an_existing_file }
it { expect(db_file).not_to have_file_content(/\A--/) }

context 'with schema_search_path option' do
before { copy_db_config('database_with_schema_search_path.yml') }

before { run_command_and_stop("bundle exec rake #{dump_schema_task} SCHEMA=db/test.sql") }
before do
copy_db_config('database_with_schema_search_path.yml')
run_command_and_stop("bundle exec rake #{dump_schema_task} SCHEMA=db/test.sql")
end

it 'includes chronomodel schemas' do
expect('db/test.sql').to have_file_content(/^CREATE SCHEMA IF NOT EXISTS history;$/)
expect(db_file).to have_file_content(/^CREATE SCHEMA IF NOT EXISTS history;$/)
.and have_file_content(/^CREATE SCHEMA IF NOT EXISTS temporal;$/)
.and have_file_content(/^CREATE SCHEMA IF NOT EXISTS public;$/)
end
Expand All @@ -39,28 +48,27 @@
before do
copy_db_config
copy('%/set_config.sql', 'db/test.sql')
run_command_and_stop('bundle exec rake db:schema:load SCHEMA=db/test.sql')
end

before { run_command_and_stop('bundle exec rake db:schema:load SCHEMA=db/test.sql') }

it { expect(last_command_started).to be_successfully_executed }
end

describe "#{load_schema_task}" do
describe load_schema_task.to_s do
let(:action) { run_command("bundle exec rake #{load_schema_task}") }
let(:last_command) { action && last_command_started }

context 'given a file db/structure.sql' do
context 'with db/structure.sql' do
before do
copy('%/empty_structure.sql', 'db/structure.sql')
end

context 'with default username and password', issue: 55 do
before { copy_db_config('database_with_default_username_and_password.yml') }

# Handle Homebrew on MacOS, whose database superuser name is
# equal to the name of the current user.
before do
copy_db_config('database_with_default_username_and_password.yml')

# Handle Homebrew on MacOS, whose database superuser name is
# equal to the name of the current user.
if which 'brew'
file_mangle!('config/database.yml') do |contents|
contents.sub('username: postgres', "username: #{Etc.getlogin}")
Expand All @@ -83,10 +91,9 @@
before do
copy_db_config
copy('%/set_config.sql', 'db/test.sql')
run_command_and_stop('bundle exec rake db:data:dump DUMP=db/test.sql')
end

before { run_command_and_stop('bundle exec rake db:data:dump DUMP=db/test.sql') }

it { expect(last_command_started).to be_successfully_executed }
it { expect('db/test.sql').to be_an_existing_file }
end
Expand All @@ -95,10 +102,9 @@
before do
copy_db_config
copy('%/empty_structure.sql', 'db/test.sql')
run_command_and_stop('bundle exec rake db:data:load DUMP=db/test.sql')
end

before { run_command_and_stop('bundle exec rake db:data:load DUMP=db/test.sql') }

it { expect(last_command_started).to be_successfully_executed }
end
end
4 changes: 3 additions & 1 deletion spec/aruba/subclasses_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'subclasses spec', type: :aruba do
Expand All @@ -10,7 +12,7 @@
copy_db_config
write_file(
'app/models/foo.rb',
"class Foo < ApplicationRecord; include ChronoModel::TimeMachine; end; "
'class Foo < ApplicationRecord; include ChronoModel::TimeMachine; end;'
)
end

Expand Down
Loading

0 comments on commit d3ac229

Please sign in to comment.