]> SALOME platform Git repositories - modules/yacs.git/blob - src/UnitTests/prepare_test.py
Salome HOME
59bc0dca9071365e1ebb489dd40ffb3d9f0e14b0
[modules/yacs.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(1)
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(1)
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       cls.hasYacs = True
53       config_appli_text += '''
54    <module name="YACS" path="'''
55       config_appli_text += yacs_path + '"/>'
56       pass
57     config_appli_text += '''
58 </modules>
59 </application>'''
60
61     f = open("config_appli.xml", 'w')
62     f.write(config_appli_text)
63     f.close()
64     
65     # create a SALOME application
66     appli_gen_file = os.path.join(kernel_path,
67                                   "bin","salome","appli_gen.py")
68     appli_dir = "appli"
69     os.system(appli_gen_file + " --prefix="+appli_dir+
70                                " --config=config_appli.xml")
71     
72     # start salome
73     import imp
74     sys.path[:0] = [os.path.join(appli_dir, "bin", "salome", "appliskel")]
75     self.salome_module = imp.load_source("SALOME", os.path.join(appli_dir, "salome"))
76     try:
77       self.salome_module.main(["start", "-t"])
78     except SystemExit, e:
79       # There is an exit() call in salome.main. Just ignore it.
80       pass
81     
82   def run(self, script):
83     ret = 0
84     try:
85       ret = self.salome_module.main(["shell", script])
86     except SystemExit, e:
87       # return exit value
88       ret = e.code
89     return ret
90   
91   def tearDown(self):
92     try:
93       self.salome_module.main(["killall"])
94     except SystemExit, e:
95       pass
96     pass
97   pass #class TestEnvironment
98
99 if __name__ == '__main__':
100   import argparse
101   parser = argparse.ArgumentParser(description=usage,
102                     formatter_class=argparse.RawDescriptionHelpFormatter)
103   parser.add_argument('command', help="Test command to be run.")
104   parser.add_argument('-d', '--define', action='append', help="VARIABLE=VALUE")
105
106   args = parser.parse_args()
107   for opt in args.define:
108     opts = opt.split('=', 1)
109     os.environ[opts[0]] = opts[1]
110   
111   envTest = TestEnvironment()
112   envTest.setUp()
113   ret = envTest.run(args.command)
114   envTest.tearDown()
115   exit(ret)
116   pass
117