1
|
|
|
import os |
2
|
|
|
import unittest |
3
|
|
|
|
4
|
|
|
from pyprint.NullPrinter import NullPrinter |
5
|
|
|
|
6
|
|
|
from coalib.misc.CachingUtilities import ( |
7
|
|
|
get_settings_hash, settings_changed, update_settings_db, |
8
|
|
|
get_data_path, pickle_load, pickle_dump, delete_files) |
9
|
|
|
from coalib.output.printers.LogPrinter import LogPrinter |
10
|
|
|
from coalib.settings.Section import Section |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class CachingUtilitiesTest(unittest.TestCase): |
14
|
|
|
|
15
|
|
|
def setUp(self): |
16
|
|
|
self.log_printer = LogPrinter(NullPrinter()) |
17
|
|
|
|
18
|
|
|
def test_corrupt_cache_files(self): |
19
|
|
|
file_path = get_data_path(self.log_printer, "corrupt_file") |
20
|
|
|
with open(file_path, "wb") as f: |
21
|
|
|
f.write(bytes([1] * 100)) |
22
|
|
|
|
23
|
|
|
self.assertTrue(os.path.isfile(file_path)) |
24
|
|
|
self.assertEqual(pickle_load( |
25
|
|
|
self.log_printer, "corrupt_file", fallback=42), 42) |
26
|
|
|
|
27
|
|
|
def test_delete_files(self): |
28
|
|
|
pickle_dump(self.log_printer, "coala_test", {"answer": 42}) |
29
|
|
|
self.assertTrue(delete_files( |
30
|
|
|
self.log_printer, ["coala_test"])) |
31
|
|
|
self.assertFalse(os.path.isfile(get_data_path( |
32
|
|
|
self.log_printer, "coala_test"))) |
33
|
|
|
|
34
|
|
|
def test_delete_invalid_file(self): |
35
|
|
|
self.assertFalse(delete_files( |
36
|
|
|
self.log_printer, ["non_existant_file"])) |
37
|
|
|
|
38
|
|
|
@unittest.mock.patch('coalib.misc.CachingUtilities.os') |
39
|
|
|
def test_delete_permission_error(self, mock_os): |
40
|
|
|
with open(get_data_path(self.log_printer, "coala_test"), "w"): |
41
|
|
|
mock_os.remove.side_effect = OSError("Permission error") |
42
|
|
|
self.assertTrue(os.path.isfile(get_data_path( |
43
|
|
|
self.log_printer, "coala_test"))) |
44
|
|
|
self.assertFalse(delete_files(self.log_printer, ["coala_test"])) |
45
|
|
|
|
46
|
|
|
@unittest.mock.patch("os.makedirs") |
47
|
|
|
def test_permission_error(self, makedirs): |
48
|
|
|
makedirs.side_effect = PermissionError |
49
|
|
|
self.assertEqual(get_data_path(self.log_printer, "test"), None) |
50
|
|
|
|
51
|
|
|
self.assertFalse(pickle_dump(self.log_printer, "test", {"answer": 42})) |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
class SettingsTest(unittest.TestCase): |
55
|
|
|
|
56
|
|
|
def setUp(self): |
57
|
|
|
self.log_printer = LogPrinter(NullPrinter()) |
58
|
|
|
|
59
|
|
|
def test_settings_change(self): |
60
|
|
|
sections = {} |
61
|
|
|
settings_hash = get_settings_hash(sections) |
62
|
|
|
update_settings_db(self.log_printer, settings_hash) |
63
|
|
|
self.assertFalse(settings_changed(self.log_printer, settings_hash)) |
64
|
|
|
|
65
|
|
|
sections = {"a": Section("a")} |
66
|
|
|
settings_hash = get_settings_hash(sections) |
67
|
|
|
self.assertTrue(settings_changed(self.log_printer, settings_hash)) |
68
|
|
|
|
69
|
|
|
def test_targets_change(self): |
70
|
|
|
sections = {"a": Section("a"), "b": Section("b")} |
71
|
|
|
self.assertNotEqual(get_settings_hash(sections), |
72
|
|
|
get_settings_hash(sections, targets=["a"])) |
73
|
|
|
|