Skip to content

Commit

Permalink
Support relative paths in uv add --script
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Sep 11, 2024
1 parent 3f011f3 commit ba14ce7
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
22 changes: 13 additions & 9 deletions crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,19 @@ pub(crate) async fn add(
Target::Script(_, _) | Target::Project(_, _) if raw_sources => {
(pep508_rs::Requirement::from(requirement), None)
}
Target::Script(ref script, _) => resolve_requirement(
requirement,
false,
editable,
rev.clone(),
tag.clone(),
branch.clone(),
&script.path,
)?,
Target::Script(ref script, _) => {
let script_path = std::path::absolute(&script.path)?;
let script_dir = script_path.parent().expect("script path has no parent");
resolve_requirement(
requirement,
false,
editable,
rev.clone(),
tag.clone(),
branch.clone(),
script_dir,
)?
}
Target::Project(ref project, _) => {
let workspace = project
.workspace()
Expand Down
3 changes: 2 additions & 1 deletion crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ pub(crate) async fn run(
.and_then(|tool| tool.uv.as_ref())
.and_then(|uv| uv.sources.as_ref())
.unwrap_or(&empty);
let script_dir = script.path.parent().expect("script path has no parent");
let script_path = std::path::absolute(script.path)?;
let script_dir = script_path.parent().expect("script path has no parent");

let requirements = dependencies
.into_iter()
Expand Down
54 changes: 54 additions & 0 deletions crates/uv/tests/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3956,6 +3956,60 @@ fn add_script() -> Result<()> {
Ok(())
}

#[test]
fn add_script_relative_path() -> Result<()> {
let context = TestContext::new("3.12");

let project = context.temp_dir.child("project");
project.child("pyproject.toml").write_str(indoc! {r#"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = []
[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
"#})?;

let script = context.temp_dir.child("script.py");
script.write_str(indoc! {r#"
print("Hello, world!")
"#})?;

uv_snapshot!(context.filters(), context.add().arg("./project").arg("--editable").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 = [
# "project",
# ]
#
# [tool.uv.sources]
# project = { path = "project", editable = true }
# ///
print("Hello, world!")
"###
);
});
Ok(())
}

/// Add to a script without an existing metadata table.
#[test]
fn add_script_without_metadata_table() -> Result<()> {
Expand Down

0 comments on commit ba14ce7

Please sign in to comment.