Permalink
Browse files

Move fpm version query behavior to `run` method.

A bug pointed out by #1162 showed that `fpm --version` would fail when
a .fpm file was loaded.

Moving the short `fpm --version` handling to the `run` method lets us do
this check before we load FPMOPTS env or .fpm files.

Updated the tests to only try calling with a lone `-v` or `--version`
flag to verify this works with and without a .fpm file.

Fixes #1162 and #1125. Original work by @drwl!
  • Loading branch information...
1 parent a6f25b9 commit 40aa7cf5caea176b720a89192ccde8a5500cb83d @jordansissel committed Sep 12, 2016
Showing with 29 additions and 20 deletions.
  1. +9 −9 lib/fpm/command.rb
  2. +20 −11 spec/fpm/command_spec.rb
View
@@ -256,20 +256,13 @@ def initialize(*args)
# Execute this command. See Clamp::Command#execute and Clamp's documentation
def execute
- # Short-circuit if someone simply runs `fpm --version`
- unless (ARGV & ["-v", "--version"]).empty?
- puts FPM::VERSION
- return 0
- end
-
logger.level = :warn
logger.level = :info if verbose? # --verbose
logger.level = :debug if debug? # --debug
if log_level
logger.level = log_level.to_sym
end
-
if (stray_flags = args.grep(/^-/); stray_flags.any?)
logger.warn("All flags should be before the first argument " \
"(stray flags found: #{stray_flags}")
@@ -514,9 +507,16 @@ def execute
end
end # def execute
- def run(*run_args)
+ def run(run_args)
logger.subscribe(STDOUT)
+ # Short circuit for a `fpm --version` or `fpm -v` short invocation that
+ # is the user asking us for the version of fpm.
+ if run_args == [ "-v" ] || run_args == [ "--version" ]
+ puts FPM::VERSION
+ return 0
+ end
+
# fpm initialization files, note the order of the following array is
# important, try .fpm in users home directory first and then the current
# directory
@@ -557,7 +557,7 @@ def run(*run_args)
ARGV.unshift(*flags)
ARGV.push(*args)
- super(*run_args)
+ super(run_args)
rescue FPM::Package::InvalidArgument => e
logger.error("Invalid package argument: #{e}")
return 1
@@ -26,31 +26,40 @@
describe "-a | --architecture"
describe "-v | --version" do
+ subject { FPM::Command.new("fpm") }
+
+ # Have output from `fpm` cli be nulled.
+ let(:null) { File.open(File::NULL, "w") }
+ let!(:stdout) { $stdout }
+
+ before do
+ $stdout = null
+ end
+
+ after do
+ $stdout = stdout
+ end
+
context "when no rc file is present" do
it "should not fail" do
- Stud::Temporary.directory do |path|
- cmd = FPM::Command.new("fpm")
+ stub_const('ARGV', ["--version"])
+ insist { subject.run(["--version"]) } == 0
- stub_const('ARGV', ["--version"])
- insist { cmd.run(["--version", path]) } == 0
-
- stub_const('ARGV', ["-v"])
- insist { cmd.run(["-v", path]) } == 0
- end
+ stub_const('ARGV', ["-v"])
+ insist { subject.run(["-v"]) } == 0
end
end
context "when rc file is present" do
it "should not fail" do
Stud::Temporary.directory do |path|
- cmd = FPM::Command.new("fpm")
File.open(File.join(path, ".fpm"), "w") { |file| file.puts("-- --rpm-sign") }
stub_const('ARGV', [ "--version" ])
- insist { cmd.run(["--version", path]) } == 0
+ insist { subject.run(["--version"]) } == 0
stub_const('ARGV', [ "-v" ])
- insist { cmd.run(["-v", path]) } == 0
+ insist { subject.run(["-v"]) } == 0
end
end
end

0 comments on commit 40aa7cf

Please sign in to comment.