File: test_gcc_cache.py

package info (click to toggle)
pypy 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 107,216 kB
  • sloc: python: 1,201,787; ansic: 62,419; asm: 5,169; cpp: 3,017; sh: 2,534; makefile: 545; xml: 243; lisp: 45; awk: 4
file content (116 lines) | stat: -rw-r--r-- 3,722 bytes parent folder | download | duplicates (5)
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
import sys
import cStringIO
import py
from rpython.tool.udir import udir
from rpython.translator.tool.cbuild import ExternalCompilationInfo
from rpython.translator.platform import CompilationError
from rpython.tool.gcc_cache import (
    cache_file_path, build_executable_cache, try_compile_cache)

localudir = udir.join('test_gcc_cache').ensure(dir=1)

def test_gcc_exec():
    f = localudir.join("x.c")
    f.write("""
    #include <stdio.h>
    #include <test_gcc_exec.h>
    int main()
    {
       printf("%d\\n", ANSWER);
       return 0;
    }
    """)
    dir1 = localudir.join('test_gcc_exec_dir1').ensure(dir=1)
    dir2 = localudir.join('test_gcc_exec_dir2').ensure(dir=1)
    dir1.join('test_gcc_exec.h').write('#define ANSWER 3\n')
    dir2.join('test_gcc_exec.h').write('#define ANSWER 42\n')
    eci = ExternalCompilationInfo(include_dirs=[str(dir1)])
    # remove cache
    path = cache_file_path([f], eci, 'build_executable_cache')
    if path.check():
        path.remove()
    res = build_executable_cache([f], eci)
    assert res == "3\n"
    assert build_executable_cache([f], eci) == "3\n"
    eci2 = ExternalCompilationInfo(include_dirs=[str(dir2)])
    assert build_executable_cache([f], eci2) == "42\n"
    f.write("#error BOOM\n")
    err = py.test.raises(CompilationError, build_executable_cache, [f], eci2)
    print '<<<'
    print err
    print '>>>'

def test_gcc_ask():
    f = localudir.join("y.c")
    f.write("""
    #include <stdio.h>
    #include <test_gcc_ask.h>
    int main()
    {
       printf("hello\\n");
       return 0;
    }
    """)
    dir1 = localudir.join('test_gcc_ask_dir1').ensure(dir=1)
    dir2 = localudir.join('test_gcc_ask_dir2').ensure(dir=1)
    dir1.join('test_gcc_ask.h').write('/* hello world */\n')
    dir2.join('test_gcc_ask.h').write('#error boom\n')
    eci = ExternalCompilationInfo(include_dirs=[str(dir1)])
    # remove cache
    path = cache_file_path([f], eci, 'try_compile_cache')
    if path.check():
        path.remove()
    assert try_compile_cache([f], eci)
    assert try_compile_cache([f], eci)
    assert build_executable_cache([f], eci) == "hello\n"
    eci2 = ExternalCompilationInfo(include_dirs=[str(dir2)])
    err = py.test.raises(CompilationError, try_compile_cache, [f], eci2)
    print '<<<'
    print err
    print '>>>'

def test_gcc_ask_doesnt_log_errors():
    f = localudir.join('z.c')
    f.write("""this file is not valid C code\n""")
    eci = ExternalCompilationInfo()
    oldstderr = sys.stderr
    try:
        sys.stderr = capture = cStringIO.StringIO()
        py.test.raises(CompilationError, try_compile_cache, [f], eci)
    finally:
        sys.stderr = oldstderr
    assert 'ERROR' not in capture.getvalue().upper()

def test_execute_code_ignore_errors():
    f = localudir.join('z.c')
    f.write("""this file is not valid C code\n""")
    eci = ExternalCompilationInfo()
    oldstderr = sys.stderr
    try:
        sys.stderr = capture = cStringIO.StringIO()
        py.test.raises(CompilationError, build_executable_cache,
                       [f], eci, True)
    finally:
        sys.stderr = oldstderr
    assert 'ERROR' not in capture.getvalue().upper()

def test_execute_code_show_runtime_error():
    f = localudir.join('z.c')
    f.write("""
    #include <stdio.h>
    int main()
    {
       fprintf(stderr, "hello\\n");
       return 0;
    }
    """)
    for i in range(2):
        eci = ExternalCompilationInfo()
        oldstderr = sys.stderr
        try:
            sys.stderr = capture = cStringIO.StringIO()
            output = build_executable_cache([f], eci, True)
        finally:
            sys.stderr = oldstderr
        assert 'hello' in capture.getvalue()
        assert output == ''