Allow override of lexer in Syntax.from_path

This commit is contained in:
Darren Burns 2022-01-27 10:38:55 +00:00
parent e5d2030f0b
commit 9247705bd8
2 changed files with 39 additions and 6 deletions

View file

@ -241,8 +241,13 @@ def test_ansi_theme():
assert theme.get_background_style() == Style()
@pytest.mark.skipif(sys.platform == "win32", reason="permissions error on Windows")
def test_from_file():
skip_windows_permission_error = pytest.mark.skipif(
sys.platform == "win32", reason="permissions error on Windows"
)
@skip_windows_permission_error
def test_from_path():
fh, path = tempfile.mkstemp("example.py")
try:
os.write(fh, b"import this\n")
@ -254,8 +259,8 @@ def test_from_file():
os.remove(path)
@pytest.mark.skipif(sys.platform == "win32", reason="permissions error on Windows")
def test_from_file_unknown_lexer():
@skip_windows_permission_error
def test_from_path_unknown_lexer():
fh, path = tempfile.mkstemp("example.nosuchtype")
try:
os.write(fh, b"import this\n")
@ -266,6 +271,30 @@ def test_from_file_unknown_lexer():
os.remove(path)
@skip_windows_permission_error
def test_from_path_lexer_override():
fh, path = tempfile.mkstemp("example.nosuchtype")
try:
os.write(fh, b"import this\n")
syntax = Syntax.from_path(path, lexer="rust")
assert syntax.lexer.name is "Rust"
assert syntax.code == "import this\n"
finally:
os.remove(path)
@skip_windows_permission_error
def test_from_path_lexer_override_invalid_lexer():
fh, path = tempfile.mkstemp("example.nosuchtype")
try:
os.write(fh, b"import this\n")
syntax = Syntax.from_path(path, lexer="blah")
assert syntax.lexer is None
assert syntax.code == "import this\n"
finally:
os.remove(path)
def test_syntax_guess_lexer():
assert Syntax.guess_lexer("banana.py") == "python"
assert Syntax.guess_lexer("banana.py", "import this") == "python"