11from pathlib import Path
22
3+ from checkmk_weblate_syncer .html_tags import ForbiddenTag
34from checkmk_weblate_syncer .portable_object import (
5+ format_forbidden_tags_error ,
46 make_soure_string_locations_relative ,
57 remove_header ,
68 remove_last_translator ,
79 remove_source_string_locations ,
10+ source_references_at ,
811)
912
1013
1114def test_remove_header () -> None :
12- assert (
13- remove_header (
14- """# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
15+ body , header_line_count = remove_header (
16+ """# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
1517# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
1618# conditions defined in the file COPYING, which is part of this source code package.
1719
@@ -46,8 +48,10 @@ def test_remove_header() -> None:
4648msgid " (Duration: %s)"
4749msgstr ""
4850""" ,
49- )
50- # pylint: disable=line-too-long
51+ )
52+ # pylint: disable=line-too-long
53+ assert (
54+ body
5155 == """#: /home/weblate/checkmk_weblate_sync/git/checkmk/cmk/gui/wato/pages/host_rename.py:640
5256#, python-format
5357msgid " (%d times)"
@@ -67,6 +71,56 @@ def test_remove_header() -> None:
6771msgstr ""
6872"""
6973 )
74+ expected_header_line_count = 27
75+ assert header_line_count == expected_header_line_count
76+
77+
78+ def test_source_references_at () -> None :
79+ content = """#: cmk/gui/foo.py:10
80+ #: cmk/gui/bar.py:20
81+ msgid "hello"
82+ msgstr "hallo"
83+
84+ #: cmk/gui/baz.py:30
85+ msgid "world"
86+ msgstr "welt"
87+ """
88+ # msgstr "hallo" is on line 4 → both refs from the first block
89+ assert source_references_at (content , 4 ) == (
90+ "cmk/gui/foo.py:10" ,
91+ "cmk/gui/bar.py:20" ,
92+ )
93+ # msgstr "welt" is on line 8 → only the ref from the second block
94+ assert source_references_at (content , 8 ) == ("cmk/gui/baz.py:30" ,)
95+ # out-of-range line returns empty
96+ assert source_references_at (content , 999 ) == ()
97+
98+
99+ def test_format_forbidden_tags_error () -> None :
100+ content = """# header line
101+ # header line
102+
103+ #: cmk/gui/foo.py:10
104+ #: cmk/gui/bar.py:20
105+ msgid "hello"
106+ msgstr "<bad>oops</bad>"
107+ """
108+ # body starts at file line 4 (1-indexed); header_line_count = 3
109+ error = format_forbidden_tags_error (
110+ content ,
111+ header_line_count = 3 ,
112+ tags = frozenset (
113+ {
114+ ForbiddenTag (line = 4 , tag = "<bad>" ),
115+ ForbiddenTag (line = 4 , tag = "</bad>" ),
116+ },
117+ ),
118+ )
119+ assert error == (
120+ "Found forbidden HTML tags:\n "
121+ " line 7: '</bad>' (cmk/gui/foo.py:10, cmk/gui/bar.py:20)\n "
122+ " line 7: '<bad>' (cmk/gui/foo.py:10, cmk/gui/bar.py:20)"
123+ )
70124
71125
72126def test_make_soure_string_locations_relative () -> None :
0 commit comments