|
| 1 | +""" |
| 2 | + test_locale_dirs_default |
| 3 | + ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 | +
|
| 5 | + Test locale_dirs default value handling. |
| 6 | +
|
| 7 | + :copyright: Copyright 2025 by Takayuki SHIMIZUKAWA. |
| 8 | + :license: BSD, see LICENSE for details. |
| 9 | +""" |
| 10 | +import os |
| 11 | +from unittest import mock |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +import pytest |
| 15 | +from click.testing import CliRunner |
| 16 | + |
| 17 | +from sphinx_intl.commands import main |
| 18 | + |
| 19 | + |
| 20 | +def test_locale_dirs_explicit_setting(tmp_path: Path): |
| 21 | + """Test that explicit locale_dirs setting is respected in ctx.locale_dir.""" |
| 22 | + # Arrange |
| 23 | + conf_content = """locale_dirs = ['custom_locale']""" |
| 24 | + conf_path = tmp_path / 'conf.py' |
| 25 | + conf_path.write_text(conf_content) |
| 26 | + pot_dir = tmp_path / '_build' / 'gettext' |
| 27 | + pot_dir.mkdir(parents=True) |
| 28 | + (pot_dir / 'test.pot').write_text('msgid "test"') |
| 29 | + |
| 30 | + # Act |
| 31 | + with mock.patch('sphinx_intl.commands.basic.update') as mock_update: |
| 32 | + result = CliRunner().invoke(main, ['-c', str(conf_path), 'update', '-p', str(pot_dir), '-l', 'ja']) |
| 33 | + |
| 34 | + # Assert |
| 35 | + assert mock_update.called, "basic.update should have been called" |
| 36 | + called_locale_dir = mock_update.call_args[0][0] |
| 37 | + expected_locale_dir = str(tmp_path / 'custom_locale') |
| 38 | + assert called_locale_dir == expected_locale_dir |
| 39 | + |
| 40 | + |
| 41 | +def test_locale_dirs_default_value(tmp_path: Path): |
| 42 | + """ |
| 43 | + Test that default locale_dirs value ['locales'] is used when not specified. |
| 44 | + |
| 45 | + This also serves as a regression test for issue #116: locale_dir should be |
| 46 | + set relative to conf.py location, not relative to the current working directory. |
| 47 | + """ |
| 48 | + # Arrange |
| 49 | + # No locale_dirs setting - should use default ['locales'] |
| 50 | + conf_content = """project = 'test'""" |
| 51 | + conf_path = tmp_path / 'conf.py' |
| 52 | + conf_path.write_text(conf_content) |
| 53 | + pot_dir = tmp_path / '_build' / 'gettext' |
| 54 | + pot_dir.mkdir(parents=True) |
| 55 | + (pot_dir / 'test.pot').write_text('msgid "test"') |
| 56 | + |
| 57 | + # Act |
| 58 | + with mock.patch('sphinx_intl.commands.basic.update') as mock_update: |
| 59 | + result = CliRunner().invoke(main, ['-c', str(conf_path), 'update', '-p', str(pot_dir), '-l', 'ja']) |
| 60 | + |
| 61 | + # Assert |
| 62 | + assert mock_update.called, "basic.update should have been called" |
| 63 | + called_locale_dir = mock_update.call_args[0][0] |
| 64 | + expected_locale_dir = str(tmp_path / 'locales') |
| 65 | + assert called_locale_dir == expected_locale_dir |
0 commit comments