Skip to content

Commit c6def3c

Browse files
committed
CLI: Force thor to exit with a correct exit status
Long-time issues on Thor related to exit status code cause msync to return a inappropriate exit code. As example: rails/thor#244
1 parent 53c9a25 commit c6def3c

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

features/cli.feature

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@ Feature: CLI
44
Scenario: When passing no arguments to the msync command
55
When I run `msync`
66
And the output should match /Commands:/
7+
Then the exit status should be 1
78

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

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

16-
Scenario: When running the help subcommand
19+
Scenario: When running the help command
1720
When I run `msync help`
1821
And the output should match /Commands:/
22+
Then the exit status should be 0
1923

2024
Scenario: When overriding a setting from the config file on the command line
2125
Given a puppet module "puppet-test" from "fakenamespace"

lib/modulesync/cli.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
require 'thor'
2+
23
require 'modulesync'
4+
require 'modulesync/cli/thor'
35
require 'modulesync/constants'
46
require 'modulesync/util'
57

68
module ModuleSync
7-
class CLI
9+
module CLI
810
def self.defaults
911
@defaults ||= Util.symbolize_keys(Util.parse_config(Constants::MODULESYNC_CONF_FILE))
1012
end

lib/modulesync/cli/thor.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'thor'
2+
require 'modulesync/cli'
3+
4+
module ModuleSync
5+
module CLI
6+
# Workaround some, still unfixed, Thor behaviors
7+
#
8+
# This class extends ::Thor class to
9+
# - exit with status code sets to `1` on Thor failure (e.g. missing required option)
10+
# - exit with status code sets to `1` when user calls `msync` (or a subcommand) without required arguments
11+
class Thor < ::Thor
12+
desc '_invalid_command_call', 'Invalid command', hide: true
13+
def _invalid_command_call
14+
self.class.new.help
15+
exit 1
16+
end
17+
default_task :_invalid_command_call
18+
19+
def self.exit_on_failure?
20+
true
21+
end
22+
end
23+
end
24+
end

0 commit comments

Comments
 (0)