11from pathlib import Path
22
3+ import pytest
4+
35from semmle .python .parser import yeast_parser
46from semmle .python .parser .tokenizer import encoding_from_source
57
@@ -9,8 +11,9 @@ def debug(self, *_args, **_kwargs):
911 pass
1012
1113
12- def test_non_utf8_source_is_passed_as_utf8 (mocker ):
13- path = Path (__file__ ).parent / "tokenizer" / "latin.py"
14+ def test_non_utf8_source_is_passed_as_utf8 (mocker , monkeypatch ):
15+ monkeypatch .delenv ("CODEQL_EXTRACTOR_PYTHON_ROOT" , raising = False )
16+ path = (Path (__file__ ).parent / "tokenizer" / "latin.py" ).resolve ()
1417 encoding , source = encoding_from_source (path .read_bytes ())
1518 source = source .decode (encoding )
1619 process = mocker .Mock (returncode = 0 )
@@ -21,10 +24,55 @@ def test_non_utf8_source_is_passed_as_utf8(mocker):
2124 )
2225
2326 assert yeast_parser .parse (str (path ), source , Logger ()) is None
27+ command , command_separator , cwd = yeast_parser ._parser_command ()
2428 popen .assert_called_once_with (
25- yeast_parser .command + yeast_parser .command_separator + [str (path )],
29+ command + command_separator + [str (path )],
30+ cwd = cwd ,
2631 stdin = yeast_parser .subprocess .PIPE ,
2732 stdout = yeast_parser .subprocess .PIPE ,
2833 stderr = yeast_parser .subprocess .PIPE ,
2934 )
3035 process .communicate .assert_called_once_with (source .encode ("utf-8" ))
36+
37+
38+ def test_packaged_command_accepts_supported_platform (tmp_path ):
39+ expected = tmp_path / "tools" / "linux64" / "yeast-python"
40+
41+ assert yeast_parser ._packaged_command (str (tmp_path ), "linux64" ) == [
42+ str (expected )
43+ ]
44+
45+
46+ def test_packaged_command_rejects_unsupported_platform (tmp_path ):
47+ with pytest .raises (ValueError , match = "Unsupported CodeQL platform" ):
48+ yeast_parser ._packaged_command (str (tmp_path ), "../linux64" )
49+
50+
51+ def test_packaged_command_rejects_tools_symlink_escape (tmp_path ):
52+ outside = tmp_path / "outside"
53+ outside .mkdir ()
54+ tools = tmp_path / "tools"
55+ tools .mkdir ()
56+ (tools / "linux64" ).symlink_to (outside , target_is_directory = True )
57+
58+ with pytest .raises (ValueError , match = "escapes extractor tools root" ):
59+ yeast_parser ._packaged_command (str (tmp_path ), "linux64" )
60+
61+
62+ def test_packaged_command_rejects_executable_symlink_escape (tmp_path ):
63+ outside = tmp_path / "outside"
64+ outside .touch ()
65+ tools = tmp_path / "tools" / "linux64"
66+ tools .mkdir (parents = True )
67+ (tools / "yeast-python" ).symlink_to (outside )
68+
69+ with pytest .raises (ValueError , match = "escapes platform tools directory" ):
70+ yeast_parser ._packaged_command (str (tmp_path ), "linux64" )
71+
72+
73+ def test_packaged_command_validation_is_deferred (monkeypatch , tmp_path ):
74+ monkeypatch .setenv ("CODEQL_EXTRACTOR_PYTHON_ROOT" , str (tmp_path ))
75+ monkeypatch .setenv ("CODEQL_PLATFORM" , "../linux64" )
76+
77+ with pytest .raises (ValueError , match = "Unsupported CodeQL platform" ):
78+ yeast_parser ._parser_command ()
0 commit comments