From d16e3615a975e16d3333edce6847580246655835 Mon Sep 17 00:00:00 2001 From: Lovro Bikic Date: Sat, 14 Aug 2021 17:45:44 +0200 Subject: [PATCH 1/4] Catch unhandled system exits See https://github.com/rspec/rspec-core/issues/2246 for more info --- spec/spec_helper.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index aa975657..03de4120 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -40,4 +40,12 @@ $stdout, $stderr = original_stdout, original_stderr end + + config.around(:all) do |example| + begin + example.run + rescue SystemExit => e + raise "Unhandled system exit (you're probably missing a raise_error(SystemExit) matcher somewhere)" + end + end end From b201432ee0bb12e0c54ce6c5041f95058e5a121d Mon Sep 17 00:00:00 2001 From: Lovro Bikic Date: Sat, 14 Aug 2021 17:46:06 +0200 Subject: [PATCH 2/4] Fix typo --- tasks/mina/rvm.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/mina/rvm.rb b/tasks/mina/rvm.rb index 20d05488..4cd0b382 100644 --- a/tasks/mina/rvm.rb +++ b/tasks/mina/rvm.rb @@ -21,7 +21,7 @@ task :'rvm:wrapper', :env, :name, :bin do |_, args| unless args[:env] && args[:name] && args[:bin] - puts "Task 'rvm:wrapper' needs an RVM environment name, an wrapper name and the binary name as arguments" + puts "Task 'rvm:wrapper' needs an RVM environment name, a wrapper name and the binary name as arguments" puts "Example: invoke :'rvm:wrapper', 'ruby-1.9.2@myapp,myapp,unicorn_rails'" exit 1 end From 1393bbab75eca1b2d0a181674538661ae0cf5c27 Mon Sep 17 00:00:00 2001 From: Lovro Bikic Date: Sat, 14 Aug 2021 17:46:25 +0200 Subject: [PATCH 3/4] Fix argument passing to rake task --- spec/support/rake_example_group.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/support/rake_example_group.rb b/spec/support/rake_example_group.rb index 4d0d5972..f1aa4dc6 100644 --- a/spec/support/rake_example_group.rb +++ b/spec/support/rake_example_group.rb @@ -18,8 +18,8 @@ def self.included(klass) end end - def invoke_all(args = nil) - subject.invoke(args) + def invoke_all(*args) + subject.invoke(*args) run_commands.invoke end From f552ac7bfabae36bbcceff4b2c187101fd6b16e2 Mon Sep 17 00:00:00 2001 From: Lovro Bikic Date: Sat, 14 Aug 2021 17:46:48 +0200 Subject: [PATCH 4/4] Write specs for version manager tasks --- spec/support/outputs/chruby.txt | 9 ---- spec/support/outputs/chruby_with_env.txt | 7 +++ spec/support/outputs/chruby_without_env.txt | 2 + spec/support/outputs/rbenv_load.txt | 10 ++++- spec/support/outputs/rvm_use.txt | 1 - spec/support/outputs/rvm_use_with_env.txt | 8 ++++ spec/support/outputs/rvm_use_without_env.txt | 2 + .../outputs/rvm_wrapper_with_arguments.txt | 8 ++++ .../outputs/rvm_wrapper_without_arguments.txt | 2 + spec/support/outputs/ry.txt | 15 ------- spec/support/outputs/ry_with_env.txt | 13 ++++++ spec/support/outputs/ry_without_env.txt | 13 ++++++ spec/tasks/chruby_spec.rb | 23 ++++++++++ spec/tasks/rbenv_spec.rb | 9 ++++ spec/tasks/ruby_managers_spec.rb | 33 -------------- spec/tasks/rvm_spec.rb | 45 +++++++++++++++++++ spec/tasks/ry_spec.rb | 37 +++++++++++++++ 17 files changed, 178 insertions(+), 59 deletions(-) delete mode 100644 spec/support/outputs/chruby.txt create mode 100644 spec/support/outputs/chruby_with_env.txt create mode 100644 spec/support/outputs/chruby_without_env.txt delete mode 100644 spec/support/outputs/rvm_use.txt create mode 100644 spec/support/outputs/rvm_use_with_env.txt create mode 100644 spec/support/outputs/rvm_use_without_env.txt create mode 100644 spec/support/outputs/rvm_wrapper_with_arguments.txt create mode 100644 spec/support/outputs/rvm_wrapper_without_arguments.txt delete mode 100644 spec/support/outputs/ry.txt create mode 100644 spec/support/outputs/ry_with_env.txt create mode 100644 spec/support/outputs/ry_without_env.txt create mode 100644 spec/tasks/chruby_spec.rb create mode 100644 spec/tasks/rbenv_spec.rb delete mode 100644 spec/tasks/ruby_managers_spec.rb create mode 100644 spec/tasks/rvm_spec.rb create mode 100644 spec/tasks/ry_spec.rb diff --git a/spec/support/outputs/chruby.txt b/spec/support/outputs/chruby.txt deleted file mode 100644 index 66e60e5d..00000000 --- a/spec/support/outputs/chruby.txt +++ /dev/null @@ -1,9 +0,0 @@ -echo "-----> chruby to version: "123"" - - if [[ ! -s "/etc/profile.d/chruby.sh" ]]; then - echo "! chruby.sh init file not found" - exit 1 - fi - -source /etc/profile.d/chruby.sh -chruby "123" || exit 1 \ No newline at end of file diff --git a/spec/support/outputs/chruby_with_env.txt b/spec/support/outputs/chruby_with_env.txt new file mode 100644 index 00000000..ea1d7f4d --- /dev/null +++ b/spec/support/outputs/chruby_with_env.txt @@ -0,0 +1,7 @@ +echo "-----> chruby to version: \\"123\\"" +if \[\[ ! -s "/etc/profile\.d/chruby\.sh" \]\]; then + echo "! chruby\.sh init file not found" + exit 1 +fi +source /etc/profile\.d/chruby\.sh +chruby "123" \|\| exit 1 \ No newline at end of file diff --git a/spec/support/outputs/chruby_without_env.txt b/spec/support/outputs/chruby_without_env.txt new file mode 100644 index 00000000..33d83517 --- /dev/null +++ b/spec/support/outputs/chruby_without_env.txt @@ -0,0 +1,2 @@ +Task 'chruby' needs a Ruby version as an argument\. +Example: invoke :chruby, 'ruby-2.4' \ No newline at end of file diff --git a/spec/support/outputs/rbenv_load.txt b/spec/support/outputs/rbenv_load.txt index 4fa628c2..eafaf643 100644 --- a/spec/support/outputs/rbenv_load.txt +++ b/spec/support/outputs/rbenv_load.txt @@ -1 +1,9 @@ -echo\ "\-\-\-\-\->\ Loading\ rbenv"\nexport\ RBENV_ROOT="\$HOME/\.rbenv"\nexport\ PATH="\$HOME/\.rbenv/bin:\$PATH"\s*if\ !\ which\ rbenv\ >/dev/null;\ then\s*echo\ "!\ rbenv\ not\ found"\s*echo\ "!\ If\ rbenv\ is\ installed,\ check\ your\ :rbenv_path\ setting\."\s*exit\ 1\s*fi\s*eval\ "\$\(rbenv\ init\ \-\)" \ No newline at end of file +echo "-----> Loading rbenv" +export RBENV_ROOT="\$HOME/\.rbenv" +export PATH="\$HOME/\.rbenv/bin:\$PATH" +if ! which rbenv >/dev/null; then + echo "! rbenv not found" + echo "! If rbenv is installed, check your :rbenv_path setting\." + exit 1 +fi +eval "\$\(rbenv init -\)" \ No newline at end of file diff --git a/spec/support/outputs/rvm_use.txt b/spec/support/outputs/rvm_use.txt deleted file mode 100644 index 9a32af59..00000000 --- a/spec/support/outputs/rvm_use.txt +++ /dev/null @@ -1 +0,0 @@ -echo\ "\-\-\-\-\->\ Using\ RVM\ environment\ \\\"123\\\"\"\s*if\ \[\[\ !\ \-s\ "\$HOME/\.rvm/scripts/rvm"\ \]\];\ then\s*echo\ "!\ Ruby\ Version\ Manager\ not\ found"\s*echo\ "!\ If\ RVM\ is\ installed,\ check\ your\ :rvm_use_path\ setting\."\s*exit 1\s*fi\s*source\ \$HOME/\.rvm/scripts/rvm\n\s*rvm\ use\ \"123\"\ \-\-create diff --git a/spec/support/outputs/rvm_use_with_env.txt b/spec/support/outputs/rvm_use_with_env.txt new file mode 100644 index 00000000..fa84b0f4 --- /dev/null +++ b/spec/support/outputs/rvm_use_with_env.txt @@ -0,0 +1,8 @@ +echo "-----> Using RVM environment \\"3\.0\.0\\"" +if \[\[ ! -s "\$HOME/\.rvm/scripts/rvm" \]\]; then + echo "! Ruby Version Manager not found" + echo "! If RVM is installed, check your :rvm_use_path setting\." + exit 1 +fi +source \$HOME/\.rvm/scripts/rvm +rvm use "3\.0\.0" --create \ No newline at end of file diff --git a/spec/support/outputs/rvm_use_without_env.txt b/spec/support/outputs/rvm_use_without_env.txt new file mode 100644 index 00000000..c1b6508f --- /dev/null +++ b/spec/support/outputs/rvm_use_without_env.txt @@ -0,0 +1,2 @@ +Task 'rvm:use' needs an RVM environment name as an argument\. +Example: invoke :'rvm:use', 'ruby-1\.9\.2@default' \ No newline at end of file diff --git a/spec/support/outputs/rvm_wrapper_with_arguments.txt b/spec/support/outputs/rvm_wrapper_with_arguments.txt new file mode 100644 index 00000000..5b51d7cc --- /dev/null +++ b/spec/support/outputs/rvm_wrapper_with_arguments.txt @@ -0,0 +1,8 @@ +echo "-----> creating RVM wrapper "myapp_unicorn" using \\"3\.0\.0\\"" +if \[\[ ! -s "\$HOME/\.rvm/scripts/rvm" \]\]; then + echo "! Ruby Version Manager not found" + echo "! If RVM is installed, check your :rvm_use_path setting\." + exit 1 +fi +source \$HOME/\.rvm/scripts/rvm +rvm wrapper 3\.0\.0 myapp unicorn \|\| exit 1 \ No newline at end of file diff --git a/spec/support/outputs/rvm_wrapper_without_arguments.txt b/spec/support/outputs/rvm_wrapper_without_arguments.txt new file mode 100644 index 00000000..9557fb84 --- /dev/null +++ b/spec/support/outputs/rvm_wrapper_without_arguments.txt @@ -0,0 +1,2 @@ +Task 'rvm:wrapper' needs an RVM environment name, a wrapper name and the binary name as arguments +Example: invoke :'rvm:wrapper', 'ruby-1\.9\.2@myapp,myapp,unicorn_rails' \ No newline at end of file diff --git a/spec/support/outputs/ry.txt b/spec/support/outputs/ry.txt deleted file mode 100644 index 9a0569bc..00000000 --- a/spec/support/outputs/ry.txt +++ /dev/null @@ -1,15 +0,0 @@ -echo "-----> Loading ry" - - if [[ ! -e "$HOME/.local/bin" ]]; then - echo "! ry not found" - echo "! If ry is installed, check your :ry_path setting." - exit 1 - fi - -export PATH="$HOME/.local/bin:$PATH" -eval "$(ry setup)" -RY_RUBY="" - - if [ -n "$RY_RUBY" ]; then - ry use $RY_RUBY || exit 1 - fi \ No newline at end of file diff --git a/spec/support/outputs/ry_with_env.txt b/spec/support/outputs/ry_with_env.txt new file mode 100644 index 00000000..f916261e --- /dev/null +++ b/spec/support/outputs/ry_with_env.txt @@ -0,0 +1,13 @@ +echo "-----> ry to version: \\\"3\.0\.0\\\"" +echo "-----> Loading ry" +if \[\[ ! -e "\$HOME/\.local/bin" \]\]; then + echo "! ry not found" + echo "! If ry is installed, check your :ry_path setting." + exit 1 +fi +export PATH="\$HOME/\.local/bin:\$PATH" +eval "\$\(ry setup\)" +RY_RUBY="3\.0\.0" +if \[ -n "\$RY_RUBY" \]; then + ry use \$RY_RUBY \|\| exit 1 +fi \ No newline at end of file diff --git a/spec/support/outputs/ry_without_env.txt b/spec/support/outputs/ry_without_env.txt new file mode 100644 index 00000000..d1c10d83 --- /dev/null +++ b/spec/support/outputs/ry_without_env.txt @@ -0,0 +1,13 @@ +echo "-----> ry to version: \\"\*\*not specified\*\*\\"" +echo "-----> Loading ry" +if \[\[ ! -e "\$HOME/\.local/bin" \]\]; then + echo "! ry not found" + echo "! If ry is installed, check your :ry_path setting." + exit 1 +fi +export PATH="\$HOME/\.local/bin:\$PATH" +eval "\$\(ry setup\)" +RY_RUBY="" +if \[ -n "\$RY_RUBY" \]; then + ry use \$RY_RUBY \|\| exit 1 +fi \ No newline at end of file diff --git a/spec/tasks/chruby_spec.rb b/spec/tasks/chruby_spec.rb new file mode 100644 index 00000000..3b426224 --- /dev/null +++ b/spec/tasks/chruby_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper' + +RSpec.describe 'chruby', type: :rake do + describe 'chruby' do + subject { rake['chruby'] } + + context 'without an argument' do + it 'exits with an error message' do + expect do + invoke_all + end.to raise_error(SystemExit).and output(output_file('chruby_without_env')).to_stdout + end + end + + context 'with an argument' do + it 'sets ruby version' do + expect do + invoke_all('123') + end.to output(output_file('chruby_with_env')).to_stdout + end + end + end +end diff --git a/spec/tasks/rbenv_spec.rb b/spec/tasks/rbenv_spec.rb new file mode 100644 index 00000000..07a5bf67 --- /dev/null +++ b/spec/tasks/rbenv_spec.rb @@ -0,0 +1,9 @@ +require 'spec_helper' + +RSpec.describe 'rbenv', type: :rake do + describe 'rbenv:load' do + it 'loads rbenv' do + expect { invoke_all }.to output(output_file('rbenv_load')).to_stdout + end + end +end diff --git a/spec/tasks/ruby_managers_spec.rb b/spec/tasks/ruby_managers_spec.rb deleted file mode 100644 index 606e3292..00000000 --- a/spec/tasks/ruby_managers_spec.rb +++ /dev/null @@ -1,33 +0,0 @@ -require 'spec_helper' - -RSpec.describe 'ruby_managers', type: :rake do - describe 'ry' do - it 'ry' do - expect { invoke_all }.to output(output_file('ry')).to_stdout - end - end - - describe 'rbenv:load' do - it 'rbenv:load' do - expect { invoke_all }.to output(output_file('rbenv_load')).to_stdout - end - end - - describe 'chruby' do - it 'chruby' do - expect { invoke_all('123') }.to output(output_file('chruby')).to_stdout - end - end - - describe 'rvm:use' do - it 'rvm:use' do - expect { invoke_all('123') }.to output(output_file('rvm_use')).to_stdout - end - end - - # describe 'rvm:wrapper' do - # it 'rails db migrate' do - # expect { invoke_all('123') }.to output('').to_stdout - # end - # end -end diff --git a/spec/tasks/rvm_spec.rb b/spec/tasks/rvm_spec.rb new file mode 100644 index 00000000..fa9e8de8 --- /dev/null +++ b/spec/tasks/rvm_spec.rb @@ -0,0 +1,45 @@ +require 'spec_helper' + +RSpec.describe 'rvm', type: :rake do + describe 'rvm:use' do + subject { rake['rvm:use'] } + + context 'without an argument' do + it 'exits with an error message' do + expect do + invoke_all + end.to raise_error(SystemExit) + .and output(output_file('rvm_use_without_env')).to_stdout + end + end + + context 'with an argument' do + it 'sets ruby version' do + expect do + invoke_all('3.0.0') + end.to output(output_file('rvm_use_with_env')).to_stdout + end + end + end + + describe 'rvm:wrapper' do + subject { rake['rvm:wrapper'] } + + context 'without all arguments' do + it 'exits with an error message' do + expect do + invoke_all('ruby-3.0.0') + end.to raise_error(SystemExit) + .and output(output_file('rvm_wrapper_without_arguments')).to_stdout + end + end + + context 'with all arguments' do + it 'calls rvm wrapper' do + expect do + invoke_all('3.0.0', 'myapp', 'unicorn') + end.to output(output_file('rvm_wrapper_with_arguments')).to_stdout + end + end + end +end diff --git a/spec/tasks/ry_spec.rb b/spec/tasks/ry_spec.rb new file mode 100644 index 00000000..03b7e42b --- /dev/null +++ b/spec/tasks/ry_spec.rb @@ -0,0 +1,37 @@ +require 'spec_helper' + +RSpec.describe 'ry', type: :rake do + describe 'ry' do + subject { rake['ry'] } + + let(:default_version_warning) { /Task 'ry' without argument will use default Ruby version\./ } + + context 'without an argument' do + it 'prints a warning' do + expect do + invoke_all + end.to output(default_version_warning).to_stdout + end + + it 'sets ruby version' do + expect do + invoke_all + end.to output(output_file('ry_without_env')).to_stdout + end + end + + context 'with an argument' do + it "doesn't print a warning" do + expect do + invoke_all('3.0.0') + end.not_to output(default_version_warning).to_stdout + end + end + + it 'sets ruby version' do + expect do + invoke_all('3.0.0') + end.to output(output_file('ry_with_env')).to_stdout + end + end +end