Skip to content

Commit b5d4c38

Browse files
rchen152meta-codesync[bot]
authored andcommitted
Avoid adding unnecessary newlines to pyproject.toml
Summary: I noticed while playing around with `pyrefly init` that we add an unnecessary newline at the beginning of an empty pyproject.toml. Not a big deal, but it's trivial to fix, so why not. Reviewed By: kinto0 Differential Revision: D89902100 fbshipit-source-id: dbea621557d5807e0decc67447c728213423b51e
1 parent 24a1b97 commit b5d4c38

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

crates/pyrefly_config/src/pyproject.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ impl PyProject {
7474
.max()
7575
.unwrap_or(0);
7676
tool_table_mut.insert("pyrefly", pyrefly_table.clone());
77-
if let Some(pyrefly_item) = tool_table_mut.get_mut("pyrefly")
77+
if !original_content.is_empty()
78+
&& let Some(pyrefly_item) = tool_table_mut.get_mut("pyrefly")
7879
&& let Some(pyrefly_table_mut) = pyrefly_item.as_table_mut()
7980
{
8081
pyrefly_table_mut.decor_mut().set_prefix("\n");
@@ -133,7 +134,8 @@ line-length = 88
133134
assert!(updated_content.contains("[tool.poetry]"));
134135
assert!(updated_content.contains("[tool.black]"));
135136

136-
assert!(updated_content.contains("[tool.pyrefly]"));
137+
// Make sure we add a blank line between the pyrefly section and the previous one
138+
assert!(updated_content.contains("\n\n[tool.pyrefly]"));
137139
assert!(updated_content.contains("project-includes = [\"new/path/**/*.py\"]"));
138140
assert!(!updated_content.contains("project_includes = [\"old/path/**/*.py\"]"));
139141
assert!(!updated_content.contains("project_excludes"));
@@ -146,7 +148,7 @@ line-length = 88
146148
let tmp = tempfile::tempdir()?;
147149
let pyproject_path = tmp.path().join("pyproject.toml");
148150

149-
let existing_content = "\n";
151+
let existing_content = "";
150152
fs_anyhow::write(&pyproject_path, existing_content)?;
151153

152154
let config = ConfigFile {
@@ -163,6 +165,9 @@ line-length = 88
163165
// Regression test for bug where we would insert an unnecessary [tool] section
164166
assert!(!updated_content.contains("[tool]"));
165167

168+
// Make sure we don't add an extra blank line
169+
assert!(!updated_content.starts_with("\n"));
170+
166171
Ok(())
167172
}
168173

0 commit comments

Comments
 (0)