commit: 4ba1cd651c648a691665359e36eee71c61fc370e Author: David Palao <david.palao <AT> gmail <DOT> com> AuthorDate: Fri Apr 14 15:21:41 2023 +0000 Commit: Sam James <sam <AT> gentoo <DOT> org> CommitDate: Fri May 26 15:44:35 2023 +0000 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=4ba1cd65
tests: pytest: added session scoped autoused fixture to prepare env This reduces the number of tests failing with pytest from 53 to 11. Signed-off-by: David Palao <david.palao <AT> gmail.com> Signed-off-by: Sam James <sam <AT> gentoo.org> lib/portage/tests/conftest.py | 89 +++++++++++++++++++++++++++++++++++++++++++ lib/portage/tests/runTests.py | 76 ------------------------------------ 2 files changed, 89 insertions(+), 76 deletions(-) diff --git a/lib/portage/tests/conftest.py b/lib/portage/tests/conftest.py new file mode 100644 index 000000000..1913d1f06 --- /dev/null +++ b/lib/portage/tests/conftest.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python +# runTests.py -- Portage Unit Test Functionality +# Copyright 2006-2023 Gentoo Authors +# Distributed under the terms of the GNU General Public License v2 + +import grp +import os +import os.path as osp +import pwd +import signal +import tempfile +import shutil +import sys +from distutils.dir_util import copy_tree + +import pytest + +import portage +from portage.util._eventloop.global_event_loop import global_event_loop +from portage.const import PORTAGE_BIN_PATH + + +def debug_signal(signum, frame): + import pdb + + pdb.set_trace() + + +signal.signal(signal.SIGUSR1, debug_signal) + + [email protected](autouse=True, scope="session") +def prepare_environment(): + # Pretend that the current user's uid/gid are the 'portage' uid/gid, + # so things go smoothly regardless of the current user and global + # user/group configuration. + os.environ["PORTAGE_USERNAME"] = pwd.getpwuid(os.getuid()).pw_name + os.environ["PORTAGE_GRPNAME"] = grp.getgrgid(os.getgid()).gr_name + + # Insert our parent dir so we can do shiny import "tests" + # This line courtesy of Marienz and Pkgcore ;) + sys.path.insert(0, osp.dirname(osp.dirname(osp.dirname(osp.realpath(__file__))))) + + portage._internal_caller = True + + # Ensure that we don't instantiate portage.settings, so that tests should + # work the same regardless of global configuration file state/existence. + portage._disable_legacy_globals() + + if portage.util.no_color(os.environ): + portage.output.nocolor() + + # import portage.tests as tests + + path = os.environ.get("PATH", "").split(":") + path = [x for x in path if x] + + insert_bin_path = True + try: + insert_bin_path = not path or not os.path.samefile(path[0], PORTAGE_BIN_PATH) + except OSError: + pass + + if insert_bin_path: + path.insert(0, PORTAGE_BIN_PATH) + os.environ["PATH"] = ":".join(path) + + # Copy GPG test keys to temporary directory + gpg_path = tempfile.mkdtemp(prefix="gpg_") + + copy_tree( + os.path.join(os.path.dirname(os.path.realpath(__file__)), ".gnupg"), gpg_path + ) + + os.chmod(gpg_path, 0o700) + os.environ["PORTAGE_GNUPGHOME"] = gpg_path + + yield + + global_event_loop().close() + shutil.rmtree(gpg_path, ignore_errors=True) + + +# if __name__ == "__main__": +# try: +# sys.exit(tests.main()) +# finally: +# global_event_loop().close() +# shutil.rmtree(gpg_path, ignore_errors=True) diff --git a/lib/portage/tests/runTests.py b/lib/portage/tests/runTests.py deleted file mode 100755 index bf4c2a7c5..000000000 --- a/lib/portage/tests/runTests.py +++ /dev/null @@ -1,76 +0,0 @@ -#!/usr/bin/env python -# runTests.py -- Portage Unit Test Functionality -# Copyright 2006-2022 Gentoo Authors -# Distributed under the terms of the GNU General Public License v2 - -import grp -import os -import os.path as osp -import pwd -import signal -import tempfile -import shutil -import sys -from distutils.dir_util import copy_tree - - -def debug_signal(signum, frame): - import pdb - - pdb.set_trace() - - -signal.signal(signal.SIGUSR1, debug_signal) - -# Pretend that the current user's uid/gid are the 'portage' uid/gid, -# so things go smoothly regardless of the current user and global -# user/group configuration. -os.environ["PORTAGE_USERNAME"] = pwd.getpwuid(os.getuid()).pw_name -os.environ["PORTAGE_GRPNAME"] = grp.getgrgid(os.getgid()).gr_name - -# Insert our parent dir so we can do shiny import "tests" -# This line courtesy of Marienz and Pkgcore ;) -sys.path.insert(0, osp.dirname(osp.dirname(osp.dirname(osp.realpath(__file__))))) - -import portage - -portage._internal_caller = True - -# Ensure that we don't instantiate portage.settings, so that tests should -# work the same regardless of global configuration file state/existence. -portage._disable_legacy_globals() - -if portage.util.no_color(os.environ): - portage.output.nocolor() - -import portage.tests as tests -from portage.util._eventloop.global_event_loop import global_event_loop -from portage.const import PORTAGE_BIN_PATH - -path = os.environ.get("PATH", "").split(":") -path = [x for x in path if x] - -insert_bin_path = True -try: - insert_bin_path = not path or not os.path.samefile(path[0], PORTAGE_BIN_PATH) -except OSError: - pass - -if insert_bin_path: - path.insert(0, PORTAGE_BIN_PATH) - os.environ["PATH"] = ":".join(path) - -# Copy GPG test keys to temporary directory -gpg_path = tempfile.mkdtemp(prefix="gpg_") - -copy_tree(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".gnupg"), gpg_path) - -os.chmod(gpg_path, 0o700) -os.environ["PORTAGE_GNUPGHOME"] = gpg_path - -if __name__ == "__main__": - try: - sys.exit(tests.main()) - finally: - global_event_loop().close() - shutil.rmtree(gpg_path, ignore_errors=True)
