Debian Codegraph @

rpython/tool/test/test_gcc_cache.py

raw symbols SCIP
1 import sys
2 import cStringIO
3 import py
4 from rpython.tool.udir import udir
5 from rpython.translator.tool.cbuild import ExternalCompilationInfo
6 from rpython.translator.platform import CompilationError
7 from rpython.tool.gcc_cache import (
8 cache_file_path, build_executable_cache, try_compile_cache)
9
10 localudir = udir.join('test_gcc_cache').ensure(dir=1)
11
12 def test_gcc_exec():
13 f = localudir.join("x.c")
14 f.write("""
15 #include <stdio.h>
16 #include <test_gcc_exec.h>
17 int main()
18 {
19 printf("%d\\n", ANSWER);
20 return 0;
21 }
22 """)
23 dir1 = localudir.join('test_gcc_exec_dir1').ensure(dir=1)
24 dir2 = localudir.join('test_gcc_exec_dir2').ensure(dir=1)
25 dir1.join('test_gcc_exec.h').write('#define ANSWER 3\n')
26 dir2.join('test_gcc_exec.h').write('#define ANSWER 42\n')
27 eci = ExternalCompilationInfo(include_dirs=[str(dir1)])
28 # remove cache
29 path = cache_file_path([f], eci, 'build_executable_cache')
30 if path.check():
31 path.remove()
32 res = build_executable_cache([f], eci)
33 assert res == "3\n"
34 assert build_executable_cache([f], eci) == "3\n"
35 eci2 = ExternalCompilationInfo(include_dirs=[str(dir2)])
36 assert build_executable_cache([f], eci2) == "42\n"
37 f.write("#error BOOM\n")
38 err = py.test.raises(CompilationError, build_executable_cache, [f], eci2)
39 print '<<<'
40 print err
41 print '>>>'
42
43 def test_gcc_ask():
44 f = localudir.join("y.c")
45 f.write("""
46 #include <stdio.h>
47 #include <test_gcc_ask.h>
48 int main()
49 {
50 printf("hello\\n");
51 return 0;
52 }
53 """)
54 dir1 = localudir.join('test_gcc_ask_dir1').ensure(dir=1)
55 dir2 = localudir.join('test_gcc_ask_dir2').ensure(dir=1)
56 dir1.join('test_gcc_ask.h').write('/* hello world */\n')
57 dir2.join('test_gcc_ask.h').write('#error boom\n')
58 eci = ExternalCompilationInfo(include_dirs=[str(dir1)])
59 # remove cache
60 path = cache_file_path([f], eci, 'try_compile_cache')
61 if path.check():
62 path.remove()
63 assert try_compile_cache([f], eci)
64 assert try_compile_cache([f], eci)
65 assert build_executable_cache([f], eci) == "hello\n"
66 eci2 = ExternalCompilationInfo(include_dirs=[str(dir2)])
67 path = cache_file_path([f], eci, 'try_compile_cache')
68 if path.check():
69 path.remove()
70 err = py.test.raises(CompilationError, try_compile_cache, [f], eci2)
71 assert path.check
72 py.test.raises(CompilationError, try_compile_cache, [f], eci2)
73
74 def test_gcc_ask_doesnt_log_errors():
75 f = localudir.join('z.c')
76 f.write("""this file is not valid C code\n""")
77 eci = ExternalCompilationInfo()
78 oldstderr = sys.stderr
79 try:
80 sys.stderr = capture = cStringIO.StringIO()
81 py.test.raises(CompilationError, try_compile_cache, [f], eci)
82 finally:
83 sys.stderr = oldstderr
84 assert 'ERROR' not in capture.getvalue().upper()
85
86 def test_execute_code_ignore_errors():
87 f = localudir.join('z.c')
88 f.write("""this file is not valid C code\n""")
89 eci = ExternalCompilationInfo()
90 oldstderr = sys.stderr
91 try:
92 sys.stderr = capture = cStringIO.StringIO()
93 py.test.raises(CompilationError, build_executable_cache,
94 [f], eci, True)
95 finally:
96 sys.stderr = oldstderr
97 assert 'ERROR' not in capture.getvalue().upper()
98
99 def test_execute_code_show_runtime_error():
100 f = localudir.join('z.c')
101 f.write("""
102 #include <stdio.h>
103 int main()
104 {
105 fprintf(stderr, "hello\\n");
106 return 0;
107 }
108 """)
109 for i in range(2):
110 eci = ExternalCompilationInfo()
111 oldstderr = sys.stderr
112 try:
113 sys.stderr = capture = cStringIO.StringIO()
114 output = build_executable_cache([f], eci, True)
115 finally:
116 sys.stderr = oldstderr
117 assert 'hello' in capture.getvalue()
118 assert output == ''