forked from autokey/autokey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_configmanager.py
More file actions
182 lines (154 loc) · 6.95 KB
/
Copy pathtest_configmanager.py
File metadata and controls
182 lines (154 loc) · 6.95 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Copyright (C) 2020 BlueDrink9
# 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 3 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, see <http://www.gnu.org/licenses/>.
import inspect
import json
import typing
import sys
import os
from unittest.mock import MagicMock, patch
import pytest
from hamcrest import *
from tests.engine_helpers import *
import tests.helpers_for_tests as testhelpers
from autokey.configmanager import configmanager
import autokey.model.folder as akfolder
from autokey.configmanager.configmanager import ConfigManager
from autokey.configmanager.configmanager_constants import CONFIG_DEFAULT_FOLDER
import autokey.configmanager.predefined_user_files
from autokey.service import PhraseRunner
import autokey.service
from autokey.scripting import Engine
confman_module_path = "autokey.configmanager.configmanager"
# These tests currently use the scripting API to create test phrases.
# If we can do it a better way, we probably should, to reduce dependencies for
# these tests.
# def get_autokey_dir():
# # return os.path.dirname(os.path.realpath(sys.argv[0]))
# return os.path.realpath(autokey.__file__)
dummy_config_path = testhelpers.dummy_config_path
def test_init(tmp_path):
backup = str(tmp_path / "autokey.json~")
config = str(testhelpers.make_dummy_config(tmp_path))
with \
patch(confman_module_path + ".CONFIG_DEFAULT_FOLDER",
str(tmp_path)), \
patch(confman_module_path + ".CONFIG_FILE",
config), \
patch(confman_module_path + ".CONFIG_FILE_BACKUP",
backup):
cm = ConfigManager(MagicMock())
assert_that(
ConfigManager.SETTINGS,
has_entries({"dummy_variable": "You found me!"}),
"Initialising configmanager doesn't load settings")
def test_create_default_folder(tmp_path):
default_folder = tmp_path / "autokey"
assert not default_folder.exists()
with patch(confman_module_path + ".CONFIG_DEFAULT_FOLDER",
default_folder):
configmanager.create_default_folder()
assert_that(default_folder.exists(),
"creating default config folder fails")
def test_recover_backup_config(tmp_path):
config = tmp_path / "autokey.json"
backup = tmp_path / "autokey.json~"
backup.touch()
assert not config.exists() and backup.exists()
with \
patch(confman_module_path + ".CONFIG_FILE", config), \
patch(confman_module_path + ".CONFIG_FILE_BACKUP", backup), \
patch(confman_module_path + "._restore_backup_config") as mock:
configmanager._recover_config_backup(False, Exception())
mock.assert_called_once()
# "creating default config folder fails")
def test_recover_backup_config_without_backup_raises_error(tmp_path):
config = tmp_path / "autokey.json"
backup = tmp_path / "autokey.json~"
assert not config.exists() and not backup.exists()
with \
patch(confman_module_path + ".CONFIG_FILE", config), \
patch(confman_module_path + ".CONFIG_FILE_BACKUP", backup):
with pytest.raises(OSError):
configmanager._recover_config_backup(False, OSError())
def test_back_up_config(tmp_path):
config = tmp_path / "autokey.json"
config.touch()
backup = tmp_path / "autokey.json~"
assert config.exists() and not backup.exists()
with \
patch(confman_module_path + ".CONFIG_FILE", config), \
patch(confman_module_path + ".CONFIG_FILE_BACKUP", backup):
configmanager._back_up_config()
assert_that(backup.exists(),
"Backing up config doesn't create a backup")
def test_restore_config(tmp_path):
config = tmp_path / "autokey.json"
backup = tmp_path / "autokey.json~"
backup.touch()
assert not config.exists() and backup.exists()
with \
patch(confman_module_path + ".CONFIG_FILE", config), \
patch(confman_module_path + ".CONFIG_FILE_BACKUP", backup):
configmanager._restore_backup_config()
assert_that(config.exists(),
"restoring backup config doesn't create a new config")
def test_sanitise_serializable_store_entries():
# This test is basically just for coverage.
with open(dummy_config_path, 'r') as f:
store = json.load(f)
configmanager._sanitise_serializable_store_entries(store)
def test_apply_settings():
# This test is basically just for coverage.
with open(dummy_config_path, 'r') as f:
data = json.load(f)
configmanager.apply_settings(data["settings"])
assert_that(
ConfigManager.SETTINGS,
has_entries({"dummy_variable": "You found me!"}),
"Applying settings doesn't add all entries to ConfigManager.SETTINGS")
def test_get_item_with_hotkey(create_engine):
engine, folder = create_engine
# --- Setup ---
modifiers = ["<ctrl>", "<alt>", "<super>", "<shift>"]
key = "a"
hotkey=(modifiers, key)
testHK = create_test_hotkey(engine, folder, hotkey)
resultHK = engine.configManager.get_item_with_hotkey(modifiers,
key, None)
assert_that(resultHK, is_(equal_to(testHK)))
# TODO: check multiple folders, and global/special hotkeys.
# Check for similar tests in test_engine.
def test_item_has_same_hotkey(create_engine):
engine, folder = create_engine
modifiers = ["<ctrl>", "<alt>", "<super>", "<shift>"]
key = "a"
hotkey=(modifiers, key)
testHK = create_test_hotkey(engine, folder, hotkey)
assert ConfigManager.item_has_same_hotkey(testHK, modifiers, key, None)
def test_get_all_folders(create_engine):
engine, folder = create_engine
cm = engine.configManager
first_child = akfolder.Folder("first child")
first_grandchild = akfolder.Folder("first grandchild")
second_grandchild = akfolder.Folder("second grandchild")
first_child.add_folder(first_grandchild)
first_child.add_folder(second_grandchild)
cm.folders.append(first_child)
expected = [folder, first_child, first_grandchild, second_grandchild]
result = cm.get_all_folders()
assert_that(result, equal_to(expected))
def test_create_predefined_user_files_my_phrases_folder(create_engine):
engine, folder = create_engine
# --- Setup ---
os.makedirs(CONFIG_DEFAULT_FOLDER, exist_ok=True)
phrases_folder = autokey.configmanager.predefined_user_files.create_my_phrases_folder()
scripts_folder = autokey.configmanager.predefined_user_files.create_sample_scripts_folder()