|
7 | 7 | import os
|
8 | 8 | import pathlib
|
9 | 9 | import re
|
| 10 | +import shutil |
10 | 11 | from typing import cast
|
11 | 12 |
|
12 | 13 | from sqlalchemy import exc as sqla_exc
|
@@ -1145,7 +1146,6 @@ class CommandLineTest(TestBase):
|
1145 | 1146 | def setup_class(cls):
|
1146 | 1147 | cls.env = staging_env()
|
1147 | 1148 | cls.cfg = _sqlite_testing_config()
|
1148 |
| - cls.a, cls.b, cls.c = three_rev_fixture(cls.cfg) |
1149 | 1149 |
|
1150 | 1150 | def tearDown(self):
|
1151 | 1151 | os.environ.pop("ALEMBIC_CONFIG", None)
|
@@ -1520,6 +1520,70 @@ def test_init_w_package(self):
|
1520 | 1520 | ],
|
1521 | 1521 | )
|
1522 | 1522 |
|
| 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 | + |
1523 | 1587 | def test_version_text(self):
|
1524 | 1588 | buf = StringIO()
|
1525 | 1589 | to_mock = "sys.stdout"
|
|
0 commit comments