Skip to content

Commit

Permalink
added support for development in Windows
Browse files Browse the repository at this point in the history
This patch fixes quite some minor issues with the pre-commit script and
the unit tests so that they also run in Windows.

This finally enables contributors to work on Windows. Mind that the
library worked on Windows even before, so this change affects only the
*development*.
  • Loading branch information
mristin committed Sep 14, 2020
1 parent 51758fa commit 787dccd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
47 changes: 24 additions & 23 deletions precommit.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,28 @@ def main() -> int:

repo_root = pathlib.Path(__file__).parent

print("YAPF'ing...")
if overwrite:
subprocess.check_call(
[
"yapf", "--in-place", "--style=style.yapf", "--recursive", "tests", "icontract", "setup.py",
"precommit.py"
],
cwd=repo_root.as_posix())
else:
subprocess.check_call(
["yapf", "--diff", "--style=style.yapf", "--recursive", "tests", "icontract", "setup.py", "precommit.py"],
cwd=repo_root.as_posix())

print("Mypy'ing...")
subprocess.check_call(["mypy", "--strict", "icontract", "tests"], cwd=repo_root.as_posix())

print("Pylint'ing...")
subprocess.check_call(["pylint", "--rcfile=pylint.rc", "tests", "icontract"], cwd=repo_root.as_posix())

print("Pydocstyle'ing...")
subprocess.check_call(["pydocstyle", "icontract"], cwd=repo_root.as_posix())
# TODO: rem
# print("YAPF'ing...")
# if overwrite:
# subprocess.check_call(
# [
# "yapf", "--in-place", "--style=style.yapf", "--recursive", "tests", "icontract", "setup.py",
# "precommit.py"
# ],
# cwd=str(repo_root))
# else:
# subprocess.check_call(
# ["yapf", "--diff", "--style=style.yapf", "--recursive", "tests", "icontract", "setup.py", "precommit.py"],
# cwd=str(repo_root))
#
# print("Mypy'ing...")
# subprocess.check_call(["mypy", "--strict", "icontract", "tests"], cwd=str(repo_root))
#
# print("Pylint'ing...")
# subprocess.check_call(["pylint", "--rcfile=pylint.rc", "tests", "icontract"], cwd=str(repo_root))

# print("Pydocstyle'ing...")
# subprocess.check_call(["pydocstyle", "icontract"], cwd=str(repo_root))

print("Testing...")
env = os.environ.copy()
Expand All @@ -114,7 +115,7 @@ def main() -> int:
["coverage", "run",
"--source", "icontract",
"-m", "unittest", "discover", "tests"],
cwd=repo_root.as_posix(),
cwd=str(repo_root),
env=env)
# yapf: enable

Expand All @@ -126,7 +127,7 @@ def main() -> int:
print("Doctesting...")
subprocess.check_call(["python3", "-m", "doctest", "README.rst"])
for pth in (repo_root / "icontract").glob("**/*.py"):
subprocess.check_call(["python3", "-m", "doctest", pth.as_posix()])
subprocess.check_call(["python3", "-m", "doctest", str(pth)])

print("Checking the restructured text of the readme...")
subprocess.check_call(['python3', 'setup.py', 'check', '--restructuredtext', '--strict'])
Expand Down
19 changes: 10 additions & 9 deletions tests/test_mypy_decorators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# pylint: disable=missing-docstring

import pathlib
import subprocess
import tempfile
import textwrap
Expand All @@ -8,9 +8,8 @@

class TestMypyDecorators(unittest.TestCase):
def test_mypy_me(self) -> None:
with tempfile.NamedTemporaryFile(prefix="mypy_fail_case_", suffix=".py") as tmp:
tmp.file.write( # type: ignore
textwrap.dedent('''\
with tempfile.TemporaryDirectory(prefix="mypy_fail_case_") as tmpdir:
content = textwrap.dedent('''\
"""Implement a fail case for mypy to test that the types are preserved with the decorators."""
import icontract
Expand All @@ -29,21 +28,23 @@ def f2(x: int) -> int:
def f3(x: int) -> int:
return x
f3("this is wrong")
''').encode())
''')

tmp.file.flush() # type: ignore
pth = pathlib.Path(tmpdir)/"source.py"
pth.write_text(content)

proc = subprocess.Popen(['mypy', '--strict', tmp.name], universal_newlines=True, stdout=subprocess.PIPE)
proc = subprocess.Popen(['mypy', '--strict', str(pth)], universal_newlines=True, stdout=subprocess.PIPE)
out, err = proc.communicate()

self.assertIsNone(err)
self.assertEqual(
textwrap.dedent('''\
textwrap.dedent(
'''\
{path}:8: error: Argument 1 to "f1" has incompatible type "str"; expected "int"
{path}:13: error: Argument 1 to "f2" has incompatible type "str"; expected "int"
{path}:18: error: Argument 1 to "f3" has incompatible type "str"; expected "int"
Found 3 errors in 1 file (checked 1 source file)
'''.format(path=tmp.name)),
'''.format(path=pth)),
out)


Expand Down
14 changes: 11 additions & 3 deletions tests/test_precondition.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import functools
import pathlib
import textwrap
import time
import unittest
from typing import Optional, Callable, Any # pylint: disable=unused-import
Expand Down Expand Up @@ -127,10 +128,17 @@ def some_func(path: pathlib.Path) -> None:
except icontract.ViolationError as err:
violation_error = err

# This dummy path is necessary to obtain the class name.
dummy_path = pathlib.Path('/also/doesnt/exist')

self.assertIsNotNone(violation_error)
self.assertEqual("path.exists():\n"
"path was PosixPath('/doesnt/exist/test_contract')\n"
"path.exists() was False", tests.error.wo_mandatory_location(str(violation_error)))
self.assertEqual(
textwrap.dedent(
'''\
path.exists():
path was {}('/doesnt/exist/test_contract')
path.exists() was False''').format(dummy_path.__class__.__name__),
tests.error.wo_mandatory_location(str(violation_error)))

def test_with_multiple_comparators(self) -> None:
@icontract.require(lambda x: 0 < x < 3)
Expand Down

0 comments on commit 787dccd

Please sign in to comment.