Skip to content

Commit

Permalink
Refactor Executor; fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Flavio Auciello committed Apr 28, 2017
1 parent 3b12ed2 commit a2b58ad
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 64 deletions.
23 changes: 15 additions & 8 deletions lib/pulsar/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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

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

Expand All @@ -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
Expand All @@ -99,5 +104,7 @@ def load_option_or_env!(option)

option_value
end

def self.exit_on_failure?; true; end
end
end
17 changes: 9 additions & 8 deletions lib/pulsar/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
39 changes: 19 additions & 20 deletions spec/features/deploy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
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

context 'requires a --conf-repo option' do
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
Expand All @@ -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" }
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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' }
Expand Down
17 changes: 8 additions & 9 deletions spec/features/install_spec.rb
Original file line number Diff line number Diff line change
@@ -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/ }

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

Expand All @@ -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')
Expand Down Expand Up @@ -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) }
Expand Down
27 changes: 13 additions & 14 deletions spec/features/list_spec.rb
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
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

context 'requires a --conf-repo option' do
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" }
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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' }
Expand Down
9 changes: 4 additions & 5 deletions spec/features/version_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit a2b58ad

Please sign in to comment.