Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
[CLI] Only redirect to help whel first two args are help and exec
Browse files Browse the repository at this point in the history
  • Loading branch information
segiddins committed May 23, 2016
1 parent 7351edd commit 53845c5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 107 deletions.
31 changes: 8 additions & 23 deletions lib/bundler/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -438,35 +438,20 @@ def env
def self.reformatted_help_args(args)
bundler_commands = all_commands.keys
help_flags = %w(--help -h)
exec_commands = %w(exec e)
help_used = args.select {|a| help_flags.include? a }
exec_used = args.select {|a| exec_commands.include? a }
command = args.select {|a| bundler_commands.include? a }
if exec_used.any? && help_used.any?
regex = /
( # Matches `exec --help` or `exec --help foo`
(#{exec_commands.join("|")})
\s
(#{help_flags.join("|")})
(.+)*
)|
( # Matches `--help exec` or `--help exec foo`
(#{help_flags.join("|")})
\s
(#{exec_commands.join("|")})
(.+)*
)
/x
arg_str = args.join(" ")
if arg_str =~ regex
exec_commands = %w(e ex exe exec)
help_used = args.index {|a| help_flags.include? a }
exec_used = args.index {|a| exec_commands.include? a }
command = args.find {|a| bundler_commands.include? a }
if exec_used && help_used
if exec_used + help_used == 1
%w(help exec)
else
args
end
elsif command.empty?
elsif command.nil?
abort("Could not find command \"#{args.join(" ")}\".")
else
["help", command.first]
["help", command || args].flatten.compact
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/bundler/friendly_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def request_issue_report_for(e)
Error details
#{e.class}: #{e.message}
#{e.backtrace.join("\n ")}
#{e.backtrace && e.backtrace.join("\n ")}
#{Bundler::Env.new.report(:print_gemfile => false, :print_gemspecs => false).gsub(/\n/, "\n ").strip}
--- TEMPLATE END ----------------------------------------------------------------
Expand Down
144 changes: 62 additions & 82 deletions spec/commands/exec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,109 +219,89 @@
end

describe "with help flags" do
describe "when exec is used" do
before(:each) do
install_gemfile <<-G
gem "rack"
G
end
each_prefix = proc do |string, &blk|
1.upto(string.length) {|l| blk.call(string[0, l]) }
end
each_prefix.call("exec") do |exec|
describe "when #{exec} is used" do
before(:each) do
install_gemfile <<-G
gem "rack"
G

create_file("print_args", <<-'RUBY')
#!/usr/bin/env ruby
puts "args: #{ARGV.inspect}"
RUBY
bundled_app("print_args").chmod(0755)
end

it "shows executable's man page when --help is after the executable" do
bundle "exec cat --help"
expect(out).to include("Usage: cat [OPTION]... [FILE]...")
end
it "shows executable's man page when --help is after the executable" do
bundle "#{exec} print_args --help"
expect(out).to eq('args: ["--help"]')
end

it "uses executable's original behavior for -h" do
bundle "exec cat -h"
expect(err).to include("cat: invalid option -- 'h'")
end
it "shows executable's man page when --help is after the executable and an argument" do
bundle "#{exec} print_args foo --help"
expect(out).to eq('args: ["foo", "--help"]')

it "shows bundle-exec's man page when --help is between exec and the executable" do
with_fake_man do
bundle "exec --help cat"
end
expect(out).to include(%(["#{root}/lib/bundler/man/bundle-exec"]))
end
bundle "#{exec} print_args foo bar --help"
expect(out).to eq('args: ["foo", "bar", "--help"]')

it "shows bundle-exec's man page when --help is before exec" do
with_fake_man do
bundle "--help exec"
bundle "#{exec} print_args foo --help bar"
expect(out).to eq('args: ["foo", "--help", "bar"]')
end
expect(out).to include(%(["#{root}/lib/bundler/man/bundle-exec"]))
end

it "shows bundle-exec's man page when -h is before exec" do
with_fake_man do
bundle "-h exec"
it "shows executable's man page when the executable has a -" do
FileUtils.mv(bundled_app("print_args"), bundled_app("docker-template"))
bundle "#{exec} docker-template build discourse --help"
expect(out).to eq('args: ["build", "discourse", "--help"]')
end
expect(out).to include(%(["#{root}/lib/bundler/man/bundle-exec"]))
end

it "shows bundle-exec's man page when --help is after exec" do
with_fake_man do
bundle "exec --help"
it "shows executable's man page when --help is after another flag" do
bundle "#{exec} print_args --bar --help"
expect(out).to eq('args: ["--bar", "--help"]')
end
expect(out).to include(%(["#{root}/lib/bundler/man/bundle-exec"]))
end

it "shows bundle-exec's man page when -h is after exec" do
with_fake_man do
bundle "exec -h"
it "uses executable's original behavior for -h" do
bundle "#{exec} print_args -h"
expect(out).to eq('args: ["-h"]')
end
expect(out).to include(%(["#{root}/lib/bundler/man/bundle-exec"]))
end
end

describe "when e is used" do
before(:each) do
install_gemfile <<-G
gem "rack"
G
end

it "shows executable's man page when --help is after the executable" do
bundle "e cat --help"
expect(out).to include("Usage: cat [OPTION]... [FILE]...")
end

it "uses executable's original behavior for -h" do
bundle "e cat -h"
expect(err).to include("cat: invalid option -- 'h'")
end

it "shows bundle-exec's man page when --help is between exec and the executable" do
with_fake_man do
bundle "e --help cat"
it "shows bundle-exec's man page when --help is between exec and the executable" do
with_fake_man do
bundle "#{exec} --help cat"
end
expect(out).to include(%(["#{root}/lib/bundler/man/bundle-exec"]))
end
expect(out).to include(%(["#{root}/lib/bundler/man/bundle-exec"]))
end

it "shows bundle-exec's man page when --help is before exec" do
with_fake_man do
bundle "--help e"
it "shows bundle-exec's man page when --help is before exec" do
with_fake_man do
bundle "--help #{exec}"
end
expect(out).to include(%(["#{root}/lib/bundler/man/bundle-exec"]))
end
expect(out).to include(%(["#{root}/lib/bundler/man/bundle-exec"]))
end

it "shows bundle-exec's man page when -h is before exec" do
with_fake_man do
bundle "-h e"
it "shows bundle-exec's man page when -h is before exec" do
with_fake_man do
bundle "-h #{exec}"
end
expect(out).to include(%(["#{root}/lib/bundler/man/bundle-exec"]))
end
expect(out).to include(%(["#{root}/lib/bundler/man/bundle-exec"]))
end

it "shows bundle-exec's man page when --help is after exec" do
with_fake_man do
bundle "e --help"
it "shows bundle-exec's man page when --help is after exec" do
with_fake_man do
bundle "#{exec} --help"
end
expect(out).to include(%(["#{root}/lib/bundler/man/bundle-exec"]))
end
expect(out).to include(%(["#{root}/lib/bundler/man/bundle-exec"]))
end

it "shows bundle-exec's man page when -h is after exec" do
with_fake_man do
bundle "e -h"
it "shows bundle-exec's man page when -h is after exec" do
with_fake_man do
bundle "#{exec} -h"
end
expect(out).to include(%(["#{root}/lib/bundler/man/bundle-exec"]))
end
expect(out).to include(%(["#{root}/lib/bundler/man/bundle-exec"]))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def bundle(cmd, options = {})
with_sudo = options.delete(:sudo)
sudo = with_sudo == :preserve_env ? "sudo -E" : "sudo" if with_sudo

options["no-color"] = true unless options.key?("no-color") || %w(exec conf).include?(cmd.to_s[0..3])
options["no-color"] = true unless options.key?("no-color") || cmd.to_s.start_with?("exec", "exe", "ex", "e", "conf")

bundle_bin = File.expand_path("../../../exe/bundle", __FILE__)

Expand Down

0 comments on commit 53845c5

Please sign in to comment.