Skip to content

Commit a0620f6

Browse files
committed
Makes setup of unittests modular
1 parent 20d75bb commit a0620f6

File tree

2 files changed

+59
-51
lines changed

2 files changed

+59
-51
lines changed

test/setup_tests.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import os
2+
import subprocess
3+
import sys
4+
5+
try:
6+
from StringIO import StringIO
7+
except ImportError:
8+
from io import StringIO
9+
root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
10+
sys.path.insert(0, root_dir)
11+
12+
from fortls.jsonrpc import ( # noqa: E402, F401
13+
path_to_uri,
14+
read_rpc_messages,
15+
write_rpc_notification,
16+
write_rpc_request,
17+
)
18+
19+
run_command = os.path.join(
20+
root_dir, "fortls.py --incremental_sync --use_signature_help"
21+
)
22+
test_dir = os.path.join(root_dir, "test", "test_source")
23+
24+
25+
def run_request(request, fortls_args=""):
26+
pid = subprocess.Popen(
27+
run_command + fortls_args,
28+
shell=True,
29+
stdin=subprocess.PIPE,
30+
stdout=subprocess.PIPE,
31+
stderr=subprocess.PIPE,
32+
)
33+
results = pid.communicate(input=request.encode())
34+
tmp_file = StringIO(results[0].decode())
35+
results = read_rpc_messages(tmp_file)
36+
parsed_results = []
37+
for result in results:
38+
try:
39+
parsed_results.append(result["result"])
40+
except KeyError:
41+
try:
42+
# Present in `method`s
43+
parsed_results.append(result["params"])
44+
except:
45+
raise RuntimeError(
46+
"Only 'result' and 'params' keys have been implemented for testing."
47+
" Please add the new key."
48+
)
49+
except:
50+
raise RuntimeError(
51+
"Unexpected error encountered trying to extract server results"
52+
)
53+
errcode = pid.poll()
54+
return errcode, parsed_results

test/test_server.py

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,12 @@
11
import os
2-
import subprocess
3-
import sys
4-
5-
try:
6-
from StringIO import StringIO
7-
except ImportError:
8-
from io import StringIO
9-
root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
10-
sys.path.insert(0, root_dir)
11-
12-
from fortls.jsonrpc import ( # noqa: E402
13-
path_to_uri,
14-
read_rpc_messages,
15-
write_rpc_notification,
2+
from setup_tests import (
3+
run_request,
164
write_rpc_request,
5+
write_rpc_notification,
6+
path_to_uri,
7+
test_dir,
178
)
189

19-
run_command = os.path.join(
20-
root_dir, "fortls.py --incremental_sync --use_signature_help"
21-
)
22-
test_dir = os.path.join(root_dir, "test", "test_source")
23-
24-
25-
def run_request(request, fortls_args=""):
26-
pid = subprocess.Popen(
27-
run_command + fortls_args,
28-
shell=True,
29-
stdin=subprocess.PIPE,
30-
stdout=subprocess.PIPE,
31-
stderr=subprocess.PIPE,
32-
)
33-
results = pid.communicate(input=request.encode())
34-
tmp_file = StringIO(results[0].decode())
35-
results = read_rpc_messages(tmp_file)
36-
parsed_results = []
37-
for result in results:
38-
try:
39-
parsed_results.append(result["result"])
40-
except KeyError:
41-
try:
42-
# Present in `method`s
43-
parsed_results.append(result["params"])
44-
except:
45-
raise RuntimeError(
46-
"Only 'result' and 'params' keys have been implemented for testing."
47-
" Please add the new key."
48-
)
49-
except:
50-
raise RuntimeError(
51-
"Unexpected error encountered trying to extract server results"
52-
)
53-
errcode = pid.poll()
54-
return errcode, parsed_results
55-
5610

5711
def test_init():
5812
def check_return(result_dict):

0 commit comments

Comments
 (0)