|
| 1 | +require 'spec_helper' |
| 2 | +require 'rubocop' |
| 3 | +require 'rubocop/rspec/cop_helper' |
| 4 | +require 'rubocop/config' |
| 5 | +require 'linters/migration/use_migration_helpers' |
| 6 | + |
| 7 | +RSpec.describe RuboCop::Cop::Migration::UseMigrationHelpers do |
| 8 | + include CopHelper |
| 9 | + |
| 10 | + subject(:cop) { described_class.new(RuboCop::Config.new({})) } |
| 11 | + |
| 12 | + let(:message) { described_class::MSG } |
| 13 | + |
| 14 | + it 'registers an offense for Sequel::Migrator.run inside an example when the context is present' do |
| 15 | + result = inspect_source(<<~RUBY) |
| 16 | + RSpec.describe 'migration' do |
| 17 | + include_context 'migration' do |
| 18 | + let(:migration_filename) { 'x.rb' } |
| 19 | + end |
| 20 | +
|
| 21 | + it 'runs' do |
| 22 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index) }.not_to raise_error |
| 23 | + end |
| 24 | + end |
| 25 | + RUBY |
| 26 | + |
| 27 | + expect(result.size).to eq(1) |
| 28 | + expect(result.map(&:message)).to eq([message]) |
| 29 | + end |
| 30 | + |
| 31 | + it 'registers an offense for an unqualified Migrator.run when the context is present' do |
| 32 | + result = inspect_source(<<~RUBY) |
| 33 | + RSpec.describe 'migration' do |
| 34 | + include_context 'migration' do |
| 35 | + let(:migration_filename) { 'x.rb' } |
| 36 | + end |
| 37 | +
|
| 38 | + it 'runs' do |
| 39 | + expect { Migrator.run(db, migrations_path, target: current_migration_index) }.not_to raise_error |
| 40 | + end |
| 41 | + end |
| 42 | + RUBY |
| 43 | + |
| 44 | + expect(result.size).to eq(1) |
| 45 | + end |
| 46 | + |
| 47 | + it 'registers one offense per direct call' do |
| 48 | + result = inspect_source(<<~RUBY) |
| 49 | + RSpec.describe 'migration' do |
| 50 | + include_context 'migration' do |
| 51 | + let(:migration_filename) { 'x.rb' } |
| 52 | + end |
| 53 | +
|
| 54 | + it 'runs' do |
| 55 | + Sequel::Migrator.run(db, migrations_path, target: current_migration_index) |
| 56 | + Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1) |
| 57 | + end |
| 58 | + end |
| 59 | + RUBY |
| 60 | + |
| 61 | + expect(result.size).to eq(2) |
| 62 | + end |
| 63 | + |
| 64 | + it 'does not register an offense for a call inside a before hook' do |
| 65 | + result = inspect_source(<<~RUBY) |
| 66 | + RSpec.describe 'migration' do |
| 67 | + include_context 'migration' do |
| 68 | + let(:migration_filename) { 'x.rb' } |
| 69 | + end |
| 70 | +
|
| 71 | + before do |
| 72 | + Sequel::Migrator.run(db, migrations_path, target: current_migration_index) |
| 73 | + end |
| 74 | + end |
| 75 | + RUBY |
| 76 | + |
| 77 | + expect(result.size).to eq(0) |
| 78 | + end |
| 79 | + |
| 80 | + it 'does not register an offense for a call inside an after hook' do |
| 81 | + result = inspect_source(<<~RUBY) |
| 82 | + RSpec.describe 'migration' do |
| 83 | + include_context 'migration' do |
| 84 | + let(:migration_filename) { 'x.rb' } |
| 85 | + end |
| 86 | +
|
| 87 | + after do |
| 88 | + Sequel::Migrator.run(db, migrations_path, target: current_migration_index) |
| 89 | + end |
| 90 | + end |
| 91 | + RUBY |
| 92 | + |
| 93 | + expect(result.size).to eq(0) |
| 94 | + end |
| 95 | + |
| 96 | + it 'does not register an offense for a call inside an around hook' do |
| 97 | + result = inspect_source(<<~RUBY) |
| 98 | + RSpec.describe 'migration' do |
| 99 | + include_context 'migration' do |
| 100 | + let(:migration_filename) { 'x.rb' } |
| 101 | + end |
| 102 | +
|
| 103 | + around do |example| |
| 104 | + Sequel::Migrator.run(db, migrations_path, target: current_migration_index) |
| 105 | + example.run |
| 106 | + end |
| 107 | + end |
| 108 | + RUBY |
| 109 | + |
| 110 | + expect(result.size).to eq(0) |
| 111 | + end |
| 112 | + |
| 113 | + it 'does not register an offense when the migration shared context is absent' do |
| 114 | + result = inspect_source(<<~RUBY) |
| 115 | + RSpec.describe 'migration' do |
| 116 | + it 'runs' do |
| 117 | + expect { Sequel::Migrator.run(db, migrations_path, target: 1) }.not_to raise_error |
| 118 | + end |
| 119 | + end |
| 120 | + RUBY |
| 121 | + |
| 122 | + expect(result.size).to eq(0) |
| 123 | + end |
| 124 | + |
| 125 | + it 'does not register an offense when the helpers are used' do |
| 126 | + result = inspect_source(<<~RUBY) |
| 127 | + RSpec.describe 'migration' do |
| 128 | + include_context 'migration' do |
| 129 | + let(:migration_filename) { 'x.rb' } |
| 130 | + end |
| 131 | +
|
| 132 | + it 'runs' do |
| 133 | + expect { run_migration }.not_to raise_error |
| 134 | + test_up_migration_idempotency |
| 135 | + end |
| 136 | + end |
| 137 | + RUBY |
| 138 | + |
| 139 | + expect(result.size).to eq(0) |
| 140 | + end |
| 141 | +end |
0 commit comments