Skip to content
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
28 changes: 22 additions & 6 deletions lib/rake/file_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,25 @@ module FileUtils
def sh(*cmd, &block)
options = (Hash === cmd.last) ? cmd.pop : {}
shell_runner = block_given? ? block : create_shell_runner(cmd)

set_verbose_option(options)
options[:noop] ||= Rake::FileUtilsExt.nowrite_flag
Rake.rake_check_options options, :noop, :verbose
Rake.rake_output_message cmd.join(" ") if options[:verbose]
verbose = options.delete :verbose
noop = options.delete(:noop) || Rake::FileUtilsExt.nowrite_flag

Rake.rake_output_message sh_show_command cmd if verbose

unless options[:noop]
res = system(*cmd)
unless noop
res = system(*cmd, options)
status = $?
status = Rake::PseudoStatus.new(1) if !res && status.nil?
shell_runner.call(res, status)
end
end

def create_shell_runner(cmd) # :nodoc:
show_command = cmd.join(" ")
show_command = sh_show_command cmd
show_command = show_command[0, 42] + "..." unless $trace

lambda do |ok, status|
ok or
fail "Command failed with status (#{status.exitstatus}): " +
Expand All @@ -69,6 +72,19 @@ def create_shell_runner(cmd) # :nodoc:
end
private :create_shell_runner

def sh_show_command(cmd) # :nodoc:
cmd = cmd.dup

if Hash === cmd.first
env = cmd.first
env = env.map { |name, value| "#{name}=#{value}" }.join " "
cmd[0] = env
end

cmd.join " "
end
private :sh_show_command

def set_verbose_option(options) # :nodoc:
unless options.key? :verbose
options[:verbose] =
Expand Down
66 changes: 66 additions & 0 deletions test/test_rake_file_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ def test_sh_with_a_single_string_argument
}
end

def test_sh_with_env
check_environment

env = {
'RAKE_TEST_SH' => 'someval'
}

verbose(false) {
sh env, RUBY, 'check_environment.rb', 'RAKE_TEST_SH', 'someval'
}
end

def test_sh_with_multiple_arguments
skip if jruby9? # https://github.com/jruby/jruby/issues/3653

Expand All @@ -152,6 +164,22 @@ def test_sh_with_multiple_arguments
}
end

def test_sh_with_spawn_options
skip 'JRuby does not support spawn options' if jruby?

echocommand

r, w = IO.pipe

verbose(false) {
sh RUBY, 'echocommand.rb', out: w
}

w.close

assert_equal "echocommand.rb\n", r.read
end

def test_sh_failure
shellcommand

Expand Down Expand Up @@ -187,6 +215,10 @@ def test_sh_noop
end

def test_sh_bad_option
# Skip on JRuby because option checking is performed by spawn via system
# now.
skip 'JRuby does not support spawn options' if jruby?

shellcommand

ex = assert_raises(ArgumentError) {
Expand Down Expand Up @@ -241,6 +273,20 @@ def test_ruby_with_a_single_string_argument
}
end

def test_sh_show_command
env = {
'RAKE_TEST_SH' => 'someval'
}

cmd = [env, RUBY, 'some_file.rb', 'some argument']

show_cmd = send :sh_show_command, cmd

expected_cmd = "RAKE_TEST_SH=someval #{RUBY} some_file.rb some argument"

assert_equal expected_cmd, show_cmd
end

def test_ruby_with_multiple_arguments
skip if jruby9? # https://github.com/jruby/jruby/issues/3653

Expand Down Expand Up @@ -279,6 +325,16 @@ def check_no_expansion
CHECK_EXPANSION
end

def check_environment
command 'check_environment.rb', <<-CHECK_ENVIRONMENT
if ENV[ARGV[0]] != ARGV[1]
exit 1
else
exit 0
end
CHECK_ENVIRONMENT
end

def check_expansion
command 'check_expansion.rb', <<-CHECK_EXPANSION
if ARGV[0] != ARGV[1]
Expand All @@ -289,6 +345,16 @@ def check_expansion
CHECK_EXPANSION
end

def echocommand
command 'echocommand.rb', <<-ECHOCOMMAND
#!/usr/bin/env ruby

puts "echocommand.rb"

exit 0
ECHOCOMMAND
end

def replace_ruby
ruby = FileUtils::RUBY
FileUtils.send :remove_const, :RUBY
Expand Down