Skip to content

Commit 7828908

Browse files
committed
Makes unittest use Path instead of os.path
This is for cross-platform compatibility when testing, since os.path tends to mess up capitalisation of paths on windows which are case-insensitive but pytest does not know that so we have to use Path instead which yields more accurate results.
1 parent 745d086 commit 7828908

File tree

4 files changed

+100
-99
lines changed

4 files changed

+100
-99
lines changed

test/setup_tests.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import os
22
import subprocess
33
import sys
4+
from io import StringIO
5+
from pathlib import Path
46

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__), ".."))
7+
root_dir = Path(__file__).parent.parent.resolve()
108
sys.path.insert(0, root_dir)
119

1210
from fortls.jsonrpc import ( # noqa: E402, F401
@@ -19,7 +17,7 @@
1917
run_command = os.path.join(
2018
root_dir, "fortls.py --incremental_sync --use_signature_help"
2119
)
22-
test_dir = os.path.join(root_dir, "test", "test_source")
20+
test_dir = root_dir / "test" / "test_source"
2321

2422

2523
def run_request(request, fortls_args=""):

test/test_interface.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import sys
3+
from pathlib import Path
34

45
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
56

@@ -85,7 +86,7 @@ def test_command_line_code_actions_options():
8586
def unittest_server_init():
8687
from fortls.langserver import LangServer
8788

88-
root = os.path.join(os.path.dirname(__file__), "test_source")
89+
root = (Path(__file__).parent / "test_source").resolve()
8990
parser = commandline_args("fortls")
9091
args = parser.parse_args("-c f90_config.json".split())
9192

@@ -105,14 +106,14 @@ def test_config_file_general_options():
105106

106107

107108
def test_config_file_dir_parsing_options():
108-
server, root = unittest_server_init()
109+
server, r = unittest_server_init()
109110
# File parsing
110111
assert server.source_dirs == set(
111-
[f"{root}/subdir", f"{root}/pp", f"{root}/pp/include"]
112+
[f'{r/"subdir"}', f'{r/"pp"}', f'{r/"pp"/"include"}']
112113
)
113114
assert server.incl_suffixes == [".FF", ".fpc", ".h", "f20"]
114115
assert server.excl_suffixes == set(["_tmp.f90", "_h5hut_tests.F90"])
115-
assert server.excl_paths == set([f"{root}/excldir", f"{root}/hover"])
116+
assert server.excl_paths == set([f'{r/"excldir"}', f'{r/"hover"}'])
116117

117118

118119
def test_config_file_autocomplete_options():
@@ -145,7 +146,7 @@ def test_config_file_preprocessor_options():
145146
server, root = unittest_server_init()
146147
# Preprocessor options
147148
assert server.pp_suffixes == [".h", ".fh"]
148-
assert server.include_dirs == set([f"{root}/include"])
149+
assert server.include_dirs == set([f'{root/"include"}'])
149150
assert server.pp_defs == {
150151
"HAVE_PETSC": "",
151152
"HAVE_ZOLTAN": "",

test/test_preproc.py

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

3-
import os
3+
from pathlib import Path
44
from setup_tests import (
55
run_request,
66
# path_to_uri,
@@ -26,17 +26,17 @@ def check_return(result_array, checks):
2626
for (i, check) in enumerate(checks):
2727
assert result_array[i]["contents"][0]["value"] == check
2828

29-
root_dir = os.path.join(test_dir, "pp")
30-
string = write_rpc_request(1, "initialize", {"rootPath": root_dir})
31-
file_path = os.path.join(test_dir, "pp", "preproc.F90")
29+
root_dir = Path(test_dir).resolve() / "pp"
30+
string = write_rpc_request(1, "initialize", {"rootPath": str(root_dir)})
31+
file_path = str(root_dir / "preproc.F90")
3232
string += hover_req(file_path, 5, 8) # user defined type
3333
string += hover_req(file_path, 7, 30) # variable
3434
string += hover_req(file_path, 7, 40) # multi-lin variable
3535
string += hover_req(file_path, 8, 7) # function with if conditional
3636
string += hover_req(file_path, 9, 7) # multiline function with if conditional
37-
file_path = os.path.join(test_dir, "pp", "preproc_keywords.F90")
37+
file_path = str(root_dir / "preproc_keywords.F90")
3838
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")
39+
errcode, results = run_request(string, f' --config={root_dir/".pp_conf.json"}')
4040
assert errcode == 0
4141

4242
# Reference solution

0 commit comments

Comments
 (0)