From: Simon Chopin <schopin@ubuntu.com>
Date: Thu, 14 Aug 2025 11:34:07 +0200
Subject: tests: change the default setting path to work around pybuild

Sadly, pybuild doesn't preserve tree structure when copying directories,
so we work around this by changing the various DEFAULTSPATH constants in
the relevant tests to point to the right one.

This could be dropped in favour of https://salsa.debian.org/python-team/tools/dh-python/-/merge_requests/60
if that ever goes in.

Forwarded: not-needed
---
 alot/addressbook/abook.py       |  3 +--
 alot/settings/manager.py        |  4 ++++
 tests/addressbook/test_abook.py |  3 +++
 tests/commands/test_envelope.py |  1 +
 tests/db/test_manager.py        |  1 +
 tests/settings/test_manager.py  |  1 +
 tests/settings/test_theme.py    |  3 +++
 tests/utilities.py              | 10 ++++++++++
 8 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/alot/addressbook/abook.py b/alot/addressbook/abook.py
index 5cdfcf2..77bb65c 100644
--- a/alot/addressbook/abook.py
+++ b/alot/addressbook/abook.py
@@ -5,6 +5,7 @@ import os
 from . import AddressBook
 from ..settings.utils import read_config
 
+DEFAULTSPATH = os.path.join(os.path.dirname(__file__), '..', 'defaults')
 
 class AbookAddressBook(AddressBook):
     """:class:`AddressBook` that parses abook's config/database files"""
@@ -14,8 +15,6 @@ class AbookAddressBook(AddressBook):
         :type path: str
         """
         AddressBook.__init__(self, **kwargs)
-        DEFAULTSPATH = os.path.join(os.path.dirname(__file__), '..',
-                                    'defaults')
         self._spec = os.path.join(DEFAULTSPATH, 'abook_contacts.spec')
         path = os.path.expanduser(path)
         self._config = read_config(path, self._spec)
diff --git a/alot/settings/manager.py b/alot/settings/manager.py
index b903668..a7eb0f9 100644
--- a/alot/settings/manager.py
+++ b/alot/settings/manager.py
@@ -23,6 +23,10 @@ from .theme import Theme
 
 
 DEFAULTSPATH = os.path.join(os.path.dirname(__file__), '..', 'defaults')
+# Workaround for pybuild not copying data files properly
+if not os.path.exists(DEFAULTSPATH):
+    DEFAULTSPATH = os.path.join(os.path.dirname(__file__), '..', '..', 'defaults')
+
 DATA_DIRS = get_xdg_env('XDG_DATA_DIRS',
                         '/usr/local/share:/usr/share').split(':')
 
diff --git a/tests/addressbook/test_abook.py b/tests/addressbook/test_abook.py
index 13452fb..ce52d6c 100644
--- a/tests/addressbook/test_abook.py
+++ b/tests/addressbook/test_abook.py
@@ -8,6 +8,9 @@ import unittest
 from alot.addressbook import abook
 from alot.settings.errors import ConfigError
 
+from .. import utilities
+utilities.fix_default_paths()
+
 
 class TestAbookAddressBook(unittest.TestCase):
 
diff --git a/tests/commands/test_envelope.py b/tests/commands/test_envelope.py
index e15d1b4..3aaa2ac 100644
--- a/tests/commands/test_envelope.py
+++ b/tests/commands/test_envelope.py
@@ -31,6 +31,7 @@ from alot.settings.manager import SettingsManager
 from alot.account import Account
 
 from .. import utilities
+utilities.fix_default_paths()
 
 # When using an assert from a mock a TestCase method might not use self. That's
 # okay.
diff --git a/tests/db/test_manager.py b/tests/db/test_manager.py
index 554db6f..bf3a978 100644
--- a/tests/db/test_manager.py
+++ b/tests/db/test_manager.py
@@ -15,6 +15,7 @@ from alot.settings.const import settings
 from notmuch2 import Database
 
 from .. import utilities
+utilities.fix_default_paths()
 
 
 class TestDBManager(utilities.TestCaseClassCleanup):
diff --git a/tests/settings/test_manager.py b/tests/settings/test_manager.py
index 6548ee8..92a6b3c 100644
--- a/tests/settings/test_manager.py
+++ b/tests/settings/test_manager.py
@@ -15,6 +15,7 @@ from alot.settings.manager import SettingsManager
 from alot.settings.errors import ConfigError, NoMatchingAccount
 
 from .. import utilities
+utilities.fix_default_paths()
 
 
 class TestSettingsManager(unittest.TestCase):
diff --git a/tests/settings/test_theme.py b/tests/settings/test_theme.py
index 2513919..e0bcddd 100644
--- a/tests/settings/test_theme.py
+++ b/tests/settings/test_theme.py
@@ -6,6 +6,9 @@ import unittest
 
 from alot.settings import theme
 
+from .. import utilities
+utilities.fix_default_paths()
+
 
 DUMMY_THEME = """\
 [bufferlist]
diff --git a/tests/utilities.py b/tests/utilities.py
index 671fe57..22327f3 100644
--- a/tests/utilities.py
+++ b/tests/utilities.py
@@ -23,6 +23,16 @@ from unittest import mock
 
 import gpg
 
+import os.path
+def fix_default_paths():
+    from alot.settings import manager, theme
+    from alot.addressbook import abook
+    if not os.path.exists(manager.DEFAULTSPATH):
+        manager.DEFAULTSPATH = os.path.join(os.path.dirname(__file__), '..', 'defaults')
+    if not os.path.exists(theme.DEFAULTSPATH):
+        theme.DEFAULTSPATH = os.path.join(os.path.dirname(__file__), '..', 'defaults')
+    if not os.path.exists(abook.DEFAULTSPATH):
+        abook.DEFAULTSPATH = os.path.join(os.path.dirname(__file__), '..', 'defaults')
 
 def _tear_down_class_wrapper(original, cls):
     """Ensure that doClassCleanups is called after tearDownClass."""
