Skip to content

Commit

Permalink
Show chdir option as a command
Browse files Browse the repository at this point in the history
Currently, it is not possible to tell from the output message whether
that option is specified for `sh`.
  • Loading branch information
nobu committed Mar 29, 2024
1 parent c68e010 commit cf2e6ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/rake/file_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def sh(*cmd, &block)
verbose = options.delete :verbose
noop = options.delete(:noop) || Rake::FileUtilsExt.nowrite_flag

Rake.rake_output_message sh_show_command cmd if verbose
Rake.rake_output_message sh_show_command(cmd, options) if verbose

unless noop
res = (Hash === cmd.last) ? system(*cmd) : system(*cmd, options)
Expand All @@ -68,7 +68,7 @@ def create_shell_runner(cmd) # :nodoc:
end
private :create_shell_runner

def sh_show_command(cmd) # :nodoc:
def sh_show_command(cmd, options = nil) # :nodoc:
cmd = cmd.dup

if Hash === cmd.first
Expand All @@ -77,7 +77,12 @@ def sh_show_command(cmd) # :nodoc:
cmd[0] = env
end

cmd.join " "
cmd = cmd.join " "
if options and chdir = options[:chdir]
"(cd #{chdir} && #{cmd})"
else
cmd
end
end
private :sh_show_command

Expand Down
14 changes: 14 additions & 0 deletions test/test_rake_file_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,20 @@ def test_sh_noop
assert true, "should not fail"
end

def test_sh_chdir
omit "JRuby does not support spawn options" if jruby?

Dir.mkdir "chdir_test"
out, err = capture_output do
verbose(true) {
sh "echo ok", chdir: "chdir_test", verbose: true, noop: true
}
end

assert_equal "(cd chdir_test && echo ok)\n", err
assert_empty out
end

def test_sh_bad_option
# Skip on JRuby because option checking is performed by spawn via system
# now.
Expand Down

0 comments on commit cf2e6ff

Please sign in to comment.