test.py 3.2 KB
Newer Older
Ingvar Stepanyan's avatar
Ingvar Stepanyan committed
1
#!/usr/bin/env python
Ryan Hunt's avatar
Ryan Hunt committed
2 3 4 5 6

import os
import glob
import subprocess
import sys
Ryan Hunt's avatar
Ryan Hunt committed
7
import filecmp
Ryan Hunt's avatar
Ryan Hunt committed
8

9 10 11 12 13 14 15
def build_cbindgen():
    try:
        subprocess.check_output(["cargo", "build"])
        return True
    except subprocess.CalledProcessError:
        return False

16
def cbindgen(path, out, c):
Ryan Hunt's avatar
Ryan Hunt committed
17
    bin = ["cargo", "run", "--"]
18
    compile = [path, "-o", out]
19 20
    flags = []

21
    if c:
22 23
        flags += ["--lang", "c"]

24 25
    config = path.replace(".rs", ".toml")
    if not os.path.isdir(path) and os.path.exists(config):
26 27 28 29 30
        flags += ["--config", config]

    command = bin + flags + compile
    print(command)
    subprocess.check_output(bin + flags + compile)
31 32

def gcc(src):
33 34 35 36
    gcc_bin = os.environ.get('CC')
    if gcc_bin == None:
        gcc_bin = 'gcc'

Ryan Hunt's avatar
Ryan Hunt committed
37 38
    subprocess.check_output([gcc_bin, "-D", "DEFINED", "-c", src, "-o", "tests/expectations/tmp.o"])
    os.remove("tests/expectations/tmp.o")
Ryan Hunt's avatar
Ryan Hunt committed
39 40

def gxx(src):
41 42 43 44
    gxx_bin = os.environ.get('CXX')
    if gxx_bin == None:
        gxx_bin = 'g++'

Ryan Hunt's avatar
Ryan Hunt committed
45 46
    subprocess.check_output([gxx_bin, "-D", "DEFINED", "-std=c++11", "-c", src, "-o", "tests/expectations/tmp.o"])
    os.remove("tests/expectations/tmp.o")
Ryan Hunt's avatar
Ryan Hunt committed
47

Ryan Hunt's avatar
Ryan Hunt committed
48
def run_compile_test(rust_src, should_verify, c):
49 50 51 52 53 54 55
    is_crate = os.path.isdir(rust_src)

    test_name = rust_src
    if is_crate:
        test_name = os.path.basename(rust_src[0:-1])
    else:
        test_name = os.path.splitext(os.path.basename(rust_src))[0]
Ryan Hunt's avatar
Ryan Hunt committed
56

57
    expectation = True
58
    if test_name.startswith("fail-"):
59 60
        expectation = False

61
    if c:
62
        out = os.path.join('tests/expectations/', test_name + ".c")
Ryan Hunt's avatar
Ryan Hunt committed
63
        verify = 'tests/expectations/__verify__.c'
64
    else:
65
        out = os.path.join('tests/expectations/', test_name + ".cpp")
Ryan Hunt's avatar
Ryan Hunt committed
66
        verify = 'tests/expectations/__verify__.cpp'
67

Ryan Hunt's avatar
Ryan Hunt committed
68
    try:
Ryan Hunt's avatar
Ryan Hunt committed
69
        if should_verify:
70
            cbindgen(rust_src, verify, c)
Ryan Hunt's avatar
Ryan Hunt committed
71 72 73 74 75 76 77 78 79 80

            if c:
                gcc(verify)
            else:
                gxx(verify)

            if not filecmp.cmp(out, verify):
                os.remove(verify)
                return False
            os.remove(verify)
81
        else:
82
            cbindgen(rust_src, out, c)
Ryan Hunt's avatar
Ryan Hunt committed
83 84 85 86 87

            if c:
                gcc(out)
            else:
                gxx(out)
88

Ryan Hunt's avatar
Ryan Hunt committed
89
    except subprocess.CalledProcessError:
90
        if os.path.exists(verify):
Ryan Hunt's avatar
Ryan Hunt committed
91
            os.remove(verify)
92
        return expectation == False
Ryan Hunt's avatar
Ryan Hunt committed
93

94
    return expectation == True
Ryan Hunt's avatar
Ryan Hunt committed
95

96 97 98 99 100
if not build_cbindgen():
    exit()

args = sys.argv[1:]
files = [x for x in args if not x.startswith("-")]
Ryan Hunt's avatar
Ryan Hunt committed
101 102 103 104 105 106 107
flags = [x for x in args if x.startswith("-")]

should_verify = False

for flag in flags:
    if flag == "-v":
        should_verify = True
Ryan Hunt's avatar
Ryan Hunt committed
108

109 110
tests = []
if len(files) == 0:
111
    tests = glob.glob("tests/rust/*.rs") + glob.glob("tests/rust/*/")
112 113 114 115 116 117
else:
    tests = files

num_pass = 0
num_fail = 0

Ryan Hunt's avatar
Ryan Hunt committed
118 119 120
# C

for test in tests:
Ryan Hunt's avatar
Ryan Hunt committed
121
    if run_compile_test(test, should_verify, True):
Ryan Hunt's avatar
Ryan Hunt committed
122 123 124 125 126 127 128 129
        num_pass += 1
        print("Pass - %s" % test)
    else:
        num_fail += 1
        print("Fail - %s" % test)

# C++

Ryan Hunt's avatar
Ryan Hunt committed
130
for test in tests:
Ryan Hunt's avatar
Ryan Hunt committed
131
    if run_compile_test(test, should_verify, False):
Ryan Hunt's avatar
Ryan Hunt committed
132 133 134 135 136 137 138
        num_pass += 1
        print("Pass - %s" % test)
    else:
        num_fail += 1
        print("Fail - %s" % test)

print("Tests complete. %i passed, %i failed." % (num_pass, num_fail))