Salome HOME
20445e9df9a9e2b74cede9ed59863e6946e5485b
[modules/kernel.git] / bin / appliskel / tests / launcher / TestLauncherSessionArgs.py
1 # Copyright (C) 2013-2014  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 unittest
21
22 import os
23 import sys
24 import imp
25 from cStringIO import StringIO
26 import logging
27
28 logger = logging.getLogger("TestLauncherLogger")
29 logger.level = logging.DEBUG
30 logger.addHandler(logging.StreamHandler())
31
32 class TestSessionArgs(unittest.TestCase):
33   #
34   logFile = "log.txt"
35   # Set some predefined command args and corresponding output messages
36   hello0 = ["hello.py", "args:outfile="+logFile]
37   hello0Msg = "Hello!"
38   hello1 = ["hello.py", "args:you,outfile="+logFile]
39   hello1Msg = "Hello to: you"
40   helloToAdd = ["hello.py", "args:add.py,1,2,3,outfile="+logFile]
41   helloToAddMsg = "Hello to: add.py, 1, 2, 3"
42   add0 = ["add.py", "args:outfile="+logFile]
43   add0Msg = "No args!"
44   add3 = ["add.py", "args:1,2,3,outfile="+logFile]
45   add3Msg = "1+2+3 = 6"
46   lines0 = ["lines.py", "args:outfile="+logFile]
47   lines0Msg = "No files given"
48   lines2 = ["lines.py", "args:hello.py,add.py,outfile="+logFile]
49   lines2Msg = "hello.py is 35 lines longadd.py is 37 lines long"
50   linesUnreadable = ["lines.py", "args:hello.py,add.py,1,2,outfile="+logFile]
51   linesUnreadableMsg = "hello.py is 35 lines longadd.py is 37 lines longFile '1' cannot be readFile '2' cannot be read"
52   #
53   def setUp(self):
54     # Initialize path to SALOME application
55     path_to_launcher = os.getenv("SALOME_LAUNCHER")
56     appli_dir = os.path.dirname(path_to_launcher)
57     envd_dir = os.path.join(appli_dir, "env.d")
58     sys.path[:0] = [os.path.join(appli_dir, "bin", "salome", "appliskel")]
59
60     # Configure session startup
61     self.SALOME = imp.load_source("SALOME", os.path.join(appli_dir,"salome"))
62     self.SALOME_args = ["shell", "--config="+envd_dir]
63
64     sys.stdout = StringIO()
65     self.removeLogFile()
66   #
67   def tearDown(self):
68     self.removeLogFile()
69   #
70   def session(self, args=[]):
71     try:
72       self.SALOME.main(self.SALOME_args + args)
73     except SystemExit, e:
74       if str(e) != '0':
75         logger.error(e)
76       pass
77   #
78   def removeLogFile(self):
79     try:
80       os.remove(self.logFile)
81     except OSError:
82       pass
83   #
84   def assertLogFileContentsEqual(self, message):
85     with open(self.logFile, "r") as f:
86       contents = f.read().replace('\n', '')
87
88     self.assertTrue(contents==message, "Contents differ!\n\tGenerated contents: %s\n\tExpected contents: %s"%(contents, message))
89   #
90   def testHello0(self):
91     self.session(self.hello0)
92     self.assertLogFileContentsEqual(self.hello0Msg)
93   #
94   def testPythonHello0(self):
95     self.session(["python"]+self.hello0)
96     self.assertLogFileContentsEqual(self.hello0Msg)
97   #
98   def testHello1(self):
99     self.session(self.hello1)
100     self.assertLogFileContentsEqual(self.hello1Msg)
101   #
102   def testAdd0(self):
103     self.session(self.add0)
104     self.assertLogFileContentsEqual(self.add0Msg)
105   #
106   def testAdd3(self):
107     self.session(self.add3)
108     self.assertLogFileContentsEqual(self.add3Msg)
109   #
110   def testHello0Add3(self):
111     self.session(self.hello0+self.add3)
112     self.assertLogFileContentsEqual(self.hello0Msg+self.add3Msg)
113   #
114   def testHello1Add3(self):
115     self.session(self.hello1+self.add3)
116     self.assertLogFileContentsEqual(self.hello1Msg+self.add3Msg)
117   #
118   def testHelloToAdd(self):
119     self.session(self.helloToAdd)
120     self.assertLogFileContentsEqual(self.helloToAddMsg)
121   #
122   def testLines0(self):
123     self.session(self.lines0)
124     self.assertLogFileContentsEqual(self.lines0Msg)
125   #
126   def testLines2(self):
127     self.session(self.lines2)
128     self.assertLogFileContentsEqual(self.lines2Msg)
129   #
130   def testLines2Add3(self):
131     self.session(self.lines2+self.add3)
132     self.assertLogFileContentsEqual(self.lines2Msg+self.add3Msg)
133   #
134   def testLinesUnreadable(self):
135     self.session(self.linesUnreadable)
136     self.assertLogFileContentsEqual(self.linesUnreadableMsg)
137   #
138   def testAddAddHello(self):
139     self.session(self.add3+self.add3+self.hello1)
140     self.assertLogFileContentsEqual(self.add3Msg+self.add3Msg+self.hello1Msg)
141   #
142   def testHello0Add3Hello0Add3Hello0(self):
143     self.session(self.hello1+self.add3+self.hello0+self.add3+self.hello0)
144     self.assertLogFileContentsEqual(self.hello1Msg+self.add3Msg+self.hello0Msg+self.add3Msg+self.hello0Msg)
145   #
146 #
147
148
149 if __name__ == "__main__":
150   path_to_launcher = os.getenv("SALOME_LAUNCHER")
151   if not path_to_launcher:
152     msg = "Error: please set SALOME_LAUNCHER variable to the salome command of your application folder."
153     raise Exception(msg)
154
155   unittest.main()
156 #