Skip to content

Commit f75f204

Browse files
committed
support 'append_delimiter' to fix #1679
Fixed rendering of ``pyproject.toml`` to include two newlines when appending content to an existing file. Pull request courtesy Jonathan Vanasco. Co-authored-by: Mike Bayer <[email protected]> Fixes: #1679 Closes: #1680 Pull-request: sqlalchemy/alembic#1680 Pull-request-sha: 20b2aca0d2f6a8f5957063b950e45874343e9a99 Change-Id: Ic2e76a3f791dbda0bd61f09a2c7bafbcdab5eb7f
1 parent 0e3df90 commit f75f204

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

alembic/script/base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,11 @@ def _append_template(self, src: Path, dest: Path, **kw: Any) -> None:
560560
**self.messaging_opts,
561561
):
562562
util.template_to_file(
563-
src, dest, self.output_encoding, append=True, **kw
563+
src,
564+
dest,
565+
self.output_encoding,
566+
append_with_newlines=True,
567+
**kw,
564568
)
565569

566570
def _generate_template(self, src: Path, dest: Path, **kw: Any) -> None:

alembic/util/pyfiles.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def template_to_file(
2626
dest: Union[str, os.PathLike[str]],
2727
output_encoding: str,
2828
*,
29-
append: bool = False,
29+
append_with_newlines: bool = False,
3030
**kw: Any,
3131
) -> None:
3232
template = Template(filename=_preserving_path_as_str(template_file))
@@ -45,7 +45,9 @@ def template_to_file(
4545
"template-oriented traceback." % fname
4646
)
4747
else:
48-
with open(dest, "ab" if append else "wb") as f:
48+
with open(dest, "ab" if append_with_newlines else "wb") as f:
49+
if append_with_newlines:
50+
f.write("\n\n".encode(output_encoding))
4951
f.write(output)
5052

5153

docs/build/unreleased/1679.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.. change::
2+
:tags: bug, command
3+
:tickets: 1679
4+
5+
Fixed rendering of ``pyproject.toml`` to include two newlines when
6+
appending content to an existing file. Pull request courtesy Jonathan
7+
Vanasco.
8+

tests/test_command.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from alembic.testing.fixtures import capture_context_buffer
4444
from alembic.testing.fixtures import capture_engine_context_buffer
4545
from alembic.testing.fixtures import TestBase
46+
from alembic.util import compat
4647
from alembic.util.sqla_compat import _connectable_has_table
4748

4849

@@ -1549,6 +1550,20 @@ def get_template_directory(self) -> str:
15491550

15501551
shutil.rmtree(templates_path)
15511552

1553+
@testing.fixture
1554+
def existing_pyproject_fixture(self):
1555+
root = pathlib.Path(_get_staging_directory())
1556+
1557+
with (root / "pyproject.toml").open("w") as file_:
1558+
file_.write(
1559+
"""[tool.sometool]
1560+
someconfig = 'bar'"""
1561+
)
1562+
yield config.Config(
1563+
self.cfg.config_file_name, toml_file=root / "pyproject.toml"
1564+
)
1565+
shutil.rmtree(root)
1566+
15521567
@testing.variation("cmd", ["list_templates", "init"])
15531568
def test_init_custom_template_location(self, cmd, custom_template_fixture):
15541569
"""test #1660"""
@@ -1573,6 +1588,31 @@ def test_init_custom_template_location(self, cmd, custom_template_fixture):
15731588
else:
15741589
cmd.fail()
15751590

1591+
def test_init_append_pyproject(self, existing_pyproject_fixture):
1592+
cfg = existing_pyproject_fixture
1593+
path = pathlib.Path(_get_staging_directory(), "myproject")
1594+
command.init(cfg, directory=path.as_posix(), template="pyproject")
1595+
with open(cfg.toml_file_name, "r") as f:
1596+
file_content = f.read()
1597+
1598+
assert file_content.startswith(
1599+
"""[tool.sometool]
1600+
someconfig = 'bar'\n\n[tool.alembic]"""
1601+
)
1602+
toml = compat.tomllib.loads(file_content)
1603+
eq_(
1604+
toml,
1605+
{
1606+
"tool": {
1607+
"sometool": {"someconfig": "bar"},
1608+
"alembic": {
1609+
"script_location": "%(here)s/myproject",
1610+
"prepend_sys_path": ["."],
1611+
},
1612+
}
1613+
},
1614+
)
1615+
15761616
def test_init_no_such_template(self):
15771617
"""test #1659"""
15781618

0 commit comments

Comments
 (0)