Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jul 22, 2024
1 parent d232bfe commit 99e2b98
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 18 deletions.
36 changes: 18 additions & 18 deletions crates/uv/src/commands/project/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use owo_colors::OwoColorize;

use pep508_rs::PackageName;
use uv_configuration::PreviewMode;
use uv_fs::Simplified;
use uv_fs::{absolutize_path, Simplified};
use uv_warnings::warn_user_once;
use uv_workspace::pyproject_mut::PyProjectTomlMut;
use uv_workspace::{Workspace, WorkspaceError};
Expand Down Expand Up @@ -59,24 +59,8 @@ pub(crate) async fn init(
);
}

// Create the directory for the project.
let src_dir = path.join("src").join(&*name.as_dist_info_name());
fs_err::create_dir_all(&src_dir)?;

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

// Discover the current workspace, if it exists.
let workspace = if isolated {
None
} else {
// Attempt to find a workspace root.
match Workspace::discover(&path, None).await {
Ok(workspace) => Some(workspace),
Err(WorkspaceError::MissingPyprojectToml) => None,
Err(err) => return Err(err.into()),
}
};
let path = absolutize_path(&path)?;

// Create the `pyproject.toml`.
let pyproject = indoc::formatdoc! {r#"
Expand All @@ -92,11 +76,27 @@ pub(crate) async fn init(
readme = if no_readme { "" } else { "\nreadme = \"README.md\"" },
};

fs_err::create_dir_all(&path)?;
fs_err::write(path.join("pyproject.toml"), pyproject)?;

// Discover the current workspace, if it exists.
let workspace = if isolated {
None
} else {
// Attempt to find a workspace root.
let parent = path.parent().expect("Project path has no parent");
match Workspace::discover(parent, None).await {
Ok(workspace) => Some(workspace),
Err(WorkspaceError::MissingPyprojectToml) => None,
Err(err) => return Err(err.into()),
}
};

// Create `src/{name}/__init__.py` if it does not already exist.
let src_dir = path.join("src").join(&*name.as_dist_info_name());
let init_py = src_dir.join("__init__.py");
if !init_py.try_exists()? {
fs_err::create_dir_all(&src_dir)?;
fs_err::write(
init_py,
indoc::formatdoc! {r#"
Expand Down
42 changes: 42 additions & 0 deletions crates/uv/tests/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,3 +724,45 @@ fn init_virtual_workspace() -> Result<()> {

Ok(())
}

/// Run `uv init` from within a workspace. The path is already included via `members`.
#[test]
fn init_matches_member() -> Result<()> {
let context = TestContext::new("3.12");

let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(indoc! {
r"
[tool.uv.workspace]
members = ['packages/*']
",
})?;

let packages = context.temp_dir.join("packages");
fs_err::create_dir_all(&packages)?;

uv_snapshot!(context.filters(), context.init().current_dir(context.temp_dir.join("packages")).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` at `[TEMP_DIR]/packages/foo`
"###);

let workspace = fs_err::read_to_string(context.temp_dir.join("pyproject.toml"))?;
insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
workspace, @r###"
[tool.uv.workspace]
members = ['packages/*', "packages/foo"]
"###
);
});

Ok(())
}

0 comments on commit 99e2b98

Please sign in to comment.