aboutsummaryrefslogtreecommitdiffstats
path: root/tests/run_tests.py
blob: e748e09d5157d06d046553ed4aa8737198070043 (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#!/usr/bin/env python2

import sys, os, subprocess, string, re, json, threading, multiprocessing, argparse
from threading import Thread
from sys import platform as _platform

def isWindows():
    return _platform == 'win32'

class QtInstallation:
    def __init__(self):
        self.int_version = 000
        self.qmake_header_path = "/usr/include/qt/"
        self.qmake_lib_path = "/usr/lib"

    def compiler_flags(self):
        return "-isystem " + self.qmake_header_path + ("" if isWindows() else " -fPIC") + " -L " + self.qmake_lib_path

class Test:
    def __init__(self, check):
        self.filename = ""
        self.minimum_qt_version = 500
        self.maximum_qt_version = 59999
        self.minimum_clang_version = 380
        self.compare_everything = False
        self.isFixedFile = False
        self.link = False # If true we also call the linker
        self.check = check
        self.expects_failure = False
        self.qt_major_version = 5 # Tests use Qt 5 by default
        self.env = os.environ
        self.checks = []
        self.flags = ""
        self.must_fail = False
        self.blacklist_platforms = []
        self.qt4compat = False
        self.only_qt = False
        self.qt_developer = False
        self.header_filter = ""
        self.ignore_dirs = ""

    def isScript(self):
        return self.filename.endswith(".sh")

    def setQtMajorVersion(self, major_version):
        if major_version == 4:
            self.qt_major_version = 4
            if self.minimum_qt_version >= 500:
                self.minimum_qt_version = 400

    def envString(self):
        result = ""
        for key in self.env:
            result += key + '="' + self.env[key] + '" '
        return result

    def setEnv(self, e):
        self.env = os.environ.copy()
        for key in e:
            key_str = key.encode('ascii', 'ignore')
            self.env[key_str] = e[key].encode('ascii', 'ignore')

class Check:
    def __init__(self, name):
        self.name = name
        self.minimum_clang_version = 380 # clang 3.8.0
        self.minimum_qt_version = 500
        self.maximum_qt_version = 59999
        self.enabled = True
        self.clazy_standalone_only = False
        self.tests = []
#-------------------------------------------------------------------------------
# utility functions #1

def get_command_output(cmd, test_env = os.environ):
    try:
        output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True, env=test_env)
    except subprocess.CalledProcessError, e:
        return e.output,False

    return output,True

def load_json(check_name):
    check = Check(check_name)
    filename = check_name + "/config.json"
    if not os.path.exists(filename):
        # Ignore this directory
        return check

    f = open(filename, 'r')
    contents = f.read()
    f.close()
    decoded = json.loads(contents)
    check_blacklist_platforms = []

    if 'minimum_clang_version' in decoded:
        check.minimum_clang_version = decoded['minimum_clang_version']

    if 'minimum_qt_version' in decoded:
        check.minimum_qt_version = decoded['minimum_qt_version']

    if 'maximum_qt_version' in decoded:
        check.maximum_qt_version = decoded['maximum_qt_version']

    if 'enabled' in decoded:
        check.enabled = decoded['enabled']

    if 'clazy_standalone_only' in decoded:
        check.clazy_standalone_only = decoded['clazy_standalone_only']

    if 'blacklist_platforms' in decoded:
        check_blacklist_platforms = decoded['blacklist_platforms']

    if 'tests' in decoded:
        for t in decoded['tests']:
            test = Test(check)
            test.blacklist_platforms = check_blacklist_platforms
            test.filename = t['filename']

            if 'minimum_qt_version' in t:
                test.minimum_qt_version = t['minimum_qt_version']
            else:
                test.minimum_qt_version = check.minimum_qt_version

            if 'maximum_qt_version' in t:
                test.maximum_qt_version = t['maximum_qt_version']
            else:
                test.maximum_qt_version = check.maximum_qt_version

            if 'minimum_clang_version' in t:
                test.minimum_clang_version = t['minimum_clang_version']
            else:
                test.minimum_clang_version = check.minimum_clang_version

            if 'blacklist_platforms' in t:
                test.blacklist_platforms = t['blacklist_platforms']
            if 'compare_everything' in t:
                test.compare_everything = t['compare_everything']
            if 'isFixedFile' in t:
                test.isFixedFile = t['isFixedFile']
            if 'link' in t:
                test.link = t['link']
            if 'qt_major_version' in t:
                test.setQtMajorVersion(t['qt_major_version'])
            if 'env' in t:
                test.setEnv(t['env'])
            if 'checks' in t:
                test.checks = t['checks']
            if 'flags' in t:
                test.flags = t['flags']
            if 'must_fail' in t:
                test.must_fail = t['must_fail']
            if 'expects_failure' in t:
                test.expects_failure = t['expects_failure']
            if 'qt4compat' in t:
                test.qt4compat = t['qt4compat']
            if 'only_qt' in t:
                test.only_qt = t['only_qt']
            if 'qt_developer' in t:
                test.qt_developer = t['qt_developer']
            if 'header_filter' in t:
                test.header_filter = t['header_filter']
            if 'ignore_dirs' in t:
                test.ignore_dirs = t['ignore_dirs']

            if not test.checks:
                test.checks.append(test.check.name)

            check.tests.append(test)
            if test.isFixedFile:
                fileToDelete = check_name + "/" + test.filename
                if os.path.exists(fileToDelete):
                    os.remove(fileToDelete)

    return check

def find_qt_installation(major_version, qmakes):
    installation = QtInstallation()

    for qmake in qmakes:
        qmake_version_str,success = get_command_output(qmake + " -query QT_VERSION")
        if success and qmake_version_str.startswith(str(major_version) + "."):
            qmake_header_path = get_command_output(qmake + " -query QT_INSTALL_HEADERS")[0].strip()
            qmake_lib_path = get_command_output(qmake + " -query QT_INSTALL_LIBS")[0].strip()
            if qmake_header_path:
                installation.qmake_header_path = qmake_header_path
                if qmake_lib_path:
                    installation.qmake_lib_path = qmake_lib_path
                ver = qmake_version_str.split('.')
                installation.int_version = int(ver[0]) * 10000 + int(ver[1]) * 100 + int(ver[2])
                if _verbose:
                    print "Found Qt " + str(installation.int_version) + " using qmake " + qmake
            break

    if installation.int_version == 0 and major_version >= 5: # Don't warn for missing Qt4 headers
        print "Error: Couldn't find a Qt" + str(major_version) + " installation"
    return installation

def libraryName():
    if _platform == 'win32':
        return 'ClangLazy.dll'
    elif _platform == 'darwin':
        return 'ClangLazy.dylib'
    else:
        return 'ClangLazy.so'

def link_flags():
    flags = "-lQt5Core -lQt5Gui -lQt5Widgets"
    if _platform.startswith('linux'):
        flags += " -lstdc++"
    return flags

def clazy_cpp_args():
    return "-Wno-unused-value -Qunused-arguments -std=c++14 "

def more_clazy_args():
    return " -Xclang -plugin-arg-clang-lazy -Xclang no-inplace-fixits " + clazy_cpp_args()

def clazy_standalone_command(test, qt):
    result = " -- " + clazy_cpp_args() + qt.compiler_flags() + " " + test.flags
    result = " -no-inplace-fixits -checks=" + string.join(test.checks, ',') + " " + result

    if not test.isFixedFile:
        result = " -enable-all-fixits " + result

    if test.qt4compat:
        result = " -qt4-compat " + result

    if test.only_qt:
        result = " -only-qt " + result

    if test.qt_developer:
        result = " -qt-developer " + result

    if test.header_filter:
        result = " -header-filter " + test.header_filter + " " + result

    if test.ignore_dirs:
        result = " -ignore-dirs " + test.ignore_dirs + " " + result

    return result

def clazy_command(qt, test, filename):
    if test.isScript():
        return "./" + filename

    if 'CLAZY_CXX' in os.environ: # In case we want to use clazy.bat
        result = os.environ['CLAZY_CXX'] + more_clazy_args() + qt.compiler_flags()
    else:
        clang = os.getenv('CLANGXX', 'clang')
        result = clang + " -Xclang -load -Xclang " + libraryName() + " -Xclang -add-plugin -Xclang clang-lazy " + more_clazy_args() + qt.compiler_flags()

    if test.qt4compat:
        result = result + " -Xclang -plugin-arg-clang-lazy -Xclang qt4-compat "

    if test.only_qt:
        result = result + " -Xclang -plugin-arg-clang-lazy -Xclang only-qt "

    if test.qt_developer:
        result = result + " -Xclang -plugin-arg-clang-lazy -Xclang qt-developer "

    if test.link and _platform.startswith('linux'): # Linking on one platform is enough. Won't waste time on macOS and Windows.
        result = result + " " + link_flags()
    else:
        result = result + " -c "

    result = result + test.flags + " -Xclang -plugin-arg-clang-lazy -Xclang " + string.join(test.checks, ',') + " "
    if not test.isFixedFile: # When compiling the already fixed file disable fixit, we don't want to fix twice
        result += _enable_fixits_argument + " "
    result += filename

    return result

def dump_ast_command(test):
    return "clang -std=c++14 -fsyntax-only -Xclang -ast-dump -fno-color-diagnostics -c " + qt_installation(test.qt_major_version).compiler_flags() + " " + test.flags + " " + test.filename

def compiler_name():
    if 'CLAZY_CXX' in os.environ:
        return os.environ['CLAZY_CXX'] # so we can set clazy.bat instead
    return os.getenv('CLANGXX', 'clang')

#-------------------------------------------------------------------------------
# Get clang version
version,success = get_command_output(compiler_name() + ' --version')

match = re.search('clang version (.*?)[ -]', version)
try:
    version = match.group(1)
except:
    print "Could not determine clang version, is it in PATH?"
    sys.exit(-1)

CLANG_VERSION = int(version.replace('.', ''))

#-------------------------------------------------------------------------------
# Setup argparse

parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", action='store_true')
parser.add_argument("--no-standalone", action='store_true', help="Don\'t run clazy-standalone")
parser.add_argument("--only-standalone", action='store_true', help='Only run clazy-standalone')
parser.add_argument("--dump-ast", action='store_true', help='Dump a unit-test AST to file')
parser.add_argument("--exclude", help='Comma separated list of checks to ignore')
parser.add_argument("check_names", nargs='*', help="The name of the check who's unit-tests will be run. Defaults to running all checks.")
args = parser.parse_args()

if args.only_standalone and args.no_standalone:
    print "Error: --only-standalone is incompatible with --no-standalone"
    sys.exit(1)

#-------------------------------------------------------------------------------
# Global variables

_enable_fixits_argument = "-Xclang -plugin-arg-clang-lazy -Xclang enable-all-fixits"
_dump_ast = args.dump_ast
_verbose = args.verbose
_no_standalone = args.no_standalone
_only_standalone = args.only_standalone
_num_threads = multiprocessing.cpu_count()
_lock = threading.Lock()
_was_successful = True
_qt5_installation = find_qt_installation(5, ["QT_SELECT=5 qmake", "qmake-qt5", "qmake"])
_qt4_installation = find_qt_installation(4, ["QT_SELECT=4 qmake", "qmake-qt4", "qmake"])
_excluded_checks = args.exclude.split(',') if args.exclude is not None else []

#-------------------------------------------------------------------------------
# utility functions #2

def qt_installation(major_version):
    if major_version == 5:
        return _qt5_installation
    elif major_version == 4:
        return _qt4_installation

    return None

def run_command(cmd, output_file = "", test_env = os.environ):
    lines,success = get_command_output(cmd, test_env)
    lines = lines.replace("std::_Container_base0", "std::_Vector_base") # Hack for Windows, we have std::_Vector_base in the expected data
    lines = lines.replace("std::__1::__vector_base_common", "std::_Vector_base") # Hack for macOS
    lines = lines.replace("std::_Vector_alloc", "std::_Vector_base")
    if not success and not output_file:
        print lines
        return False

    if _verbose:
        print "Running: " + cmd
        print "output_file=" + output_file

    lines = lines.replace('\r\n', '\n')
    if output_file:
        f = open(output_file, 'w')
        f.writelines(lines)
        f.close()
    else:
        print lines

    return success

def files_are_equal(file1, file2):
    try:
        f = open(file1, 'r')
        lines1 = f.readlines()
        f.close()

        f = open(file2, 'r')
        lines2 = f.readlines()
        f.close()

        return lines1 == lines2
    except:
        return False

def get_check_names():
    return filter(lambda entry: os.path.isdir(entry), os.listdir("."))

# Returns all files with .cpp_fixed extension. These were rewritten by clang.
def get_fixed_files():
    return filter(lambda entry: entry.endswith('.cpp_fixed.cpp'), os.listdir("."))

def print_differences(file1, file2):
    # Returns true if the the files are equal
    return run_command("diff -Naur {} {}".format(file1, file2))

def normalizedCwd():
    return os.getcwd().replace('\\', '/')

def extract_word(word, in_file, out_file):
    in_f = open(in_file, 'r')
    out_f = open(out_file, 'w')
    for line in in_f:
        if word in line:
            line = line.replace('\\', '/')
            line = line.replace(normalizedCwd() + '/', "") # clazy-standalone prints the complete cpp file path for some reason. Normalize it so it compares OK with the expected output.
            out_f.write(line)
    in_f.close()
    out_f.close()

def print_file(filename):
    f = open(filename, 'r')
    print f.read()
    f.close()


def run_unit_test(test, is_standalone):
    if test.check.clazy_standalone_only and not is_standalone:
        return True

    qt = qt_installation(test.qt_major_version)

    if _verbose:
        print
        print "Qt version: " + str(qt.int_version)
        print "Qt headers: " + qt.qmake_header_path

    if qt.int_version < test.minimum_qt_version or qt.int_version > test.maximum_qt_version or CLANG_VERSION < test.minimum_clang_version:
        if (_verbose):
            print "Skipping " + test.check_name + " because required version is not available"
        return True

    if _platform in test.blacklist_platforms:
        if (_verbose):
            print "Skipping " + test.check_name + " because it is blacklisted for this platform"
        return True

    checkname = test.check.name
    filename = checkname + "/" + test.filename

    output_file = filename + ".out"
    result_file = filename + ".result"
    expected_file = filename + ".expected"

    if is_standalone and test.isScript():
        return True

    if is_standalone:
        cmd_to_run = "clazy-standalone " + filename + " " + clazy_standalone_command(test, qt)
    else:
        cmd_to_run = clazy_command(qt, test, filename)

    if test.compare_everything:
        result_file = output_file

    if test.isFixedFile:
        result_file = filename

    must_fail = test.must_fail

    cmd_success = run_command(cmd_to_run, output_file, test.env)

    if (not cmd_success and not must_fail) or (cmd_success and must_fail):
        print "[FAIL] " + checkname + " (Failed to build test. Check " + output_file + " for details)"
        print "-------------------"
        print "Contents of %s:" % output_file
        print_file(output_file)
        print "-------------------"
        print
        return False

    if not test.compare_everything and not test.isFixedFile:
        word_to_grep = "warning:" if not must_fail else "error:"
        extract_word(word_to_grep, output_file, result_file)

    printableName = checkname
    if len(test.check.tests) > 1:
        printableName += "/" + test.filename

    if is_standalone:
        printableName += " (standalone)"

    success = files_are_equal(expected_file, result_file)

    if test.expects_failure:
        if success:
            print "[XOK]   " + printableName
            return False
        else:
            print "[XFAIL] " + printableName
            print_differences(expected_file, result_file)
    else:
        if success:
            print "[OK]   " + printableName
        else:
            print "[FAIL] " + printableName
            print_differences(expected_file, result_file)
            return False

    return True

def run_unit_tests(tests):
    result = True
    for test in tests:
        if not _only_standalone:
            result = result and run_unit_test(test, False)

        if not _no_standalone:
            result = result and run_unit_test(test, True)

    global _was_successful, _lock
    with _lock:
        _was_successful = _was_successful and result

def dump_ast(check):
    for test in check.tests:
        ast_filename = test.filename + ".ast"
        run_command(dump_ast_command(test) + " > " + ast_filename)
        print "Dumped AST to " + os.getcwd() + "/" + ast_filename
#-------------------------------------------------------------------------------
def load_checks(all_check_names):
    checks = []
    for name in all_check_names:
        try:
            check = load_json(name)
            if check.enabled:
                checks.append(check)
        except:
            print "Error while loading " + name
            raise
            sys.exit(-1)
    return checks
#-------------------------------------------------------------------------------
# main

if 'CLAZY_NO_WERROR' in os.environ:
    del os.environ['CLAZY_NO_WERROR']

os.environ['CLAZY_CHECKS'] = ''

all_check_names = get_check_names()
all_checks = load_checks(all_check_names)
requested_check_names = args.check_names
requested_check_names = map(lambda x: x.strip("/\\"), requested_check_names)

for check_name in requested_check_names:
    if check_name not in all_check_names:
        print "Unknown check: " + check_name
        print
        sys.exit(-1)

if not requested_check_names:
    requested_check_names = all_check_names

requested_checks = filter(lambda check: check.name in requested_check_names and check.name not in _excluded_checks, all_checks)
requested_checks = filter(lambda check: check.minimum_clang_version <= CLANG_VERSION, requested_checks)

threads = []

if _dump_ast:
    for check in requested_checks:
        os.chdir(check.name)
        dump_ast(check)
        os.chdir("..")
else:
    list_of_chunks = [[] for x in range(_num_threads)]  # Each list is a list of Test to be worked on by a thread
    i = _num_threads
    for check in requested_checks:
        for test in check.tests:
            if not test.isFixedFile:
                i = (i + 1) % _num_threads

            list_of_chunks[i].append(test)

    for tests in list_of_chunks:
        if not tests:
            continue;

        t = Thread(target=run_unit_tests, args=(tests,))
        t.start()
        threads.append(t)

for thread in threads:
    thread.join()

if _was_successful:
    print "SUCCESS"
    sys.exit(0)
else:
    print "FAIL"
    sys.exit(-1)