diff --git a/Makefile b/Makefile index 323a9c0..e57f2a3 100644 --- a/Makefile +++ b/Makefile @@ -57,8 +57,9 @@ envtest: install-test: compile pip3 install .[test] +# TODO: maybe use setup.py test? test: install-test - pytest + pytest --forked # Development diff --git a/pyproject.toml b/pyproject.toml index 8fab321..ada2cf0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,11 @@ [build-system] requires = ["setuptools >=39.2.0", "setuptools_scm >= 4.1.2", "wheel >= 0.35.1"] build-backend = "setuptools.build_meta" + +[tool.pytest.ini_options] +minversion = "6.0" +addopts = "" +testpaths = [ + "tests_new", +] + diff --git a/setup.cfg b/setup.cfg index 09c3b60..e65917a 100755 --- a/setup.cfg +++ b/setup.cfg @@ -28,7 +28,7 @@ classifiers = zip_safe = false include_package_data = true python_requires = >= 3.6 -test_suite = tests +test_suite = tests_new install_requires = # deprecated/redundant with pyproject.toml, but lets keep both ways around for now setup_requires = @@ -39,4 +39,5 @@ setup_requires = # To install these dependencies, run pip install .[test] test = pytest + pytest-forked doc = sphinx diff --git a/tests/conftest.py b/tests_new/conftest.py similarity index 82% rename from tests/conftest.py rename to tests_new/conftest.py index c1bb7d8..23e220a 100644 --- a/tests/conftest.py +++ b/tests_new/conftest.py @@ -1,4 +1,5 @@ """pytest configuration for the unit tests.""" + import os import pytest @@ -28,14 +29,15 @@ def datadir(request): return D(request.fspath.dirpath("data")) -@pytest.fixture(scope='function') -def tmpdir(tmpdir_factory, request): +@pytest.fixture() +def ctx_init(tmpdir_factory, request): """Create a tmp dir for the tests""" - base = str(hash(request.node.nodeid))[:3] + base = str(abs(hash(request.node.nodeid)))[:3] bn = tmpdir_factory.mktemp(base) + print(bn) import os + os.environ["PEP_HOME"] = str(bn) os.environ["HOME"] = str(bn) - return bn @pytest.fixture() @@ -51,27 +53,25 @@ def bob_pub_key_data(datadir): @pytest.fixture() -def create_alice_identity(tmpdir, alice_sec_key_data, bob_pub_key_data): - os.environ["HOME"] = str(tmpdir) +def create_alice_identity(ctx_init, alice_sec_key_data): import pEp pEp.import_key(alice_sec_key_data) alice = pEp.Identity( constants.ALICE_ADDRESS, constants.ALICE_NAME, constants.ALICE_NAME_ADDR, constants.ALICE_FP, 0, '' - ) + ) pEp.set_own_key(alice, constants.ALICE_FP) return alice @pytest.fixture() -def create_bob_identity(tmpdir, bob_pub_key_data): - os.environ["HOME"] = str(tmpdir) +def create_bob_identity(ctx_init, bob_pub_key_data): import pEp pEp.import_key(bob_pub_key_data) bob = pEp.Identity( constants.BOB_ADDRESS, constants.BOB_NAME, '', constants.BOB_FP, 56, '' - ) + ) return bob diff --git a/tests/constants.py b/tests_new/constants.py similarity index 82% rename from tests/constants.py rename to tests_new/constants.py index f83f3bb..57d21aa 100644 --- a/tests/constants.py +++ b/tests_new/constants.py @@ -16,8 +16,7 @@ BOB_NAME_ADDR = NAME_ADDR.format(BOB_NAME, ANGLE_ADDR.format(BOB_ADDRESS)) ALICE_NAME = "Alice Lovelace" ALICE_ADDRESS = "alice@openpgp.example" ALICE_FP = "EB85BB5FA33A75E15E944E63F231550C4F47E38E" -ALICE_NAME_ADDR = NAME_ADDR.format(ALICE_NAME, - ANGLE_ADDR.format(ALICE_ADDRESS)) +ALICE_NAME_ADDR = NAME_ADDR.format(ALICE_NAME, ANGLE_ADDR.format(ALICE_ADDRESS)) SUBJECT = "This is a subject" BODY = "Hi world!\n" diff --git a/tests/data/README.md b/tests_new/data/README.md similarity index 100% rename from tests/data/README.md rename to tests_new/data/README.md diff --git a/tests/data/alice@openpgp.example.pub.asc b/tests_new/data/alice@openpgp.example.pub.asc similarity index 100% rename from tests/data/alice@openpgp.example.pub.asc rename to tests_new/data/alice@openpgp.example.pub.asc diff --git a/tests/data/alice@openpgp.example.sec.asc b/tests_new/data/alice@openpgp.example.sec.asc similarity index 100% rename from tests/data/alice@openpgp.example.sec.asc rename to tests_new/data/alice@openpgp.example.sec.asc diff --git a/tests/data/bob@openpgp.example.pub.asc b/tests_new/data/bob@openpgp.example.pub.asc similarity index 100% rename from tests/data/bob@openpgp.example.pub.asc rename to tests_new/data/bob@openpgp.example.pub.asc diff --git a/tests/data/bob@openpgp.example.sec.asc b/tests_new/data/bob@openpgp.example.sec.asc similarity index 100% rename from tests/data/bob@openpgp.example.sec.asc rename to tests_new/data/bob@openpgp.example.sec.asc diff --git a/tests/test_identity.py b/tests_new/test_identity.py similarity index 93% rename from tests/test_identity.py rename to tests_new/test_identity.py index 03829d0..329458d 100644 --- a/tests/test_identity.py +++ b/tests_new/test_identity.py @@ -4,7 +4,7 @@ import os from . import constants -def test_create_one_identity_succeed(tmpdir): +def test_create_one_identity_succeed(ctx_init): # It has to be imported here to get the management db initialized, import pEp alice = pEp.Identity() @@ -15,7 +15,7 @@ def test_create_one_identity_succeed(tmpdir): expected_alice = pEp.Identity( constants.ALICE_ADDRESS, constants.ALICE_NAME, '', constants.ALICE_FP, 0, '' - ) + ) # XXX: Can't compare objects # assert alice == expected_alice @@ -43,14 +43,13 @@ def test_create_one_identity_succeed(tmpdir): assert alice.flags == expected_alice.flags -def test_two_identities_succeed(tmpdir, bob_pub_key_data): - os.environ["HOME"] = str(tmpdir) +def test_two_identities_succeed(ctx_init, bob_pub_key_data): import pEp alice = pEp.Identity( constants.ALICE_ADDRESS, constants.ALICE_NAME, '', constants.ALICE_FP, 0, '' - ) + ) assert alice.address == constants.ALICE_ADDRESS assert alice.username == constants.ALICE_NAME assert alice.fpr == constants.ALICE_FP @@ -67,7 +66,7 @@ def test_two_identities_succeed(tmpdir, bob_pub_key_data): expected_bob = pEp.Identity( constants.BOB_ADDRESS, constants.BOB_NAME, '', constants.BOB_FP, 56, '' - ) + ) assert str(bob) == constants.BOB_NAME_ADDR assert bob.address == expected_bob.address @@ -88,8 +87,7 @@ def test_two_identities_succeed(tmpdir, bob_pub_key_data): assert bob.flags == 0 -def test_set_own_key(tmpdir, alice_sec_key_data): - os.environ["HOME"] = str(tmpdir) +def test_set_own_key(ctx_init, alice_sec_key_data): import pEp pEp.import_key(alice_sec_key_data) @@ -102,7 +100,7 @@ def test_set_own_key(tmpdir, alice_sec_key_data): expected_alice = pEp.Identity( constants.ALICE_ADDRESS, constants.ALICE_NAME, '', constants.ALICE_FP, 0, '' - ) + ) pEp.set_own_key(alice, alice.fpr) # assert str(alice) == constants.ALICE_NAME_ADDR diff --git a/tests/test_message.py b/tests_new/test_message.py similarity index 95% rename from tests/test_message.py rename to tests_new/test_message.py index 9787133..45fc4e4 100644 --- a/tests/test_message.py +++ b/tests_new/test_message.py @@ -30,8 +30,7 @@ def test_msg_enc_dec_roundtrip(create_alice_identity, create_bob_identity): assert str(enc_msg.from_) == constants.ALICE_NAME_ADDR assert str(enc_msg.to[0]) == constants.BOB_NAME_ADDR assert enc_msg.shortmsg == "p≡p" - assert enc_msg.longmsg == \ - "this message was encrypted with p≡p https://pEp-project.org" + assert enc_msg.longmsg == "this message was encrypted with p≡p https://pEp-project.org" # Decrypt message. dec_msg, key_list, rating, r = enc_msg.decrypt() @@ -54,7 +53,8 @@ def test_msg_enc_dec_roundtrip(create_alice_identity, create_bob_identity): # Content-Transfer-Encoding: doesn't print `quoted-printable` anymore. # Content-Disposition: is not present anymore. # `!` is not replaced by `=21` anymore. - expected_dec_lines = """From: Alice Lovelace + expected_dec_lines = \ +"""From: Alice Lovelace To: Bob Babagge Subject: This is a subject X-pEp-Version: 2.1 @@ -129,7 +129,8 @@ def test_dec_msg_len(create_alice_identity, create_bob_identity): dec_msg_len = len(str(dec_msg)) assert dec_msg.longmsg.replace("\r", "") == constants.BODY # msg.longmsg - expected_dec_msg = """From: Alice Lovelace \r + expected_dec_msg = \ +"""From: Alice Lovelace \r To: Bob Babagge \r Subject: This is a subject\r X-pEp-Version: 2.1\r diff --git a/tests/test_pep.py b/tests_new/test_pep.py similarity index 91% rename from tests/test_pep.py rename to tests_new/test_pep.py index 3e63973..79c052c 100755 --- a/tests/test_pep.py +++ b/tests_new/test_pep.py @@ -1,10 +1,10 @@ """Unit test for pEp package, not for subpackages or modules.""" -from pEp import __version__ def test_pep_version(): """ Test that __version__ is not None or empty and is not 0.0.0.""" + from pEp import __version__ # We could also test that match the regex, but that is already a test in # setuptools_scm itself. assert __version__