Skip to content

Commit 115cff0

Browse files
committed
Makes unittest input to be a list
Should make it easier to write cross platform unittests. Also, removed the `shell=True` we do not need that anymore, only the `sys.executable`. Some additional sanitisation of input and Path fixups
1 parent 7828908 commit 115cff0

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

test/setup_tests.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import os
1+
from __future__ import annotations
2+
3+
import shlex
24
import subprocess
35
import sys
46
from io import StringIO
@@ -14,16 +16,23 @@
1416
write_rpc_request,
1517
)
1618

17-
run_command = os.path.join(
18-
root_dir, "fortls.py --incremental_sync --use_signature_help"
19-
)
2019
test_dir = root_dir / "test" / "test_source"
2120

2221

23-
def run_request(request, fortls_args=""):
22+
def run_request(request, fortls_args: list[str] = None):
23+
command = [
24+
sys.executable,
25+
str(root_dir / "fortls.py"),
26+
"--incremental_sync",
27+
"--use_signature_help",
28+
]
29+
if fortls_args:
30+
# Input args might not be sanitised, fix that
31+
for i in fortls_args:
32+
command.extend(shlex.split(i))
33+
2434
pid = subprocess.Popen(
25-
run_command + fortls_args,
26-
shell=True,
35+
command,
2736
stdin=subprocess.PIPE,
2837
stdout=subprocess.PIPE,
2938
stderr=subprocess.PIPE,

test/test_preproc.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
from pathlib import Path
43
from setup_tests import (
54
run_request,
65
# path_to_uri,
@@ -16,7 +15,7 @@ def hover_req(file_path: str, ln: int, col: int) -> str:
1615
1,
1716
"textDocument/hover",
1817
{
19-
"textDocument": {"uri": file_path},
18+
"textDocument": {"uri": str(file_path)},
2019
"position": {"line": ln, "character": col},
2120
},
2221
)
@@ -26,17 +25,17 @@ def check_return(result_array, checks):
2625
for (i, check) in enumerate(checks):
2726
assert result_array[i]["contents"][0]["value"] == check
2827

29-
root_dir = Path(test_dir).resolve() / "pp"
28+
root_dir = test_dir / "pp"
3029
string = write_rpc_request(1, "initialize", {"rootPath": str(root_dir)})
31-
file_path = str(root_dir / "preproc.F90")
30+
file_path = root_dir / "preproc.F90"
3231
string += hover_req(file_path, 5, 8) # user defined type
3332
string += hover_req(file_path, 7, 30) # variable
3433
string += hover_req(file_path, 7, 40) # multi-lin variable
3534
string += hover_req(file_path, 8, 7) # function with if conditional
3635
string += hover_req(file_path, 9, 7) # multiline function with if conditional
37-
file_path = str(root_dir / "preproc_keywords.F90")
36+
file_path = root_dir / "preproc_keywords.F90"
3837
string += hover_req(file_path, 6, 2) # ignores PP across Fortran line continuations
39-
errcode, results = run_request(string, f' --config={root_dir/".pp_conf.json"}')
38+
errcode, results = run_request(string, [f'--config={root_dir/".pp_conf.json"}'])
4039
assert errcode == 0
4140

4241
# Reference solution

test/test_server.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def check_return(result_dict):
5151
def test_logger():
5252
"""Test the logger"""
5353
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
54-
errcode, results = run_request(string, " --debug_log")
54+
errcode, results = run_request(string, ["--debug_log"])
5555
assert errcode == 0
5656
assert results[1]["type"] == 3
5757
assert results[1]["message"] == "FORTLS debugging enabled"
@@ -63,7 +63,7 @@ def test_open():
6363
string += write_rpc_notification(
6464
"textDocument/didOpen", {"textDocument": {"uri": file_path}}
6565
)
66-
errcode, results = run_request(string, fortls_args=" --disable_diagnostics")
66+
errcode, results = run_request(string, fortls_args=["--disable_diagnostics"])
6767
#
6868
assert errcode == 0
6969
assert len(results) == 1
@@ -126,7 +126,7 @@ def test_change():
126126
string += write_rpc_request(
127127
3, "textDocument/documentSymbol", {"textDocument": {"uri": str(file_path)}}
128128
)
129-
errcode, results = run_request(string, fortls_args=" --disable_diagnostics")
129+
errcode, results = run_request(string, fortls_args=["--disable_diagnostics"])
130130
#
131131
assert errcode == 0
132132
assert len(results) == 3
@@ -551,7 +551,7 @@ def check_return(result_array, checks):
551551
file_path = test_dir / "hover" / "pointers.f90"
552552
string += hover_req(file_path, 1, 26)
553553
errcode, results = run_request(
554-
string, fortls_args=" --variable_hover --sort_keywords"
554+
string, fortls_args=["--variable_hover", "--sort_keywords"]
555555
)
556556
assert errcode == 0
557557
#
@@ -699,7 +699,7 @@ def check_return(results, ref_results):
699699
"textDocument/didOpen", {"textDocument": {"uri": file_path}}
700700
)
701701
file_path = str(test_dir / "diag" / "conf_long_lines.json")
702-
errcode, res = run_request(string, f" --config {file_path}")
702+
errcode, res = run_request(string, [f"--config {file_path}"])
703703
assert errcode == 0
704704
results.extend(res[1:])
705705

0 commit comments

Comments
 (0)