uv init normalize directory name (#5292)

## Summary

Resolves #5255
This commit is contained in:
Jo 2024-07-23 00:09:24 +08:00 committed by GitHub
parent b6f470416e
commit c52b767474
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 52 additions and 1 deletions

View file

@ -60,7 +60,7 @@ pub(crate) async fn init(
}
// Create the directory for the project.
let src_dir = path.join("src").join(name.as_ref());
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.

View file

@ -460,3 +460,54 @@ fn init_workspace_outside() -> Result<()> {
Ok(())
}
#[test]
fn init_invalid_names() -> Result<()> {
let context = TestContext::new("3.12");
// `foo-bar` normalized to `foo_bar`.
uv_snapshot!(context.filters(), context.init().current_dir(&context.temp_dir).arg("foo-bar"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
warning: `uv init` is experimental and may change without warning
Initialized project foo-bar in [TEMP_DIR]/foo-bar
"###);
let child = context.temp_dir.child("foo-bar");
let pyproject = fs_err::read_to_string(child.join("pyproject.toml"))?;
let _ = fs_err::read_to_string(child.join("src/foo_bar/__init__.py"))?;
insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
pyproject, @r###"
[project]
name = "foo-bar"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
dependencies = []
[tool.uv]
dev-dependencies = []
"###
);
});
// "bar baz" is not allowed.
uv_snapshot!(context.filters(), context.init().current_dir(&context.temp_dir).arg("bar baz"), @r###"
success: false
exit_code: 2
----- stdout -----
----- stderr -----
warning: `uv init` is experimental and may change without warning
error: Not a valid package or extra name: "bar baz". Names must start and end with a letter or digit and may only contain -, _, ., and alphanumeric characters.
"###);
Ok(())
}