From 86343ee86befe17553c83d5574109820cad59dd3 Mon Sep 17 00:00:00 2001 From: Serge Rehbinder Date: Mon, 1 Feb 2016 09:42:55 +0100 Subject: [PATCH] Add first testing procedure --- src/salomeTools.py | 4 +- test/__TOOLS__/tools.py | 54 ++++++++++++++++++++++ test/config/integration/option_value.py | 59 +++++++++++++++++++++++++ test/test.py | 3 +- 4 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 test/__TOOLS__/tools.py create mode 100644 test/config/integration/option_value.py diff --git a/src/salomeTools.py b/src/salomeTools.py index 78dc0af..b640e42 100755 --- a/src/salomeTools.py +++ b/src/salomeTools.py @@ -128,14 +128,14 @@ class salomeTools(object): if not self.cfg: # read the configuration from all the pyconf files cfgManager = config.ConfigManager() - self.cfg = cfgManager.getConfig(dataDir=self.dataDir, application=appliToLoad, options=self.options) + self.cfg = cfgManager.getConfig(dataDir=self.dataDir, application=appliToLoad, options=self.options, command=__nameCmd__) return __module__.run(argv, self) # Make sure that run_command will be redefined at each iteration of the loop globals_up = {} globals_up.update(run_command.__globals__) - globals_up.update({'__name__': nameCmd, '__module__' : module}) + globals_up.update({'__nameCmd__': nameCmd, '__module__' : module}) func = types.FunctionType(run_command.__code__, globals_up, run_command.__name__,run_command.__defaults__, run_command.__closure__) # set the attribute corresponding to the command diff --git a/test/__TOOLS__/tools.py b/test/__TOOLS__/tools.py new file mode 100644 index 0000000..daf88a7 --- /dev/null +++ b/test/__TOOLS__/tools.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +#-*- coding:utf-8 -*- +# Copyright (C) 2010-2012 CEA/DEN +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import tempfile +import sys +import subprocess + +class outRedirection(): + '''redirection of standart output + useful for testing the terminal display + ''' + def __init__(self): + '''initialization + ''' + self._fstream = tempfile.NamedTemporaryFile(mode='w') + self.saveout = sys.stdout + sys.stdout = self._fstream + + def flush(self): + self._fstream.flush() + + def end_redirection(self): + self._fstream.seek(0) + ff = open(self._fstream.name, 'r') + self.res = ff.read() + self._fstream.close() + sys.stdout = self.saveout + + def read_results(self): + try: + return self.res + except Exception as exc: + print('Problem with redirection : %s' % exc) + sys.exit(1) + +def check_proc_existence(cmd, text_to_find): + + p = subprocess.Popen(cmd, shell=True) + p.communicate() \ No newline at end of file diff --git a/test/config/integration/option_value.py b/test/config/integration/option_value.py new file mode 100644 index 0000000..883039a --- /dev/null +++ b/test/config/integration/option_value.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +#-*- coding:utf-8 -*- +# Copyright (C) 2010-2012 CEA/DEN +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import unittest +import os +import sys + +# get execution path +testdir = os.path.dirname(os.path.realpath(__file__)) +sys.path.append(os.path.join(testdir, '..', '..', '..', 'src')) +sys.path.append(os.path.join(testdir, '..', '..', '..', 'test', '__TOOLS__')) + +from salomeTools import salomeTools +from tools import outRedirection + +class TestConfig(unittest.TestCase): + '''pyunit class : each method execute one test. + ''' + + def test_option_value(self): + '''Test the display of the right value of "sat config -v VARS.hostname" + ''' + # expected value + expected = '\x1b[36mhostname\x1b[0m: is221560\n' + + # output redirection + my_out = outRedirection() + + # The command to test + sat = salomeTools('') + sat.config('-v VARS.hostname') + + # stop output redirection + my_out.end_redirection() + + # get results + res = my_out.read_results() + + # pyunit method to compare 2 str + self.assertEqual(res, expected) + +# test launch +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/test/test.py b/test/test.py index 842475a..5c52d4b 100644 --- a/test/test.py +++ b/test/test.py @@ -16,7 +16,8 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -import os, sys +import os +import sys # get execution path testdir = os.path.dirname(os.path.realpath(__file__)) -- 2.39.2