Skip to content

Commit

Permalink
Updated tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
vbharadwaj-bk committed May 27, 2024
1 parent 2647cbd commit 9dcfae8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ htmlcov
**/.DS_Store
.eggs
cppimport/_version.py
*.lock
3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def pytest_addoption(parser):
parser.addoption('--multiprocessing', action='store_true', dest="multiprocessing",
default=False, help="enable multiprocessing tests with filelock")
21 changes: 17 additions & 4 deletions tests/test_cppimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import shutil
import subprocess
import sys
import pytest
from multiprocessing import Process
from tempfile import TemporaryDirectory

Expand All @@ -13,6 +14,10 @@
import cppimport.templating
from cppimport.find import find_module_cpppath

cppimport.settings["use_filelock"] = False # No need for filelock except for
# multiprocessing test.
multiprocessing_enable = pytest.mark.skipif("not config.getoption('multiprocessing')")

root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)

Expand All @@ -22,7 +27,6 @@
handler.setFormatter(formatter)
root_logger.addHandler(handler)


@contextlib.contextmanager
def appended(filename, text):
with open(filename, "r") as f:
Expand Down Expand Up @@ -127,8 +131,11 @@ def test_with_file_in_syspath():
def test_rebuild_after_failed_compile():
cppimport.imp("mymodule")
test_code = """
import cppimport; mymodule = cppimport.imp("mymodule");assert(mymodule.add(1,2) == 3)
"""
import cppimport;
cppimport.settings["use_filelock"] = False;
mymodule = cppimport.imp("mymodule");
assert(mymodule.add(1,2) == 3)
"""
with appended("tests/mymodule.cpp", ";asdf;"):
subprocess_check(test_code, 1)
subprocess_check(test_code, 0)
Expand All @@ -149,6 +156,7 @@ def test_no_rebuild_if_no_deps_change():
cppimport.imp("mymodule")
test_code = """
import cppimport;
cppimport.settings["use_filelock"] = False;
mymodule = cppimport.imp("mymodule");
assert(not hasattr(mymodule, 'Thing'))
"""
Expand All @@ -160,6 +168,7 @@ def test_rebuild_header_after_change():
cppimport.imp("mymodule")
test_code = """
import cppimport;
cppimport.settings["use_filelock"] = False;
mymodule = cppimport.imp("mymodule");
mymodule.Thing().cheer()
"""
Expand Down Expand Up @@ -214,8 +223,12 @@ def test_relative_import():
print(f())
assert f() == 3


@multiprocessing_enable
def test_multiple_processes():
'''
Only runs if the flag --multiprocessing is passed to
pytest. This function requires file locking enabled.
'''
with tmp_dir(["tests/hook_test.cpp"]) as tmp_path:
test_code = f"""
import os;
Expand Down

0 comments on commit 9dcfae8

Please sign in to comment.