Skip to content

Commit

Permalink
Update tests, CI/CD workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
KJhellico committed Jan 5, 2025
1 parent 512ac28 commit 59b6b06
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 46 deletions.
2 changes: 0 additions & 2 deletions .flake8

This file was deleted.

22 changes: 18 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,34 @@ on:
- push

jobs:
pre-commit:
name: Run pre-commit
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
check-latest: true
python-version: '3.13'

- name: Run pre-commit
uses: pre-commit/[email protected]

test:
name: Test Python ${{ matrix.python-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
needs:
- pre-commit
strategy:
matrix:
os:
- macos-latest
- ubuntu-latest
- windows-latest
python-version:
- '3.8'
- '3.9'
- '3.10'
- '3.11'
Expand All @@ -32,9 +49,6 @@ jobs:
check-latest: true
python-version: ${{ matrix.python-version }}

- name: flake8
uses: py-actions/flake8@v2

- name: Install
run: |
python -m pip install .[chameleon,dev]
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v5.0.0
hooks:
- id: check-ast
- id: check-builtin-literals
Expand All @@ -15,7 +15,7 @@ repos:
- id: trailing-whitespace

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.7
rev: v0.8.6
hooks:
- id: ruff
- id: ruff-format
Expand Down
8 changes: 3 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ dependencies = [

[project.optional-dependencies]
dev = [
"black >= 22.1.0",
"flake8 >= 4.0.1",
"pytest >=7.0.1",
"pytest >= 8.0.0",
"ruff >= 0.8.0"
]
chameleon = [
"chameleon"
Expand Down Expand Up @@ -65,10 +64,9 @@ profile = "black"

[tool.ruff]
line-length = 99
target-version = "py311"

[tool.ruff.lint]
select = ["E4", "E7", "E9", "F"]
select = ["E4", "E5", "E7", "E9", "F", "W"]

[tool.ruff.lint.flake8-errmsg]
max-string-length = 99
4 changes: 2 additions & 2 deletions src/lingva/extractors/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ def visit(self, kind, args):
return visitor(*args)
else:
print(
"Warning: Unknown node type '%s' in %s, linenumbers might be off. Please report this warning."
% (kind, self.filename),
"Warning: Unknown node type '%s' in %s, linenumbers might be off. "
"Please report this warning." % (kind, self.filename),
file=sys.stderr,
)

Expand Down
70 changes: 39 additions & 31 deletions tests/extractors/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,12 +539,14 @@ def test_nested_braces(self):
@pytest.mark.usefixtures("fake_source")
def test_python_expression_in_tales_expressions():
global source
source = """
source = b"""\
<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="lingva">
<dummy tal:define="css_class css_class|string:${field.widget.css_class};">Dummy</dummy>
<dummy tal:define="css_class css_class|string:${field.widget.css_class};">
Dummy
</dummy>
</html>
""".encode("utf-8")
"""
assert list(xml_extractor("filename", _options())) == []


Expand Down Expand Up @@ -619,38 +621,38 @@ def test_multiline_replace_with_structure():
@pytest.mark.usefixtures("fake_source")
def test_spaces_around_tal_pipe_symbol():
global source
source = """\
source = b"""\
<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="lingva">
<div tal:repeat="choice values | field.widget.values"/>
</html>
""".encode("utf-8")
"""
list(xml_extractor("filename", _options()))


@pytest.mark.usefixtures("fake_source")
def test_empty_element():
global source
source = """\
source = b"""\
<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="lingva">
<img title="message" i18n:attributes="title">
<p></p>
</html>
""".encode("utf-8")
"""
messages = list(xml_extractor("filename", _options()))
assert len(messages) == 1


@pytest.mark.usefixtures("fake_source")
def test_brace_in_python_expression():
global source
source = """\
source = b"""\
<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="lingva">
<p>${some_method(_('abc'), {'a':'b'})}</p>
</html>
""".encode("utf-8")
"""
messages = list(xml_extractor("filename", _options()))
assert len(messages) == 1
assert messages[0].msgid == "abc"
Expand All @@ -659,11 +661,11 @@ def test_brace_in_python_expression():
@pytest.mark.usefixtures("fake_source")
def test_translation_context():
global source
source = """<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
source = b"""<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="lingva">
<button i18n:context="form" i18n:translate="">Save</button>
</html>
""".encode("utf-8")
"""
messages = list(xml_extractor("filename", _options()))
assert messages[0].msgid == "Save"
assert messages[0].msgctxt == "form"
Expand All @@ -672,11 +674,11 @@ def test_translation_context():
@pytest.mark.usefixtures("fake_source")
def test_translation_comment():
global source
source = """<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
source = b"""<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="lingva">
<button i18n:comment="Generic save button" i18n:translate="">Save</button>
</html>
""".encode("utf-8")
"""
messages = list(xml_extractor("filename", _options()))
assert messages[0].msgid == "Save"
assert messages[0].comment == "Generic save button"
Expand All @@ -685,11 +687,13 @@ def test_translation_comment():
@pytest.mark.usefixtures("fake_source")
def test_translation_comment_for_attribute():
global source
source = """<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
source = b"""<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="lingva">
<input i18n:comment="Placeholder text" i18n:attributes="placeholder" placeholder="Email address">
<input i18n:comment="Placeholder text"
i18n:attributes="placeholder"
placeholder="Email address">
</html>
""".encode("utf-8")
"""
messages = list(xml_extractor("filename", _options()))
assert messages[0].msgid == "Email address"
assert messages[0].comment == "Placeholder text"
Expand All @@ -698,11 +702,14 @@ def test_translation_comment_for_attribute():
@pytest.mark.usefixtures("fake_source")
def test_translation_comment_and_msgid():
global source
source = """<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
source = b"""<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="lingva">
<button i18n:comment="Generic save button" i18n:translate="btn_save">Save</button>
<button i18n:comment="Generic save button"
i18n:translate="btn_save">
Save
</button>
</html>
""".encode("utf-8")
"""
messages = list(xml_extractor("filename", _options()))
assert messages[0].msgid == "btn_save"
assert messages[0].comment == "Generic save button\nDefault: Save"
Expand All @@ -711,13 +718,13 @@ def test_translation_comment_and_msgid():
@pytest.mark.usefixtures("fake_source")
def test_inherit_translation_comment():
global source
source = """<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
source = b"""<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="lingva">
<div i18n:comment="Form buttons">
<button i18n:translate="">Save</button>
</div>
</html>
""".encode("utf-8")
"""
messages = list(xml_extractor("filename", _options()))
assert messages[0].msgid == "Save"
assert messages[0].comment == "Form buttons"
Expand All @@ -726,7 +733,7 @@ def test_inherit_translation_comment():
@pytest.mark.usefixtures("fake_source")
def test_linenumbers():
global source
source = """<!DOCTYPE html>
source = b"""<!DOCTYPE html>
<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="lingva">
<span />
Expand All @@ -748,7 +755,8 @@ def test_linenumbers():
<div foo="bar"
class="blubb">
<dummy i18n:comment="Generic
save button"><span tal:omit-tag="" i18n:translate="dummy3">Foo</span></dummy>
save button">
<span tal:omit-tag="" i18n:translate="dummy3">Foo</span></dummy>
</div>
<script>
// <![CDATA[
Expand All @@ -775,10 +783,10 @@ def test_linenumbers():
</dummy>
</metal:action>
</tal:macros>
""".encode("utf-8")
"""
messages = list(xml_extractor("filename", _options()))
got = [(x.msgid, x.location[1]) for x in messages]
assert got == [("dummy1", 9), ("dummy2", 14), ("dummy3", 23), ("dummy4", 46)]
assert got == [("dummy1", 9), ("dummy2", 14), ("dummy3", 24), ("dummy4", 47)]


@pytest.mark.usefixtures("fake_source")
Expand All @@ -796,36 +804,36 @@ def test_domain_filter():
@pytest.mark.usefixtures("fake_source")
def test_domain_filter_for_attribute():
global source
source = """\
source = b"""\
<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="lingva">
<dummy title="Dummy text" i18n:attributes="title"></dummy>
</html>""".encode("utf-8")
</html>"""
messages = list(xml_extractor("filename", _options(domain="other")))
assert len(messages) == 0


@pytest.mark.usefixtures("fake_source")
def test_domain_filter_for_expression():
global source
source = """\
source = b"""\
<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="lingva">
<dummy>${_(u'foo')}</dummy>
</html>""".encode("utf-8")
</html>"""
messages = list(xml_extractor("filename", _options(domain="other")))
assert len(messages) == 0


@pytest.mark.usefixtures("fake_source")
def test_context_for_attributes():
global source
source = """\
source = b"""\
<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="lingva">
<span title="message" i18n:context="figure" i18n:attributes="title"></span>
</html>
""".encode("utf-8")
"""
messages = list(xml_extractor("filename", _options()))
assert len(messages) == 1
assert messages[0].msgctxt == "figure"

0 comments on commit 59b6b06

Please sign in to comment.