Salome HOME
Merge branch 'V7_dev'
[modules/kernel.git] / bin / runTests.py
1 # Copyright (C) 2015-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 import os
21 import sys
22 import select
23 import subprocess
24
25 def __configureTests(args=None, exe=None):
26   if args is None:
27     args = []
28   if exe:
29       usage = "Usage: %s [options]"%exe
30   else:
31       usage = "Usage: %prog [options]"
32   epilog  = """\n
33 Run tests of SALOME components provided with application.\n
34 Principal options are:
35     -h,--help
36         Show this help message and exit.
37
38     --print-labels
39         Print the list of all labels associated with the test set.
40         This option will not run any tests.
41
42     -V,--verbose
43         Enable verbose output from tests.
44     -VV,--extra-verbose
45         Enable more verbose output from tests.
46     -Q,--quiet
47         Suppress all output.
48
49     -N,--show-only
50         Show available tests (without running them).
51
52     -R <regex>, --tests-regex <regex>
53         Run tests matching regular expression.
54     -E <regex>, --exclude-regex <regex>
55         Exclude tests matching regular expression.
56
57     -L <regex>, --label-regex <regex>
58         Run tests with labels matching regular expression.
59     -LE <regex>, --label-exclude <regex>
60         Exclude tests with labels matching regular expression.
61
62 For complete description of available options, pleaser refer to ctest documentation.\n
63 """
64   if not args:
65     return []
66
67   if args[0] in ["-h", "--help"]:
68     print usage + epilog
69     sys.exit(0)
70
71   return args
72 #
73
74 # tests must be in ${ABSOLUTE_APPLI_PATH}/${__testSubDir}/
75 __testSubDir = "bin/salome/test"
76
77 # Both display process stdout&stderr to console and capture them to variables
78 def __runTest(command, workdir):
79   p = subprocess.Popen(command, cwd=workdir, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0)
80   stdout = []
81   stderr = []
82
83   while True:
84     reads = [p.stdout.fileno(), p.stderr.fileno()]
85     ret = select.select(reads, [], [], 0)
86
87     for fd in ret[0]:
88       if fd == p.stdout.fileno():
89         read = p.stdout.readline()
90         sys.stdout.write(read)
91         stdout.append(read)
92         pass
93       if fd == p.stderr.fileno():
94         read = p.stderr.readline()
95         sys.stderr.write(read)
96         stderr.append(read)
97         pass
98       pass
99
100     returncode = p.poll()
101     if not returncode is None:
102       break
103     pass
104
105   return p.returncode, "".join(stdout), "".join(stderr)
106 #
107
108 def runTests(args, exe=None):
109   args = __configureTests(args, exe)
110
111   appliPath = os.getenv("ABSOLUTE_APPLI_PATH")
112   if not appliPath:
113       raise SalomeContextException("Unable to find application path. Please check that the variable ABSOLUTE_APPLI_PATH is set.")
114
115   testPath = os.path.join(appliPath, __testSubDir)
116
117   command = ["ctest"] + args
118   res, out, err = __runTest(command, testPath)
119
120   sys.exit(res)
121 #