Salome HOME
4a9f365a003578df01aef538f9cb92f3ec7aa072
[modules/kernel.git] / bin / appliskel / tests / salomeCommand / TestLauncherSessionArgs.py
1 # Copyright (C) 2013-2016  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 io 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   @classmethod
35   def setUpClass(cls):
36     # Set some predefined command args and corresponding output messages
37     cls.hello0 = ["hello.py", "args:outfile=LOGFILE"]
38     cls.hello0Msg = "Hello!"
39     cls.hello1 = ["hello.py", "args:you,outfile=LOGFILE"]
40     cls.hello1Msg = "Hello to: you"
41     cls.helloToAdd = ["hello.py", "args:add.py,1,2,3,outfile=LOGFILE"]
42     cls.helloToAddMsg = "Hello to: add.py, 1, 2, 3"
43     cls.helloToList = ["hello.py", "args:['file1','file2'],1,2,3,[True,False],'ok',outfile=LOGFILE"]
44     cls.helloToListMsg = "Hello to: ['file1','file2'], 1, 2, 3, [True,False], 'ok'"
45     cls.add0 = ["add.py", "args:outfile=LOGFILE"]
46     cls.add0Msg = "No args!"
47     cls.add3 = ["add.py", "args:1,2,3,outfile=LOGFILE"]
48     cls.add3Msg = "1+2+3 = 6"
49     cls.lines0 = ["lines.py", "args:outfile=LOGFILE"]
50     cls.lines0Msg = "No files given"
51     cls.lines2 = ["lines.py", "args:hello.py,add.py,outfile=LOGFILE"]
52     cls.lines2Msg = "hello.py is 35 lines longadd.py is 37 lines long"
53     cls.linesUnreadable = ["lines.py", "args:hello.py,add.py,1,2,outfile=LOGFILE"]
54     cls.linesUnreadableMsg = "hello.py is 35 lines longadd.py is 37 lines longFile '1' cannot be readFile '2' cannot be read"
55     sys.stdout = StringIO()
56   #
57   def setUp(self):
58     import tempfile
59     self.logFile = tempfile.NamedTemporaryFile()
60   #
61   def tearDown(self):
62     self.logFile.close()
63   #
64   def session(self, args=None):
65     if args is None:
66       args = []
67     args = [x.replace("LOGFILE",self.logFile.name) for x in args]
68     try:
69       import setenv
70       setenv.main(True)
71       import runSession
72       params, args = runSession.configureSession(args, exe="salome shell")
73       return runSession.runSession(params, args)
74     except SystemExit as e:
75       if str(e) != '0':
76         logger.error(e)
77       import traceback
78       traceback.print_exc()
79       pass
80   #
81   def assertLogFileContentsEqual(self, message):
82     with open(self.logFile.name, "r") as f:
83       contents = f.read().replace('\n', '')
84
85     self.assertTrue(contents==message, "Contents differ!\n\tGenerated contents: %s\n\tExpected contents: %s"%(contents, message))
86   #
87   def testHello0(self):
88     self.session(self.hello0)
89     self.assertLogFileContentsEqual(self.hello0Msg)
90   #
91   def testPythonHello0(self):
92     self.session(["python"]+self.hello0)
93     self.assertLogFileContentsEqual(self.hello0Msg)
94   #
95   def testHello1(self):
96     self.session(self.hello1)
97     self.assertLogFileContentsEqual(self.hello1Msg)
98   #
99   def testAdd0(self):
100     self.session(self.add0)
101     self.assertLogFileContentsEqual(self.add0Msg)
102   #
103   def testAdd3(self):
104     self.session(self.add3)
105     self.assertLogFileContentsEqual(self.add3Msg)
106   #
107   def testHello0Add3(self):
108     self.session(self.hello0+self.add3)
109     self.assertLogFileContentsEqual(self.hello0Msg+self.add3Msg)
110   #
111   def testHello1Add3(self):
112     self.session(self.hello1+self.add3)
113     self.assertLogFileContentsEqual(self.hello1Msg+self.add3Msg)
114   #
115   def testHelloToAdd(self):
116     self.session(self.helloToAdd)
117     self.assertLogFileContentsEqual(self.helloToAddMsg)
118   #
119   def testHelloToList(self):
120     self.session(self.helloToList)
121     self.assertLogFileContentsEqual(self.helloToListMsg)
122   #
123   def testLines0(self):
124     self.session(self.lines0)
125     self.assertLogFileContentsEqual(self.lines0Msg)
126   #
127   def testLines2(self):
128     self.session(self.lines2)
129     self.assertLogFileContentsEqual(self.lines2Msg)
130   #
131   def testLines2Add3(self):
132     self.session(self.lines2+self.add3)
133     self.assertLogFileContentsEqual(self.lines2Msg+self.add3Msg)
134   #
135   def testLinesUnreadable(self):
136     self.session(self.linesUnreadable)
137     self.assertLogFileContentsEqual(self.linesUnreadableMsg)
138   #
139   def testAddAddHello(self):
140     self.session(self.add3+self.add3+self.hello1)
141     self.assertLogFileContentsEqual(self.add3Msg+self.add3Msg+self.hello1Msg)
142   #
143   def testHello0Add3Hello0Add3Hello0(self):
144     self.session(self.hello1+self.add3+self.hello0+self.add3+self.hello0)
145     self.assertLogFileContentsEqual(self.hello1Msg+self.add3Msg+self.hello0Msg+self.add3Msg+self.hello0Msg)
146   #
147 #
148
149 if __name__ == "__main__":
150   unittest.main()
151 #