Skip to content

config: make sure running the unittests can never mutate prod config - #10884

Open
SomberNight wants to merge 1 commit into
spesmilo:masterfrom
SomberNight:202608_unittests_config_2
Open

SomberNight wants to merge 1 commit into
spesmilo:masterfrom
SomberNight:202608_unittests_config_2

Conversation

@SomberNight

Copy link
Copy Markdown
Member

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/config instead of /home/user/.electrum/config: we always know we are in a unittest context, and if so, we add an extra level of nesting.

@SomberNight SomberNight added topic-config related to how Electrum stores config options; e.g. simple_config.py testing 🔎✅ Unit- and Regtests labels Aug 21, 2026
@f321x

f321x commented Aug 26, 2026

Copy link
Copy Markdown
Member

Might be nice if regtest tests also have some fallback, the unixsocket test for example writes into the ~/.electrum/regtest dir.

This might also work and gets passed on to the regtests without requiring modifications to SimpleConfig or writing in the user dir at all. Otoh setting an env variable might be more fragile?

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):

Comment thread electrum/simple_config.py
make_dir(path, allow_symlink=False)
if _RUNNING_UNITTESTS:
path = os.path.join(path, "unittests")
make_dir(path, allow_symlink=False)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing 🔎✅ Unit- and Regtests topic-config related to how Electrum stores config options; e.g. simple_config.py

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants