|
1
|
|
|
import unittest |
|
2
|
|
|
import re |
|
3
|
|
|
import os |
|
4
|
|
|
from unittest.mock import patch |
|
5
|
|
|
|
|
6
|
|
|
from pyprint.NullPrinter import NullPrinter |
|
7
|
|
|
|
|
8
|
|
|
from coalib.misc.Caching import FileCache |
|
9
|
|
|
from coalib.misc.CachingUtilities import pickle_load, pickle_dump |
|
10
|
|
|
from coalib.output.printers.LogPrinter import LogPrinter |
|
11
|
|
|
from coalib import coala |
|
12
|
|
|
from coalib.misc.ContextManagers import prepare_file |
|
13
|
|
|
from coalib.misc.ContextManagers import simulate_console_inputs |
|
14
|
|
|
from tests.TestUtilities import execute_coala, bear_test_module |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
class CachingTest(unittest.TestCase): |
|
18
|
|
|
|
|
19
|
|
|
def setUp(self): |
|
20
|
|
|
current_dir = os.path.split(__file__)[0] |
|
21
|
|
|
self.caching_test_dir = os.path.join( |
|
22
|
|
|
current_dir, |
|
23
|
|
|
"caching_testfiles") |
|
24
|
|
|
self.log_printer = LogPrinter(NullPrinter()) |
|
25
|
|
|
self.cache = FileCache(self.log_printer, "coala_test", flush_cache=True) |
|
26
|
|
|
|
|
27
|
|
|
def test_file_tracking(self): |
|
28
|
|
|
self.cache.track_files({"test.c", "file.py"}) |
|
29
|
|
|
self.assertEqual(self.cache.data, {"test.c": -1, "file.py": -1}) |
|
30
|
|
|
|
|
31
|
|
|
self.cache.untrack_files({"test.c"}) |
|
32
|
|
|
self.assertFalse("test.c" in self.cache.data) |
|
33
|
|
|
self.assertTrue("file.py" in self.cache.data) |
|
34
|
|
|
|
|
35
|
|
|
self.cache.untrack_files({"test.c", "file.py"}) |
|
36
|
|
|
self.assertFalse("test.c" in self.cache.data) |
|
37
|
|
|
self.assertFalse("file.py" in self.cache.data) |
|
38
|
|
|
|
|
39
|
|
|
def test_write(self): |
|
40
|
|
|
self.cache.track_files({"test2.c"}) |
|
41
|
|
|
self.assertEqual(self.cache.data["test2.c"], -1) |
|
42
|
|
|
|
|
43
|
|
|
self.cache.write() |
|
44
|
|
|
self.assertNotEqual(self.cache.data["test2.c"], -1) |
|
45
|
|
|
|
|
46
|
|
|
@patch('coalib.misc.Caching.os') |
|
47
|
|
|
def test_get_uncached_files(self, mock_os): |
|
48
|
|
|
file_path = os.path.join(self.caching_test_dir, "test.c") |
|
49
|
|
|
cache = FileCache(self.log_printer, "coala_test3", flush_cache=True) |
|
50
|
|
|
|
|
51
|
|
|
# Since this is a new FileCache object, the return must be the full set |
|
52
|
|
|
cache.current_time = 0 |
|
53
|
|
|
mock_os.path.getmtime.return_value = 0 |
|
54
|
|
|
self.assertEqual(cache.get_uncached_files({file_path}), {file_path}) |
|
55
|
|
|
|
|
56
|
|
|
cache.track_files({file_path}) |
|
57
|
|
|
self.assertEqual(cache.get_uncached_files({file_path}), {file_path}) |
|
58
|
|
|
|
|
59
|
|
|
cache.write() |
|
60
|
|
|
self.assertEqual(cache.get_uncached_files({file_path}), set()) |
|
61
|
|
|
|
|
62
|
|
|
# Simulate changing the file and then getting uncached files |
|
63
|
|
|
# Since the file has been edited since the last run it's returned |
|
64
|
|
|
cache.current_time = 1 |
|
65
|
|
|
mock_os.path.getmtime.return_value = 1 |
|
66
|
|
|
cache.track_files({file_path}) |
|
67
|
|
|
self.assertEqual(cache.get_uncached_files({file_path}), {file_path}) |
|
68
|
|
|
cache.write() |
|
69
|
|
|
|
|
70
|
|
|
# Not changing the file should NOT return it the next time |
|
71
|
|
|
cache.current_time = 2 |
|
72
|
|
|
self.assertEqual(cache.get_uncached_files({file_path}), set()) |
|
73
|
|
|
|
|
74
|
|
|
def test_persistence(self): |
|
75
|
|
|
with FileCache(self.log_printer, "test3", flush_cache=True) as cache: |
|
76
|
|
|
cache.track_files({"file.c"}) |
|
77
|
|
|
self.assertTrue("file.c" in cache.data) |
|
78
|
|
|
|
|
79
|
|
|
with FileCache(self.log_printer, "test3", flush_cache=False) as cache: |
|
80
|
|
|
self.assertTrue("file.c" in cache.data) |
|
81
|
|
|
|
|
82
|
|
|
def test_time_travel(self): |
|
83
|
|
|
cache = FileCache(self.log_printer, "coala_test2", flush_cache=True) |
|
84
|
|
|
cache.track_files({"file.c"}) |
|
85
|
|
|
cache.write() |
|
86
|
|
|
self.assertTrue("file.c" in cache.data) |
|
87
|
|
|
|
|
88
|
|
|
cache_data = pickle_load(self.log_printer, "coala_test2", {}) |
|
89
|
|
|
# Back to the future :) |
|
90
|
|
|
cache_data["time"] = 2000000000 |
|
91
|
|
|
pickle_dump(self.log_printer, "coala_test2", cache_data) |
|
92
|
|
|
|
|
93
|
|
|
cache = FileCache(self.log_printer, "coala_test2", flush_cache=False) |
|
94
|
|
|
self.assertFalse("file.c" in cache.data) |
|
95
|
|
|
|
|
96
|
|
|
def test_caching_results(self): |
|
97
|
|
|
""" |
|
98
|
|
|
A simple integration test to assert that results are not dropped |
|
99
|
|
|
when coala is ran multiple times with caching enabled. |
|
100
|
|
|
""" |
|
101
|
|
|
with bear_test_module(), \ |
|
102
|
|
|
prepare_file(["a=(5,6)"], None) as (lines, filename): |
|
103
|
|
|
with simulate_console_inputs("0"): |
|
104
|
|
|
retval, output = execute_coala( |
|
105
|
|
|
coala.main, |
|
106
|
|
|
"coala", |
|
107
|
|
|
"-c", os.devnull, |
|
108
|
|
|
"--disable-caching", |
|
109
|
|
|
"--flush-cache", |
|
110
|
|
|
"-f", re.escape(filename), |
|
111
|
|
|
"-b", "LineCountTestBear", |
|
112
|
|
|
"-L", "DEBUG") |
|
113
|
|
|
self.assertIn("This file has", output) |
|
114
|
|
|
|
|
115
|
|
|
# Due to the change in configuration from the removal of |
|
116
|
|
|
# ``--flush-cache`` this run will not be sufficient to |
|
117
|
|
|
# assert this behavior. |
|
118
|
|
|
retval, output = execute_coala( |
|
119
|
|
|
coala.main, |
|
120
|
|
|
"coala", |
|
121
|
|
|
"-c", os.devnull, |
|
122
|
|
|
"-f", re.escape(filename), |
|
123
|
|
|
"-b", "LineCountTestBear") |
|
124
|
|
|
self.assertIn("This file has", output) |
|
125
|
|
|
|
|
126
|
|
|
retval, output = execute_coala( |
|
127
|
|
|
coala.main, |
|
128
|
|
|
"coala", |
|
129
|
|
|
"-c", os.devnull, |
|
130
|
|
|
"-f", re.escape(filename), |
|
131
|
|
|
"-b", "LineCountTestBear") |
|
132
|
|
|
self.assertIn("This file has", output) |
|
133
|
|
|
|