Skip to content

Commit

Permalink
Avoid writing invalid PEP 723 scripts on tool.uv.sources
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 27, 2024
1 parent cee0d2d commit e1fa0c5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
9 changes: 4 additions & 5 deletions crates/uv-scripts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,12 @@ fn serialize_metadata(metadata: &str) -> String {
output.push('\n');

for line in metadata.lines() {
if line.is_empty() {
output.push('\n');
} else {
output.push_str("# ");
output.push('#');
if !line.is_empty() {
output.push(' ');
output.push_str(line);
output.push('\n');
}
output.push('\n');
}

output.push_str("# ///");
Expand Down
52 changes: 52 additions & 0 deletions crates/uv/tests/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3792,6 +3792,58 @@ fn add_script_without_metadata_table_with_docstring() -> Result<()> {
Ok(())
}

/// Add to a PEP 732 script with a `tool.uv.sources` dependency.
#[test]
fn add_script_sources() -> Result<()> {
let context = TestContext::new("3.12");

let script = context.temp_dir.child("script.py");
script.write_str(indoc! {r#"
import requests
from rich.pretty import pprint
resp = requests.get("https://peps.python.org/api/peps.json")
data = resp.json()
pprint([(k, v["title"]) for k, v in data.items()][:10])
"#})?;

uv_snapshot!(context.filters(), context.add(&["anyio @ git+https://github.com/agronholm/anyio"]).arg("--script").arg("script.py"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Updated `script.py`
"###);

let script_content = fs_err::read_to_string(context.temp_dir.join("script.py"))?;

insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
script_content, @r###"
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "anyio",
# ]
#
# [tool.uv.sources]
# anyio = { git = "https://github.com/agronholm/anyio" }
# ///
import requests
from rich.pretty import pprint
resp = requests.get("https://peps.python.org/api/peps.json")
data = resp.json()
pprint([(k, v["title"]) for k, v in data.items()][:10])
"###
);
});
Ok(())
}

/// Remove from a PEP732 script,
#[test]
fn remove_script() -> Result<()> {
Expand Down

0 comments on commit e1fa0c5

Please sign in to comment.