1 # Copyright (C) 2013-2014 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 from cStringIO import StringIO
28 logger = logging.getLogger("TestLauncherLogger")
29 logger.level = logging.DEBUG
30 logger.addHandler(logging.StreamHandler())
32 class TestSessionArgs(unittest.TestCase):
35 # Initialize path to SALOME application
36 path_to_launcher = os.getenv("SALOME_LAUNCHER")
37 appli_dir = os.path.dirname(path_to_launcher)
38 envd_dir = os.path.join(appli_dir, "env.d")
40 # Configure session startup
41 cls.SALOME = imp.load_source("SALOME", os.path.join(appli_dir,"salome"))
42 cls.SALOME_args = ["shell", "--config="+envd_dir]
44 cls.logFile = "log.txt"
45 sys.stdout = StringIO()
47 # Set some predefined command args and corresponding output messages
48 cls.hello0 = ["hello.py", "args:outfile="+cls.logFile]
49 cls.hello0Msg = "Hello!"
50 cls.hello1 = ["hello.py", "args:you,outfile="+cls.logFile]
51 cls.hello1Msg = "Hello to: you"
52 cls.helloToAdd = ["hello.py", "args:add.py,1,2,3,outfile="+cls.logFile]
53 cls.helloToAddMsg = "Hello to: add.py, 1, 2, 3"
54 cls.add0 = ["add.py", "args:outfile="+cls.logFile]
55 cls.add0Msg = "No args!"
56 cls.add3 = ["add.py", "args:1,2,3,outfile="+cls.logFile]
57 cls.add3Msg = "1+2+3 = 6"
58 cls.lines0 = ["lines.py", "args:outfile="+cls.logFile]
59 cls.lines0Msg = "No files given"
60 cls.lines2 = ["lines.py", "args:hello.py,add.py,outfile="+cls.logFile]
61 cls.lines2Msg = "hello.py is 16 lines longadd.py is 18 lines long"
62 cls.linesUnreadable = ["lines.py", "args:hello.py,add.py,1,2,outfile="+cls.logFile]
63 cls.linesUnreadableMsg = "hello.py is 16 lines longadd.py is 18 lines longFile '1' cannot be readFile '2' cannot be read"
66 def tearDownClass(cls):
75 def session(self, args=[]):
76 self.SALOME.main(self.SALOME_args + args)
78 def removeLogFile(self):
80 os.remove(self.logFile)
84 def assertLogFileContentsEqual(self, message):
85 with open(self.logFile, "r") as f:
86 contents = f.read().replace('\n', '')
88 #sys.stderr.write("Generated contents :%s\n"%contents)
89 #sys.stderr.write("Expected contents :%s\n"%message)
90 self.assertTrue(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)
124 def testLines0(self):
125 self.session(self.lines0)
126 self.assertLogFileContentsEqual(self.lines0Msg)
128 def testLines2(self):
129 self.session(self.lines2)
130 self.assertLogFileContentsEqual(self.lines2Msg)
132 def testLines2Add3(self):
133 self.session(self.lines2+self.add3)
134 self.assertLogFileContentsEqual(self.lines2Msg+self.add3Msg)
136 def testLinesUnreadable(self):
137 self.session(self.linesUnreadable)
138 self.assertLogFileContentsEqual(self.linesUnreadableMsg)
140 def testAddAddHello(self):
141 self.session(self.add3+self.add3+self.hello1)
142 self.assertLogFileContentsEqual(self.add3Msg+self.add3Msg+self.hello1Msg)
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)
151 if __name__ == "__main__":
152 path_to_launcher = os.getenv("SALOME_LAUNCHER")
153 if not path_to_launcher:
154 msg = "Error: please set SALOME_LAUNCHER variable to the salome command of your application folder."