forked from metabrainz/picard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicardtestcase.py
149 lines (120 loc) · 4.19 KB
/
picardtestcase.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
138
139
140
141
142
143
144
145
146
147
148
149
# -*- coding: utf-8 -*-
#
# Picard, the next-generation MusicBrainz tagger
#
# Copyright (C) 2018 Wieland Hoffmann
# Copyright (C) 2019-2024 Philipp Wolfer
# Copyright (C) 2020-2021 Laurent Monin
# Copyright (C) 2021 Bob Swift
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import json
import logging
import os
import shutil
import struct
from tempfile import (
mkdtemp,
mkstemp,
)
import unittest
from unittest.mock import (
MagicMock,
Mock,
)
from PyQt6 import QtCore
from picard import (
config,
log,
)
from picard.i18n import setup_gettext
from picard.releasegroup import ReleaseGroup
class FakeThreadPool(QtCore.QObject):
def start(self, runnable, priority):
runnable.run()
class FakeTagger(QtCore.QObject):
tagger_stats_changed = QtCore.pyqtSignal()
def __init__(self):
super().__init__()
self.tagger_stats_changed.connect(self.emit)
self.exit_cleanup = []
self.files = {}
self.stopping = False
self.thread_pool = FakeThreadPool()
self.priority_thread_pool = FakeThreadPool()
self.window = MagicMock()
self.webservice = MagicMock()
def register_cleanup(self, func):
self.exit_cleanup.append(func)
def run_cleanup(self):
for f in self.exit_cleanup:
f()
def emit(self, *args):
pass
def get_release_group_by_id(self, rg_id): # pylint: disable=no-self-use
return ReleaseGroup(rg_id)
class PicardTestCase(unittest.TestCase):
def setUp(self):
super().setUp()
log.set_level(logging.DEBUG)
setup_gettext(None, 'C')
self.tagger = FakeTagger()
QtCore.QCoreApplication.instance = lambda: self.tagger
self.addCleanup(self.tagger.run_cleanup)
self.init_config()
@staticmethod
def init_config():
fake_config = Mock()
fake_config.setting = {}
fake_config.persist = {}
fake_config.profiles = {}
# Make config object available for legacy use
config.config = fake_config
config.setting = fake_config.setting
config.persist = fake_config.persist
config.profiles = fake_config.profiles
@staticmethod
def set_config_values(setting=None, persist=None, profiles=None):
if setting:
for key, value in setting.items():
config.config.setting[key] = value
if persist:
for key, value in persist.items():
config.config.persist[key] = value
if profiles:
for key, value in profiles.items():
config.config.profiles[key] = value
def mktmpdir(self, ignore_errors=False):
tmpdir = mkdtemp(suffix=self.__class__.__name__)
self.addCleanup(shutil.rmtree, tmpdir, ignore_errors=ignore_errors)
return tmpdir
def copy_file_tmp(self, filepath, ext):
fd, copy = mkstemp(suffix=ext)
os.close(fd)
self.addCleanup(self.remove_file_tmp, copy)
shutil.copy(filepath, copy)
return copy
@staticmethod
def remove_file_tmp(filepath):
if os.path.isfile(filepath):
os.unlink(filepath)
def get_test_data_path(*paths):
return os.path.join('test', 'data', *paths)
def create_fake_png(extra):
"""Creates fake PNG data that satisfies Picard's internal image type detection"""
return b'\x89PNG\x0D\x0A\x1A\x0A' + (b'a' * 4) + b'IHDR' + struct.pack('>LL', 100, 100) + extra
def load_test_json(filename):
with open(get_test_data_path('ws_data', filename), encoding='utf-8') as f:
return json.load(f)