summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthäus G. Chajdas <dev@anteru.net>2020-03-01 15:21:25 +0100
committerMatthäus G. Chajdas <dev@anteru.net>2020-03-01 15:50:52 +0100
commit11ea92ad40218608a7e3e060b7bf290cb70a3904 (patch)
tree132dca6365f9bbbba382578a1bb2df81a78ca956
parent15b31a090f4e415c6d1bbd8a704cc89b34802449 (diff)
downloadpygments-git-11ea92ad40218608a7e3e060b7bf290cb70a3904.tar.gz
Fix tests on Windows.
This fixes three test failures on Windows: * Two due to incorrect handling of : (on Windows, multiple : can be part of a path.) * One due to newline differences
-rw-r--r--pygments/cmdline.py23
-rw-r--r--tests/test_basic_api.py7
2 files changed, 25 insertions, 5 deletions
diff --git a/pygments/cmdline.py b/pygments/cmdline.py
index d5af5f54..4df35230 100644
--- a/pygments/cmdline.py
+++ b/pygments/cmdline.py
@@ -337,8 +337,17 @@ def main_inner(popts, args, usage):
# custom lexer, located relative to user's cwd
if allow_custom_lexer_formatter and '.py' in lexername:
try:
+ filename = None
+ name = None
if ':' in lexername:
filename, name = lexername.rsplit(':', 1)
+
+ if '.py' in name:
+ # This can happen on Windows: If the lexername is
+ # C:\lexer.py -- return to normal load path in that case
+ name = None
+
+ if filename and name:
lexer = load_lexer_from_file(filename, name,
**parsed_opts)
else:
@@ -427,10 +436,18 @@ def main_inner(popts, args, usage):
# custom formatter, located relative to user's cwd
if allow_custom_lexer_formatter and '.py' in fmter:
try:
+ filename = None
+ name = None
if ':' in fmter:
- file, fmtername = fmter.rsplit(':', 1)
- fmter = load_formatter_from_file(file, fmtername,
- **parsed_opts)
+ # Same logic as above for custom lexer
+ filename, name = fmter.rsplit(':', 1)
+
+ if '.py' in name:
+ name = None
+
+ if filename and name:
+ fmter = load_formatter_from_file(filename, name,
+ **parsed_opts)
else:
fmter = load_formatter_from_file(fmter, **parsed_opts)
except ClassNotFound as err:
diff --git a/tests/test_basic_api.py b/tests/test_basic_api.py
index 7f71791e..81ca3c30 100644
--- a/tests/test_basic_api.py
+++ b/tests/test_basic_api.py
@@ -268,8 +268,11 @@ class TestFilters:
for x, args in filters_args:
lx = lexers.PythonLexer()
lx.add_filter(x, **args)
- with open(TESTFILE, 'rb') as fp:
- text = fp.read().decode('utf-8')
+ # We don't read as binary and decode, but instead read as text, as
+ # we need consistent line endings. Otherwise we'll get \r\n on
+ # Windows
+ with open(TESTFILE, 'r', encoding='utf-8') as fp:
+ text = fp.read()
tokens = list(lx.get_tokens(text))
assert all(isinstance(t[1], str) for t in tokens), \
'%s filter did not return Unicode' % x