2 # Copyright (C) 2013-2020 CEA/DEN, EDF R&D, 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 from salomeContextUtils import setOmniOrbUserPath
62 self.logFile = tempfile.NamedTemporaryFile()
63 if sys.platform == "win32": # Close file because of permission denined on Windows
69 def session(self, args=None):
72 args = [x.replace("LOGFILE",self.logFile.name) for x in args]
77 params, args = runSession.configureSession(args, exe="salome shell")
78 return runSession.runSession(params, args)
79 except SystemExit as e:
86 def assertLogFileContentsEqual(self, message):
87 with open(self.logFile.name, "r") as f:
88 contents = f.read().replace('\n', '')
90 self.assertTrue(contents==message, "Contents differ!\n\tGenerated contents: %s\n\tExpected contents: %s"%(contents, message))
93 self.session(self.hello0)
94 self.assertLogFileContentsEqual(self.hello0Msg)
96 def testPythonHello0(self):
97 self.session(["python"]+self.hello0)
98 self.assertLogFileContentsEqual(self.hello0Msg)
100 def testHello1(self):
101 self.session(self.hello1)
102 self.assertLogFileContentsEqual(self.hello1Msg)
105 self.session(self.add0)
106 self.assertLogFileContentsEqual(self.add0Msg)
109 self.session(self.add3)
110 self.assertLogFileContentsEqual(self.add3Msg)
112 def testHello0Add3(self):
113 self.session(self.hello0+self.add3)
114 self.assertLogFileContentsEqual(self.hello0Msg+self.add3Msg)
116 def testHello1Add3(self):
117 self.session(self.hello1+self.add3)
118 self.assertLogFileContentsEqual(self.hello1Msg+self.add3Msg)
120 def testHelloToAdd(self):
121 self.session(self.helloToAdd)
122 self.assertLogFileContentsEqual(self.helloToAddMsg)
126 if __name__ == "__main__":