config: make sure running the unittests can never mutate prod config - #10884
Open
SomberNight wants to merge 1 commit into
Open
SomberNight wants to merge 1 commit into
SomberNight wants to merge 1 commit into
Conversation
Member
|
Might be nice if regtest tests also have some fallback, the This might also work and gets passed on to the regtests without requiring modifications to diff --git a/tests/__init__.py b/tests/__init__.py
index 1dc6c680ae..dc64d4228d 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -1,4 +1,4 @@
-import asyncio
+import atexit
import os
import unittest
import threading
@@ -30,7 +30,8 @@ FAST_TESTS = False
electrum.logging._configure_stderr_logging(verbosity="*")
electrum.util.AS_LIB_USER_I_WANT_TO_MANAGE_MY_OWN_ASYNCIO_LOOP = True
-electrum.simple_config._RUNNING_UNITTESTS = True
+os.environ["ELECTRUMDIR"] = _unittests_datadir = tempfile.mkdtemp(prefix="electrum-unittests-datadir-")
+atexit.register(lambda: shutil.rmtree(_unittests_datadir, ignore_errors=True))
class ElectrumTestCase(unittest.IsolatedAsyncioTestCase, Logger): |
ecdsa
reviewed
Sep 9, 2026
| make_dir(path, allow_symlink=False) | ||
| if _RUNNING_UNITTESTS: | ||
| path = os.path.join(path, "unittests") | ||
| make_dir(path, allow_symlink=False) |
Member
There was a problem hiding this comment.
I do not like the multiplication of paths.
#10833 introduced tempfile.mkdtemp(prefix="electrum-unittest-base-")
Why not assert here that electrum_path has been set and is starting with that prefix?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
related: #10883
Running the unittests must never mutate the mainnet/production config file (or other files in the datadir) of a dev machine. I am not sure how exactly to prevent that, but this is one approach.
The idea is that a test case constructing e.g. a config file should use a tempfolder for it (and then clean-up after itself), however in case that is accidentally not done, we should have a belt-and-suspenders last-resort protection. In this PR I propose using e.g.
/home/user/.electrum/unittests/configinstead of/home/user/.electrum/config: we always know we are in a unittest context, and if so, we add an extra level of nesting.