Skip to content

Commit 6c86856

Browse files
authored
fix(python): Use pathlib for os-independent unit tests (#158)
1 parent 49790d9 commit 6c86856

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

src/REST/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from io import open
88
from json import dumps, load, loads
9-
from os import path
9+
from pathlib import Path
1010
from urllib.parse import urlparse
1111

1212
from pygments import formatters, highlight, lexers
@@ -235,7 +235,7 @@ def _input_object(value):
235235
if isinstance(value, (dict)):
236236
return value
237237
try:
238-
if path.isfile(value):
238+
if Path(value).is_file():
239239
json_value = REST._input_json_from_file(value)
240240
else:
241241
json_value = loads(value)
@@ -254,7 +254,7 @@ def _input_array(value):
254254
if isinstance(value, (list)):
255255
return value
256256
try:
257-
if path.isfile(value):
257+
if Path(value).is_file():
258258
json_value = REST._input_json_from_file(value)
259259
else:
260260
json_value = loads(value)
@@ -339,7 +339,7 @@ def _input_ssl_verify(value):
339339
return REST._input_boolean(value)
340340
except RuntimeError:
341341
value = REST._input_string(value)
342-
if not path.isfile(value):
342+
if not Path(value).is_file():
343343
raise RuntimeError(
344344
"SSL verify option is not "
345345
+ "a Python or a JSON boolean or a path to an existing "
@@ -384,7 +384,7 @@ def _input_data(value):
384384
except RuntimeError:
385385
if isinstance(value, bytes):
386386
data = value
387-
elif path.isfile(value):
387+
elif Path(value).is_file():
388388
with open(value, "rb") as file:
389389
data = file.read()
390390
else:

src/REST/keywords.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from datetime import datetime
1111
from io import open
1212
from json import dumps
13-
from os import getcwd, path
13+
from pathlib import Path
1414
from urllib.parse import parse_qsl, urljoin, urlparse
1515

1616
with warnings.catch_warnings():
@@ -1050,7 +1050,7 @@ def input(self, what):
10501050
return None
10511051
if not isinstance(what, str):
10521052
return self._input_json_from_non_string(what)
1053-
if path.isfile(what):
1053+
if Path(what).is_file():
10541054
return self._input_json_from_file(what)
10551055
try:
10561056
return self._input_json_as_string(what)
@@ -1136,7 +1136,7 @@ def output_schema(
11361136
write_mode = "a" if self._input_boolean(append) else "w"
11371137
try:
11381138
with open(
1139-
path.join(getcwd(), file_path), write_mode, encoding="utf-8"
1139+
Path.cwd() / file_path, write_mode, encoding="utf-8"
11401140
) as file:
11411141
file.write(content)
11421142
except OSError as e:
@@ -1238,7 +1238,7 @@ def output(
12381238
write_mode = "a" if self._input_boolean(append) else "w"
12391239
try:
12401240
with open(
1241-
path.join(getcwd(), file_path), write_mode, encoding="utf-8"
1241+
Path.cwd() / file_path, write_mode, encoding="utf-8"
12421242
) as file:
12431243
file.write(content)
12441244
except OSError as e:
@@ -1273,10 +1273,12 @@ def rest_instances(self, file_path=None, sort_keys=False):
12731273
outputdir_path = BuiltIn().get_variable_value("${OUTPUTDIR}")
12741274
if self.request["netloc"]:
12751275
file_path = (
1276-
path.join(outputdir_path, self.request["netloc"]) + ".json"
1277-
)
1276+
Path(outputdir_path) / self.request["netloc"]
1277+
).with_suffix(".json")
12781278
else:
1279-
file_path = path.join(outputdir_path, "instances") + ".json"
1279+
file_path = (Path(outputdir_path) / "instances").with_suffix(
1280+
".json"
1281+
)
12801282
sort_keys = self._input_boolean(sort_keys)
12811283
content = dumps(
12821284
self.instances,
@@ -1287,7 +1289,7 @@ def rest_instances(self, file_path=None, sort_keys=False):
12871289
sort_keys=sort_keys,
12881290
)
12891291
try:
1290-
with open(file_path, "w", encoding="utf-8") as file:
1292+
with open(Path(file_path), "w", encoding="utf-8") as file:
12911293
file.write(content)
12921294
except OSError as e:
12931295
raise RuntimeError(

test/test_output.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import io
2-
import os
32
import re
43
import sys
54
import unittest
5+
from pathlib import Path
66
from unittest.mock import MagicMock, mock_open, patch
77

88
from src import REST
@@ -199,8 +199,9 @@ def test_output_file_and_console(self):
199199
self.library.output(self.output_dict, file_path="rest.log")
200200
log_clean = self._remove_ansi(self.log_buf.getvalue())
201201
self.assertEqual(log_clean, self.output_console)
202+
202203
mock_log.assert_called_with(
203-
f"{os.getcwd()}/rest.log", "w", encoding="utf-8"
204+
Path.cwd() / "rest.log", "w", encoding="utf-8"
204205
)
205206
handle = mock_log()
206207
handle.write.assert_called_once_with(self.output_console_file)
@@ -214,7 +215,7 @@ def test_output_file_and_no_console(self):
214215
log_clean = self._remove_ansi(self.log_buf.getvalue())
215216
self.assertEqual(log_clean, "")
216217
mock_log.assert_called_with(
217-
f"{os.getcwd()}/rest.log", "w", encoding="utf-8"
218+
Path.cwd() / "rest.log", "w", encoding="utf-8"
218219
)
219220
handle = mock_log()
220221
handle.write.assert_called_once_with(self.output_console_file)
@@ -252,7 +253,7 @@ def test_output_schema_file_and_console(self):
252253
log_clean = self._remove_ansi(self.log_buf.getvalue())
253254
self.assertEqual(log_clean, self.output_schema_console)
254255
mock_log.assert_called_with(
255-
f"{os.getcwd()}/rest.log", "w", encoding="utf-8"
256+
Path.cwd() / "rest.log", "w", encoding="utf-8"
256257
)
257258
handle = mock_log()
258259
handle.write.assert_called_once_with(self.output_schema_file)
@@ -266,7 +267,7 @@ def test_output_schema_file_and_no_console(self):
266267
log_clean = self._remove_ansi(self.log_buf.getvalue())
267268
self.assertEqual(log_clean, "")
268269
mock_log.assert_called_with(
269-
f"{os.getcwd()}/rest.log", "w", encoding="utf-8"
270+
Path.cwd() / "rest.log", "w", encoding="utf-8"
270271
)
271272
handle = mock_log()
272273
handle.write.assert_called_once_with(self.output_schema_file)

0 commit comments

Comments
 (0)