Skip to content

Commit b1080aa

Browse files
committed
Test pygettext --exclude-file option
1 parent 94f4d87 commit b1080aa

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# SOME DESCRIPTIVE TITLE.
2+
# Copyright (C) YEAR ORGANIZATION
3+
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
4+
#
5+
msgid ""
6+
msgstr ""
7+
"Project-Id-Version: PACKAGE VERSION\n"
8+
"POT-Creation-Date: 2000-01-01 00:00+0000\n"
9+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
10+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
11+
"Language-Team: LANGUAGE <[email protected]>\n"
12+
"MIME-Version: 1.0\n"
13+
"Content-Type: text/plain; charset=UTF-8\n"
14+
"Content-Transfer-Encoding: 8bit\n"
15+
"Generated-By: pygettext.py 1.5\n"
16+
17+
18+
#: excluded.py:4
19+
msgid "foo"
20+
msgstr ""
21+
22+
#: excluded.py:12
23+
msgid ""
24+
"msgids containing newlines \n"
25+
"cannot be currently excluded"
26+
msgstr ""
27+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import gettext as _
2+
3+
4+
_('foo')
5+
6+
_('excluded')
7+
8+
_('also excluded')
9+
10+
_('excluded with trailing whitespace ')
11+
12+
_('msgids containing newlines \n'
13+
'cannot be currently excluded')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
excluded
2+
also excluded
3+
excluded with trailing whitespace

Lib/test/test_tools/test_i18n.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from textwrap import dedent
88
from pathlib import Path
99

10-
from test.support.script_helper import assert_python_ok
10+
from test.support.script_helper import assert_python_failure, assert_python_ok
1111
from test.test_tools import imports_under_tool, skip_if_missing, toolsdir
1212
from test.support.os_helper import temp_cwd, temp_dir
1313

@@ -516,8 +516,19 @@ def test_parse_keyword_spec(self):
516516
parse_spec(spec)
517517
self.assertEqual(str(cm.exception), message)
518518

519+
def test_missing_exclude_file(self):
520+
"""
521+
Test that an error is raised if the exclude file (passed via
522+
--exclude-file) does not exist.
523+
"""
524+
_, _, stderr = assert_python_failure(self.script,
525+
'--exclude-file=foo.txt')
526+
self.assertIn("Can't read --exclude-file: foo.txt",
527+
stderr.decode('utf-8'))
528+
519529

520530
def extract_from_snapshots():
531+
exclude_file = DATA_DIR / 'excluded.txt'
521532
snapshots = {
522533
'messages.py': (),
523534
'fileloc.py': ('--docstrings',),
@@ -526,6 +537,8 @@ def extract_from_snapshots():
526537
'custom_keywords.py': ('--keyword=foo', '--keyword=nfoo:1,2',
527538
'--keyword=pfoo:1c,2',
528539
'--keyword=npfoo:1c,2,3', '--keyword=_:1,2'),
540+
# Test excluded msgids with an exclude file
541+
'excluded.py': (f'--exclude-file={exclude_file}',),
529542
}
530543

531544
for filename, args in snapshots.items():

Tools/i18n/pygettext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ class Options:
743743
if options.excludefilename:
744744
try:
745745
with open(options.excludefilename) as fp:
746-
options.toexclude = fp.readlines()
746+
options.toexclude = fp.read().splitlines()
747747
except IOError:
748748
print(f"Can't read --exclude-file: {options.excludefilename}",
749749
file=sys.stderr)

0 commit comments

Comments
 (0)