|
| 1 | +import io |
| 2 | +import logging |
1 | 3 | import os |
2 | 4 | import pathlib |
3 | 5 | import sys |
@@ -45,6 +47,66 @@ def tearDown(self): |
45 | 47 |
|
46 | 48 |
|
47 | 49 | class ConfigTest(TestBase): |
| 50 | + def test_config_logging_with_file(self): |
| 51 | + buf = io.StringIO() |
| 52 | + handler = logging.StreamHandler(buf) |
| 53 | + handler.setLevel(logging.INFO) |
| 54 | + |
| 55 | + logger = logging.getLogger("alembic.config") |
| 56 | + # logger.x=True |
| 57 | + with ( |
| 58 | + mock.patch.object(logger, "handlers", []), |
| 59 | + mock.patch.object(logger, "level", logging.NOTSET), |
| 60 | + ): |
| 61 | + logger.addHandler(handler) |
| 62 | + logger.setLevel(logging.INFO) |
| 63 | + |
| 64 | + cfg = _write_config_file( |
| 65 | + """ |
| 66 | +[alembic] |
| 67 | +script_location = %(base_path)s/db/migrations |
| 68 | +""" |
| 69 | + ) |
| 70 | + test_cfg = config.Config( |
| 71 | + cfg.config_file_name, config_args=dict(base_path="/tmp") |
| 72 | + ) |
| 73 | + test_cfg.cmd_opts = mock.Mock(verbose=True) |
| 74 | + |
| 75 | + _ = test_cfg.file_config |
| 76 | + |
| 77 | + output = buf.getvalue() |
| 78 | + assert "Loading config from file" in output |
| 79 | + assert cfg.config_file_name.replace("/", os.path.sep) in output |
| 80 | + |
| 81 | + def tearDown(self): |
| 82 | + clear_staging_env() |
| 83 | + |
| 84 | + def test_config_logging_without_file(self): |
| 85 | + buf = io.StringIO() |
| 86 | + handler = logging.StreamHandler(buf) |
| 87 | + handler.setLevel(logging.INFO) |
| 88 | + |
| 89 | + logger = logging.getLogger("alembic.config") |
| 90 | + with ( |
| 91 | + mock.patch.object(logger, "handlers", []), |
| 92 | + mock.patch.object(logger, "level", logging.NOTSET), |
| 93 | + ): |
| 94 | + |
| 95 | + logger.addHandler(handler) |
| 96 | + logger.setLevel(logging.INFO) |
| 97 | + |
| 98 | + test_cfg = config.Config() |
| 99 | + test_cfg.cmd_opts = mock.Mock(verbose=True) |
| 100 | + |
| 101 | + _ = test_cfg.file_config |
| 102 | + |
| 103 | + output = buf.getvalue() |
| 104 | + assert "No config file provided" in output |
| 105 | + assert ( |
| 106 | + test_cfg.config_file_name is None |
| 107 | + and test_cfg._config_file_path is None |
| 108 | + ) |
| 109 | + |
48 | 110 | def test_config_no_file_main_option(self): |
49 | 111 | cfg = config.Config() |
50 | 112 | cfg.set_main_option("url", "postgresql://foo/bar") |
|
0 commit comments