]> SALOME platform Git repositories - modules/kernel.git/blob - src/UnitTests/prepare_test.py
Salome HOME
Merge remote branch 'origin/V7_dev'
[modules/kernel.git] / src / UnitTests / prepare_test.py
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 usage="""
5 This script prepares the test environment and runs a test script:
6  - clean and create test directory
7  - create a SALOME application
8  - launch salome
9  - launch the test script within SALOME environment
10  - kill salome
11
12  This script uses the following environment variables:
13    - ROOT_SALOME : directory which contains salome_context.cfg.
14                   This variable is usually defined in salome_prerequisites.sh
15    - KERNEL_ROOT_DIR and YACS_ROOT_DIR : directories of modules installation
16                   Those variables are usually defined in salome_modules.sh
17  Environment variables can be passed to the script using the -d option.
18 """
19
20 import os
21 import sys
22
23 class TestEnvironment:
24   def setUp(self):
25     import shutil
26     shutil.rmtree("appli", ignore_errors=True)
27     
28     # create config_appli.xml in current directory
29     salome_path = os.getenv("ROOT_SALOME", "")
30     salome_context_file = os.path.join(salome_path, "salome_context.cfg")
31     if not os.path.isfile(salome_context_file):
32       print "File salome_context.cfg not found."
33       print "Search path:" + salome_path
34       print "This test needs ROOT_SALOME environment variable in order to run"
35       exit(0)
36     
37     config_appli_text = '''<application>
38 <context path="''' + salome_context_file + '''"/>
39 <modules>
40    <module name="KERNEL" path="'''
41     kernel_path = os.getenv("KERNEL_ROOT_DIR", "")
42     if not os.path.isdir(kernel_path) :
43       print "KERNEL_ROOT_DIR not defined"
44       exit(0)
45       pass
46     
47     config_appli_text += kernel_path + '"/>'
48         
49     # some tests need YACS module.
50     yacs_path = os.getenv("YACS_ROOT_DIR", "")
51     if os.path.isdir(yacs_path):
52       config_appli_text += '''
53    <module name="YACS" path="'''
54       config_appli_text += yacs_path + '"/>'
55       pass
56     config_appli_text += '''
57 </modules>
58 </application>'''
59
60     f = open("config_appli.xml", 'w')
61     f.write(config_appli_text)
62     f.close()
63     
64     # create a SALOME application
65     appli_gen_file = os.path.join(kernel_path,
66                                   "bin","salome","appli_gen.py")
67     appli_dir = "appli"
68     os.system(appli_gen_file + " --prefix="+appli_dir+
69                                " --config=config_appli.xml")
70     
71     # start salome
72     import imp
73     sys.path[:0] = [os.path.join(appli_dir, "bin", "salome", "appliskel")]
74     self.salome_module = imp.load_source("SALOME", os.path.join(appli_dir, "salome"))
75     try:
76       self.salome_module.main(["start", "-t"])
77     except SystemExit, e:
78       # There is an exit() call in salome.main. Just ignore it.
79       pass
80     
81   def run(self, script):
82     ret = 0
83     try:
84       ret = self.salome_module.main(["shell", script])
85     except SystemExit, e:
86       # return exit value
87       ret = e.code
88     return ret
89   
90   def tearDown(self):
91     try:
92       self.salome_module.main(["killall"])
93     except SystemExit, e:
94       pass
95     pass
96   pass #class TestEnvironment
97
98 if __name__ == '__main__':
99   import argparse
100   parser = argparse.ArgumentParser(description=usage,
101                     formatter_class=argparse.RawDescriptionHelpFormatter)
102   parser.add_argument('command', help="Test command to be run.")
103   parser.add_argument('-d', '--define', action='append', help="VARIABLE=VALUE")
104
105   args = parser.parse_args()
106   for opt in args.define:
107     opts = opt.split('=', 1)
108     os.environ[opts[0]] = opts[1]
109   
110   envTest = TestEnvironment()
111   envTest.setUp()
112   ret = envTest.run(args.command)
113   envTest.tearDown()
114   exit(ret)
115   pass
116