Skip to content

Commit

Permalink
CLI: Force thor to exit with a correct exit status
Browse files Browse the repository at this point in the history
Long-time issues on Thor related to exit status code cause msync to return a inappropriate exit code.

As example: rails/thor#244
  • Loading branch information
neomilium committed Dec 22, 2020
1 parent dc88b18 commit 116b8bf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
6 changes: 5 additions & 1 deletion features/cli.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ Feature: CLI
Scenario: When passing no arguments to the msync command
When I run `msync`
And the output should match /Commands:/
Then the exit status should be 1

Scenario: When passing invalid arguments to the msync update command
When I run `msync update`
And the output should match /No value provided for required option/
Then the exit status should be 1

Scenario: When passing invalid arguments to the msync hook command
When I run `msync hook`
And the output should match /Commands:/
Then the exit status should be 1

Scenario: When running the help subcommand
Scenario: When running the help command
When I run `msync help`
And the output should match /Commands:/
Then the exit status should be 0

Scenario: When overriding a setting from the config file on the command line
Given a basic setup with a puppet module "puppet-test" from "fakenamespace"
Expand Down
4 changes: 3 additions & 1 deletion lib/modulesync/cli.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require 'thor'

require 'modulesync'
require 'modulesync/cli/thor'
require 'modulesync/constants'
require 'modulesync/util'

module ModuleSync
class CLI
module CLI
def self.defaults
@defaults ||= Util.symbolize_keys(Util.parse_config(Constants::MODULESYNC_CONF_FILE))
end
Expand Down
24 changes: 24 additions & 0 deletions lib/modulesync/cli/thor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'thor'
require 'modulesync/cli'

module ModuleSync
module CLI
# Workaround some, still unfixed, Thor behaviors
#
# This class extends ::Thor class to
# - exit with status code sets to `1` on Thor failure (e.g. missing required option)
# - exit with status code sets to `1` when user calls `msync` (or a subcommand) without required arguments
class Thor < ::Thor
desc '_invalid_command_call', 'Invalid command', hide: true
def _invalid_command_call
self.class.new.help
exit 1
end
default_task :_invalid_command_call

def self.exit_on_failure?
true
end
end
end
end

0 comments on commit 116b8bf

Please sign in to comment.