Skip to content

Commit

Permalink
Fix uv init a sub-package by path (#5247)
Browse files Browse the repository at this point in the history
## Summary

Resolves #5242

## Test Plan

See the [CI
failure](https://github.com/astral-sh/uv/actions/runs/10020728203/job/27698630302?pr=5247)
for the new failing test.
  • Loading branch information
j178 authored Jul 20, 2024
1 parent 0611c7b commit 48921f9
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
4 changes: 3 additions & 1 deletion crates/uv/src/commands/project/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::path::PathBuf;

use anyhow::Result;
use owo_colors::OwoColorize;

use pep508_rs::PackageName;
use uv_configuration::PreviewMode;
use uv_fs::Simplified;
Expand Down Expand Up @@ -70,6 +69,9 @@ pub(crate) async fn init(
let src_dir = path.join("src").join(name.as_ref());
fs_err::create_dir_all(&src_dir)?;

// Canonicalize the path to the project.
let path = path.canonicalize()?;

// Create the `pyproject.toml`.
let pyproject = indoc::formatdoc! {r#"
[project]
Expand Down
95 changes: 95 additions & 0 deletions crates/uv/tests/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,98 @@ fn init_workspace() -> Result<()> {

Ok(())
}

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

let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(indoc! {
r#"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["anyio==3.7.0"]
"#,
})?;

let child = context.temp_dir.join("foo");
fs_err::create_dir(&child)?;

uv_snapshot!(context.filters(), context.init().current_dir(&context.temp_dir).arg("foo"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
warning: `uv init` is experimental and may change without warning
Adding foo as member of workspace [TEMP_DIR]/
Initialized project foo in [TEMP_DIR]/foo
"###);

let pyproject = fs_err::read_to_string(child.join("pyproject.toml"))?;
let init_py = fs_err::read_to_string(child.join("src/foo/__init__.py"))?;

let _ = fs_err::read_to_string(child.join("README.md")).unwrap();

insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
pyproject, @r###"
[project]
name = "foo"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
dependencies = []
[tool.uv]
dev-dependencies = []
"###
);
});

insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
init_py, @r###"
def hello() -> str:
return "Hello from foo!"
"###
);
});

let workspace = fs_err::read_to_string(context.temp_dir.join("pyproject.toml"))?;
insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
workspace, @r###"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["anyio==3.7.0"]
[tool.uv.workspace]
members = ["foo"]
"###
);
});

// Run `uv lock` in the workspace.
uv_snapshot!(context.filters(), context.lock(), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
warning: `uv lock` is experimental and may change without warning
Resolved 5 packages in [TIME]
"###);

Ok(())
}

0 comments on commit 48921f9

Please sign in to comment.