Accept directories with space names in `uv init` (#10246)

## Summary

Closes https://github.com/astral-sh/uv/issues/10245.
This commit is contained in:
Charlie Marsh 2024-12-30 21:05:00 -05:00 committed by GitHub
parent dcd96a83aa
commit 7f1ee9c6dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 18 deletions

View File

@ -121,7 +121,10 @@ pub(crate) async fn init(
.and_then(|path| path.to_str()) .and_then(|path| path.to_str())
.context("Missing directory name")?; .context("Missing directory name")?;
PackageName::new(name.to_string())? // Pre-normalize the package name by removing any leading or trailing
// whitespace, and replacing any internal whitespace with hyphens.
let name = name.trim().replace(' ', "-");
PackageName::new(name)?
} }
}; };

View File

@ -1218,7 +1218,7 @@ fn init_workspace_outside() -> Result<()> {
fn init_normalized_names() -> Result<()> { fn init_normalized_names() -> Result<()> {
let context = TestContext::new("3.12"); let context = TestContext::new("3.12");
// `foo-bar` module is normalized to `foo_bar`. // `foo-bar` module is normalized to `foo-bar`.
uv_snapshot!(context.filters(), context.init().current_dir(&context.temp_dir).arg("foo-bar").arg("--lib"), @r###" uv_snapshot!(context.filters(), context.init().current_dir(&context.temp_dir).arg("foo-bar").arg("--lib"), @r###"
success: true success: true
exit_code: 0 exit_code: 0
@ -1252,17 +1252,17 @@ fn init_normalized_names() -> Result<()> {
); );
}); });
// `foo-bar` module is normalized to `foo_bar`. // `bar_baz` module is normalized to `bar-baz`.
uv_snapshot!(context.filters(), context.init().current_dir(&context.temp_dir).arg("foo-bar").arg("--app"), @r###" uv_snapshot!(context.filters(), context.init().current_dir(&context.temp_dir).arg("bar_baz").arg("--app"), @r###"
success: false success: true
exit_code: 2 exit_code: 0
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
error: Project is already initialized in `[TEMP_DIR]/foo-bar` (`pyproject.toml` file exists) Initialized project `bar-baz` at `[TEMP_DIR]/bar_baz`
"###); "###);
let child = context.temp_dir.child("foo-bar"); let child = context.temp_dir.child("bar_baz");
let pyproject = fs_err::read_to_string(child.join("pyproject.toml"))?; let pyproject = fs_err::read_to_string(child.join("pyproject.toml"))?;
insta::with_settings!({ insta::with_settings!({
@ -1271,30 +1271,45 @@ fn init_normalized_names() -> Result<()> {
assert_snapshot!( assert_snapshot!(
pyproject, @r###" pyproject, @r###"
[project] [project]
name = "foo-bar" name = "bar-baz"
version = "0.1.0" version = "0.1.0"
description = "Add your description here" description = "Add your description here"
readme = "README.md" readme = "README.md"
requires-python = ">=3.12" requires-python = ">=3.12"
dependencies = [] dependencies = []
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
"### "###
); );
}); });
// "bar baz" is not allowed. // "baz bop" is normalized to "baz-bop".
uv_snapshot!(context.filters(), context.init().current_dir(&context.temp_dir).arg("bar baz"), @r###" uv_snapshot!(context.filters(), context.init().current_dir(&context.temp_dir).arg("baz bop"), @r###"
success: false success: true
exit_code: 2 exit_code: 0
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
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. Initialized project `baz-bop` at `[TEMP_DIR]/baz bop`
"###); "###);
let child = context.temp_dir.child("baz bop");
let pyproject = fs_err::read_to_string(child.join("pyproject.toml"))?;
insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
pyproject, @r###"
[project]
name = "baz-bop"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = []
"###
);
});
Ok(()) Ok(())
} }