Salome HOME
updated copyright message
[modules/kernel.git] / src / UnitTests / prepare_test.py
1 #! /usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2014-2023  CEA, EDF
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
22 usage="""
23 This script prepares the test environment and runs a test script:
24  - clean and create test directory
25  - create a SALOME application
26  - launch salome
27  - launch the test script within SALOME environment
28  - kill salome
29
30  This script uses the following environment variables:
31    - ROOT_SALOME : directory which contains salome_context.cfg.
32                   This variable is usually defined in salome_prerequisites.sh
33    - KERNEL_ROOT_DIR and YACS_ROOT_DIR : directories of modules installation
34                   Those variables are usually defined in salome_modules.sh
35  Environment variables can be passed to the script using the -d option.
36 """
37
38 import os
39 import sys
40
41 class TestEnvironment:
42   def setUp(self):
43     import shutil
44     shutil.rmtree("appli", ignore_errors=True)
45     
46     # create config_appli.xml in current directory
47     salome_path = os.getenv("ROOT_SALOME", "")
48     salome_context_file = os.path.join(salome_path, "salome_context.cfg")
49     if not os.path.isfile(salome_context_file):
50       print("File salome_context.cfg not found.")
51       print("Search path:" + salome_path)
52       print("This test needs ROOT_SALOME environment variable in order to run")
53       exit(0)
54     
55     config_appli_text = '''<application>
56 <context path="''' + salome_context_file + '''"/>
57 <modules>
58    <module name="KERNEL" path="'''
59     kernel_path = os.getenv("KERNEL_ROOT_DIR", "")
60     if not os.path.isdir(kernel_path) :
61       print("KERNEL_ROOT_DIR not defined")
62       exit(0)
63       pass
64     
65     config_appli_text += kernel_path + '"/>'
66         
67     # some tests need YACS module.
68     yacs_path = os.getenv("YACS_ROOT_DIR", "")
69     if os.path.isdir(yacs_path):
70       config_appli_text += '''
71    <module name="YACS" path="'''
72       config_appli_text += yacs_path + '"/>'
73       pass
74     config_appli_text += '''
75 </modules>
76 </application>'''
77
78     f = open("config_appli.xml", 'w')
79     f.write(config_appli_text)
80     f.close()
81     
82     # create a SALOME application
83     appli_gen_file = os.path.join(kernel_path,
84                                   "bin","salome","appli_gen.py")
85     appli_dir = "appli"
86     os.system(appli_gen_file + " --prefix="+appli_dir+
87                                " --config=config_appli.xml")
88     
89     # start salome
90     import imp
91     sys.path[:0] = [os.path.join(appli_dir, "bin", "salome", "appliskel")]
92     self.salome_module = imp.load_source("SALOME", os.path.join(appli_dir, "salome"))
93     try:
94       self.salome_module.main(["start", "-t"])
95     except SystemExit as e:
96       # There is an exit() call in salome.main. Just ignore it.
97       pass
98     
99   def run(self, script):
100     ret = 0
101     try:
102       ret = self.salome_module.main(["shell", script])
103     except SystemExit as e:
104       # return exit value
105       ret = e.code
106     return ret
107   
108   def tearDown(self):
109     try:
110       self.salome_module.main(["killall"])
111     except SystemExit as e:
112       pass
113     pass
114   pass #class TestEnvironment
115
116 if __name__ == '__main__':
117   import argparse
118   parser = argparse.ArgumentParser(description=usage,
119                     formatter_class=argparse.RawDescriptionHelpFormatter)
120   parser.add_argument('command', help="Test command to be run.")
121   parser.add_argument('-d', '--define', action='append', help="VARIABLE=VALUE")
122
123   args = parser.parse_args()
124   for opt in args.define:
125     opts = opt.split('=', 1)
126     os.environ[opts[0]] = opts[1]
127   
128   envTest = TestEnvironment()
129   envTest.setUp()
130   ret = envTest.run(args.command)
131   envTest.tearDown()
132   exit(ret)
133   pass
134