From c09f739c4a50992e0474dfbfbe1ac5c23e877a64 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Mon, 27 Jul 2026 07:01:35 -0400 Subject: [PATCH] don't let behave tests refer to HOME resources * don't refer to a logfile in $HOME, moving it to a tempfile * don't refer to a .mylogin.cnf in $HOME, removing all references Ideally $HOME would be set to a temporary directory. --- changelog.md | 1 + test/features/environment.py | 22 ++++------------------ 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/changelog.md b/changelog.md index bf55757c..ac023725 100644 --- a/changelog.md +++ b/changelog.md @@ -10,6 +10,7 @@ Features Internal --------- * Add Trove classifiers for PyPi. +* Don't let behave tests refer to real home-directory resources. 2.7.0 (2026/07/25) diff --git a/test/features/environment.py b/test/features/environment.py index 3b17cf51..b61ed0b9 100644 --- a/test/features/environment.py +++ b/test/features/environment.py @@ -12,16 +12,12 @@ from mycli.constants import DEFAULT_HOST, DEFAULT_PORT, DEFAULT_USER from steps.wrappers import run_cli, wait_prompt -test_log_file = os.path.join(os.environ["HOME"], ".mycli.test.log") - +fd, TEST_LOG_FILE = tempfile.mkstemp(prefix='mycli-behave-', suffix='.test.log') +os.close(fd) SELF_CONNECTING_FEATURES = ("test/features/connection.feature",) -MYLOGIN_CNF_PATH = os.path.expanduser("~/.mylogin.cnf") -MYLOGIN_CNF_BACKUP_PATH = f"{MYLOGIN_CNF_PATH}.backup" - - def get_db_name_from_context(context): return context.config.userdata.get("my_test_db", None) or "mycli_behave_tests" @@ -111,19 +107,16 @@ def before_scenario(context, arg): # Skip flaky editor test in CI if os.getenv('GITHUB_ACTION') and 'skip_ci' in arg.tags: arg.skip('Skipped in CI') - with open(test_log_file, "w") as f: + with open(TEST_LOG_FILE, "w") as f: f.write("") if arg.location.filename not in SELF_CONNECTING_FEATURES: run_cli(context) wait_prompt(context) - if os.path.exists(MYLOGIN_CNF_PATH): - shutil.move(MYLOGIN_CNF_PATH, MYLOGIN_CNF_BACKUP_PATH) - def after_scenario(context, _): """Cleans up after each test complete.""" - with open(test_log_file) as f: + with open(TEST_LOG_FILE) as f: for line in f: if "error" in line.lower(): raise RuntimeError(f"Error in log file: {line}") @@ -139,13 +132,6 @@ def after_scenario(context, _): context.cli.sendcontrol("d") context.cli.expect_exact(pexpect.EOF, timeout=5) - if os.path.exists(MYLOGIN_CNF_BACKUP_PATH): - shutil.move(MYLOGIN_CNF_BACKUP_PATH, MYLOGIN_CNF_PATH) - elif os.path.exists(MYLOGIN_CNF_PATH): - # This file was moved in `before_scenario`. - # If it exists now, it has been created during a test - os.remove(MYLOGIN_CNF_PATH) - # TODO: uncomment to debug a failure # def after_step(context, step):