Skip to content

fixes #116: Update doesn't respect default value of locale_dirs from -c passed conf.py #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions sphinx_intl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,9 @@ def main(ctx, config, tag):
ctx.locale_dir = None
if ctx.config:
cfg = read_config(ctx.config, tag)
if "locale_dirs" in cfg:
ctx.locale_dir = os.path.join(
os.path.dirname(ctx.config), cfg["locale_dirs"][0]
)
# Use explicit locale_dirs if set, otherwise use Sphinx's default ['locales']
locale_dirs = cfg.get("locale_dirs", ["locales"])
ctx.locale_dir = os.path.join(os.path.dirname(ctx.config), locale_dirs[0])

# for pot_dir
ctx.pot_dir = None
Expand Down
65 changes: 65 additions & 0 deletions tests/test_locale_dirs_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
test_locale_dirs_default
~~~~~~~~~~~~~~~~~~~~~~~~

Test locale_dirs default value handling.

:copyright: Copyright 2025 by Takayuki SHIMIZUKAWA.
:license: BSD, see LICENSE for details.
"""
import os
from unittest import mock
from pathlib import Path

import pytest
from click.testing import CliRunner

from sphinx_intl.commands import main


def test_locale_dirs_explicit_setting(tmp_path: Path):
"""Test that explicit locale_dirs setting is respected in ctx.locale_dir."""
# Arrange
conf_content = """locale_dirs = ['custom_locale']"""
conf_path = tmp_path / 'conf.py'
conf_path.write_text(conf_content)
pot_dir = tmp_path / '_build' / 'gettext'
pot_dir.mkdir(parents=True)
(pot_dir / 'test.pot').write_text('msgid "test"')

# Act
with mock.patch('sphinx_intl.commands.basic.update') as mock_update:
result = CliRunner().invoke(main, ['-c', str(conf_path), 'update', '-p', str(pot_dir), '-l', 'ja'])

# Assert
assert mock_update.called, "basic.update should have been called"
called_locale_dir = mock_update.call_args[0][0]
expected_locale_dir = str(tmp_path / 'custom_locale')
assert called_locale_dir == expected_locale_dir


def test_locale_dirs_default_value(tmp_path: Path):
"""
Test that default locale_dirs value ['locales'] is used when not specified.

This also serves as a regression test for issue #116: locale_dir should be
set relative to conf.py location, not relative to the current working directory.
"""
# Arrange
# No locale_dirs setting - should use default ['locales']
conf_content = """project = 'test'"""
conf_path = tmp_path / 'conf.py'
conf_path.write_text(conf_content)
pot_dir = tmp_path / '_build' / 'gettext'
pot_dir.mkdir(parents=True)
(pot_dir / 'test.pot').write_text('msgid "test"')

# Act
with mock.patch('sphinx_intl.commands.basic.update') as mock_update:
result = CliRunner().invoke(main, ['-c', str(conf_path), 'update', '-p', str(pot_dir), '-l', 'ja'])

# Assert
assert mock_update.called, "basic.update should have been called"
called_locale_dir = mock_update.call_args[0][0]
expected_locale_dir = str(tmp_path / 'locales')
assert called_locale_dir == expected_locale_dir