Salome HOME
34a543f43d50b4671746a8af28193476cf358ccd
[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 def __runTest(command, workdir):
78   p = subprocess.Popen(command, cwd=workdir, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
79
80   while True:
81     try:
82       out = p.stdout.readline()
83       sys.stdout.write(out)
84     except: # raised IOError or OSError if output is empty
85       pass
86
87     returncode = p.poll()
88     if not returncode is None:
89       sys.stdout.flush()
90       break
91     pass
92
93   return p.returncode
94 #
95
96 def runTests(args, exe=None):
97   args = __configureTests(args, exe)
98
99   appliPath = os.getenv("ABSOLUTE_APPLI_PATH")
100   if not appliPath:
101       raise SalomeContextException("Unable to find application path. Please check that the variable ABSOLUTE_APPLI_PATH is set.")
102
103   testPath = os.path.join(appliPath, __testSubDir)
104
105   command = ["ctest"] + args
106   res = __runTest(command, testPath)
107
108   sys.exit(res)
109 #