Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests for Rails tasks #694

Merged
merged 1 commit into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion spec/support/outputs/console.txt

This file was deleted.

1 change: 0 additions & 1 deletion spec/support/outputs/log.txt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
echo "-----> Precompiling asset files"
RAILS_ENV="production" bundle exec rake assets:precompile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Can't be run outside deploy do block. Please use mina 'rake\[assets_precompile\]' instead
7 changes: 7 additions & 0 deletions spec/support/outputs/rails_assets_precompile_with_diff.txt
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/support/outputs/rails_console.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\(cd .*/deploy/current && RAILS_ENV="production" bundle exec rails console && cd -\)
1 change: 0 additions & 1 deletion spec/support/outputs/rails_db_migrate.txt

This file was deleted.

2 changes: 2 additions & 0 deletions spec/support/outputs/rails_db_migrate_force_migrate.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
echo "-----> Migrating database"
RAILS_ENV="production" bundle exec rake db:migrate
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Can't be run outside deploy do block\. Please use mina 'rake\[db_migrate\]' instead
7 changes: 7 additions & 0 deletions spec/support/outputs/rails_db_migrate_with_diff.txt
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/support/outputs/rails_log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\(cd .*/deploy/shared/log && tail -f production\.log && cd -\)
1 change: 1 addition & 0 deletions spec/support/outputs/rails_with_arguments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\(cd .*/deploy/current && RAILS_ENV="production" bundle exec rails db:rollback STEP=2 && cd -\)
1 change: 1 addition & 0 deletions spec/support/outputs/rails_without_arguments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You need to provide arguments. Try: mina "rails\[console\]"
1 change: 1 addition & 0 deletions spec/support/outputs/rake_with_arguments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\(cd .*/deploy/current && RAILS_ENV="production" bundle exec rake db:primary:migrate && cd -\)
1 change: 1 addition & 0 deletions spec/support/outputs/rake_without_arguments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You need to provide arguments. Try: mina "rake\[db:migrate\]"
157 changes: 142 additions & 15 deletions spec/tasks/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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