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

Fail execution when a command fails #99

Merged
merged 3 commits into from
Dec 1, 2022
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
14 changes: 8 additions & 6 deletions cobra_commander/lib/cobra_commander/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ def exec(script_or_components, script = nil)
components_filtered(script && script_or_components),
script || script_or_components
)
CobraCommander::Executor.execute(jobs: jobs, workers: options.concurrency,
output_mode: options.interactive && jobs.count > 1 ? :interactive : :markdown,
output: $stdout, status_output: $stderr)
output_mode = options.interactive && jobs.count > 1 ? :interactive : :markdown
execution = CobraCommander::Executor.execute(jobs: jobs, workers: options.concurrency,
output_mode: output_mode, output: $stdout, status_output: $stderr)
exit 1 unless execution.success?
end

desc "cmd [components] <command>", "Executes the command in the context of a given component or set thereof. " \
Expand All @@ -68,9 +69,10 @@ def cmd(command_or_components, command = nil)
components_filtered(command && command_or_components),
command || command_or_components
)
CobraCommander::Executor.execute(jobs: jobs, workers: options.concurrency,
output_mode: options.interactive && jobs.count > 1 ? :interactive : :markdown,
output: $stdout, status_output: $stderr)
output_mode = options.interactive && jobs.count > 1 ? :interactive : :markdown
execution = CobraCommander::Executor.execute(jobs: jobs, workers: options.concurrency,
output_mode: output_mode, output: $stdout, status_output: $stderr)
exit 1 unless execution.success?
end

desc "tree [component]", "Prints the dependency tree of a given component or umbrella"
Expand Down
8 changes: 8 additions & 0 deletions cobra_commander/lib/cobra_commander/executor/execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@ def initialize(jobs, workers:)
merge! create_futures(jobs)
end

# Wait for all jobs to complete, returns a future with all execution futures
# @return [Concurrent::Promises::Future]
def wait
Concurrent::Promises.zip_futures_on(@executor, *values)
.tap(&:wait)
end

# The execution succeeds when all jobs succeeded
# @return [Boolean]
def success?
values.all?(&:fulfilled?)
end

private

def create_future(job)
Expand Down
3 changes: 1 addition & 2 deletions cobra_commander/lib/cobra_commander/umbrella.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ def resolve(path)
#
# umbrella.load(ruby: true)
#
# @param path [String,Pathname] the path to be resolved
# @return [::CobraCommander::Component,nil] the component where the path is
# @see CobraCommander::Registry
#
def load(**source_selector)
Source.load(path, config["sources"], **source_selector).flatten.each do |package|
Expand Down
9 changes: 9 additions & 0 deletions cobra_commander/spec/cobra_commander/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
it "errors gently if component doesn't exist" do
run_command_and_stop("cobra cmd --no-interactive -a #{umbrella.path} non_existent pwd", fail_on_error: false)

expect(last_command_started).to_not be_successfully_executed
expect(last_command_output).to match(/Component non_existent not found/)
end

Expand All @@ -89,6 +90,14 @@

expect(last_command_output).to include("install memory deps")
end

it "exists with error when a job fails" do
run_command_and_stop("cobra cmd --no-interactive -a #{umbrella.path} --memory " \
"--dependents directory failing_command",
fail_on_error: false)

expect(last_command_started).to_not be_successfully_executed
end
end

describe "cobra graph" do
Expand Down
1 change: 1 addition & 0 deletions cobra_commander/spec/fixtures/app/cobra.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ sources:
deps: "echo 'install stub deps'"
:memory:
commands:
failing_command: "lol"
deps: "echo 'install memory deps'"