2 # Copyright (C) 2013-2024 CEA, EDF, OPEN CASCADE
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
26 from io import StringIO
29 logger = logging.getLogger("TestLauncherLogger")
30 logger.level = logging.DEBUG
31 logger.addHandler(logging.StreamHandler())
33 class TestSessionArgs(unittest.TestCase):
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()
60 self.logFile = tempfile.NamedTemporaryFile()
61 if sys.platform == "win32": # Close file because of permission denined on Windows
67 def session(self, args=None):
70 args = [x.replace("LOGFILE",self.logFile.name) for x in args]
75 params, args = runSession.configureSession(args, exe="salome shell")
76 return runSession.runSession(params, args)
77 except SystemExit as e:
84 def assertLogFileContentsEqual(self, message):
85 with open(self.logFile.name, "r") as f:
86 contents = f.read().replace('\n', '')
88 self.assertTrue(contents==message, "Contents differ!\n\tGenerated contents: %s\n\tExpected contents: %s"%(contents, message))
91 self.session(self.hello0)
92 self.assertLogFileContentsEqual(self.hello0Msg)
94 def testPythonHello0(self):
95 self.session(["python"]+self.hello0)
96 self.assertLogFileContentsEqual(self.hello0Msg)
99 self.session(self.hello1)
100 self.assertLogFileContentsEqual(self.hello1Msg)
103 self.session(self.add0)
104 self.assertLogFileContentsEqual(self.add0Msg)
107 self.session(self.add3)
108 self.assertLogFileContentsEqual(self.add3Msg)
110 def testHello0Add3(self):
111 self.session(self.hello0+self.add3)
112 self.assertLogFileContentsEqual(self.hello0Msg+self.add3Msg)
114 def testHello1Add3(self):
115 self.session(self.hello1+self.add3)
116 self.assertLogFileContentsEqual(self.hello1Msg+self.add3Msg)
118 def testHelloToAdd(self):
119 self.session(self.helloToAdd)
120 self.assertLogFileContentsEqual(self.helloToAddMsg)
122 def testHelloToList(self):
123 self.session(self.helloToList)
124 self.assertLogFileContentsEqual(self.helloToListMsg)
126 def testLines0(self):
127 self.session(self.lines0)
128 self.assertLogFileContentsEqual(self.lines0Msg)
130 def testLines2(self):
131 self.session(self.lines2)
132 self.assertLogFileContentsEqual(self.lines2Msg)
134 def testLines2Add3(self):
135 self.session(self.lines2+self.add3)
136 self.assertLogFileContentsEqual(self.lines2Msg+self.add3Msg)
138 def testLinesUnreadable(self):
139 self.session(self.linesUnreadable)
140 self.assertLogFileContentsEqual(self.linesUnreadableMsg)
142 def testAddAddHello(self):
143 self.session(self.add3+self.add3+self.hello1)
144 self.assertLogFileContentsEqual(self.add3Msg+self.add3Msg+self.hello1Msg)
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)
152 if __name__ == "__main__":