Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv committed Oct 2, 2024
1 parent d6ffb67 commit 0780b56
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
31 changes: 24 additions & 7 deletions crates/rattler/src/install/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ fn convert_shebang_to_env(shebang: Cow<'_, str>) -> Cow<'_, str> {
let exe_name = path.rsplit_once('/').map_or(path, |(_, f)| f);
if PYTHON_REGEX.is_match(exe_name) {
Cow::Owned(format!(
"#!/bin/sh\n'''exec' \"{}\" {} \"$0\" \"$@\" #'''",
"#!/bin/sh\n'''exec' \"{}\"{} \"$0\" \"$@\" #'''",
path, &captures[3]
))
} else {
Expand All @@ -587,11 +587,9 @@ fn replace_shebang<'a>(
// If the new shebang would contain a space, return a `#!/usr/bin/env` shebang
assert!(
shebang.starts_with("#!"),
"Shebang does not start with #! ({})",
shebang
"Shebang does not start with #! ({shebang})",
);


if old_new.1.contains(' ') {
// we convert the shebang without spaces to a new shebang, and only then replace
// which is relevant for the Python case
Expand All @@ -606,10 +604,10 @@ fn replace_shebang<'a>(
return shebang;
}

if !is_valid_shebang_length(&shebang, platform) {
convert_shebang_to_env(shebang)
} else {
if is_valid_shebang_length(&shebang, platform) {
shebang
} else {
convert_shebang_to_env(shebang)
}
}

Expand Down Expand Up @@ -860,6 +858,25 @@ mod test {
assert_eq!(replaced, "#!/usr/bin/env exe\\ cutable -o \"te st\" -x");
}

#[test]
fn replace_python_shebang() {
let short_shebang = "#!/path/to/python3.12".into();
let replaced = super::replace_shebang(
short_shebang,
("/path/to", "/new/prefix/with spaces/bin"),
&Platform::Linux64,
);
insta::assert_snapshot!(replaced);

let short_shebang = "#!/path/to/python3.12 -x 123".into();
let replaced = super::replace_shebang(
short_shebang,
("/path/to", "/new/prefix/with spaces/bin"),
&Platform::Linux64,
);
insta::assert_snapshot!(replaced);
}

#[test]
fn test_replace_long_prefix_in_text_file() {
let test_data_dir =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: crates/rattler/src/install/link.rs
expression: replaced
---
#!/bin/sh
'''exec' "/new/prefix/with spaces/bin/python3.12" -x 123 "$0" "$@" #'''
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: crates/rattler/src/install/link.rs
expression: replaced
---
#!/bin/sh
'''exec' "/new/prefix/with spaces/bin/python3.12" "$0" "$@" #'''

0 comments on commit 0780b56

Please sign in to comment.