Skip to content
Open
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
23 changes: 22 additions & 1 deletion crates/shell/src/commands/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,28 @@ async fn execute_time(context: &mut ShellCommandContext) -> Result<(), i32> {
return Err(1);
}

let command_line = context.args.join(" ");
let command_line = context
.args
.iter()
.map(|arg| {
if arg.is_empty() {
"''".to_string()
} else if arg.contains(|c: char| {
c.is_whitespace()
|| matches!(
c,
'\'' | '"' | '\\' | '$' | '(' | ')' | '|' | '&' | ';' | '<' | '>'
| '`' | '!' | '{' | '}' | '[' | ']' | '*' | '?' | '#' | '~'
)
}) {
// Wrap in single quotes, escaping any internal single quotes
format!("'{}'", arg.replace('\'', "'\\''"))
} else {
arg.clone()
}
})
.collect::<Vec<_>>()
.join(" ");

#[cfg(unix)]
let before_usage = get_resource_usage();
Expand Down
28 changes: 28 additions & 0 deletions crates/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2218,6 +2218,34 @@ async fn test_brace_group() {
.await;
}

#[tokio::test]
async fn time_preserves_quoted_args() {
// time should preserve quoted arguments when re-parsing the command
// Note: time's execute uses raw stdout, so we can't assert_stdout here.
// Instead we verify commands succeed (exit code 0) and stderr contains timing info.
TestBuilder::new()
.command(r#"time echo "hello world""#)
.assert_stderr_contains("real")
.assert_exit_code(0)
.run()
.await;

TestBuilder::new()
.command(r#"time echo 'hello world'"#)
.assert_stderr_contains("real")
.assert_exit_code(0)
.run()
.await;

// Arguments with special shell characters should be preserved
TestBuilder::new()
.command(r#"time echo "foo(bar)""#)
.assert_stderr_contains("real")
.assert_exit_code(0)
.run()
.await;
}

#[cfg(test)]
fn no_such_file_error_text() -> &'static str {
if cfg!(windows) {
Expand Down
Loading