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
37 changes: 31 additions & 6 deletions crates/uv-shell/src/shlex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ pub fn shlex_posix(executable: impl AsRef<Path>) -> String {
// Convert to a display path.
let executable = executable.as_ref().portable_display().to_string();

// Like Python's `shlex.quote`:
// > Use single quotes, and put single quotes into double quotes
// > The string $'b is then quoted as '$'"'"'b'
if executable.contains(' ') {
format!("'{}'", escape_posix_for_single_quotes(&executable))
} else {
// Match Python's `shlex.quote` and leave only shell-safe ASCII characters unquoted.
if !executable.is_empty()
&& executable
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || b"@%+=:,./-_".contains(&byte))
{
executable
} else {
format!("'{}'", escape_posix_for_single_quotes(&executable))
}
}

Expand Down Expand Up @@ -48,3 +50,26 @@ pub fn shlex_windows(executable: impl AsRef<Path>, shell: Shell) -> String {
executable
}
}

#[cfg(test)]
mod tests {
use super::shlex_posix;

#[test]
fn posix_safe_path() {
assert_eq!(shlex_posix("/usr/bin/python3.12"), "/usr/bin/python3.12");
}

#[test]
fn posix_empty_path() {
assert_eq!(shlex_posix(""), "''");
}

#[test]
fn posix_path_with_metacharacters() {
assert_eq!(
shlex_posix("Testing's/$venv;activate"),
r#"'Testing'"'"'s/$venv;activate'"#
);
}
}
2 changes: 1 addition & 1 deletion crates/uv/tests/python/venv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1610,7 +1610,7 @@ fn create_venv_apostrophe() {
----- stderr -----
Using CPython 3.12.[X] interpreter at: [PYTHON-3.12]
Creating virtual environment at: Testing's
Activate with: source Testing's/[BIN]/activate
Activate with: source 'Testing'\"'\"'s/[BIN]/activate'
"
);

Expand Down
Loading