~reducedmodelling/fluidity/ReducedModel

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
#!/usr/bin/env python

import os
import os.path
import glob

class UnitTest:
    def __init__(self, exe):
        self.exe = os.curdir + os.sep + exe.split(os.sep)[-1]
        self.verbose = True
        self.dir = os.sep.join((os.curdir + os.sep + exe).split(os.sep)[:-1])
        self.cwd = os.getcwd()

    def log(self, msg):
        if self.verbose and msg != '':
            print "    %s: %s" % (self.exe.split('/')[-1], msg)

    def run(self):
        os.chdir(self.dir)
#        self.log("chdir " + self.dir)
        self.log("Running")
        f = os.popen(self.exe)
        self.output = f.read()
        os.chdir(self.cwd)
        
        exitStatus = f.close()
        if exitStatus is None:
          return 0
        else:
          return exitStatus

    def parse(self):
        passcount = 0
        warncount = 0
        failcount = 0

        for line in self.output.split('\n'):
            line = line.lstrip()
            self.log(line)
            if line.startswith("Pass"): passcount = passcount + 1
            if line.startswith("Warn"): warncount = warncount + 1
            if line.startswith("Fail"): failcount = failcount + 1

        return (passcount, warncount, failcount)

class UnitTestHarness:
    def __init__(self, dir):
        self.tests = []
        if dir[-1] == '/': dir = dir + "*"
        else: dir = dir + "/*"

        files = glob.glob(dir)
        for file in files:
            if not os.path.isdir(file):
                self.tests.append(UnitTest(file))

    def run(self):
        passcount = 0
        warncount = 0
        failcount = 0

        warntests = []
        failtests = []

        for test in self.tests:
            exitStatus = test.run()
            
            (P, W, F) = test.parse()

            if (P, W, F) == (0, 0, 0):
              print "    WARNING: no output from test"
              warncount += 1
              warntests.append(test.exe)

            if W > 0:
              warntests.append(test.exe)

            if F > 0:
              failtests.append(test.exe)
              
            if not exitStatus == 0:
              print "    ERROR: non-zero exit code from test"
              failcount += 1
              if not test.exe in failtests:
	        failtests.append(test.exe)

            passcount += P
            warncount += W
            failcount += F

        print "RESULTS"
        print "    Passes:   %d" % passcount
        if len(warntests) == 0:
            print "    Warnings: %d" % warncount
        else:
            print "    Warnings: %d; tests = %s" % (warncount, warntests)
        if len(failtests) == 0:
            print "    Failures: %d" % failcount
        else:
            print "    Failures: %d; tests = %s" % (failcount, failtests)

if __name__ == "__main__":
    import sys

    try:
        os.environ["PYTHONPATH"] = os.path.abspath(os.path.join(os.getcwd(), "python")) + ":" + os.environ["PYTHONPATH"]
    except KeyError:
        os.putenv("PYTHONPATH", os.path.abspath(os.path.join(os.getcwd(), "python")))

    try:
        os.environ["LD_LIBRARY_PATH"] = os.getcwd() + os.sep + sys.argv[1] + os.sep + "lib:" + os.environ["LD_LIBRARY_PATH"]
    except KeyError:
        os.putenv("LD_LIBRARY_PATH", os.getcwd() + os.sep + sys.argv[1] + os.sep + "lib")

    if "--electricfence" in sys.argv:
      os.putenv("LD_PRELOAD", "/usr/lib/libefence.so.0.0")
      #os.putenv("EF_DISABLE_BANNER", "1")

    TestHarness = UnitTestHarness(sys.argv[-1])
    TestHarness.run()