From e63380d1986ee0f1386d4ce7c1f03b38308fcae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lovro=20Biki=C4=87?= Date: Tue, 17 Aug 2021 12:29:06 +0200 Subject: [PATCH] Update tests for rails tasks (#694) --- spec/support/outputs/console.txt | 1 - spec/support/outputs/log.txt | 1 - ...ils_assets_precompile_force_precompile.txt | 2 + ...assets_precompile_outside_deploy_block.txt | 1 + .../rails_assets_precompile_with_diff.txt | 7 + spec/support/outputs/rails_console.txt | 1 + spec/support/outputs/rails_db_migrate.txt | 1 - .../rails_db_migrate_force_migrate.txt | 2 + .../rails_db_migrate_outside_deploy_block.txt | 1 + .../outputs/rails_db_migrate_with_diff.txt | 7 + spec/support/outputs/rails_log.txt | 1 + spec/support/outputs/rails_with_arguments.txt | 1 + .../outputs/rails_without_arguments.txt | 1 + spec/support/outputs/rake_with_arguments.txt | 1 + .../outputs/rake_without_arguments.txt | 1 + spec/tasks/rails_spec.rb | 157 ++++++++++++++++-- 16 files changed, 168 insertions(+), 18 deletions(-) delete mode 100644 spec/support/outputs/console.txt delete mode 100644 spec/support/outputs/log.txt create mode 100644 spec/support/outputs/rails_assets_precompile_force_precompile.txt create mode 100644 spec/support/outputs/rails_assets_precompile_outside_deploy_block.txt create mode 100644 spec/support/outputs/rails_assets_precompile_with_diff.txt create mode 100644 spec/support/outputs/rails_console.txt delete mode 100644 spec/support/outputs/rails_db_migrate.txt create mode 100644 spec/support/outputs/rails_db_migrate_force_migrate.txt create mode 100644 spec/support/outputs/rails_db_migrate_outside_deploy_block.txt create mode 100644 spec/support/outputs/rails_db_migrate_with_diff.txt create mode 100644 spec/support/outputs/rails_log.txt create mode 100644 spec/support/outputs/rails_with_arguments.txt create mode 100644 spec/support/outputs/rails_without_arguments.txt create mode 100644 spec/support/outputs/rake_with_arguments.txt create mode 100644 spec/support/outputs/rake_without_arguments.txt diff --git a/spec/support/outputs/console.txt b/spec/support/outputs/console.txt deleted file mode 100644 index abfa13b8..00000000 --- a/spec/support/outputs/console.txt +++ /dev/null @@ -1 +0,0 @@ -\(cd\ .*/deploy/current\ &&\ RAILS_ENV="production"\ bundle\ exec\ rails\ console\ &&\ cd\ -\) diff --git a/spec/support/outputs/log.txt b/spec/support/outputs/log.txt deleted file mode 100644 index e0f09237..00000000 --- a/spec/support/outputs/log.txt +++ /dev/null @@ -1 +0,0 @@ -\(cd\ .*deploy/shared/log\ &&\ tail\ \-f\ production\.log\ &&\ cd\ -\) diff --git a/spec/support/outputs/rails_assets_precompile_force_precompile.txt b/spec/support/outputs/rails_assets_precompile_force_precompile.txt new file mode 100644 index 00000000..7396b65e --- /dev/null +++ b/spec/support/outputs/rails_assets_precompile_force_precompile.txt @@ -0,0 +1,2 @@ +echo "-----> Precompiling asset files" +RAILS_ENV="production" bundle exec rake assets:precompile \ No newline at end of file diff --git a/spec/support/outputs/rails_assets_precompile_outside_deploy_block.txt b/spec/support/outputs/rails_assets_precompile_outside_deploy_block.txt new file mode 100644 index 00000000..ff171763 --- /dev/null +++ b/spec/support/outputs/rails_assets_precompile_outside_deploy_block.txt @@ -0,0 +1 @@ +Can't be run outside deploy do block. Please use mina 'rake\[assets_precompile\]' instead \ No newline at end of file diff --git a/spec/support/outputs/rails_assets_precompile_with_diff.txt b/spec/support/outputs/rails_assets_precompile_with_diff.txt new file mode 100644 index 00000000..5bf27393 --- /dev/null +++ b/spec/support/outputs/rails_assets_precompile_with_diff.txt @@ -0,0 +1,7 @@ +if diff -qrN ".*/deploy/current/vendor/assets/" "\./vendor/assets/" 2>/dev/null && diff -qrN ".*/deploy/current/app/assets/" "\./app/assets/" 2>/dev/null && diff -qrN ".*/deploy/current/app/javascript/" "\./app/javascript/" 2>/dev/null && diff -qrN ".*/deploy/current/package\.json" "\./package\.json" 2>/dev/null +then + echo "-----> Skipping asset precompilation" +else + echo "-----> Precompiling asset files" + RAILS_ENV="production" bundle exec rake assets:precompile +fi \ No newline at end of file diff --git a/spec/support/outputs/rails_console.txt b/spec/support/outputs/rails_console.txt new file mode 100644 index 00000000..d3ecda18 --- /dev/null +++ b/spec/support/outputs/rails_console.txt @@ -0,0 +1 @@ +\(cd .*/deploy/current && RAILS_ENV="production" bundle exec rails console && cd -\) \ No newline at end of file diff --git a/spec/support/outputs/rails_db_migrate.txt b/spec/support/outputs/rails_db_migrate.txt deleted file mode 100644 index 2971cfd4..00000000 --- a/spec/support/outputs/rails_db_migrate.txt +++ /dev/null @@ -1 +0,0 @@ -if\ diff\ \-qrN\ ".*deploy/current/db/migrate"\ "\./db/migrate"\ 2>/dev/null\s*then\s*echo\ "\-\-\-\-\->\ DB\ migrations\ unchanged;\ skipping\ DB\ migration"\s*else\s*echo\ "\-\-\-\-\->\ Migrating\ database"\s*RAILS_ENV="production"\ bundle\ exec\ rake\ db:migrate\s*fi diff --git a/spec/support/outputs/rails_db_migrate_force_migrate.txt b/spec/support/outputs/rails_db_migrate_force_migrate.txt new file mode 100644 index 00000000..09e88a68 --- /dev/null +++ b/spec/support/outputs/rails_db_migrate_force_migrate.txt @@ -0,0 +1,2 @@ +echo "-----> Migrating database" +RAILS_ENV="production" bundle exec rake db:migrate \ No newline at end of file diff --git a/spec/support/outputs/rails_db_migrate_outside_deploy_block.txt b/spec/support/outputs/rails_db_migrate_outside_deploy_block.txt new file mode 100644 index 00000000..6c4debdd --- /dev/null +++ b/spec/support/outputs/rails_db_migrate_outside_deploy_block.txt @@ -0,0 +1 @@ +Can't be run outside deploy do block\. Please use mina 'rake\[db_migrate\]' instead \ No newline at end of file diff --git a/spec/support/outputs/rails_db_migrate_with_diff.txt b/spec/support/outputs/rails_db_migrate_with_diff.txt new file mode 100644 index 00000000..d587a2f7 --- /dev/null +++ b/spec/support/outputs/rails_db_migrate_with_diff.txt @@ -0,0 +1,7 @@ +if diff -qrN ".*/deploy/current/db/migrate" "\./db/migrate" 2>/dev/null +then + echo "-----> DB migrations unchanged; skipping DB migration" +else + echo "-----> Migrating database" + RAILS_ENV="production" bundle exec rake db:migrate +fi \ No newline at end of file diff --git a/spec/support/outputs/rails_log.txt b/spec/support/outputs/rails_log.txt new file mode 100644 index 00000000..70dc4504 --- /dev/null +++ b/spec/support/outputs/rails_log.txt @@ -0,0 +1 @@ +\(cd .*/deploy/shared/log && tail -f production\.log && cd -\) \ No newline at end of file diff --git a/spec/support/outputs/rails_with_arguments.txt b/spec/support/outputs/rails_with_arguments.txt new file mode 100644 index 00000000..d5aebe15 --- /dev/null +++ b/spec/support/outputs/rails_with_arguments.txt @@ -0,0 +1 @@ +\(cd .*/deploy/current && RAILS_ENV="production" bundle exec rails db:rollback STEP=2 && cd -\) \ No newline at end of file diff --git a/spec/support/outputs/rails_without_arguments.txt b/spec/support/outputs/rails_without_arguments.txt new file mode 100644 index 00000000..4dbbe2b4 --- /dev/null +++ b/spec/support/outputs/rails_without_arguments.txt @@ -0,0 +1 @@ +You need to provide arguments. Try: mina "rails\[console\]" \ No newline at end of file diff --git a/spec/support/outputs/rake_with_arguments.txt b/spec/support/outputs/rake_with_arguments.txt new file mode 100644 index 00000000..d63773d8 --- /dev/null +++ b/spec/support/outputs/rake_with_arguments.txt @@ -0,0 +1 @@ +\(cd .*/deploy/current && RAILS_ENV="production" bundle exec rake db:primary:migrate && cd -\) \ No newline at end of file diff --git a/spec/support/outputs/rake_without_arguments.txt b/spec/support/outputs/rake_without_arguments.txt new file mode 100644 index 00000000..240cb951 --- /dev/null +++ b/spec/support/outputs/rake_without_arguments.txt @@ -0,0 +1 @@ +You need to provide arguments. Try: mina "rake\[db:migrate\]" \ No newline at end of file diff --git a/spec/tasks/rails_spec.rb b/spec/tasks/rails_spec.rb index 382baaf8..370043a9 100644 --- a/spec/tasks/rails_spec.rb +++ b/spec/tasks/rails_spec.rb @@ -3,21 +3,66 @@ RSpec.describe 'rails', type: :rake do describe 'console' do it 'starts console' do - expect { invoke_all }.to output(output_file('console')).to_stdout + expect { invoke_all }.to output(output_file('rails_console')).to_stdout end end describe 'log' do it 'tails log' do - expect { invoke_all }.to output(output_file('log')).to_stdout + expect { invoke_all }.to output(output_file('rails_log')).to_stdout end end describe 'rails:db_migrate' do - it 'rails db migrate' do - Mina::Configuration.instance.set(:deploy_block, true) - expect { invoke_all }.to output(output_file('rails_db_migrate')).to_stdout - Mina::Configuration.instance.set(:deploy_block, false) + subject { rake['rails:db_migrate'] } + + context 'when outside deploy block' do + around do |example| + original_flag = Mina::Configuration.instance.remove(:deploy_block) + example.run + Mina::Configuration.instance.set(:deploy_block, original_flag) + end + + it 'exits with an error message' do + expect do + invoke_all + end.to raise_error(SystemExit) + .and output(output_file('rails_db_migrate_outside_deploy_block')).to_stdout + end + end + + context 'with force_migrate flag' do + before do + Mina::Configuration.instance.set(:force_migrate, true) + Mina::Configuration.instance.set(:deploy_block, true) + end + + after do + Mina::Configuration.instance.remove(:force_migrate) + Mina::Configuration.instance.set(:deploy_block, false) + end + + it 'runs rails db:migrate' do + expect do + invoke_all + end.to output(output_file('rails_db_migrate_force_migrate')).to_stdout + end + end + + context 'without force_migrate flag' do + before do + Mina::Configuration.instance.set(:deploy_block, true) + end + + after do + Mina::Configuration.instance.set(:deploy_block, false) + end + + it 'runs rails db:migrate but checks for changes first' do + expect do + invoke_all + end.to output(output_file('rails_db_migrate_with_diff')).to_stdout + end end end @@ -34,10 +79,55 @@ end describe 'rails:assets_precompile' do - it 'rails assets precompile' do - Mina::Configuration.instance.set(:deploy_block, true) - expect { invoke_all }.to output(output_file('rails_assets_precompile')).to_stdout - Mina::Configuration.instance.set(:deploy_block, false) + subject { rake['rails:assets_precompile'] } + + context 'when outside deploy block' do + around do |example| + original_flag = Mina::Configuration.instance.remove(:deploy_block) + example.run + Mina::Configuration.instance.set(:deploy_block, original_flag) + end + + it 'exits with an error message' do + expect do + invoke_all + end.to raise_error(SystemExit) + .and output(output_file('rails_assets_precompile_outside_deploy_block')).to_stdout + end + end + + context 'with force_asset_precompile flag' do + before do + Mina::Configuration.instance.set(:force_asset_precompile, true) + Mina::Configuration.instance.set(:deploy_block, true) + end + + after do + Mina::Configuration.instance.remove(:force_asset_precompile) + Mina::Configuration.instance.set(:deploy_block, false) + end + + it 'runs rails assets:precompile' do + expect do + invoke_all + end.to output(output_file('rails_assets_precompile_force_precompile')).to_stdout + end + end + + context 'without force_asset_precompile flag' do + before do + Mina::Configuration.instance.set(:deploy_block, true) + end + + after do + Mina::Configuration.instance.set(:deploy_block, false) + end + + it 'runs rails assets:precompile but checks for changes first' do + expect do + invoke_all + end.to output(output_file('rails_assets_precompile_with_diff')).to_stdout + end end end @@ -52,9 +142,46 @@ expect { invoke_all }.to output(output_file('rails_db_schema_load')).to_stdout end end - # describe 'rollback' do - # it 'rollback' do - # expect { invoke_all }.to output(output_file('rollback')).to_stdout - # end - # end + + describe 'rails' do + subject { rake['rails'] } + + context 'without arguments' do + it 'exits with an error message' do + expect do + invoke_all + end.to raise_error(SystemExit) + .and output(output_file('rails_without_arguments')).to_stdout + end + end + + context 'with arguments' do + it 'executes the arguments' do + expect do + invoke_all('db:rollback STEP=2') + end.to output(output_file('rails_with_arguments')).to_stdout + end + end + end + + describe 'rake' do + subject { rake['rake'] } + + context 'without arguments' do + it 'exits with an error message' do + expect do + invoke_all + end.to raise_error(SystemExit) + .and output(output_file('rake_without_arguments')).to_stdout + end + end + + context 'with arguments' do + it 'executes the arguments' do + expect do + invoke_all('db:primary:migrate') + end.to output(output_file('rake_with_arguments')).to_stdout + end + end + end end