diff --git a/lib/pulsar/cli.rb b/lib/pulsar/cli.rb index 8df4b5b..64a86c0 100644 --- a/lib/pulsar/cli.rb +++ b/lib/pulsar/cli.rb @@ -5,7 +5,7 @@ module Pulsar class CLI < Thor - def self.exit_on_failure?; true; end + def self.exit_with_status_on_failure?; true; end map %w[--version -v] => :__print_version desc 'install [DIRECTORY]', 'Install initial repository in DIRECTORY' @@ -21,11 +21,11 @@ def install(directory = './pulsar-conf') if result.success? puts 'Successfully created intial repo!' - exit 0 + exit_with_status 0 else puts 'Failed to create intial repo.' puts result.error - exit result.error.exit_code + exit_with_status result.error.is_a?(Pulsar::ContextError) ? result.error.exit_code : 1 end end @@ -43,11 +43,11 @@ def list result.applications.each do |app, stages| puts "#{app}: #{stages.join(', ')}" end - exit 0 + exit_with_status 0 else puts 'Failed to list application and environments.' puts result.error - exit result.error.exit_code + exit_with_status result.error.is_a?(Pulsar::ContextError) ? result.error.exit_code : 1 end end @@ -67,22 +67,27 @@ def deploy(application, environment) if result.success? puts "Deployed #{application} on #{environment}!" - exit 0 + exit_with_status 0 else puts "Failed to deploy #{application} on #{environment}." puts result.error - exit result.error.exit_code + exit_with_status result.error.is_a?(Pulsar::ContextError) ? result.error.exit_code : 1 end end desc "--version, -v", "print the version" def __print_version puts Pulsar::VERSION - exit 0 + exit_with_status 0 end private + def exit_with_status(status) + return if ENV['COVERAGE'] + exit status + end + def load_config Dotenv.load(PULSAR_CONF) # Load configurations for Pulsar end @@ -99,5 +104,7 @@ def load_option_or_env!(option) option_value end + + def exit_on_failure?; true; end end end diff --git a/lib/pulsar/executor.rb b/lib/pulsar/executor.rb index 6c7dfe4..c439400 100644 --- a/lib/pulsar/executor.rb +++ b/lib/pulsar/executor.rb @@ -3,15 +3,16 @@ module Pulsar module Executor def self.sh(cmd) - stdin, stdout, stderr, wait_thr = Open3.popen3(cmd) - output = stdout.gets(nil) - stdout.close - stderr.gets(nil) - stderr.close - stdin.close + stdin_stream, stdout_stream, stderr_stream, wait_thr = Open3.popen3(cmd) + stdout = stdout_stream.gets(nil) || "" + stderr = stderr_stream.gets(nil) || "" + stdout_stream.close + stderr_stream.gets(nil) + stderr_stream.close + stdin_stream.close exit_code = wait_thr.value - yield exit_code, output if block_given? - [exit_code, output] + yield exit_code, stdout, stderr if block_given? + { status: exit_code.exitstatus, output: stdout + stderr } end end end diff --git a/spec/features/deploy_spec.rb b/spec/features/deploy_spec.rb index 2168d40..7021a12 100644 --- a/spec/features/deploy_spec.rb +++ b/spec/features/deploy_spec.rb @@ -1,24 +1,23 @@ require 'spec_helper' RSpec.describe 'Deploy' do - subject { -> { command } } + subject { command_output } let(:command) do - `DRY_RUN=true #{RSpec.configuration.pulsar_command} deploy #{options} #{arguments}` + Pulsar::Executor.sh("DRY_RUN=true #{RSpec.configuration.pulsar_command} deploy #{options} #{arguments}") end - let(:exit_status) do - Pulsar::Executor.sh("DRY_RUN=true #{RSpec.configuration.pulsar_command} deploy #{options} #{arguments}")[0].exitstatus - end - let(:repo) { RSpec.configuration.pulsar_conf_path } - let(:options) { "--conf-repo #{repo}" } - let(:app) { 'blog' } - let(:environment) { 'production' } - let(:arguments) { "#{app} #{environment}" } + let(:command_output) { command[:output] } + let(:exit_status) { command[:status] } + let(:repo) { RSpec.configuration.pulsar_conf_path } + let(:options) { "--conf-repo #{repo}" } + let(:app) { 'blog' } + let(:environment) { 'production' } + let(:arguments) { "#{app} #{environment}" } context 'via a subcommand named deploy' do let(:error) { /Could not find command/ } - it { is_expected.not_to output(error).to_stderr_from_any_process } + it { is_expected.not_to match(error) } it { expect(exit_status).to eq(0) } end @@ -26,19 +25,19 @@ let(:options) { nil } let(:error) { /No value provided for required options '--conf-repo'/ } - it { is_expected.to output(error).to_stderr_from_any_process } + it { is_expected.to match(error) } context 'can be specified via the alias -c' do let(:options) { "-c #{repo}" } - it { is_expected.not_to output(error).to_stderr_from_any_process } + it { is_expected.not_to match(error) } it { expect(exit_status).to eq(0) } end context 'can be specified via the environment variable PULSAR_CONF_REPO' do before { ENV['PULSAR_CONF_REPO'] = repo } - it { is_expected.not_to output(error).to_stderr_from_any_process } + it { is_expected.not_to match(error) } it { expect(exit_status).to eq(0) } end end @@ -48,12 +47,12 @@ let(:environment) { nil } let(:error) { /Usage: "pulsar deploy APPLICATION ENVIRONMENT"/ } - it { is_expected.to output(error).to_stderr_from_any_process } + it { is_expected.to match(error) } it { expect(exit_status).to eq(1) } end context 'when succeeds' do - subject { command } + subject { command_output } context 'deploys an application on a environment in the pulsar configuration' do let(:output) { "Deployed blog on production!\n" } @@ -71,7 +70,7 @@ context 'leaves the tmp folder empty' do subject { Dir.glob("#{Pulsar::PULSAR_TMP}/*") } - before { command } + before { command_output } it { is_expected.to be_empty } end @@ -89,7 +88,7 @@ context 'leaves the tmp folder empty' do subject { Dir.glob("#{Pulsar::PULSAR_TMP}/*") } - before { command } + before { command_output } it { is_expected.to be_empty } end @@ -107,7 +106,7 @@ context 'leaves the tmp folder empty' do subject { Dir.glob("#{Pulsar::PULSAR_TMP}/*") } - before { command } + before { command_output } it { is_expected.to be_empty } end @@ -116,7 +115,7 @@ end context 'when fails' do - subject { command } + subject { command_output } context 'because of wrong directory' do let(:repo) { './some-wrong-directory' } diff --git a/spec/features/install_spec.rb b/spec/features/install_spec.rb index dba122a..2bb1167 100644 --- a/spec/features/install_spec.rb +++ b/spec/features/install_spec.rb @@ -1,19 +1,18 @@ require 'spec_helper' RSpec.describe 'Install' do - subject { command } + subject { command_output } let(:command) do - `#{RSpec.configuration.pulsar_command} install #{arguments}` - end - let(:exit_status) do - Pulsar::Executor.sh("#{RSpec.configuration.pulsar_command} install #{arguments}")[0].exitstatus + Pulsar::Executor.sh("#{RSpec.configuration.pulsar_command} install #{arguments}") end + let(:command_output) { command[:output] } + let(:exit_status) { command[:status] } let(:arguments) { nil } context 'via a subcommand named install' do - subject { -> { command } } + subject { -> { command_output } } let(:error) { /Could not find command/ } @@ -25,7 +24,7 @@ it { is_expected.to eql "Successfully created intial repo!\n" } context 'creates a directory' do - subject { -> { command } } + subject { -> { command_output } } it { expect(exit_status).to eq(0) } @@ -34,7 +33,7 @@ Dir.entries('./../../../lib/pulsar/generators/initial_repo/') end - before { command } + before { command_output } it 'contains the initial directory structure' do is_expected.to eql Dir.entries('./pulsar-conf') @@ -64,7 +63,7 @@ it { is_expected.to match "Failed to create intial repo.\n" } context 'does not create a directory' do - subject { -> { command } } + subject { -> { command_output } } it { is_expected.not_to change { File.exist?('./my-dir') }.from(false) } it { expect(exit_status).to eq(1) } diff --git a/spec/features/list_spec.rb b/spec/features/list_spec.rb index ad1418a..974a67c 100644 --- a/spec/features/list_spec.rb +++ b/spec/features/list_spec.rb @@ -1,21 +1,20 @@ require 'spec_helper' RSpec.describe 'List' do - subject { -> { command } } + subject { command_output } let(:command) do - `#{RSpec.configuration.pulsar_command} list #{arguments}` - end - let(:exit_status) do - Pulsar::Executor.sh("#{RSpec.configuration.pulsar_command} list #{arguments}")[0].exitstatus + Pulsar::Executor.sh("#{RSpec.configuration.pulsar_command} list #{arguments}") end + let(:command_output) { command[:output] } + let(:exit_status) { command[:status] } let(:repo) { RSpec.configuration.pulsar_conf_path } let(:arguments) { "--conf-repo #{repo}" } context 'via a subcommand named list' do let(:error) { /Could not find command/ } - it { is_expected.not_to output(error).to_stderr_from_any_process } + it { is_expected.not_to match(error) } it { expect(exit_status).to eq(0) } end @@ -23,26 +22,26 @@ let(:arguments) { nil } let(:error) { /No value provided for required options '--conf-repo'/ } - it { is_expected.to output(error).to_stderr_from_any_process } + it { is_expected.to match(error) } it { expect(exit_status).to eq(1) } context 'can be specified via the alias -c' do let(:arguments) { "-c #{repo}" } - it { is_expected.not_to output(error).to_stderr_from_any_process } + it { is_expected.not_to match(error) } it { expect(exit_status).to eq(0) } end context 'can be specified via the environment variable PULSAR_CONF_REPO' do before { ENV['PULSAR_CONF_REPO'] = repo } - it { is_expected.not_to output(error).to_stderr_from_any_process } + it { is_expected.not_to match(error) } it { expect(exit_status).to eq(0) } end end context 'when succeeds' do - subject { command } + subject { command_output } context 'lists applications in the pulsar configuration' do let(:output) { "blog: production, staging\necommerce: staging\n" } @@ -60,7 +59,7 @@ context 'leaves the tmp folder empty' do subject { Dir.glob("#{Pulsar::PULSAR_TMP}/*") } - before { command } + before { command_output } it { is_expected.to be_empty } end @@ -76,7 +75,7 @@ context 'leaves the tmp folder empty' do subject { Dir.glob("#{Pulsar::PULSAR_TMP}/*") } - before { command } + before { command_output } it { is_expected.to be_empty } end @@ -92,7 +91,7 @@ context 'leaves the tmp folder empty' do subject { Dir.glob("#{Pulsar::PULSAR_TMP}/*") } - before { command } + before { command_output } it { is_expected.to be_empty } end @@ -101,7 +100,7 @@ end context 'when fails' do - subject { command } + subject { command_output } context 'because of wrong directory' do let(:repo) { './some-wrong-directory' } diff --git a/spec/features/version_spec.rb b/spec/features/version_spec.rb index b123e86..59816fd 100644 --- a/spec/features/version_spec.rb +++ b/spec/features/version_spec.rb @@ -1,14 +1,13 @@ require 'spec_helper' RSpec.describe 'Version' do - subject { command } + subject { command_output } let(:command) do - `#{RSpec.configuration.pulsar_command} #{arguments}` - end - let(:exit_status) do - Pulsar::Executor.sh("#{RSpec.configuration.pulsar_command} #{arguments}")[0].exitstatus + Pulsar::Executor.sh("#{RSpec.configuration.pulsar_command} #{arguments}") end + let(:command_output) { command[:output] } + let(:exit_status) { command[:status] } let(:arguments) { "--version" } context 'via a --version flag' do