Salome HOME
Improve tests
authorSerge Rehbinder <serge.rehbinder@cea.fr>
Fri, 19 Feb 2016 08:59:45 +0000 (09:59 +0100)
committerSerge Rehbinder <serge.rehbinder@cea.fr>
Fri, 19 Feb 2016 08:59:45 +0000 (09:59 +0100)
commands/log.py
src/logger.py
src/xmlManager.py
test/config/create_user_pyconf.py
test/config/option_edit.py [new file with mode: 0644]
test/config/option_value.py
test/log/launch_browser.py
test/run_all.sh

index 1ceb3dcd6402583f45c3af531519ad19164fde61..27a3694034d58c5de518756e40cf1e4f2af4e888 100644 (file)
@@ -56,8 +56,8 @@ def print_log_command_in_terminal(filePath, logger):
     :param logger Logger: the logging instance to use in order to print.  
     '''
     logger.write(_("Reading ") + src.printcolors.printcHeader(filePath) + "\n", 5)
-    # Instantiate the readXmlFile class that reads xml files
-    xmlRead = src.xmlManager.readXmlFile(filePath)
+    # Instantiate the ReadXmlFile class that reads xml files
+    xmlRead = src.xmlManager.ReadXmlFile(filePath)
     # Get the attributes containing the context (user, OS, time, etc..)
     dAttrText = xmlRead.get_attrib('Site')
     # format dAttrText and print the context
index e2d2d1ee897880cf4400f941cd7905a92fa06dab..61673bba53b929185699a2142b1c5f3285c095fe 100644 (file)
@@ -227,7 +227,7 @@ def show_command_log(logFilePath, cmd, application, notShownCommands):
         return False, None
  
     # Get the application of the log file
-    logFileXml = src.xmlManager.readXmlFile(logFilePath)
+    logFileXml = src.xmlManager.ReadXmlFile(logFilePath)
     if 'application' in logFileXml.xmlroot.keys():
         appliLog = logFileXml.xmlroot.get('application')
         # if it corresponds, then the log has to be shown
index 978077888c17a357cc5a9154f7f5d12aea15c966..fa175ff1c94518a73f81621397e3308c3558b9f6 100644 (file)
@@ -85,7 +85,7 @@ class XmlLogFile(object):
         '''
         self.xmlroot.find(node_name).attrib.update(attrib)
 
-class readXmlFile(object):
+class ReadXmlFile(object):
     '''Class to manage reading of an xml log file
     '''
     def __init__(self, filePath):
index 5b1e19a595def6904aa0a319bf00adc53d1b633d..0bc6e96138dbd05b7c50c138d322e35952dba1e5 100644 (file)
@@ -33,12 +33,14 @@ class TestConfig(unittest.TestCase):
     '''pyunit class : each method execute one test.
     '''
     
-    def test_option_value(self):
+    def test_user_dir_creation(self):
         '''Creation of ~/.salomeTools/salomeTools.pyconf
         '''
         res = "KO"
         user_dir = os.path.expanduser(os.path.join('~','.salomeTools'))
         user_dir_save = os.path.expanduser(os.path.join('~','.salomeTools_save'))
+        if os.path.exists(user_dir_save):
+            shutil.rmtree(user_dir_save)
         if os.path.exists(user_dir):
             shutil.move(user_dir, user_dir_save)
                
@@ -57,6 +59,81 @@ class TestConfig(unittest.TestCase):
         # pyunit method to compare 2 str
         self.assertEqual(res, "OK")
 
+    def test_override_VARS(self):
+        '''override VARS
+        '''
+        OK = "KO"
+        
+        # The command to test
+        sat = Sat("-oVARS.user='user_test'")
+        sat.config()
+
+        if sat.cfg.VARS.user == 'user_test':
+            OK = "OK"
+
+        # pyunit method to compare 2 str
+        self.assertEqual(OK, "OK")
+
+    def test_override_INTERNAL(self):
+        '''override INTERNAL
+        '''
+        OK = "KO"
+        
+        # The command to test
+        sat = Sat("-oINTERNAL.sat_version='V0'")
+        sat.config()
+
+        if sat.cfg.INTERNAL.sat_version == 'V0':
+            OK = "OK"
+
+        # pyunit method to compare 2 str
+        self.assertEqual(OK, "OK")
+
+    def test_override_SITE(self):
+        '''override SITE
+        '''
+        OK = "KO"
+        
+        # The command to test
+        sat = Sat("-oSITE.prepare.default_git_server='testgit'")
+        sat.config()
+
+        if sat.cfg.SITE.prepare.default_git_server == 'testgit':
+            OK = "OK"
+
+        # pyunit method to compare 2 str
+        self.assertEqual(OK, "OK")
+
+    def test_override_APPLICATION(self):
+        '''override APPLICATION
+        '''
+        OK = "KO"
+        
+        # The command to test
+        sat = Sat("-oAPPLICATION.out_dir='/tmp'")
+        sat.config('appli-test')
+
+        if sat.cfg.APPLICATION.out_dir == '/tmp':
+            OK = "OK"
+
+        # pyunit method to compare 2 str
+        self.assertEqual(OK, "OK")
+
+    def test_override_SOFTWARE(self):
+        '''override SOFTWARE
+        '''
+        OK = "KO"
+        
+        # The command to test
+        sat = Sat("-oSOFTWARE.softA.compile_method='test'")
+        sat.config('')
+
+        if sat.cfg.SOFTWARE.softA.compile_method == 'test':
+            OK = "OK"
+
+        # pyunit method to compare 2 str
+        self.assertEqual(OK, "OK")
+
 # test launch
 if __name__ == '__main__':
     HTMLTestRunner.main()
diff --git a/test/config/option_edit.py b/test/config/option_edit.py
new file mode 100644 (file)
index 0000000..f0a1b13
--- /dev/null
@@ -0,0 +1,84 @@
+#!/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
+import threading
+import time
+
+# get execution path
+testdir = os.path.dirname(os.path.realpath(__file__))
+sys.path.append(os.path.join(testdir, '..', '..'))
+sys.path.append(os.path.join(testdir, '..', '_testTools'))
+
+from salomeTools import Sat
+import src
+from tools import outRedirection
+from tools import check_proc_existence_and_kill
+import HTMLTestRunner
+
+sleep_time = 3
+
+class TestConfig(unittest.TestCase):
+    '''OPTION EDIT.
+    '''
+    
+    def test_edit_userconfig(self):
+        '''Test the launch of the editor when invoking the config -e
+        '''
+
+        OK = "KO"
+
+        sat = Sat("-oUSER.editor='cooledit'")
+        cmd_config = threading.Thread(target=sat.config, args=('-e',))
+        cmd_config.start()
+
+        time.sleep(sleep_time)
+
+        editor = sat.cfg.USER.editor
+        pid = check_proc_existence_and_kill(editor + ".*" + "salomeTools\.pyconf")
+
+        if pid:
+            OK = "OK"
+        # pyunit method to compare 2 str
+        self.assertEqual(OK, "OK")
+
+    def test_edit_appli(self):
+        '''Test the launch of the editor when invoking the config -e appli-test
+        '''
+
+        OK = "KO"
+
+        sat = Sat("-oUSER.editor='cooledit'")
+        cmd_config = threading.Thread(target=sat.config, args=('appli-test -e',))
+        cmd_config.start()
+
+        time.sleep(sleep_time)
+
+        editor = sat.cfg.USER.editor
+        pid = check_proc_existence_and_kill(editor + ".*" + "appli-test\.pyconf")
+
+        if pid:
+            OK = "OK"
+        # pyunit method to compare 2 str
+        self.assertEqual(OK, "OK")
+# test launch
+if __name__ == '__main__':
+    HTMLTestRunner.main()
index 3ea8a37e372d49afef8234b4c2ae71aa6395d17b..b6b80556cd76097f17a32733b8d08abbe617daa1 100644 (file)
@@ -20,6 +20,7 @@ import unittest
 import os
 import sys
 import platform
+import shutil
 
 # get execution path
 testdir = os.path.dirname(os.path.realpath(__file__))
@@ -27,6 +28,7 @@ sys.path.append(os.path.join(testdir, '..', '..'))
 sys.path.append(os.path.join(testdir, '..', '_testTools'))
 
 from salomeTools import Sat
+import src
 from tools import outRedirection
 import HTMLTestRunner
 
@@ -84,7 +86,43 @@ class TestConfig(unittest.TestCase):
 
         # pyunit method to compare 2 str
         self.assertEqual(OK, "OK")
-
+    
+    """    
+    def test_error_salomeToolspyconf(self):
+        '''Test the exception when salomeTools.pyconf has errors
+        '''      
+        
+        OK = "KO"
+        
+        # The command to test
+        sat = Sat()
+        sat.config()
+        
+        salomeToolspyconfPath = os.path.join(sat.cfg.VARS.srcDir, "internal_config", "salomeTools.pyconf")
+        salomeToolspyconfPath_save = os.path.join(sat.cfg.VARS.srcDir, "internal_config", "salomeTools.pyconf_save")
+        if os.path.exists(salomeToolspyconfPath_save):
+            os.remove(salomeToolspyconfPath_save)
+        shutil.copyfile(salomeToolspyconfPath, salomeToolspyconfPath_save)
+        f_read = open(salomeToolspyconfPath, 'r')
+        text = f_read.read()
+        f_read.close()
+        os.remove(salomeToolspyconfPath)
+        f_write = open(salomeToolspyconfPath, 'w')
+        f_write.write(text.replace(':', ''))
+        f_write.close()
+        
+        try:
+            sat.config()
+        except TypeError:
+            OK = "OK"
+        finally:
+            shutil.copyfile(salomeToolspyconfPath_save, salomeToolspyconfPath)
+            os.remove(salomeToolspyconfPath_save)
+        
+        # pyunit method to compare 2 str
+        self.assertEqual(OK, "OK")
+    """       
+        
 # test launch
 if __name__ == '__main__':
     HTMLTestRunner.main()
index e2c589340f557dde0c1e479ddf7f7e510a246d6d..9a26384a47d14813eb9ebefbd2bc30dfd2539c3b 100644 (file)
@@ -21,15 +21,22 @@ import os
 import sys
 import threading
 import time
+import shutil
+import io
 
 # get execution path
 testdir = os.path.dirname(os.path.realpath(__file__))
 sys.path.append(os.path.join(testdir, '..', '..'))
 sys.path.append(os.path.join(testdir, '..', '_testTools'))
+sys.path.append(os.path.join(testdir, '..', '..','commands'))
 
 from salomeTools import Sat
 from tools import check_proc_existence_and_kill
+from tools import outRedirection
 import HTMLTestRunner
+import src.xmlManager
+
+sleep_time = 3
 
 class TestLog(unittest.TestCase):
     '''Test of log command: launch of browser
@@ -45,7 +52,7 @@ class TestLog(unittest.TestCase):
         cmd_log = threading.Thread(target=sat.log, args=('',))
         cmd_log.start()
 
-        time.sleep(2)
+        time.sleep(sleep_time)
 
         browser = sat.cfg.USER.browser
         pid = check_proc_existence_and_kill(browser + ".*" + "xml")
@@ -76,7 +83,7 @@ class TestLog(unittest.TestCase):
         self.assertEqual(OK, "OK")
 
     def test_option_terminal(self):
-        '''Test the write of xml log when invoking a command
+        '''Test the terminal option without application
         '''
 
         OK = "KO"
@@ -84,13 +91,14 @@ class TestLog(unittest.TestCase):
         # launch the command that will write a log
         sat = Sat()
         
-        # overwrite the raw_input function in order to be able to test
-        new_ask_value = lambda x: 1
-        sys.modules['log'].ask_value = new_ask_value
+        one = u"1"
+        sys.stdin = io.StringIO(one)
+        
         
         try:
             sat.log('-t')
             OK = "OK"
+            sys.stdin = sys.__stdin__
         except:
             pass
         
@@ -98,29 +106,241 @@ class TestLog(unittest.TestCase):
         self.assertEqual(OK, "OK")
 
     def test_option_terminal2(self):
-        '''Test the write of xml log when invoking a command
+        '''Test the terminal option with application
         '''
 
         OK = "KO"
         
         # launch the command that will write a log
         sat = Sat()
-        
-        # overwrite the raw_input function in order to be able to test
-        new_ask_value = lambda x: 1
-        sys.modules['log'].ask_value = new_ask_value
-        
+              
         sat.config('appli-test -v VARS.python')
         
+        one = u"1"
+        sys.stdin = io.StringIO(one)
+        
         try:
-            sat.log('appli-test -t')
+            sat.log('appli-test --last')
             OK = "OK"
+            sys.stdin = sys.__stdin__
         except:
             pass
         
         # pyunit method to compare 2 str
         self.assertEqual(OK, "OK")
 
+    def test_option_terminal3(self):
+        '''Test the terminal option with 0 as input
+        '''
+
+        OK = "KO"
+        
+        # launch the command that will write a log
+        sat = Sat()
+              
+        sat.config('appli-test -v VARS.python')
+        
+        zero = u"0\n1"
+        sys.stdin = io.StringIO(zero)
+        
+        try:
+            sat.log('--terminal')
+            OK = "OK"
+        finally:
+            sys.stdin = sys.__stdin__
+        
+        # pyunit method to compare 2 str
+        self.assertEqual(OK, "OK")
+
+    def test_option_terminal4(self):
+        '''Test the terminal option with input bigger than the number of logs
+        '''
+
+        OK = "KO"
+        
+        # launch the command that will write a log
+        sat = Sat()
+              
+        sat.config('appli-test -v VARS.python')
+        
+        nb_logs = len(os.listdir(sat.cfg.SITE.log.logDir))
+        
+        nb_logs_u = unicode(str(nb_logs) + "\n1")
+        sys.stdin = io.StringIO(nb_logs_u)
+        
+        try:
+            sat.log('--terminal')
+            OK = "OK"
+        finally:
+            sys.stdin = sys.__stdin__
+        
+        # pyunit method to compare 2 str
+        self.assertEqual(OK, "OK")
+
+    def test_option_terminal5(self):
+        '''Test the terminal option with input return
+        '''
+
+        OK = "KO"
+        
+        # launch the command that will write a log
+        sat = Sat()
+              
+        sat.config('appli-test -v VARS.python')
+        
+        ret = unicode("\n0")
+        sys.stdin = io.StringIO(ret)
+        
+        try:
+            sat.log('--terminal')
+            OK = "OK"
+        finally:
+            sys.stdin = sys.__stdin__
+        
+        # pyunit method to compare 2 str
+        self.assertEqual(OK, "OK")
+
+    def test_option_terminal6(self):
+        '''Test the terminal option with input not int
+        '''
+
+        OK = "KO"
+        
+        # launch the command that will write a log
+        sat = Sat()
+              
+        sat.config('appli-test -v VARS.python')
+        
+        ret = unicode("blabla\n0")
+        sys.stdin = io.StringIO(ret)
+        
+        try:
+            sat.log('--terminal')
+            OK = "OK"
+        finally:
+            sys.stdin = sys.__stdin__
+        
+        # pyunit method to compare 2 str
+        self.assertEqual(OK, "OK")
+
+    def test_option_last(self):
+        '''Test the option --last
+        '''
+
+        OK = "KO"
+        
+        # launch the command that will write a log
+        sat = Sat("-oUSER.browser='konqueror'")
+              
+        sat.config('appli-test -v VARS.python')
+        
+        cmd_log = threading.Thread(target=sat.log, args=('appli-test --last',))
+        cmd_log.start()
+        
+        time.sleep(sleep_time)
+
+        browser = sat.cfg.USER.browser
+        pid = check_proc_existence_and_kill(browser + ".*" + "xml")
+        
+        if pid:
+            OK = "OK"
+        
+        # pyunit method to compare 2 str
+        self.assertEqual(OK, "OK")
+
+    def test_option_clean(self):
+        '''Test the option --clean
+        '''
+
+        OK = "KO"
+        
+        # launch the command that will write a log
+        sat = Sat()
+               
+        sat.config('-v VARS.user')
+        
+        nb_logs_t0 = len(os.listdir(sat.cfg.SITE.log.logDir))
+
+        sat.log('--clean 1')
+        
+        nb_logs_t1 = len(os.listdir(sat.cfg.SITE.log.logDir))
+        
+        if nb_logs_t1-nb_logs_t0 == 0:
+            OK = "OK"
+        
+        # pyunit method to compare 2 str
+        self.assertEqual(OK, "OK")
+
+    def test_option_clean2(self):
+        '''Test the option --clean with big number of files to clean
+        '''
+
+        OK = "KO"
+        
+        # launch the command that will write a log
+        sat = Sat()
+               
+        sat.config('-v VARS.user')
+        
+        nb_logs_t0 = len(os.listdir(sat.cfg.SITE.log.logDir))
+        
+        if os.path.exists(sat.cfg.SITE.log.logDir + "_save"):
+            shutil.rmtree(sat.cfg.SITE.log.logDir + "_save")
+        shutil.copytree(sat.cfg.SITE.log.logDir,sat.cfg.SITE.log.logDir + "_save")
+        
+        sat.log('--clean ' + str(nb_logs_t0))
+        
+        nb_logs_t1 = len(os.listdir(sat.cfg.SITE.log.logDir))
+        
+        shutil.rmtree(sat.cfg.SITE.log.logDir)
+        shutil.move(sat.cfg.SITE.log.logDir + "_save", sat.cfg.SITE.log.logDir)
+                
+        if nb_logs_t0-nb_logs_t1 > 10:
+            OK = "OK"
+        
+        # pyunit method to compare 2 str
+        self.assertEqual(OK, "OK")
+
+    def test_option_full(self):
+        '''Test the option --full
+        '''
+
+        OK = "KO"
+
+        sat = Sat("-oUSER.browser='konqueror'")
+        cmd_log = threading.Thread(target=sat.log, args=('--full',))
+        cmd_log.start()
+
+        time.sleep(sleep_time)
+
+        browser = sat.cfg.USER.browser
+        check_proc_existence_and_kill(browser + ".*" + "xml")
+        
+        # Read and check the hat.xml file contains at least one log file corresponding to log
+        hatFilePath = os.path.join(sat.cfg.SITE.log.logDir, "hat.xml")
+        xmlHatFile = src.xmlManager.ReadXmlFile(hatFilePath)
+        for field in xmlHatFile.xmlroot:
+            if field.attrib[b'cmd'] == b'log':
+                OK = "OK"
+                break
+
+        # pyunit method to compare 2 str
+        self.assertEqual(OK, "OK")
+
+    def test_description(self):
+        '''Test the sat -h log
+        '''        
+
+        OK = "KO"
+
+        import log
+        
+        if "Gives access to the logs produced" in log.description():
+            OK = "OK"
+
+        # pyunit method to compare 2 str
+        self.assertEqual(OK, "OK")
+
 # test launch
 if __name__ == '__main__':
     HTMLTestRunner.main()
index 9fa73c692c0a6765c0a845bd47ab036de58f4c2c..b9bed1a4a2e6a3f1fbb958f42ab69010f7abbacd 100755 (executable)
@@ -20,5 +20,6 @@ coverage run --source=../commands/config.py    config/option_value.py > test_res
 coverage run --source=../commands/config.py -a config/option_value_2.py >> test_res.html
 coverage run --source=../commands/config.py -a config/create_user_pyconf.py >> test_res.html
 coverage run --source=../commands/config.py -a config/option_copy.py >> test_res.html
+coverage run --source=../commands/config.py -a config/option_edit.py >> test_res.html
 coverage run --source=../commands/config.py,../commands/log.py,../src/xmlManager.py,../src/logger.py -a log/launch_browser.py >> test_res.html
 coverage html