Skip to content

Commit 8f9977a

Browse files
zzzeekGerrit Code Review
authored andcommitted
Merge "honor get_templates_path() first" into main
2 parents 8973b31 + a272da9 commit 8f9977a

File tree

3 files changed

+86
-6
lines changed

3 files changed

+86
-6
lines changed

alembic/config.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,10 @@ def get_template_directory(self) -> str:
264264
commands.
265265
266266
"""
267-
return self._get_template_path().as_posix()
267+
import alembic
268+
269+
package_dir = Path(alembic.__file__).absolute().parent
270+
return str(package_dir / "templates")
268271

269272
def _get_template_path(self) -> Path:
270273
"""Return the directory where Alembic setup templates are found.
@@ -275,10 +278,7 @@ def _get_template_path(self) -> Path:
275278
.. versionadded:: 1.16.0
276279
277280
"""
278-
import alembic
279-
280-
package_dir = Path(alembic.__file__).absolute().parent
281-
return package_dir / "templates"
281+
return Path(self.get_template_directory())
282282

283283
@overload
284284
def get_section(

docs/build/unreleased/1660.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.. change::
2+
:tags: bug, command
3+
:tickets: 1660
4+
5+
Fixed regression caused by the ``pathlib`` refactoring that removed the use
6+
of :meth:`.Config.get_template_directory` as the canonical source of
7+
templates; the method is still present however it no longer would be
8+
consulted for a custom config subclass, as was the case with flask-migrate.
9+
10+
.. change::
11+
:tags: bug, command
12+
:tickets: 1659
13+
14+
Fixed regression caused by the ``pathlib`` refactoring where the "missing
15+
template" error message failed to render the name of the template that
16+
could not be found.

tests/test_command.py

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import pathlib
99
import re
10+
import shutil
1011
from typing import cast
1112

1213
from sqlalchemy import exc as sqla_exc
@@ -1145,7 +1146,6 @@ class CommandLineTest(TestBase):
11451146
def setup_class(cls):
11461147
cls.env = staging_env()
11471148
cls.cfg = _sqlite_testing_config()
1148-
cls.a, cls.b, cls.c = three_rev_fixture(cls.cfg)
11491149

11501150
def tearDown(self):
11511151
os.environ.pop("ALEMBIC_CONFIG", None)
@@ -1520,6 +1520,70 @@ def test_init_w_package(self):
15201520
],
15211521
)
15221522

1523+
@testing.fixture
1524+
def custom_template_fixture(self):
1525+
templates_path = pathlib.Path(
1526+
_get_staging_directory(), "my_special_templates_place"
1527+
)
1528+
1529+
os.makedirs(templates_path / "mytemplate")
1530+
1531+
with pathlib.Path(templates_path, "mytemplate", "myfile.txt").open(
1532+
"w"
1533+
) as file_:
1534+
file_.write("This is myfile.txt")
1535+
with pathlib.Path(templates_path, "mytemplate", "README").open(
1536+
"w"
1537+
) as file_:
1538+
file_.write("This is my template")
1539+
with pathlib.Path(
1540+
templates_path, "mytemplate", "alembic.ini.mako"
1541+
).open("w") as file_:
1542+
file_.write("[alembic]\nscript_directory=%(here)s\n")
1543+
1544+
class MyConfig(config.Config):
1545+
def get_template_directory(self) -> str:
1546+
return templates_path.as_posix()
1547+
1548+
yield MyConfig(self.cfg.config_file_name)
1549+
1550+
shutil.rmtree(templates_path)
1551+
1552+
@testing.variation("cmd", ["list_templates", "init"])
1553+
def test_init_custom_template_location(self, cmd, custom_template_fixture):
1554+
"""test #1660"""
1555+
1556+
cfg = custom_template_fixture
1557+
1558+
if cmd.init:
1559+
path = pathlib.Path(_get_staging_directory(), "foobar")
1560+
command.init(cfg, directory=path.as_posix(), template="mytemplate")
1561+
1562+
eq_(
1563+
(path / "myfile.txt").open().read(),
1564+
"This is myfile.txt",
1565+
)
1566+
elif cmd.list_templates:
1567+
cfg.stdout = buf = StringIO()
1568+
command.list_templates(cfg)
1569+
assert buf.getvalue().startswith(
1570+
"Available templates:\n\nmytemplate - This is my template"
1571+
)
1572+
1573+
else:
1574+
cmd.fail()
1575+
1576+
def test_init_no_such_template(self):
1577+
"""test #1659"""
1578+
1579+
path = os.path.join(_get_staging_directory(), "foobar")
1580+
1581+
with expect_raises_message(
1582+
util.CommandError,
1583+
r"No such template .*asfd",
1584+
):
1585+
command.init(self.cfg, directory=path, template="asfd")
1586+
15231587
def test_version_text(self):
15241588
buf = StringIO()
15251589
to_mock = "sys.stdout"

0 commit comments

Comments
 (0)