From 580e3f824b7aeafa05bd0850a5833a5bec95ced9 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 27 Aug 2024 13:23:26 -0400 Subject: [PATCH] Avoid writing invalid PEP 723 scripts on tool.uv.sources --- crates/uv-scripts/src/lib.rs | 9 ++++----- crates/uv/tests/edit.rs | 27 ++++++++++++--------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/crates/uv-scripts/src/lib.rs b/crates/uv-scripts/src/lib.rs index 470c452f83b2..089765f11c83 100644 --- a/crates/uv-scripts/src/lib.rs +++ b/crates/uv-scripts/src/lib.rs @@ -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("# ///"); diff --git a/crates/uv/tests/edit.rs b/crates/uv/tests/edit.rs index c8425aa140c2..54d1c659b4ad 100644 --- a/crates/uv/tests/edit.rs +++ b/crates/uv/tests/edit.rs @@ -1,6 +1,7 @@ #![cfg(all(feature = "python", feature = "pypi"))] use anyhow::Result; +use assert_cmd::assert::OutputAssertExt; use assert_fs::prelude::*; use indoc::indoc; use insta::assert_snapshot; @@ -3918,16 +3919,12 @@ fn add_git_to_script() -> Result<()> { # /// script # requires-python = ">=3.11" # dependencies = [ - # "rich", + # "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]) + import anyio + import uv_public_pypackage "#})?; uv_snapshot!(context.filters(), context @@ -3953,23 +3950,23 @@ fn add_git_to_script() -> Result<()> { # /// script # requires-python = ">=3.11" # dependencies = [ - # "rich", + # "anyio", # "uv-public-pypackage", # ] - + # # [tool.uv.sources] # uv-public-pypackage = { git = "https://github.com/astral-test/uv-public-pypackage", tag = "0.0.1" } # /// - 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]) + import anyio + import uv_public_pypackage "### ); }); + + // Ensure that the script runs without error. + context.run().arg("script.py").assert().success(); + Ok(()) }