Skip to content

Commit cf2e6ff

Browse files
committed
Show chdir option as a command
Currently, it is not possible to tell from the output message whether that option is specified for `sh`.
1 parent c68e010 commit cf2e6ff

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

lib/rake/file_utils.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def sh(*cmd, &block)
4848
verbose = options.delete :verbose
4949
noop = options.delete(:noop) || Rake::FileUtilsExt.nowrite_flag
5050

51-
Rake.rake_output_message sh_show_command cmd if verbose
51+
Rake.rake_output_message sh_show_command(cmd, options) if verbose
5252

5353
unless noop
5454
res = (Hash === cmd.last) ? system(*cmd) : system(*cmd, options)
@@ -68,7 +68,7 @@ def create_shell_runner(cmd) # :nodoc:
6868
end
6969
private :create_shell_runner
7070

71-
def sh_show_command(cmd) # :nodoc:
71+
def sh_show_command(cmd, options = nil) # :nodoc:
7272
cmd = cmd.dup
7373

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

80-
cmd.join " "
80+
cmd = cmd.join " "
81+
if options and chdir = options[:chdir]
82+
"(cd #{chdir} && #{cmd})"
83+
else
84+
cmd
85+
end
8186
end
8287
private :sh_show_command
8388

test/test_rake_file_utils.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,20 @@ def test_sh_noop
240240
assert true, "should not fail"
241241
end
242242

243+
def test_sh_chdir
244+
omit "JRuby does not support spawn options" if jruby?
245+
246+
Dir.mkdir "chdir_test"
247+
out, err = capture_output do
248+
verbose(true) {
249+
sh "echo ok", chdir: "chdir_test", verbose: true, noop: true
250+
}
251+
end
252+
253+
assert_equal "(cd chdir_test && echo ok)\n", err
254+
assert_empty out
255+
end
256+
243257
def test_sh_bad_option
244258
# Skip on JRuby because option checking is performed by spawn via system
245259
# now.

0 commit comments

Comments
 (0)