forked from e1miran/Now-Playing-Serato
-
Notifications
You must be signed in to change notification settings - Fork 11
/
conftest.py
executable file
·137 lines (116 loc) · 4.29 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
''' pytest fixtures '''
import contextlib
import logging
import os
import pathlib
import shutil
import sys
import tempfile
import unittest.mock
import pytest
from PySide6.QtCore import QCoreApplication, QSettings, QStandardPaths # pylint: disable=import-error, no-name-in-module
import nowplaying.bootstrap
import nowplaying.config
import nowplaying.db
# if sys.platform == 'darwin':
# import psutil
# import pwd
# DO NOT CHANGE THIS TO BE com.github.whatsnowplaying
# otherwise your actual bits will disappear!
DOMAIN = 'com.github.whatsnowplaying.testsuite'
try:
from pytest_cov.embed import cleanup_on_sigterm
except ImportError:
pass
else:
cleanup_on_sigterm()
def reboot_macosx_prefs():
''' work around Mac OS X's preference caching '''
if sys.platform == 'darwin':
os.system(f'defaults delete {DOMAIN}')
#
# old method:
#
# for process in psutil.process_iter():
# try:
# if 'cfprefsd' in process.name() and pwd.getpwuid(
# os.getuid()).pw_name == process.username():
# process.terminate()
# process.wait()
# except psutil.NoSuchProcess:
# pass
@pytest.fixture
def getroot(pytestconfig):
''' get the base of the source tree '''
return pytestconfig.rootpath
@pytest.fixture
def bootstrap(getroot): # pylint: disable=redefined-outer-name
''' bootstrap a configuration '''
with contextlib.suppress(PermissionError): # Windows blows
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as newpath:
dbinit_patch = unittest.mock.patch('nowplaying.db.MetadataDB.init_db_var')
dbinit_mock = dbinit_patch.start()
dbdir = pathlib.Path(newpath).joinpath('mdb')
dbdir.mkdir()
dbfile = dbdir.joinpath('test.db')
dbinit_mock.return_value = dbfile
with unittest.mock.patch.dict(os.environ, {
"WNP_CONFIG_TEST_DIR": str(newpath),
"WNP_METADB_TEST_FILE": str(dbfile)
}):
rmdir = newpath
bundledir = pathlib.Path(getroot).joinpath('nowplaying')
nowplaying.bootstrap.set_qt_names(domain=DOMAIN, appname='testsuite')
config = nowplaying.config.ConfigFile(bundledir=bundledir,
logpath=newpath,
testmode=True)
config.cparser.setValue('acoustidmb/enabled', False)
config.cparser.sync()
config.testdir = pathlib.Path(newpath)
yield config
dbinit_mock.stop()
if pathlib.Path(rmdir).exists():
shutil.rmtree(rmdir)
#
# OS X has a lot of caching wrt preference files
# so we have do a lot of work to make sure they
# don't stick around
#
@pytest.fixture(autouse=True, scope="function")
def clear_old_testsuite():
''' clear out old testsuite configs '''
if sys.platform == "win32":
qsettingsformat = QSettings.IniFormat
else:
qsettingsformat = QSettings.NativeFormat
nowplaying.bootstrap.set_qt_names(appname='testsuite')
config = QSettings(qsettingsformat, QSettings.SystemScope, QCoreApplication.organizationName(),
QCoreApplication.applicationName())
config.clear()
config.sync()
cachedir = pathlib.Path(QStandardPaths.standardLocations(QStandardPaths.CacheLocation)[0])
if 'testsuite' in cachedir.name and cachedir.exists():
logging.info('Removing %s', cachedir)
shutil.rmtree(cachedir)
config = QSettings(qsettingsformat, QSettings.UserScope, QCoreApplication.organizationName(),
QCoreApplication.applicationName())
config.clear()
config.sync()
filename = pathlib.Path(config.fileName())
del config
if filename.exists():
filename.unlink()
reboot_macosx_prefs()
if filename.exists():
filename.unlink()
reboot_macosx_prefs()
if filename.exists():
logging.error('Still exists, wtf?')
yield filename
if filename.exists():
filename.unlink()
reboot_macosx_prefs()
if filename.exists():
filename.unlink()
reboot_macosx_prefs()