Salome HOME
updated copyright message
[modules/kernel.git] / bin / appliskel / tests / salomeCommand / TestLauncherSessionArgs.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2013-2023  CEA, EDF, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 import unittest
22
23 import os
24 import sys
25 import imp
26 from io import StringIO
27 import logging
28
29 logger = logging.getLogger("TestLauncherLogger")
30 logger.level = logging.DEBUG
31 logger.addHandler(logging.StreamHandler())
32
33 class TestSessionArgs(unittest.TestCase):
34   #
35   @classmethod
36   def setUpClass(cls):
37     # Set some predefined command args and corresponding output messages
38     cls.hello0 = ["hello.py", "args:outfile=LOGFILE"]
39     cls.hello0Msg = "Hello!"
40     cls.hello1 = ["hello.py", "args:you,outfile=LOGFILE"]
41     cls.hello1Msg = "Hello to: you"
42     cls.helloToAdd = ["hello.py", "args:add.py,1,2,3,outfile=LOGFILE"]
43     cls.helloToAddMsg = "Hello to: add.py, 1, 2, 3"
44     cls.helloToList = ["hello.py", "args:['file1','file2'],1,2,3,[True,False],'ok',outfile=LOGFILE"]
45     cls.helloToListMsg = "Hello to: ['file1','file2'], 1, 2, 3, [True,False], 'ok'"
46     cls.add0 = ["add.py", "args:outfile=LOGFILE"]
47     cls.add0Msg = "No args!"
48     cls.add3 = ["add.py", "args:1,2,3,outfile=LOGFILE"]
49     cls.add3Msg = "1+2+3 = 6"
50     cls.lines0 = ["lines.py", "args:outfile=LOGFILE"]
51     cls.lines0Msg = "No files given"
52     cls.lines2 = ["lines.py", "args:hello.py,add.py,outfile=LOGFILE"]
53     cls.lines2Msg = "hello.py is 35 lines longadd.py is 37 lines long"
54     cls.linesUnreadable = ["lines.py", "args:hello.py,add.py,1,2,outfile=LOGFILE"]
55     cls.linesUnreadableMsg = "hello.py is 35 lines longadd.py is 37 lines longFile '1' cannot be readFile '2' cannot be read"
56     sys.stdout = StringIO()
57   #
58   def setUp(self):
59     import tempfile
60     self.logFile = tempfile.NamedTemporaryFile()
61     if sys.platform == "win32": # Close file because of permission denined on Windows
62         self.logFile.close()
63   #
64   def tearDown(self):
65     self.logFile.close()
66   #
67   def session(self, args=None):
68     if args is None:
69       args = []
70     args = [x.replace("LOGFILE",self.logFile.name) for x in args]
71     try:
72       import setenv
73       setenv.main(True)
74       import runSession
75       params, args = runSession.configureSession(args, exe="salome shell")
76       return runSession.runSession(params, args)
77     except SystemExit as e:
78       if str(e) != '0':
79         logger.error(e)
80       import traceback
81       traceback.print_exc()
82       pass
83   #
84   def assertLogFileContentsEqual(self, message):
85     with open(self.logFile.name, "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 testHelloToList(self):
123     self.session(self.helloToList)
124     self.assertLogFileContentsEqual(self.helloToListMsg)
125   #
126   def testLines0(self):
127     self.session(self.lines0)
128     self.assertLogFileContentsEqual(self.lines0Msg)
129   #
130   def testLines2(self):
131     self.session(self.lines2)
132     self.assertLogFileContentsEqual(self.lines2Msg)
133   #
134   def testLines2Add3(self):
135     self.session(self.lines2+self.add3)
136     self.assertLogFileContentsEqual(self.lines2Msg+self.add3Msg)
137   #
138   def testLinesUnreadable(self):
139     self.session(self.linesUnreadable)
140     self.assertLogFileContentsEqual(self.linesUnreadableMsg)
141   #
142   def testAddAddHello(self):
143     self.session(self.add3+self.add3+self.hello1)
144     self.assertLogFileContentsEqual(self.add3Msg+self.add3Msg+self.hello1Msg)
145   #
146   def testHello0Add3Hello0Add3Hello0(self):
147     self.session(self.hello1+self.add3+self.hello0+self.add3+self.hello0)
148     self.assertLogFileContentsEqual(self.hello1Msg+self.add3Msg+self.hello0Msg+self.add3Msg+self.hello0Msg)
149   #
150 #
151
152 if __name__ == "__main__":
153   unittest.main()
154 #