Skip to content

Commit

Permalink
Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 11, 2024
1 parent 7ef3dfe commit 079915c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
2 changes: 1 addition & 1 deletion crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2457,7 +2457,7 @@ pub struct AddArgs {
/// a new one will be created and added to the script. When executed via `uv run`,
/// uv will create a temporary environment for the script with all inline
/// dependencies installed.
#[arg(long)]
#[arg(long, conflicts_with = "dev", conflicts_with = "optional")]
pub script: Option<PathBuf>,

/// The Python interpreter to use for resolving and syncing.
Expand Down
14 changes: 7 additions & 7 deletions crates/uv-workspace/src/pyproject_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::pyproject::{DependencyType, Source};
/// preserving comments and other structure, such as `uv add` and `uv remove`.
pub struct PyProjectTomlMut {
doc: DocumentMut,
dependency_target: DependencyTarget,
target: DependencyTarget,
}

#[derive(Error, Debug)]
Expand Down Expand Up @@ -57,10 +57,10 @@ pub enum DependencyTarget {

impl PyProjectTomlMut {
/// Initialize a [`PyProjectTomlMut`] from a [`str`].
pub fn from_toml(raw: &str, dependency_target: DependencyTarget) -> Result<Self, Error> {
pub fn from_toml(raw: &str, target: DependencyTarget) -> Result<Self, Error> {
Ok(Self {
doc: raw.parse().map_err(Box::new)?,
dependency_target,
target,
})
}

Expand Down Expand Up @@ -93,8 +93,8 @@ impl PyProjectTomlMut {
}

/// Retrieves a mutable reference to the root `Table` of the TOML document, creating the `project` table if necessary.
fn doc(&mut self) -> Result<&mut toml_edit::Table, Error> {
let doc = match self.dependency_target {
fn doc(&mut self) -> Result<&mut Table, Error> {
let doc = match self.target {
DependencyTarget::Script => self.doc.as_table_mut(),
DependencyTarget::PyProjectToml => self
.doc
Expand All @@ -107,8 +107,8 @@ impl PyProjectTomlMut {
}

/// Retrieves an optional mutable reference to the `project` `Table`, returning `None` if it doesn't exist.
fn doc_mut(&mut self) -> Result<Option<&mut toml_edit::Table>, Error> {
let doc = match self.dependency_target {
fn doc_mut(&mut self) -> Result<Option<&mut Table>, Error> {
let doc = match self.target {
DependencyTarget::Script => Some(self.doc.as_table_mut()),
DependencyTarget::PyProjectToml => self
.doc
Expand Down
3 changes: 0 additions & 3 deletions crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ pub(crate) async fn add(

let target = if let Some(script) = script {
// If we found a PEP 723 script and the user provided a project-only setting, warn.
if !extras.is_empty() {
warn_user_once!("Extras are not supported for Python scripts with inline metadata");
}
if package.is_some() {
warn_user_once!(
"`--package` is a no-op for Python scripts with inline metadata, which always run in isolation"
Expand Down
56 changes: 55 additions & 1 deletion crates/uv/tests/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3075,7 +3075,61 @@ fn add_script_without_metadata_table_with_shebang() -> Result<()> {
Ok(())
}

/// Remove from a PEP732 script
/// Add to a script without metadata table
#[test]
fn add_script_without_metadata_table_with_docstring() -> Result<()> {
let context = TestContext::new("3.12");

let script = context.temp_dir.child("script.py");
script.write_str(indoc! {r#"
#!/usr/bin/env python3
"""This is a script."""
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(&["rich", "requests<3"]).arg("--script").arg(script.path()), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
warning: `uv add` is experimental and may change without warning
"###);

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###"
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "rich",
# "requests<3",
# ]
# ///
"""This is a script."""
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<()> {
let context = TestContext::new("3.12");
Expand Down

0 comments on commit 079915c

Please sign in to comment.