From 888bc9c839821de2bfaaaea6ec061a3172a59623 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Wed, 30 Aug 2023 16:19:31 +0200 Subject: [PATCH] Refactor aruba specs - Use copy instead of write - Move fixtures to proper path - Use `it` instead of specify --- Rakefile | 2 +- spec/aruba/dbconsole_spec.rb | 10 ++-- spec/aruba/migrations_spec.rb | 28 ++++------- spec/aruba/rake_task_spec.rb | 46 +++++++++---------- ...ase_with_default_username_and_password.yml | 0 .../database_with_schema_search_path.yml | 0 ...database_without_username_and_password.yml | 0 spec/{aruba => }/fixtures/empty_structure.sql | 0 .../56/20160812190335_create_impressions.rb | 0 ...9_add_temporal_extension_to_impressions.rb | 0 .../fixtures/railsapp/config/application.rb | 0 .../fixtures/railsapp/config/boot.rb | 0 .../config/environments/development.rb | 0 .../railsapp/config/initializers/assets.rb | 0 spec/{aruba => }/fixtures/set_config.sql | 0 spec/support/aruba.rb | 4 ++ 16 files changed, 38 insertions(+), 52 deletions(-) rename spec/{aruba => }/fixtures/database_with_default_username_and_password.yml (100%) rename spec/{aruba => }/fixtures/database_with_schema_search_path.yml (100%) rename spec/{aruba => }/fixtures/database_without_username_and_password.yml (100%) rename spec/{aruba => }/fixtures/empty_structure.sql (100%) rename spec/{aruba => }/fixtures/migrations/56/20160812190335_create_impressions.rb (100%) rename spec/{aruba => }/fixtures/migrations/56/20171115195229_add_temporal_extension_to_impressions.rb (100%) rename spec/{aruba => }/fixtures/railsapp/config/application.rb (100%) rename spec/{aruba => }/fixtures/railsapp/config/boot.rb (100%) rename spec/{aruba => }/fixtures/railsapp/config/environments/development.rb (100%) rename spec/{aruba => }/fixtures/railsapp/config/initializers/assets.rb (100%) rename spec/{aruba => }/fixtures/set_config.sql (100%) diff --git a/Rakefile b/Rakefile index 50672dcd..6665e969 100644 --- a/Rakefile +++ b/Rakefile @@ -18,7 +18,7 @@ namespace :testapp do FileUtils.rm_rf('railsapp') sh 'rails new railsapp --skip-bundle --skip-javascript --skip-webpack-install --skip-git' end - FileUtils.cp_r('spec/aruba/fixtures/railsapp/.', 'tmp/railsapp/') + FileUtils.cp_r('spec/fixtures/railsapp/.', 'tmp/railsapp/') FileUtils.rm('tmp/railsapp/Gemfile') end end diff --git a/spec/aruba/dbconsole_spec.rb b/spec/aruba/dbconsole_spec.rb index 8105d901..3501843e 100644 --- a/spec/aruba/dbconsole_spec.rb +++ b/spec/aruba/dbconsole_spec.rb @@ -5,18 +5,14 @@ require 'spec_helper' RSpec.describe 'rails dbconsole' do - before do - write_file( - 'config/database.yml', - File.read(File.expand_path('fixtures/database_without_username_and_password.yml', __dir__))) - end + before { copy_db_config } describe 'rails dbconsole', type: :aruba do let(:action) { run_command("bash -c \"echo 'select 1 as foo_column; \\q' | bundle exec rails db\"") } let(:last_command) { action && last_command_started } - specify { expect(last_command).to be_successfully_executed } - specify { expect(last_command).to have_output(/\bfoo_column\b/) } + it { expect(last_command).to be_successfully_executed } + it { expect(last_command).to have_output(/\bfoo_column\b/) } end end diff --git a/spec/aruba/migrations_spec.rb b/spec/aruba/migrations_spec.rb index d2dcf2f0..21383aa4 100644 --- a/spec/aruba/migrations_spec.rb +++ b/spec/aruba/migrations_spec.rb @@ -1,47 +1,37 @@ require 'spec_helper' RSpec.describe 'database migrations', type: :aruba do - context 'after a migration was generated' do - before { write_file('config/database.yml', - File.read(File.expand_path('fixtures/database_without_username_and_password.yml', __dir__))) } + before { copy_db_config } + context 'after a migration was generated' do before { run_command_and_stop('bundle exec rails g migration CreateModels name:string') } describe 'bundle exec rake db:migrate' do let(:action) { run_command('bundle exec rake db:migrate') } let(:last_command) { action && last_command_started } - specify { expect(last_command).to be_successfully_executed } - specify { expect(last_command).to have_output(/CreateModels: migrated/) } + it { expect(last_command).to be_successfully_executed } + it { expect(last_command).to have_output(/CreateModels: migrated/) } end end 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 do - copy( - '../../spec/aruba/fixtures/database_without_username_and_password.yml', - 'config/database.yml' - ) - copy( - '../../spec/aruba/fixtures/migrations/56/', - 'db/migrate' - ) - end + before { copy('%/migrations/56/', 'db/migrate') } let(:action) { run_command(command) } let(:regex) { /-- change_table\(:impressions, {:temporal=>true, :copy_data=>true}\)/ } describe 'once' do let(:last_command) { action && last_command_started } - specify { expect(last_command).to be_successfully_executed } - specify { expect(last_command).to have_output(regex) } + 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 } - specify { expect(last_command).to be_successfully_executed } - specify { expect(last_command).to have_output(regex) } + it { expect(last_command).to be_successfully_executed } + it { expect(last_command).to have_output(regex) } end end end diff --git a/spec/aruba/rake_task_spec.rb b/spec/aruba/rake_task_spec.rb index 02b77215..5108425f 100644 --- a/spec/aruba/rake_task_spec.rb +++ b/spec/aruba/rake_task_spec.rb @@ -14,8 +14,7 @@ end describe "#{dump_schema_task}" do - let(:database_yml) { 'fixtures/database_without_username_and_password.yml' } - before { write_file('config/database.yml', File.read(File.expand_path(database_yml, __dir__))) } + before { copy_db_config } before { run_command_and_stop("bundle exec rake #{dump_schema_task} SCHEMA=db/test.sql") } @@ -24,7 +23,7 @@ it { expect('db/test.sql').not_to have_file_content(/\A--/) } context 'with schema_search_path option' do - let(:database_yml) { 'fixtures/database_with_schema_search_path.yml' } + 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") } @@ -37,11 +36,10 @@ end describe 'db:schema:load' do - let(:database_yml) { 'fixtures/database_without_username_and_password.yml' } - before { write_file('config/database.yml', File.read(File.expand_path(database_yml, __dir__))) } - - let(:structure_sql) { 'fixtures/set_config.sql' } - before { write_file('db/test.sql', File.read(File.expand_path(structure_sql, __dir__))) } + before do + copy_db_config + copy('%/set_config.sql', 'db/test.sql') + end before { run_command_and_stop('bundle exec rake db:schema:load SCHEMA=db/test.sql') } @@ -53,18 +51,15 @@ let(:last_command) { action && last_command_started } context 'given a file db/structure.sql' do - let(:structure_sql) { 'fixtures/empty_structure.sql' } - before { write_file('db/structure.sql', File.read(File.expand_path(structure_sql, __dir__))) } - before { write_file('config/database.yml', File.read(File.expand_path(database_yml, __dir__))) } - - subject { expect(last_command).to be_successfully_executed } + before do + copy('%/empty_structure.sql', 'db/structure.sql') + end context 'with default username and password', issue: 55 do - let(:database_yml) { 'fixtures/database_with_default_username_and_password.yml' } + 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 if which 'brew' file_mangle!('config/database.yml') do |contents| @@ -73,20 +68,22 @@ end end - specify { subject } + it { expect(last_command).to be_successfully_executed } end context 'without a specified username and password', issue: 55 do - let(:database_yml) { 'fixtures/database_without_username_and_password.yml' } + before { copy_db_config } - specify { subject } + it { expect(last_command).to be_successfully_executed } end end end describe 'db:data:dump' do - let(:database_yml) { 'fixtures/database_without_username_and_password.yml' } - before { write_file('config/database.yml', File.read(File.expand_path(database_yml, __dir__))) } + before do + copy_db_config + copy('%/set_config.sql', 'db/test.sql') + end before { run_command_and_stop('bundle exec rake db:data:dump DUMP=db/test.sql') } @@ -95,11 +92,10 @@ end describe 'db:data:load' do - let(:database_yml) { 'fixtures/database_without_username_and_password.yml' } - before { write_file('config/database.yml', File.read(File.expand_path(database_yml, __dir__))) } - - let(:structure_sql) { 'fixtures/empty_structure.sql' } - before { write_file('db/test.sql', File.read(File.expand_path(structure_sql, __dir__))) } + before do + copy_db_config + copy('%/empty_structure.sql', 'db/test.sql') + end before { run_command_and_stop('bundle exec rake db:data:load DUMP=db/test.sql') } diff --git a/spec/aruba/fixtures/database_with_default_username_and_password.yml b/spec/fixtures/database_with_default_username_and_password.yml similarity index 100% rename from spec/aruba/fixtures/database_with_default_username_and_password.yml rename to spec/fixtures/database_with_default_username_and_password.yml diff --git a/spec/aruba/fixtures/database_with_schema_search_path.yml b/spec/fixtures/database_with_schema_search_path.yml similarity index 100% rename from spec/aruba/fixtures/database_with_schema_search_path.yml rename to spec/fixtures/database_with_schema_search_path.yml diff --git a/spec/aruba/fixtures/database_without_username_and_password.yml b/spec/fixtures/database_without_username_and_password.yml similarity index 100% rename from spec/aruba/fixtures/database_without_username_and_password.yml rename to spec/fixtures/database_without_username_and_password.yml diff --git a/spec/aruba/fixtures/empty_structure.sql b/spec/fixtures/empty_structure.sql similarity index 100% rename from spec/aruba/fixtures/empty_structure.sql rename to spec/fixtures/empty_structure.sql diff --git a/spec/aruba/fixtures/migrations/56/20160812190335_create_impressions.rb b/spec/fixtures/migrations/56/20160812190335_create_impressions.rb similarity index 100% rename from spec/aruba/fixtures/migrations/56/20160812190335_create_impressions.rb rename to spec/fixtures/migrations/56/20160812190335_create_impressions.rb diff --git a/spec/aruba/fixtures/migrations/56/20171115195229_add_temporal_extension_to_impressions.rb b/spec/fixtures/migrations/56/20171115195229_add_temporal_extension_to_impressions.rb similarity index 100% rename from spec/aruba/fixtures/migrations/56/20171115195229_add_temporal_extension_to_impressions.rb rename to spec/fixtures/migrations/56/20171115195229_add_temporal_extension_to_impressions.rb diff --git a/spec/aruba/fixtures/railsapp/config/application.rb b/spec/fixtures/railsapp/config/application.rb similarity index 100% rename from spec/aruba/fixtures/railsapp/config/application.rb rename to spec/fixtures/railsapp/config/application.rb diff --git a/spec/aruba/fixtures/railsapp/config/boot.rb b/spec/fixtures/railsapp/config/boot.rb similarity index 100% rename from spec/aruba/fixtures/railsapp/config/boot.rb rename to spec/fixtures/railsapp/config/boot.rb diff --git a/spec/aruba/fixtures/railsapp/config/environments/development.rb b/spec/fixtures/railsapp/config/environments/development.rb similarity index 100% rename from spec/aruba/fixtures/railsapp/config/environments/development.rb rename to spec/fixtures/railsapp/config/environments/development.rb diff --git a/spec/aruba/fixtures/railsapp/config/initializers/assets.rb b/spec/fixtures/railsapp/config/initializers/assets.rb similarity index 100% rename from spec/aruba/fixtures/railsapp/config/initializers/assets.rb rename to spec/fixtures/railsapp/config/initializers/assets.rb diff --git a/spec/aruba/fixtures/set_config.sql b/spec/fixtures/set_config.sql similarity index 100% rename from spec/aruba/fixtures/set_config.sql rename to spec/fixtures/set_config.sql diff --git a/spec/support/aruba.rb b/spec/support/aruba.rb index fa6bf9c8..02efdd5a 100644 --- a/spec/support/aruba.rb +++ b/spec/support/aruba.rb @@ -15,6 +15,10 @@ def load_schema_task(as_regexp: false) as_regexp ? Regexp.new(str) : str end + def copy_db_config(file = 'database_without_username_and_password.yml') + copy("%/#{file}", 'config/database.yml') + end + def dump_schema_task(as_regexp: false) str = if Rails.version < '7.0'