Skip to content

Commit 50129f8

Browse files
authored
Adapted for Windows (#53)
The current code use extensively `.as_posix()` on `pathlib.Path`'s as we developed and ran mapry only on Linux. This patch makes the necessary changes so that mapry can now be run on Windows as well.
1 parent 9e38aca commit 50129f8

6 files changed

Lines changed: 46 additions & 50 deletions

File tree

mapry/parse.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,7 @@ def schema_from_json_file(path: pathlib.Path) -> mapry.Schema:
385385

386386
try:
387387
obj = json.load(fp=fid, object_pairs_hook=_OrderedDictPP)
388-
return schema_from_mapping(
389-
mapping=obj, ref='{}#'.format(path.as_posix()))
388+
return schema_from_mapping(mapping=obj, ref='{}#'.format(str(path)))
390389
except json.JSONDecodeError as err:
391390
jsonerr = err
392391

precommit.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ def main() -> int:
3838
subprocess.check_call([
3939
"yapf", "--in-place", "--style=style.yapf", "--recursive",
4040
"tests", "mapry", "setup.py", "precommit.py"],
41-
cwd=repo_root.as_posix())
41+
cwd=str(repo_root))
4242
# yapf: enable
4343
else:
4444
# yapf: disable
4545
subprocess.check_call([
4646
"yapf", "--diff", "--style=style.yapf", "--recursive",
4747
"tests", "mapry", "setup.py", "precommit.py"],
48-
cwd=repo_root.as_posix())
48+
cwd=str(repo_root))
4949
# yapf: enable
5050

5151
print("Mypy'ing...")
5252
subprocess.check_call(["mypy", "--strict", "mapry", "tests"],
53-
cwd=repo_root.as_posix())
53+
cwd=str(repo_root))
5454

5555
print("Isort'ing...")
5656
# yapf: disable
@@ -86,11 +86,11 @@ def main() -> int:
8686
subprocess.check_call(cmd)
8787

8888
print("Pydocstyle'ing...")
89-
subprocess.check_call(["pydocstyle", "mapry"], cwd=repo_root.as_posix())
89+
subprocess.check_call(["pydocstyle", "mapry"], cwd=str(repo_root))
9090

9191
print("Pylint'ing...")
9292
subprocess.check_call(["pylint", "--rcfile=pylint.rc", "tests", "mapry"],
93-
cwd=repo_root.as_posix())
93+
cwd=str(repo_root))
9494

9595
print("Testing...")
9696
env = os.environ.copy()
@@ -101,29 +101,29 @@ def main() -> int:
101101
["coverage", "run",
102102
"--source", "mapry",
103103
"-m", "unittest", "discover", "tests"],
104-
cwd=repo_root.as_posix(),
104+
cwd=str(repo_root),
105105
env=env)
106106
# yapf: enable
107107

108108
subprocess.check_call(["coverage", "report"])
109109

110110
print("Doctesting...")
111111
subprocess.check_call(
112-
["python3", "-m", "doctest", (repo_root / "README.rst").as_posix()])
112+
[sys.executable, "-m", "doctest",
113+
str(repo_root / "README.rst")])
113114

114115
for pth in sorted((repo_root / "mapry").glob("**/*.py")):
115-
subprocess.check_call(["python3", "-m", "doctest", pth.as_posix()])
116+
subprocess.check_call([sys.executable, "-m", "doctest", str(pth)])
116117

117118
print("pyicontract-lint'ing...")
118119
for pth in sorted((repo_root / "mapry").glob("**/*.py")):
119-
subprocess.check_call(["pyicontract-lint", pth.as_posix()])
120+
subprocess.check_call(["pyicontract-lint", str(pth)])
120121

121122
print("Checking with twine ...")
122-
subprocess.check_call(["python3", "setup.py", "sdist"],
123-
cwd=repo_root.as_posix())
123+
subprocess.check_call([sys.executable, "setup.py", "sdist"],
124+
cwd=str(repo_root))
124125

125-
subprocess.check_call(["twine", "check", "dist/*"],
126-
cwd=repo_root.as_posix())
126+
subprocess.check_call(["twine", "check", "dist/*"], cwd=str(repo_root))
127127

128128
return 0
129129

tests/cpp/live_test_generate_jsoncpp.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ class Case:
210210
for example_path in example_paths),
211211
"Necessary since we need to re-use the test identifier "
212212
"for the example and the schemas")
213+
@icontract.require(lambda rel_path: not rel_path.is_absolute())
213214
# yapf: enable
214215
def __init__(
215216
self, schema_path: pathlib.Path,
@@ -230,7 +231,7 @@ def __init__(
230231
self.rel_path = rel_path
231232

232233
self.executable_name = "{}_parse_serialize".format(
233-
rel_path.as_posix().replace("/", "_"))
234+
'_'.join(rel_path.parts))
234235

235236

236237
class Params:
@@ -348,8 +349,8 @@ def say(message: str) -> None:
348349

349350
# yapf: disable
350351
proc = subprocess.Popen(
351-
[(bin_dir / case.executable_name).as_posix(),
352-
example_pth.as_posix()],
352+
[str(bin_dir / case.executable_name),
353+
str(example_pth)],
353354
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
354355
universal_newlines=True)
355356
# yapf: enable
@@ -499,8 +500,8 @@ def main() -> None:
499500

500501
build_dir.mkdir(exist_ok=True)
501502
subprocess.check_call(['cmake', '../src', '-DCMAKE_BUILD_TYPE=Debug'],
502-
cwd=build_dir.as_posix())
503-
subprocess.check_call(['make', 'all'], cwd=build_dir.as_posix())
503+
cwd=str(build_dir))
504+
subprocess.check_call(['make', 'all'], cwd=str(build_dir))
504505

505506
##
506507
# Run

tests/go/live_test_generate_jsonable.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,15 @@ def say(message: str) -> None:
246246
(src_dir / "parse_serialize" / "main.go").chmod(mode=0o755)
247247

248248
env = os.environ.copy()
249-
env['GOPATH'] = gopath.as_posix()
249+
env['GOPATH'] = str(gopath)
250250

251251
##
252252
# Validate
253253
##
254254

255255
say("go vet'ing the generated filies in {} ...".format(gopath))
256256
subprocess.check_call(['go', 'vet', '.'],
257-
cwd=(src_dir / "parse_serialize").as_posix(),
257+
cwd=str(src_dir / "parse_serialize"),
258258
env=env)
259259

260260
##
@@ -263,7 +263,7 @@ def say(message: str) -> None:
263263

264264
say("go build'ing {} ...".format(gopath))
265265
subprocess.check_call(['go', 'build', '.'],
266-
cwd=(src_dir / "parse_serialize").as_posix(),
266+
cwd=str(src_dir / "parse_serialize"),
267267
env=env)
268268

269269
##
@@ -284,10 +284,8 @@ def say(message: str) -> None:
284284

285285
# yapf: disable
286286
proc = subprocess.Popen(
287-
[(
288-
gopath / 'src' / 'parse_serialize' / 'parse_serialize'
289-
).as_posix(),
290-
'-path', example_pth.as_posix()],
287+
[str(gopath / 'src' / 'parse_serialize' / 'parse_serialize'),
288+
'-path', str(example_pth)],
291289
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
292290
universal_newlines=True)
293291
# yapf: enable

tests/py/live_test_generate_jsonable.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import pathlib
88
import subprocess
9+
import sys
910
from typing import Any, Sequence
1011

1112
import icontract
@@ -226,13 +227,13 @@ def say(message: str) -> None:
226227
##
227228

228229
say("Mypy'ing the generated filies in {} ...".format(case_src_dir))
229-
subprocess.check_call(['mypy', '--strict', case_src_dir.as_posix()])
230+
subprocess.check_call(['mypy', '--strict', str(case_src_dir)])
230231

231232
say("doctest'ing the generated filies in {} ...".format(case_src_dir))
232233
for pth in sorted(case_src_dir.glob("**/*.py")):
233-
subprocess.check_call(['python3', '-m', 'doctest',
234-
pth.as_posix()],
235-
cwd=case_src_dir.as_posix())
234+
subprocess.check_call([sys.executable, '-m', 'doctest',
235+
str(pth)],
236+
cwd=str(case_src_dir))
236237

237238
##
238239
# Execute
@@ -252,12 +253,12 @@ def say(message: str) -> None:
252253

253254
env = os.environ.copy()
254255
env['PYTHONPATH'] = '{}:{}'.format(
255-
os.environ.get('PYTHONPATH', ''), case_src_dir.as_posix())
256+
os.environ.get('PYTHONPATH', ''), str(case_src_dir))
256257

257258
# yapf: disable
258259
proc = subprocess.Popen(
259-
[(module_dir / 'parse_serialize.py').as_posix(),
260-
'--path', example_pth.as_posix()],
260+
[str(module_dir / 'parse_serialize.py'),
261+
'--path', str(example_pth)],
261262
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
262263
env=env,
263264
universal_newlines=True)

tests/test_parse.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,32 @@
1313

1414
class TestSchema(unittest.TestCase):
1515
def test_schema_from_invalid_mapping(self) -> None: # pylint: disable=invalid-name
16-
# Test with list
17-
with tempfile.NamedTemporaryFile() as tmpfile:
18-
tmpfile.file.write('[]'.encode()) # type: ignore
19-
tmpfile.file.flush() # type: ignore
16+
with tempfile.TemporaryDirectory() as tmpdir:
17+
# Test with list
18+
tmpfile = pathlib.Path(tmpdir) / "test-schema-with-list.yml"
19+
tmpfile.write_text('[]')
2020

2121
try:
22-
mapry.parse.schema_from_json_file(
23-
path=pathlib.Path(tmpfile.name))
22+
mapry.parse.schema_from_json_file(path=tmpfile)
2423
except mapry.validation.SchemaError as err:
2524
self.assertEqual((
2625
"{}#: Does not follow json schema: "
27-
"[] is not of type 'object'").format(tmpfile.name),
28-
str(err))
26+
"[] is not of type 'object'").format(tmpfile), str(err))
2927

30-
# Test with name as object instead of string
31-
with tempfile.NamedTemporaryFile() as tmpfile:
32-
tmpfile.file.write( # type: ignore
28+
# Test with name as object instead of string
29+
tmpfile = pathlib.Path(tmpdir) / "name-as-object.yml"
30+
31+
tmpfile.write_text(
3332
'{"name": {"unexpected": "value"}, '
34-
'"description": "some description."}'.encode())
35-
tmpfile.file.flush() # type: ignore
33+
'"description": "some description."}')
3634

3735
try:
38-
mapry.parse.schema_from_json_file(
39-
path=pathlib.Path(tmpfile.name))
36+
mapry.parse.schema_from_json_file(path=tmpfile)
4037
except mapry.validation.SchemaError as err:
4138
self.assertEqual(
4239
str(err), "{}#/name: Does not follow json schema: "
4340
"{{'unexpected': 'value'}} is not of type 'string'".format(
44-
tmpfile.name))
41+
tmpfile))
4542

4643
def test_cases(self) -> None: # pylint: disable=no-self-use
4744
cases_dir = tests.path.REPO_DIR / 'test_cases' / 'general'

0 commit comments

Comments
 (0)