Salome HOME
tests for salome command
[modules/kernel.git] / bin / appliskel / tests / salomeCommand / TestLauncherSessionArgs.py
1 # Copyright (C) 2013-2015  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     from salome_instance import SalomeInstance
55     self.instance = SalomeInstance.start()
56     print "Instance created and now running on port", self.instance.get_port()
57
58     sys.stdout = StringIO()
59     self.removeLogFile()
60   #
61   def tearDown(self):
62     self.removeLogFile()
63     print "Terminate instance running on port", self.instance.get_port()
64     self.instance.stop()
65   #
66   def session(self, args=None):
67     if args is None:
68       args = []
69     try:
70       import setenv
71       setenv.main(True)
72       import runSession
73       params, args = runSession.configureSession(args, exe="salome shell")
74       return runSession.runSession(params, args)
75     except SystemExit, e:
76       if str(e) != '0':
77         logger.error(e)
78       pass
79   #
80   def removeLogFile(self):
81     try:
82       os.remove(self.logFile)
83     except OSError:
84       pass
85   #
86   def assertLogFileContentsEqual(self, message):
87     with open(self.logFile, "r") as f:
88       contents = f.read().replace('\n', '')
89
90     self.assertTrue(contents==message, "Contents differ!\n\tGenerated contents: %s\n\tExpected contents: %s"%(contents, message))
91   #
92   def testHello0(self):
93     self.session(self.hello0)
94     self.assertLogFileContentsEqual(self.hello0Msg)
95   #
96   def testPythonHello0(self):
97     self.session(["python"]+self.hello0)
98     self.assertLogFileContentsEqual(self.hello0Msg)
99   #
100   def testHello1(self):
101     self.session(self.hello1)
102     self.assertLogFileContentsEqual(self.hello1Msg)
103   #
104   def testAdd0(self):
105     self.session(self.add0)
106     self.assertLogFileContentsEqual(self.add0Msg)
107   #
108   def testAdd3(self):
109     self.session(self.add3)
110     self.assertLogFileContentsEqual(self.add3Msg)
111   #
112   def testHello0Add3(self):
113     self.session(self.hello0+self.add3)
114     self.assertLogFileContentsEqual(self.hello0Msg+self.add3Msg)
115   #
116   def testHello1Add3(self):
117     self.session(self.hello1+self.add3)
118     self.assertLogFileContentsEqual(self.hello1Msg+self.add3Msg)
119   #
120   def testHelloToAdd(self):
121     self.session(self.helloToAdd)
122     self.assertLogFileContentsEqual(self.helloToAddMsg)
123   #
124   def testLines0(self):
125     self.session(self.lines0)
126     self.assertLogFileContentsEqual(self.lines0Msg)
127   #
128   def testLines2(self):
129     self.session(self.lines2)
130     self.assertLogFileContentsEqual(self.lines2Msg)
131   #
132   def testLines2Add3(self):
133     self.session(self.lines2+self.add3)
134     self.assertLogFileContentsEqual(self.lines2Msg+self.add3Msg)
135   #
136   def testLinesUnreadable(self):
137     self.session(self.linesUnreadable)
138     self.assertLogFileContentsEqual(self.linesUnreadableMsg)
139   #
140   def testAddAddHello(self):
141     self.session(self.add3+self.add3+self.hello1)
142     self.assertLogFileContentsEqual(self.add3Msg+self.add3Msg+self.hello1Msg)
143   #
144   def testHello0Add3Hello0Add3Hello0(self):
145     self.session(self.hello1+self.add3+self.hello0+self.add3+self.hello0)
146     self.assertLogFileContentsEqual(self.hello1Msg+self.add3Msg+self.hello0Msg+self.add3Msg+self.hello0Msg)
147   #
148 #
149
150 if __name__ == "__main__":
151   unittest.main()
152 #