From f0349fcc5461f916890ff273fa35fa97fda4cef7 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sat, 7 May 2022 02:24:18 +0200 Subject: [PATCH] check that borg does not require pytest for normal usage, fixes #6563 also: move the note about this to the very top of the affected modules. --- src/borg/selftest.py | 8 ++++++++ src/borg/testsuite/chunker.py | 6 +++--- src/borg/testsuite/crypto.py | 3 --- src/borg/testsuite/hashindex.py | 6 +++--- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/borg/selftest.py b/src/borg/selftest.py index dfe7abb54..ec83cf397 100644 --- a/src/borg/selftest.py +++ b/src/borg/selftest.py @@ -1,3 +1,6 @@ +# Note: these tests are part of the self test, do not use or import pytest functionality here. +# See borg.selftest for details. If you add/remove test methods, update SELFTEST_COUNT + """ Self testing module =================== @@ -12,6 +15,7 @@ To assert that self test discovery works correctly the number of tests is kept i variable. SELFTEST_COUNT must be updated if new tests are added or removed to or from any of the tests used here. """ + import os import sys import time @@ -62,6 +66,10 @@ def selftest(logger): result = SelfTestResult() test_suite = TestSuite() for test_case in SELFTEST_CASES: + module = sys.modules[test_case.__module__] + # a normal borg user does not have pytest installed, we must not require it in the test modules used here. + # note: this only detects the usual toplevel import + assert 'pytest' not in dir(module), "pytest must not be imported in %s" % module.__name__ test_suite.addTest(defaultTestLoader.loadTestsFromTestCase(test_case)) test_suite.run(result) result.log_results(logger) diff --git a/src/borg/testsuite/chunker.py b/src/borg/testsuite/chunker.py index 2a14bd604..d308b33f2 100644 --- a/src/borg/testsuite/chunker.py +++ b/src/borg/testsuite/chunker.py @@ -1,12 +1,12 @@ +# Note: these tests are part of the self test, do not use or import pytest functionality here. +# See borg.selftest for details. If you add/remove test methods, update SELFTEST_COUNT + from io import BytesIO from ..chunker import Chunker, buzhash, buzhash_update from ..constants import * # NOQA from . import BaseTestCase -# Note: these tests are part of the self test, do not use or import py.test functionality here. -# See borg.selftest for details. If you add/remove test methods, update SELFTEST_COUNT - class ChunkerTestCase(BaseTestCase): diff --git a/src/borg/testsuite/crypto.py b/src/borg/testsuite/crypto.py index 6406064d1..0db480656 100644 --- a/src/borg/testsuite/crypto.py +++ b/src/borg/testsuite/crypto.py @@ -5,9 +5,6 @@ from ..crypto.low_level import increment_iv, bytes16_to_int, int_to_bytes16 from ..crypto.low_level import hkdf_hmac_sha512 from . import BaseTestCase -# Note: these tests are part of the self test, do not use or import py.test functionality here. -# See borg.selftest for details. If you add/remove test methods, update SELFTEST_COUNT - class CryptoTestCase(BaseTestCase): diff --git a/src/borg/testsuite/hashindex.py b/src/borg/testsuite/hashindex.py index 0c8634ad0..6ef149730 100644 --- a/src/borg/testsuite/hashindex.py +++ b/src/borg/testsuite/hashindex.py @@ -1,3 +1,6 @@ +# Note: these tests are part of the self test, do not use or import pytest functionality here. +# See borg.selftest for details. If you add/remove test methods, update SELFTEST_COUNT + import base64 import hashlib import io @@ -10,9 +13,6 @@ from .. import hashindex from ..crypto.file_integrity import IntegrityCheckedFile, FileIntegrityError from . import BaseTestCase -# Note: these tests are part of the self test, do not use or import py.test functionality here. -# See borg.selftest for details. If you add/remove test methods, update SELFTEST_COUNT - def H(x): # make some 32byte long thing that depends on x