From 76f6a3407eb1e8ecca70a8f979473cd34cfd5b1b Mon Sep 17 00:00:00 2001 From: Frederic Leger Date: Thu, 19 Oct 2023 12:03:51 +0200 Subject: [PATCH] Fix for rlimit foreground test. Using the `TERM=dummy` environment fixed the prompt coloring issue. Some minor changes to the test file itself and `setup.py` to avoid IDE warnings. --- setup.py | 19 ++++++++++++++- tests/tests_e3/os/process/main_test.py | 33 ++++++++++++++++---------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/setup.py b/setup.py index 5e7e88cd..9af29248 100644 --- a/setup.py +++ b/setup.py @@ -1,3 +1,5 @@ +import sys + from setuptools import setup, find_packages import os @@ -20,9 +22,24 @@ # There are some backward incompatible checks in typeguard 3.x "typeguard<3.0.0", ], - "test": ["mock", "pytest-html", "pytest-socket", "ansi2html", "httpretty"], + "test": [ + "mock", + "pytest", + "pytest-html", + "pytest-socket", + "ansi2html", + "httpretty", + "ptyprocess", + "psutil", + ], } +# Some IDEs are able to detect that packages are not in the setup.py. Still +# adding the package afterward does not work. Better set it in the default +# list, and remove it if needed. +if sys.platform == "win32": + extras_require["test"].remove("ptyprocess") + for p in ("darwin", "linux", "linux2", "win32"): platform_string = ":sys_platform=='%s'" % p extras_require[platform_string] = ["psutil"] diff --git a/tests/tests_e3/os/process/main_test.py b/tests/tests_e3/os/process/main_test.py index bf2a80d3..660c3520 100644 --- a/tests/tests_e3/os/process/main_test.py +++ b/tests/tests_e3/os/process/main_test.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import os import sys import subprocess @@ -107,7 +109,9 @@ def run_test(): if sys.platform == "win32": # On Windows make sure that rlimit works when # setting the build environment to 64bit windows - e = e3.env.Env() + from e3.env import Env + + e = Env() e.store() e.set_build("x86_64-windows") run_test() @@ -205,6 +209,9 @@ def test_rlimit_foreground_option(): # Test with --foreground os.environ["PS1"] = "$ " + # Use TERM=dummy to avoid prompt coloring to interfere with the result + # string. + os.environ["TERM"] = "dummy" p = PtyProcess.spawn( [e3.os.process.get_rlimit(), "--foreground", "30", "bash", "--norc", "-i"], env=os.environ, @@ -216,7 +223,7 @@ def test_rlimit_foreground_option(): # Ensure that the process is killed p.kill(signal.SIGKILL) - # Test without foreground (Should failed) + # Test without foreground (Should fail) p = PtyProcess.spawn( [e3.os.process.get_rlimit(), "30", "bash", "--norc", "-i"], env=os.environ, @@ -391,7 +398,7 @@ def test_is_running(): assert e3.os.process.is_running(p.pid) p.kill(recursive=False) - # On windows we don't want to wait as otherwise pid will be reused + # On windows, we don't want to wait as otherwise pid will be reused # Note also that the semantic is slightly different between Unix # and Windows. is_running will report false on Windows once the # process is in a waitable state. @@ -408,7 +415,9 @@ def test_is_running_non_existant(): pid_list = psutil.pids() pid_list.sort() - # Try to found a non existing process + running: bool = True + + # Try to found a non-existing process for a in range(1, 1000): running = e3.os.process.is_running(pid_list[-1] + a) if not running: @@ -506,14 +515,12 @@ def get_one_child(idx): def test_run_with_env(): os.environ["EXT_VAR"] = "bar" - cmd = ( - [ - sys.executable, - "-c", - 'import os; print(os.environ.get("TEST_RUN_VAR")' - ' + os.environ.get("EXT_VAR", "world"))', - ], - ) + cmd = [ + sys.executable, + "-c", + 'import os; print(os.environ.get("TEST_RUN_VAR")' + ' + os.environ.get("EXT_VAR", "world"))', + ] p1 = e3.os.process.Run(cmd, env={"TEST_RUN_VAR": "foo"}, ignore_environ=False) assert p1.out.strip() == "foobar" @@ -538,7 +545,7 @@ def test_no_rlimit(caplog): def test_shell_override(): """Unix shell shebang handling. - On windows we ensure that /bin/bash /bin/sh shebangs are replaced by + On windows, we ensure that /bin/bash /bin/sh shebangs are replaced by SHELL env var. """ work_dir = os.getcwd()