Skip to content

Commit 2cc10e0

Browse files
authored
fixes #116: Update doesn't respect default value of locale_dirs from -c passed conf.py (#122)
* fixes #116: Update doesn't respect default value of locale_dirs from `-c` passed `conf.py` * lint fix
1 parent 6d36662 commit 2cc10e0

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

sphinx_intl/commands.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,9 @@ def main(ctx, config, tag):
233233
ctx.locale_dir = None
234234
if ctx.config:
235235
cfg = read_config(ctx.config, tag)
236-
if "locale_dirs" in cfg:
237-
ctx.locale_dir = os.path.join(
238-
os.path.dirname(ctx.config), cfg["locale_dirs"][0]
239-
)
236+
# Use explicit locale_dirs if set, otherwise use Sphinx's default ['locales']
237+
locale_dirs = cfg.get("locale_dirs", ["locales"])
238+
ctx.locale_dir = os.path.join(os.path.dirname(ctx.config), locale_dirs[0])
240239

241240
# for pot_dir
242241
ctx.pot_dir = None

tests/test_locale_dirs_default.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)