-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathnut_config_test.py
346 lines (280 loc) · 13.4 KB
/
nut_config_test.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import os
import unittest
from pathlib import Path
from pyfakefs.fake_filesystem_unittest import TestCase
from nut import Nsps, Print
from nut import Config
from nut.config_impl.download import Download
Print.enableDebug = True
def _get_default_config_object():
return {'paths': {'titleBase': 'titles/{name}[{id}][v{version}].nsp', 'titleDLC':
'titles/DLC/{name}[{id}][v{version}].nsp', 'titleUpdate':
'titles/updates/{name}[{id}][v{version}].nsp', 'titleDemo':
'titles/demos/{name}[{id}][v{version}].nsp', 'titleDemoUpdate':
'titles/demos/updates/{name}[{id}][v{version}].nsp', 'nsxTitleBase': None,
'nsxTitleDLC': None, 'nsxTitleUpdate': None, 'nsxTitleDemo': None,
'nsxTitleDemoUpdate': None, 'nszTitleBase': None, 'nszTitleDLC': None,
'nszTitleUpdate': None, 'nszTitleDemo': None, 'nszTitleDemoUpdate': None,
'xciTitleBase': None, 'xciTitleDLC': None, 'xciTitleUpdate': None, 'xciTitleDemo': None,
'xciTitleDemoUpdate': None, 'scan': ['.'], 'titleDatabase': 'titledb',
'keys': 'keys.txt', 'calibration': 'PRODINFO.bin', 'shopNCert': 'ShopN.pem',
'nspOut': '_NSPOUT', 'titleImages': 'titles/images/', 'duplicates': 'duplicates/'},
'compression': {'level': 19, 'auto': False}, 'pullUrls': [], 'threads': 1, 'download':
{'base': True, 'demo': False, 'DLC': True, 'update': False,
'sansTitleKey': False, 'deltas': False, 'regions': [], 'rankMin': None, 'rankMax': None,
'fileSizeMax': None, 'fileSizeMin': None, 'ratingMin': None, 'ratingMax': None,
'releaseDateMin': None, 'releaseDateMax': None, 'mtime_min': None, 'mtime_max': None},
'server': {'hostname': '0.0.0.0', 'port': 9000},
'autolaunchBrowser': True, 'autoUpdateTitleDb': True, 'allowNoMetadata': True}
def _get_default_config_path():
return 'conf/nut.conf'
def _create_empty_config_file(fs):
conf_file = _get_default_config_path()
fs.create_file(conf_file)
return conf_file
def _create_files(fs, folder_obj):
for f in folder_obj["files"]:
fs.create_file(os.path.join(folder_obj["path"], f))
def _get_default_languages():
return json.loads(Config.DEFAULT_REGION_LANGUAGES)
class NutConfigTest(TestCase):
"""Tests for nut/Config.py
"""
def setUp(self):
self.setUpPyfakefs(modules_to_reload=[Config])
def test_default_region(self):
self.assertEqual(Config.region, 'US')
def test_default_language(self):
self.assertEqual(Config.language, 'en')
def test_set(self):
paths = Config.Paths()
server = Config.Server()
j = {}
Config.jset(j, ['paths', 'scan'], paths.scan)
Config.jset(j, ['server', 'hostname'], server.hostname)
Config.jset(j, ['server', 'port'], server.port)
self.assertEqual(j['paths']['scan'], paths.scan)
self.assertEqual(j['server']['hostname'], server.hostname)
self.assertEqual(j['server']['port'], server.port)
def test_get_gdrive_credentials_file_without_files(self):
self.assertIsNone(Config.getGdriveCredentialsFile())
def test_get_gdrive_credentials_file_with_credentials_file(self):
self.fs.create_file('credentials.json')
self.assertIsNotNone(Config.getGdriveCredentialsFile())
def test_get_gdrive_credentials_file_with_conf_credentials_file(self):
self.fs.create_file('conf/credentials.json')
self.assertIsNotNone(Config.getGdriveCredentialsFile())
def test_load_non_existing_file(self):
with self.assertRaises(OSError):
Config.load(_get_default_config_path())
self.assertListEqual(Config.paths.scan, ['.'])
self.assertEqual(Config.server.hostname, '0.0.0.0')
self.assertEqual(Config.server.port, 9000)
def test_load_conf_default(self):
conf_file = 'conf/nut.default.conf'
conf_content = """
{
"paths": {
"scan": ["./"]
},
"server": {
"hostname": "127.0.0.1",
"port": 9005
}
}
"""
self.fs.create_file(conf_file, contents=conf_content)
Config.load(conf_file)
self.assertListEqual(Config.paths.scan, ['./'])
self.assertEqual(Config.server.hostname, '127.0.0.1')
self.assertEqual(Config.server.port, 9005)
def __compare_config_in_file_with_object(self, conf_file, obj):
self.assertTrue(os.path.exists(conf_file))
with open(conf_file, 'r', encoding="utf8") as f:
obj_from_file = json.load(f)
self.assertEqual(obj_from_file, obj)
def test_save_default_config_with_explicit_config_path(self):
conf_file = _get_default_config_path()
self.assertFalse(os.path.exists(conf_file))
Config.save(conf_file)
self.__compare_config_in_file_with_object(conf_file, _get_default_config_object())
def test_save_default_config_with_implicit_config_path(self):
conf_file = _get_default_config_path()
self.assertFalse(os.path.exists(conf_file))
Config.save()
self.__compare_config_in_file_with_object(conf_file, _get_default_config_object())
def test_save_custom_config_with_explicit_config_path(self):
conf_file = _create_empty_config_file(self.fs)
nsps_folder = "/Users/user1/Nsps"
server_hostname = "127.0.0.1"
server_port = 9001
Config.paths.scan = [nsps_folder]
Config.server.hostname = server_hostname
Config.server.port = server_port
object_to_compare = _get_default_config_object()
object_to_compare["paths"]["scan"] = [nsps_folder]
server = object_to_compare["server"]
server["hostname"] = server_hostname
server["port"] = server_port
Config.save(conf_file)
self.__compare_config_in_file_with_object(conf_file, object_to_compare)
def test_update_scan_paths_for_default_config_and_empty_Nsps(self):
_create_empty_config_file(self.fs)
self.assertEqual(Config.paths.scan, ['.'])
new_paths = ['/Users/user1/path1', '/Users/user1/path2']
Config.update_scan_paths(new_paths, Nsps.files)
self.assertEqual(Config.paths.scan, new_paths)
def test_update_main_path_clears_out_Nsps(self):
_create_empty_config_file(self.fs)
new_paths = ["folder2"]
folder1 = {"path": Config.paths.scan[0],
"files": ["title1 [abcdefa112345678].nsp", "title2 [abcdefa212345678].nsp"]}
Nsps.files = {"files": folder1["files"]}
Config.update_scan_paths(new_paths, Nsps.files)
self.assertEqual(Nsps.files, {})
def test_update_scan_paths_with_same_path(self):
_old_nsp_files = Nsps.files
initial_scan_paths = ['.']
Config.paths.scan = initial_scan_paths
initial_nsp_files = {"1.nsp", "2.nsp"}
Nsps.files = initial_nsp_files
Config.update_scan_paths(initial_scan_paths, Nsps.files)
self.assertEqual(Config.paths.scan, initial_scan_paths)
self.assertEqual(Nsps.files, initial_nsp_files)
Nsps.files = _old_nsp_files
def test_update_scan_paths_with_single_path(self):
initial_scan_paths = ['.']
Config.paths.scan = initial_scan_paths
new_scan_path = "titles"
Config.update_scan_paths(new_scan_path, Nsps.files)
self.assertEqual(Config.paths.scan, [new_scan_path])
def test_region_languages_with_empty_file(self):
self.assertEqual(Config.regionLanguages(), _get_default_languages())
# return same object
self.assertEqual(Config.regionLanguages(), _get_default_languages())
def test_regional_languages_from_file(self):
file_ = 'titledb/languages.json'
languages = '{"CO":["en","es"],"AR":["en","es"],"CL":["en","es"]}'
self.fs.create_file(file_, contents=languages)
self.assertEqual(Config.regionLanguages(), json.loads(languages))
class NutConfigServerTest(TestCase):
"""Tests for nut/Config.py Server
"""
def setUp(self):
self.setUpPyfakefs(modules_to_reload=[Config])
def test_default_hostname(self):
self.assertEqual(Config.server.hostname, '0.0.0.0')
def test_default_port(self):
self.assertEqual(Config.server.port, 9000)
class NutConfigPathsTest(TestCase):
"""Tests for nut/Config.py Paths
"""
def setUp(self):
self.setUpPyfakefs(modules_to_reload=[Config])
def test_init(self):
self.assertListEqual(Config.paths.scan, ['.'])
def test_mapping_default(self):
self.assertEqual(Config.paths.mapping(), {'.': '.'})
def test_mapping_with_gdrive_credentials_file(self):
self.assertIsNone(Config.getGdriveCredentialsFile())
self.fs.create_file('credentials.json')
self.assertEqual(Config.paths.mapping()['gdrive'], '')
self.assertEqual(Config.paths.mapping()['.'], '.')
def test_get_title_base(self):
path = Path('titles')
self.assertEqual(Config.paths.getTitleBase(False, None), None)
self.assertEqual(Path(Config.paths.getTitleBase(False, 'name [123][v0].nsp')), Path(Config.paths.titleBase))
self.assertEqual(Path(Config.paths.getTitleBase(False, 'name [123][v0].nsz')), \
path / 'nsz' / '{name}[{id}][v{version}].nsz')
self.assertEqual(Path(Config.paths.getTitleBase(False, 'name [123][v0].nsx')), \
path / '{name}[{id}][v{version}].nsp')
self.assertEqual(Path(Config.paths.getTitleBase(False, 'name [123][v0].xci')), \
path / 'xci' / '{name}[{id}][v{version}].xci')
self.assertEqual(Path(Config.paths.getTitleBase(True, 'name [123][v0].nsp')), \
path / '{name}[{id}][v{version}].nsx')
self.assertEqual(Path(Config.paths.getTitleBase(True, 'name [123][v0].nsx')), \
path / '{name}[{id}][v{version}].nsx')
def test_get_title_dlc(self):
path = Path('titles') / 'DLC'
self.assertEqual(Config.paths.getTitleDLC(False, None), None)
self.assertEqual(Path(Config.paths.getTitleDLC(False, 'name [123][v0].nsp')), Path(Config.paths.titleDLC))
self.assertEqual(Path(Config.paths.getTitleDLC(False, 'name [123][v0].nsz')), \
path / 'nsz' / '{name}[{id}][v{version}].nsz')
self.assertEqual(Path(Config.paths.getTitleDLC(False, 'name [123][v0].nsx')), \
path / '{name}[{id}][v{version}].nsp')
self.assertEqual(Path(Config.paths.getTitleDLC(False, 'name [123][v0].xci')), \
path / 'xci' / '{name}[{id}][v{version}].xci')
self.assertEqual(Path(Config.paths.getTitleDLC(True, 'name [123][v0].nsp')), \
path / '{name}[{id}][v{version}].nsx')
self.assertEqual(Path(Config.paths.getTitleDLC(True, 'name [123][v0].nsx')), \
path / '{name}[{id}][v{version}].nsx')
def test_get_title_update(self):
path = Path('titles') / 'updates'
self.assertEqual(Config.paths.getTitleUpdate(False, None), None)
self.assertEqual(Path(Config.paths.getTitleUpdate(False, 'name [123][v0].nsp')), Path(Config.paths.titleUpdate))
self.assertEqual(Path(Config.paths.getTitleUpdate(False, 'name [123][v0].nsz')), \
path / 'nsz' / '{name}[{id}][v{version}].nsz')
self.assertEqual(Path(Config.paths.getTitleUpdate(False, 'name [123][v0].nsx')), \
path / '{name}[{id}][v{version}].nsx')
self.assertEqual(Path(Config.paths.getTitleUpdate(False, 'name [123][v0].xci')), \
path / 'xci' / '{name}[{id}][v{version}].xci')
self.assertEqual(Path(Config.paths.getTitleUpdate(True, 'name [123][v0].nsp')), \
path / '{name}[{id}][v{version}].nsx')
self.assertEqual(Path(Config.paths.getTitleUpdate(True, 'name [123][v0].nsx')), \
path / '{name}[{id}][v{version}].nsx')
def test_get_title_demo(self):
path = Path('titles') / 'demos'
self.assertEqual(Config.paths.getTitleDemo(False, None), None)
self.assertEqual(Path(Config.paths.getTitleDemo(False, 'name [123][v0].nsp')), Path(Config.paths.titleDemo))
self.assertEqual(Path(Config.paths.getTitleDemo(False, 'name [123][v0].nsz')), \
path / 'nsz' / '{name}[{id}][v{version}].nsz')
self.assertEqual(Path(Config.paths.getTitleDemo(False, 'name [123][v0].nsx')), \
path / '{name}[{id}][v{version}].nsp')
self.assertEqual(Path(Config.paths.getTitleDemo(False, 'name [123][v0].xci')), \
path / 'xci' / '{name}[{id}][v{version}].xci')
self.assertEqual(Path(Config.paths.getTitleDemo(True, 'name [123][v0].nsp')), \
path / '{name}[{id}][v{version}].nsx')
self.assertEqual(Path(Config.paths.getTitleDemo(True, 'name [123][v0].nsx')), \
path / '{name}[{id}][v{version}].nsx')
def test_get_title_demo_update(self):
path = Path('titles') / 'demos' / 'updates'
self.assertEqual(Config.paths.getTitleDemoUpdate(False, None), None)
self.assertEqual(Path(Config.paths.getTitleDemoUpdate(False, 'name [123][v0].nsp')), Path(Config.paths.titleDemoUpdate))
self.assertEqual(Path(Config.paths.getTitleDemoUpdate(False, 'name [123][v0].nsz')), \
path / 'nsz' / '{name}[{id}][v{version}].nsz')
self.assertEqual(Path(Config.paths.getTitleDemoUpdate(False, 'name [123][v0].nsx')), \
path / '{name}[{id}][v{version}].nsp')
self.assertEqual(Path(Config.paths.getTitleDemoUpdate(False, 'name [123][v0].xci')), \
path / 'xci' / '{name}[{id}][v{version}].xci')
self.assertEqual(Path(Config.paths.getTitleDemoUpdate(True, 'name [123][v0].nsp')), \
path / '{name}[{id}][v{version}].nsx')
self.assertEqual(Path(Config.paths.getTitleDemoUpdate(True, 'name [123][v0].nsx')), \
path / '{name}[{id}][v{version}].nsx')
class NutConfigDownloadTest(TestCase):
"""Tests for nut/config_impl/download.py
"""
def test_add_region(self):
download_section = Download()
self.assertListEqual(download_section.regions, [])
download_section.addRegion("US")
self.assertListEqual(download_section.regions, ["US"])
def test_remove_region(self):
download_section = Download()
download_section.addRegion("US")
self.assertListEqual(download_section.regions, ["US"])
download_section.removeRegion("US")
self.assertListEqual(download_section.regions, [])
download_section.removeRegion("US") # no error for non-existing region
def test_has_region(self):
download_section = Download()
self.assertFalse(download_section.hasRegion(["US"], default=False))
self.assertTrue(download_section.hasRegion(["US"], default=True))
download_section.addRegion("US")
self.assertTrue(download_section.hasRegion(["US"], default=False))
self.assertFalse(download_section.hasRegion(["RU"], default=False))
self.assertFalse(download_section.hasRegion(["RU"]))
if __name__ == "__main__":
unittest.main()