Skip to content

Commit

Permalink
Avoid clobbering existing py.typed files contents in uv init (#7338)
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb authored Sep 12, 2024
1 parent adcb67a commit dcde459
Show file tree
Hide file tree
Showing 2 changed files with 37 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 @@ -552,7 +552,9 @@ impl InitProjectKind {

// Create a `py.typed` file
let py_typed = src_dir.join("py.typed");
fs_err::write(py_typed, "")?;
if !py_typed.try_exists()? {
fs_err::write(py_typed, "")?;
}

// Write .python-version if it doesn't exist.
if let Some(python_request) = python_request {
Expand Down
34 changes: 34 additions & 0 deletions crates/uv/tests/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,40 @@ fn init_library() -> Result<()> {
Ok(())
}

/// Run `uv init --lib` with an existing py.typed file
#[test]
fn init_py_typed_exists() -> Result<()> {
let context = TestContext::new("3.12");

let child = context.temp_dir.child("foo");
child.create_dir_all()?;

let foo = child.child("src").child("foo");
foo.create_dir_all()?;

let py_typed = foo.join("py.typed");
fs_err::write(&py_typed, "partial")?;

uv_snapshot!(context.filters(), context.init().current_dir(&child).arg("--lib"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Initialized project `foo`
"###);

let py_typed = fs_err::read_to_string(py_typed)?;
insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
py_typed, @"partial"
);
});
Ok(())
}

/// Using `uv init --lib --no-package` isn't allowed
#[test]
fn init_library_no_package() -> Result<()> {
Expand Down

0 comments on commit dcde459

Please sign in to comment.