Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix uv add comment handling for empty arrays #8504

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions crates/uv-workspace/src/pyproject_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,6 @@ impl PyProjectTomlMut {

// Set the position to the minimum, if it's not already the first element.
if let Some(min) = existing.iter().filter_map(toml_edit::Table::position).min() {
// if !table.position().is_some_and(|position| position < min) {
table.set_position(min);

// Increment the position of all existing elements.
Expand All @@ -375,7 +374,6 @@ impl PyProjectTomlMut {
table.set_position(position + 1);
}
}
// }
}

// Push the item to the table.
Expand Down Expand Up @@ -805,11 +803,16 @@ pub fn add_dependency(
let index = index.unwrap_or(deps.len());

let mut value = Value::from(req_string.as_str());

let decor = value.decor_mut();
decor.set_prefix(deps.trailing().clone());
deps.set_trailing("");

if index == deps.len() {
decor.set_prefix(deps.trailing().clone());
deps.set_trailing("");
}

deps.insert_formatted(index, value);

// `reformat_array_multiline` uses the indentation of the first dependency entry.
// Therefore, we retrieve the indentation of the first dependency entry and apply it to
// the new entry. Note that it is only necessary if the newly added dependency is going
Expand Down Expand Up @@ -992,6 +995,11 @@ fn reformat_array_multiline(deps: &mut Array) {
.and_then(|s| s.lines().last())
.unwrap_or_default();

let decor_prefix = decor_prefix
.split_once('#')
.map(|(s, _)| s)
.unwrap_or(decor_prefix);

// If there is no indentation, use four-space.
indentation_prefix = Some(if decor_prefix.is_empty() {
" ".to_string()
Expand Down Expand Up @@ -1023,7 +1031,16 @@ fn reformat_array_multiline(deps: &mut Array) {
let mut rv = String::new();
if comments.peek().is_some() {
for comment in comments {
rv.push_str("\n ");
match comment.comment_type {
CommentType::OwnLine => {
let indentation_prefix_str =
format!("\n{}", indentation_prefix.as_ref().unwrap());
rv.push_str(&indentation_prefix_str);
}
CommentType::EndOfLine => {
rv.push(' ');
}
}
rv.push_str(&comment.text);
}
}
Expand Down
237 changes: 237 additions & 0 deletions crates/uv/tests/it/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6591,3 +6591,240 @@ fn add_preserves_open_bracket_comment() -> Result<()> {
});
Ok(())
}

#[test]
fn add_preserves_empty_comment() -> 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 = [
# First line.
# Second line.
]

[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
"#})?;

uv_snapshot!(context.filters(), context.add().arg("anyio==3.7.0"), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Resolved 4 packages in [TIME]
Prepared 4 packages in [TIME]
Installed 4 packages in [TIME]
+ anyio==3.7.0
+ idna==3.6
+ project==0.1.0 (from file://[TEMP_DIR]/)
+ sniffio==1.3.1
"###);

let pyproject_toml = context.read("pyproject.toml");

insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
pyproject_toml, @r###"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
# First line.
# Second line.
"anyio==3.7.0",
]

[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
"###
);
});

Ok(())
}

#[test]
fn add_preserves_trailing_comment() -> 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 = [
"idna",
"iniconfig", # Use iniconfig.
# First line.
# Second line.
]

[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
"#})?;

uv_snapshot!(context.filters(), context.add().arg("anyio==3.7.0"), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Resolved 5 packages in [TIME]
Prepared 5 packages in [TIME]
Installed 5 packages in [TIME]
+ anyio==3.7.0
+ idna==3.6
+ iniconfig==2.0.0
+ project==0.1.0 (from file://[TEMP_DIR]/)
+ sniffio==1.3.1
"###);

let pyproject_toml = context.read("pyproject.toml");

insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
pyproject_toml, @r###"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"anyio==3.7.0",
"idna",
"iniconfig", # Use iniconfig.
# First line.
# Second line.
]

[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
"###
);
});

uv_snapshot!(context.filters(), context.add().arg("typing-extensions"), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Resolved 6 packages in [TIME]
Prepared 2 packages in [TIME]
Uninstalled 1 package in [TIME]
Installed 2 packages in [TIME]
~ project==0.1.0 (from file://[TEMP_DIR]/)
+ typing-extensions==4.10.0
"###);

let pyproject_toml = context.read("pyproject.toml");

insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
pyproject_toml, @r###"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"anyio==3.7.0",
"idna",
"iniconfig", # Use iniconfig.
# First line.
# Second line.
"typing-extensions>=4.10.0",
]

[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
"###
);
});

Ok(())
}

#[test]
fn add_preserves_trailing_depth() -> 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 = [
"idna",
"iniconfig",# Use iniconfig.
# First line.
# Second line.
]

[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
"#})?;

uv_snapshot!(context.filters(), context.add().arg("anyio==3.7.0"), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Resolved 5 packages in [TIME]
Prepared 5 packages in [TIME]
Installed 5 packages in [TIME]
+ anyio==3.7.0
+ idna==3.6
+ iniconfig==2.0.0
+ project==0.1.0 (from file://[TEMP_DIR]/)
+ sniffio==1.3.1
"###);

let pyproject_toml = context.read("pyproject.toml");

insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
pyproject_toml, @r###"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"anyio==3.7.0",
"idna",
"iniconfig", # Use iniconfig.
# First line.
# Second line.
]

[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
"###
);
});

Ok(())
}
Loading