-
Notifications
You must be signed in to change notification settings - Fork 41
/
file_cache_tester.py
118 lines (98 loc) · 3.85 KB
/
file_cache_tester.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
# Copyright 2014-2016 Insight Software Consortium.
# Copyright 2004-2008 Roman Yakovenko.
# Distributed under the Boost Software License, Version 1.0.
# See http://www.boost.org/LICENSE_1_0.txt
import os
import sys
import unittest
import autoconfig
import subprocess
import parser_test_case
from pygccxml import parser
class Test(parser_test_case.parser_test_case_t):
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
def __init__(self, *args):
parser_test_case.parser_test_case_t.__init__(self, *args)
self.header = os.path.join(autoconfig.data_directory, 'core_cache.hpp')
self.cache_file = os.path.join(
autoconfig.data_directory,
'pygccxml.cache')
if os.path.exists(self.cache_file) and os.path.isfile(self.cache_file):
os.remove(self.cache_file)
def touch(self):
# Need to change file.
with open(self.header, "a+") as header:
header.write("//touch")
def test_update(self):
# Save the content of the header file for later
with open(self.header, "r") as old_header:
content = old_header.read()
declarations = parser.parse([self.header], self.config)
cache = parser.file_cache_t(self.cache_file)
cache.update(
source_file=self.header,
configuration=self.config,
declarations=declarations,
included_files=[])
self.assertTrue(
declarations == cache.cached_value(
self.header,
self.config),
"cached declarations and source declarations are different")
self.touch()
self.assertTrue(
cache.cached_value(self.header, self.config) is None,
"cache didn't recognize that some files on disk has been changed")
# We wrote a //touch in the header file. Just replace the file with the
# original content. The touched file would be sometimes commited by
# error as it was modified.
with open(self.header, "w") as new_header:
new_header.write(content)
def test_from_file(self):
declarations = parser.parse([self.header], self.config)
cache = parser.file_cache_t(self.cache_file)
cache.update(
source_file=self.header,
configuration=self.config,
declarations=declarations,
included_files=[])
self.assertTrue(
declarations == cache.cached_value(
self.header,
self.config),
"cached declarations and source declarations are different")
cache.flush()
cache = parser.file_cache_t(self.cache_file)
self.assertTrue(
declarations == cache.cached_value(
self.header,
self.config),
("cached declarations and source declarations are different, " +
"after pickling"))
@staticmethod
def test_reopen_cache():
"""
Test opening cache files in a subprocess (with a clean environment).
"""
env = os.environ.copy()
# Get the path to current directory
path = os.path.dirname(os.path.realpath(__file__))
# Set the COVERAGE_PROCESS_START env. variable.
# Allows to cover files run in a subprocess
# http://nedbatchelder.com/code/coverage/subprocess.html
env["COVERAGE_PROCESS_START"] = path + "/../.coveragerc"
p = subprocess.Popen(
[sys.executable, "unittests/reopen_cache_tester.py"],
stdout=subprocess.PIPE,
env=env)
print(p.stdout.read())
p.wait()
p.stdout.close()
def create_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(Test))
return suite
def run_suite():
unittest.TextTestRunner(verbosity=2).run(create_suite())
if __name__ == "__main__":
run_suite()