From: Gbkng Date: Wed, 14 Feb 2024 22:43:07 +0000 (+0100) Subject: feat(test)!: rename and fix tests X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=01add19a39e83ea7bb9d79088f907f728f4c9733;p=tools%2Fsat.git feat(test)!: rename and fix tests --- diff --git a/test/init_test_env.py b/test/init_test_env.py new file mode 100755 index 0000000..ee2dad3 --- /dev/null +++ b/test/init_test_env.py @@ -0,0 +1,23 @@ +import os +import sys +import pprint as pp + +def init_test_env(): + """ + if sat path is not in PYTHONPATH, prepend it with it. + + This is necessary for sat unit tests (unittest framwork) to find sat modules + and call sat commands + """ + satdir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + + if satdir not in sys.path: + # prepend path to sat sources first + # make the 'src' & 'commands' packages accessible for all the tests + sys.path.insert(0, satdir) + sys.stdout.write( + f"info: sat paths not detected in sys.path. Set automatically:\n\ + sys.path prepend '{satdir}'\n\ + sys.path:\n{pp.pformat(sys.path)}\n\ + You can set the path yourself with:\n 'export PYTHONPATH={satdir}:${{PYTHONPATH}}'\n" + ) diff --git a/test/initializeTest.py b/test/initializeTest.py deleted file mode 100755 index 9b6a842..0000000 --- a/test/initializeTest.py +++ /dev/null @@ -1,20 +0,0 @@ -import os -import sys -import pprint as pp - -""" -initialize PATH etc... for salomeTools unittest test files -""" - -satdir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) - -if satdir not in sys.path: - # prepend path to sat sources first - # make the 'src' & 'commands' packages accessible for all the tests - sys.path.insert(0, satdir) - sys.stderr.write( - f"warning: sat paths not detected in sys.path. Set automatically:\n\ - sys.path prepend '{satdir}'\n\ - sys.path:\n{pp.pformat(sys.path)}\n\ - You can set the path yourself with:\n 'export PYTHONPATH={satdir}:${{PYTHONPATH}}'\n" - ) diff --git a/test/test_020_debug.py b/test/test_020_debug.py deleted file mode 100755 index fb7e173..0000000 --- a/test/test_020_debug.py +++ /dev/null @@ -1,121 +0,0 @@ -#!/usr/bin/env python -# -*- coding:utf-8 -*- - -# Copyright (C) 2010-2018 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 initializeTest # set PATH etc for test - -import src.debug as DBG # Easy print stderr (for DEBUG only) -import src.pyconf as PYF # 0.3.7 - -_EXAMPLES = { - 1: """\ - messages: - [ - { - stream : "sys.stderr" # modified - message: 111 # modified - name: 'Harry' - } - { - stream : $messages[0].stream - message: 1.23e4 # modified do not work 0.3.7 - name: 'Ruud' - } - { - stream : "HELLO " + $messages[0].stream - message: 'Bienvenue' - name: "Yves" - } - ] -""", - 2: """\ - aa: 111 - bb: $aa + 222 -""", - 3: """\ - aa: Yves - bb: "Herve" # avoid Hervé -> 'utf8' codec can't decode byte -""", - 4: """\ - aa: Yves - bb: "Hervé" # avoid Hervé -> 'utf8' codec can't decode byte -""", -} - - -class TestCase(unittest.TestCase): - "Test the debug.py" "" - - def test_000(self): - # one shot setUp() for this TestCase - # DBG.push_debug(True) - # SAT.setNotLocale() # test english - return - - def test_005(self): - res = DBG.getLocalEnv() - self.assertTrue(len(res.split()) > 0) - self.assertTrue("USER :" in res) - self.assertTrue("LANG :" in res) - - def test_010(self): - inStream = DBG.InStream(_EXAMPLES[1]) - self.assertEqual(inStream.getvalue(), _EXAMPLES[1]) - cfg = PYF.Config(inStream) - self.assertEqual(len(cfg.messages), 3) - outStream = DBG.OutStream() - DBG.saveConfigStd(cfg, outStream) - res = outStream.value - DBG.write("test_010 cfg std", res) - self.assertTrue("messages :" in res) - self.assertTrue("'sys.stderr'" in res) - - def test_020(self): - inStream = DBG.InStream(_EXAMPLES[2]) - cfg = PYF.Config(inStream) - res = DBG.getStrConfigDbg(cfg) - DBG.write("test_020 cfg dbg", res) - ress = res.split("\n") - self.assertTrue(".aa" in ress[0]) - self.assertTrue(": '111'" in ress[0]) - self.assertTrue(".bb" in ress[1]) - self.assertTrue(": $aa + 222 " in ress[1]) - self.assertTrue("--> '333'" in ress[1]) - - def test_025(self): - inStream = DBG.InStream(_EXAMPLES[1]) - cfg = PYF.Config(inStream) - outStream = DBG.OutStream() - DBG.saveConfigDbg(cfg, outStream) - res = outStream.value - DBG.write("test_025 cfg dbg", res) - for i in range(len(cfg.messages)): - self.assertTrue("messages[%i].name" % i in res) - self.assertTrue("--> 'HELLO sys.stderr'" in res) - - def test_999(self): - # one shot tearDown() for this TestCase - # SAT.setLocale() # end test english - # DBG.pop_debug() - return - - -if __name__ == "__main__": - unittest.main(exit=False) diff --git a/test/test_021_versionMinorMajorPatch.py b/test/test_021_versionMinorMajorPatch.py deleted file mode 100755 index a268e6f..0000000 --- a/test/test_021_versionMinorMajorPatch.py +++ /dev/null @@ -1,255 +0,0 @@ -#!/usr/bin/env python -# -*- coding:utf-8 -*- - -# Copyright (C) 2010-2018 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 - - -"""\ -class and utilities to define a version as MAJOR.MINOR.PATCH, -and range of versions - -| Given a version number MAJOR.MINOR.PATCH separator "_" or "." -| increment the -| MAJOR version when you make incompatible API changes, -| MINOR version when you add functionality in a backwards-compatible manner, -| PATCH version when you make backwards-compatible bug fixes. -""" - - -import unittest -import pprint as PP - -import initializeTest # set PATH etc for test - -import src.versionMinorMajorPatch as VMMP - -verbose = False # True - - -class TestCase(unittest.TestCase): - "Test the versionMajorMinorPatch.py" "" - - def test_010(self): - if verbose: - print(PP.pformat(dir(self))) - self.assertTrue(VMMP.only_numbers("") is None) - self.assertEqual(VMMP.only_numbers("1.2.3"), "123") - self.assertEqual(VMMP.only_numbers("\n11.12.13\n"), "111213") - self.assertEqual(VMMP.only_numbers(" \n 11.\t\n\t..12.13-rc2\n"), "1112132") - - def test_015(self): - res = "a_b_c" - self.assertEqual( - VMMP.remove_startswith("version_a_b_c", "version_".split()), res - ) - self.assertEqual(VMMP.remove_startswith("v_a_b_c", "version_ v_".split()), res) - self.assertEqual(VMMP.remove_startswith("va_b_c", "version_ v_ v".split()), res) - - ini = "version_a_b_c" - self.assertEqual(VMMP.remove_startswith(ini, "V".split()), ini) - self.assertEqual(VMMP.remove_startswith(ini, "_".split()), ini) - self.assertEqual(VMMP.remove_startswith(ini, "a_b_c".split()), ini) - self.assertEqual(VMMP.remove_startswith(ini, "VERSION".split()), ini) - - def test_020(self): - res = [11, 222, 3333] - self.assertEqual(VMMP.toList_majorMinorPatch("11.222.3333"), res) - self.assertEqual(VMMP.toList_majorMinorPatch("11_222_3333"), res) - self.assertEqual(VMMP.toList_majorMinorPatch("11.222_3333"), res) - self.assertEqual(VMMP.toList_majorMinorPatch(" 11. 222 . 3333 "), res) - self.assertEqual( - VMMP.toList_majorMinorPatch("\n 11 . 222 . 3333 \n"), res - ) - self.assertEqual( - VMMP.toList_majorMinorPatch(" \n11.\t222.\r3333\n "), res - ) # could be tricky - - self.assertEqual(VMMP.toList_majorMinorPatch("V11.222.3333"), res) - self.assertEqual(VMMP.toList_majorMinorPatch("Version11_222_3333"), res) - self.assertEqual(VMMP.toList_majorMinorPatch("Version_11_222_3333"), res) - - self.assertEqual(VMMP.toList_majorMinorPatch("11"), [11, 0, 0]) - self.assertEqual(VMMP.toList_majorMinorPatch("11.0"), [11, 0, 0]) - self.assertEqual(VMMP.toList_majorMinorPatch("11.2"), [11, 2, 0]) - self.assertEqual(VMMP.toList_majorMinorPatch("\n1 . 2 \n"), [1, 2, 0]) - - with self.assertRaises(Exception): - VMMP.toList_majorMinorPatch("") - with self.assertRaises(Exception): - VMMP.toList_majorMinorPatch("11.") - with self.assertRaises(Exception): - VMMP.toList_majorMinorPatch("11.2.") - with self.assertRaises(Exception): - VMMP.toList_majorMinorPatch("11.2.3.") - with self.assertRaises(Exception): - VMMP.toList_majorMinorPatch(".11") - with self.assertRaises(Exception): - VMMP.toList_majorMinorPatch("1_2_3_4") - with self.assertRaises(Exception): - VMMP.toList_majorMinorPatch("_1_2_3_") - with self.assertRaises(Exception): - VMMP.toList_majorMinorPatch(" \n 11...22.333-rc2\n") - with self.assertRaises(Exception): - VMMP.toList_majorMinorPatch(" \n 11...22.333-rc2\n") - with self.assertRaises(Exception): - VMMP.toList_majorMinorPatch(" \n 11...22.333-rc2\n") - - def test_040(self): - MMP = VMMP.MinorMajorPatch - v = [1, 2, 3] - self.assertEqual(MMP(v).__str__(), "1.2.3") - self.assertEqual(MMP(v).__str__(sep="_"), "1_2_3") - self.assertEqual(str(MMP(v)), "1.2.3") - - self.assertEqual(MMP(v).__repr__(), "version_1_2_3") - self.assertEqual(MMP(v).__repr__(sep="."), "version_1.2.3") - - self.assertEqual(MMP(v).strSalome(), "1_2_3") - self.assertEqual(MMP(v).strClassic(), "1.2.3") - - self.assertEqual(MMP([" 123 \n", 2, 10]).strClassic(), "123.2.10") - self.assertEqual(MMP([" 123 \n", 2, 10]).strSalome(), "123_2_10") - - with self.assertRaises(Exception): - MMP([-5, 2, 10]) - with self.assertRaises(Exception): - MMP([5, -2, 10]) - with self.assertRaises(Exception): - MMP([5, 2, -10]) - with self.assertRaises(Exception): - MMP(["-123", 2, 10]) - - def test_050(self): - MMP = VMMP.MinorMajorPatch - v000 = MMP("0.0.0") - v010 = MMP("0.1.0") - v100 = MMP("1.0.0") - v101 = MMP("1.0.1") - - va = v000 - vb = MMP("0.0.0") - self.assertTrue(va == vb) - self.assertTrue(va >= vb) - self.assertTrue(va <= vb) - self.assertFalse(va != vb) - self.assertFalse(va > vb) - self.assertFalse(va < vb) - - va = v000 - vb = v010 - self.assertFalse(va == vb) - self.assertFalse(va >= vb) - self.assertTrue(va <= vb) - self.assertTrue(va != vb) - self.assertFalse(va > vb) - self.assertTrue(va < vb) - - va = v101 - vb = v100 - self.assertFalse(va == vb) - self.assertTrue(va >= vb) - self.assertFalse(va <= vb) - self.assertTrue(va != vb) - self.assertTrue(va > vb) - self.assertFalse(va < vb) - - def test_060(self): - MMP = VMMP.MinorMajorPatch - v0 = MMP("0") - v1 = MMP("1") - v2 = MMP("2") - v123 = MMP("1.2.3") - v456 = MMP("4.5.6") - - tests = """\ -toto_from_1_to_2 - _from_1.0.0_to_2.0.0 -_from_1_0. 0_to_ 2.0_0 -_from_V1.0.0_to_2.0.0 -_from_version_1.0.0_to_2.0.0 -version_1.0.0_to_2.0.0 -VERSION_1.0.0_to_2.0.0""".split( - "\n" - ) - - for a in tests: - # print("test '%s'" % a) - r1, r2 = VMMP.getRange_majorMinorPatch(a) - self.assertEqual(r1, v1) - self.assertEqual(r2, v2) - - a = "toto_to_2" - r1, r2 = VMMP.getRange_majorMinorPatch(a) - self.assertEqual(r1, v0) - self.assertEqual(r2, v2) - - a = "toto_to_Version2" - r1, r2 = VMMP.getRange_majorMinorPatch(a) - self.assertEqual(r1, v0) - self.assertEqual(r2, v2) - - a = "toto_from_1.2.3_to_Version4_5_6" - r1, r2 = VMMP.getRange_majorMinorPatch(a) - self.assertEqual(r1, v123) - self.assertEqual(r2, v456) - - a = "toto_from_1.2.3_to_Version1_2_3" - r1, r2 = VMMP.getRange_majorMinorPatch(a) - self.assertEqual(r1, v123) - self.assertEqual(r2, v123) - - # _from_ without _to_ does not matter - tests = """\ - -toto -from -to -_from_ -toto_from_2""".split( - "\n" - ) - - for a in tests: - rx = VMMP.getRange_majorMinorPatch(a, verbose=False) - self.assertEqual(rx, None) - - # _to_ without _from_ does not matter, as implicit _from_ '0.0.0' - # empty _to_ raise error - with self.assertRaises(Exception): - VMMP.getRange_majorMinorPatch("_to_") - with self.assertRaises(Exception): - VMMP.getRange_majorMinorPatch("_from_to_") - with self.assertRaises(Exception): - VMMP.getRange_majorMinorPatch("_from__to_") - with self.assertRaises(Exception): - VMMP.getRange_majorMinorPatch("toto_from__to_") - with self.assertRaises(Exception): - VMMP.getRange_majorMinorPatch("toto_from_123_to_") - with self.assertRaises(Exception): - VMMP.getRange_majorMinorPatch("version_123_to_") - with self.assertRaises(Exception): - VMMP.getRange_majorMinorPatch("version_to_") - - # min > max does matter - with self.assertRaises(Exception): - VMMP.getRange_majorMinorPatch("_from_3_to_2") - with self.assertRaises(Exception): - VMMP.getRange_majorMinorPatch("_from_3.2.5_to_V2_1_1") - - -if __name__ == "__main__": - unittest.main(exit=False) diff --git a/test/test_024_logging.py b/test/test_024_logging.py deleted file mode 100755 index 6713cc6..0000000 --- a/test/test_024_logging.py +++ /dev/null @@ -1,153 +0,0 @@ -#!/usr/bin/env python -# -*- coding:utf-8 -*- - -# Copyright (C) 2010-2018 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 - -""" -see: http://sametmax.com/ecrire-des-logs-en-python - -| # creation d'un formateur qui va ajouter le temps, le niveau -| # de chaque message quand on ecrira un message dans le log -| formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s') -| -| # creation d'un handler qui va rediriger une ecriture du log vers -| # un fichier en mode 'append', avec 1 backup et une taille max de 1Mo -| file_handler = RotatingFileHandler('activity.log', 'a', 1000000, 1) -| -| # on lui met le niveau sur DEBUG, on lui dit qu'il doit utiliser le formateur -| # cree precedement et on ajoute ce handler au logger -| file_handler.setLevel(logging.DEBUG) -| file_handler.setFormatter(formatter) -| logger.addHandler(file_handler) -| -| # creation d'un second handler qui va rediriger chaque ecriture de log -| # sur la console -| stream_handler = logging.StreamHandler() -| stream_handler.setLevel(logging.DEBUG) -| logger.addHandler(stream_handler) -| -| # Après 3 heures, on peut enfin logguer -| # Il est temps de spammer votre code avec des logs partout : -| logger.info('Hello') -| logger.warning('Testing %s', 'foo') -""" - -import unittest -import logging as LOGI - -import src.debug as DBG - -verbose = False # True - -_TRACE = LOGI.INFO - 2 # just below info - - -class LoggerSat(LOGI.Logger): - """ - Elementary prototype for logger sat - add a level TRACE as log.trace(msg) - below log.info(msg) - above log.debug(msg) - to assume store long log asci in files txt under/outside files xml - - see: /usr/lib64/python2.7/logging/xxx__init__.py etc. - """ - - def __init__(self, name, level=LOGI.INFO): - """ - Initialize the logger with a name and an optional level. - """ - super(LoggerSat, self).__init__(name, level) - LOGI.addLevelName(_TRACE, "TRACE") - # LOGI.TRACE = _TRACE # only for coherency, - - def trace(self, msg, *args, **kwargs): - """ - Log 'msg % args' with severity '_TRACE'. - - To pass exception information, use the keyword argument exc_info with - a true value, e.g. - - logger.trace("Houston, we have a %s", "long trace to follow") - """ - if self.isEnabledFor(_TRACE): - self._log(_TRACE, msg, args, **kwargs) - - -class TestCase(unittest.TestCase): - "Test the debug.py" "" - - initialLoggerClass = [] # to keep clean module logging - - def test_000(self): - # one shot setUp() for this TestCase - self.initialLoggerClass.append(LOGI._loggerClass) - LOGI.setLoggerClass(LoggerSat) - if verbose: - DBG.push_debug(True) - # DBG.write("assert unittest", [a for a in dir(self) if "assert" in a]) - - def test_999(self): - # one shot tearDown() for this TestCase - if verbose: - DBG.pop_debug() - LOGI.setLoggerClass(self.initialLoggerClass[0]) - return - - def test_010(self): - # LOGI.setLoggerClass(LoggerSat) # done once in test_000 - name = "testLogging" - lgr = LOGI.getLogger(name) # create it - lgr.setLevel("DEBUG") - self.assertEqual(lgr.__class__, LoggerSat) - self.assertEqual(lgr.name, name) - self.assertIn("trace", dir(lgr)) - self.assertIn("TRACE", LOGI._levelNames.keys()) - self.assertIn(_TRACE, LOGI._levelNames.keys()) - self.assertEqual(LOGI.getLevelName(LOGI.INFO), "INFO") - self.assertEqual(LOGI.getLevelName(_TRACE), "TRACE") - - # creation d'un handler pour chaque log sur la console - formatter = LOGI.Formatter("%(levelname)-8s :: %(message)s") - # stream_handler = LOGI.handlers.StreamHandler() # log outputs in console - stream_handler = LOGI.handlers.BufferingHandler(1000) # log outputs in memory - stream_handler.setLevel(LOGI.DEBUG) - stream_handler.setFormatter(formatter) - lgr.addHandler(stream_handler) - # print # skip one line if outputs in console - lgr.warning("!!! test warning") - lgr.info("!!! test info") - lgr.trace("!!! test trace") - lgr.debug("!!! test debug") - self.assertEqual(len(stream_handler.buffer), 4) - rec = stream_handler.buffer[-1] - self.assertEqual(rec.levelname, "DEBUG") - self.assertEqual(rec.msg, "!!! test debug") - self.assertEqual(stream_handler.get_name(), None) # what to serve ? - - def test_020(self): - # LOGI.setLoggerClass(LoggerSat) - name = "testLogging" - lgr = LOGI.getLogger(name) # find it as created yet in test_010 - stream_handler = lgr.handlers[0] - rec = stream_handler.buffer[-1] - self.assertEqual(rec.levelname, "DEBUG") - self.assertEqual(rec.msg, "!!! test debug") - - -if __name__ == "__main__": - unittest.main(exit=False) diff --git a/test/test_035_pyconf.py b/test/test_035_pyconf.py deleted file mode 100755 index c465937..0000000 --- a/test/test_035_pyconf.py +++ /dev/null @@ -1,256 +0,0 @@ -#!/usr/bin/env python -# -*- coding:utf-8 -*- - -# Copyright (C) 2010-2018 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 initializeTest # set PATH etc for test - -import src.debug as DBG # Easy print stderr (for DEBUG only) -import src.pyconf as PYF # 0.3.7 - -_EXAMPLES = { - 1: """\ - messages: - [ - { - stream : "sys.stderr" # modified - message: 'Welcome' - name: 'Harry' - } - { - stream : "sys.stdout" # modified - message: 'Welkom' - name: 'Ruud' - } - { - stream : $messages[0].stream - message: 'Bienvenue' - name: "Yves" - } - ] -""", - 2: """\ - aa: 111 - bb: $aa + 222 -""", - 3: """\ - aa: Yves - bb: "Herve" # avoid Hervé -> 'utf8' codec can't decode byte -""", - 4: """\ - aa: Yves - bb: "Hervé" # avoid Hervé -> 'utf8' codec can't decode byte -""", - 5: """\ - aa: Yves - bb: "Herve" - cc: [ - cc1 - cc2 - cc3 - $bb + " hello" - ] - dd: { - d1 : dd11 - d2 : dd22 - d3 : dd33 - d4 : $bb + " bye" - } -""", - # error circular - 6: """\ - aa: Yves - bb: $cc - cc: $bb -""", - 7: """\ - aa: Yves - bb: $cc - cc: [ - cc1 - $bb - ] -""", - 8: """\ - aa: Yves - bb: $cc - cc: { - cc1: cc11 - cc2: $bb - } -""", -} - - -class TestCase(unittest.TestCase): - "Test the pyconf.py" "" - - def test_000(self): - # one shot setUp() for this TestCase - # DBG.push_debug(True) - # SAT.setNotLocale() # test english - return - - def test_010(self): - # pyconf.py doc example 0.3.7 - # https://www.red-dove.com/config-doc/ is 0.3.9 ! - # which, when run, would yield the console output: - - expected = """\ -Welcome, Harry -Welkom, Ruud -Bienvenue, Yves -""" - inStream = DBG.InStream(_EXAMPLES[1]) - cfg = PYF.Config(inStream) - res = "" - for m in cfg.messages: - res += "%s, %s\n" % (m.message, m.name) - self.assertEqual(res, expected) - outStream = DBG.OutStream() - cfg.__save__(outStream) # sat renamed save() in __save__() - res = outStream.value - DBG.write("test_010 cfg", res) - self.assertTrue("name : 'Harry'" in res) - self.assertTrue("name : 'Ruud'" in res) - self.assertTrue("name : 'Yves'" in res) - - def test_020(self): - cfg = PYF.Config() - self.assertEqual(str(cfg), "{}") - self.assertEqual(cfg.__repr__(), "{}") - cfg.aa = "1111" - self.assertEqual(str(cfg), "{'aa': '1111'}") - cfg.bb = 2222 - self.assertTrue("'bb': 2222" in str(cfg)) - self.assertTrue("'aa': '1111'" in str(cfg)) - cfg.cc = 3333.0 - self.assertTrue("'cc': 3333." in str(cfg)) - - def test_030(self): - inStream = DBG.InStream(_EXAMPLES[2]) - cfg = PYF.Config(inStream) - self.assertEqual(str(cfg), "{'aa': 111, 'bb': $aa + 222}") - self.assertEqual(cfg.aa, 111) - self.assertEqual(cfg.bb, 333) - - def test_040(self): - inStream = DBG.InStream(_EXAMPLES[3]) - cfg = PYF.Config(inStream) - self.assertEqual(cfg.aa, "Yves") - self.assertEqual(cfg.bb, "Herve") - self.assertEqual(type(cfg.bb), str) - cfg.bb = "Hervé" # try this - self.assertEqual(type(cfg.bb), str) - self.assertEqual(cfg.bb, "Hervé") - - def test_045(self): - # make Hervé valid only with pyconf.py as 0.3.9 - inStream = DBG.InStream(_EXAMPLES[4]) - outStream = DBG.OutStream() - with self.assertRaises(Exception): - cfg = PYF.Config(inStream) - - return # TODO only with pyconf.py as 0.3.9 - cfg.save(outStream) # OK - # TODO: cfg = PYF.Config(inStream) - # cfg.__save__(outStream) # KO and sat renamed save() in __save__() - res = outStream.value - DBG.write("test_045 cfg", res) - self.assertTrue("aa : 'Yves'" in res) - self.assertTrue(r"bb : 'Herv\xc3\xa9'" in res) - self.assertEqual(cfg.bb, "Hervé") - - def test_100(self): - inStream = DBG.InStream(_EXAMPLES[5]) - outStream = DBG.OutStream() - cfg = PYF.Config(inStream) # KO - cfg.__save__(outStream) # sat renamed save() in __save__() - res = outStream.value - DBG.write("test_100 cfg save", res) - DBG.write("test_100 cfg debug", cfg) - DBG.write("test_100 cfg.cc debug", cfg.cc) - - cc = cfg.cc - # DBG.write("test_100 type cc[3]", dir(cc), True) - DBG.write("test_100 cc", [cc.data[i] for i in range(len(cc))]) - - def test_100(self): - inStream = DBG.InStream(_EXAMPLES[5]) - outStream = DBG.OutStream() - cfg = PYF.Config(inStream) # KO - cfg.__save__(outStream) # sat renamed save() in __save__() - res = outStream.value - DBG.write("test_100 cfg save", res) - DBG.write("test_100 cfg debug", cfg) - DBG.write("test_100 cfg.cc debug", cfg.cc) - - cc = cfg.cc - # DBG.write("test_100 type cc[3]", dir(cc), True) - DBG.write("test_100 cc", [cc.data[i] for i in range(len(cc))]) - - def test_110(self): - inStream = DBG.InStream(_EXAMPLES[6]) - outStream = DBG.OutStream() - cfg = PYF.Config(inStream) - cfg.__save__(outStream) - - res = outStream.value - DBG.write("test_110 cfg save", res) - self.assertNotIn("ERROR", res) - - res = DBG.getStrConfigDbg(cfg) - DBG.write("test_110 cfg debug", res) - self.assertIn("ERROR", res) - self.assertIn("unable to evaluate $cc", res) - self.assertIn("unable to evaluate $bb", res) - - def test_120(self): - for ii in [7, 8]: - inStream = DBG.InStream(_EXAMPLES[ii]) - outStream = DBG.OutStream() - cfg = PYF.Config(inStream) - cfg.__save__(outStream) - - res = outStream.value - DBG.write("test_120 cfg save", res) - self.assertNotIn("ERROR", res) - - res = DBG.getStrConfigDbg(cfg) - - DBG.write("test_120 cfg debug", res) - # no error circular !!! - # self.assertIn("ERROR", res) # no error circular !!! - # self.assertIn("unable to evaluate $cc", res) - # self.assertIn("unable to evaluate $bb", res) - res = cfg.bb - DBG.write("test_120 cfg.bb debug", res) - - res = cfg.cc - DBG.write("test_120 cfg.cc debug", res) - - def test_999(self): - # one shot tearDown() for this TestCase - # SAT.setLocale() # end test english - # DBG.pop_debug() - return - - -if __name__ == "__main__": - unittest.main(exit=False) diff --git a/test/test_100_satHelp.py b/test/test_100_satHelp.py deleted file mode 100755 index 18d3a6d..0000000 --- a/test/test_100_satHelp.py +++ /dev/null @@ -1,145 +0,0 @@ -#!/usr/bin/env python -# -*- coding:utf-8 -*- - -# Copyright (C) 2010-2018 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 initializeTest # set PATH etc for test - -import src.salomeTools as SAT -import src.debug as DBG # Easy print stderr (for DEBUG only) -import src.loggingSimple as LOG - - -class TestCase(unittest.TestCase): - "Test the sat --help commands" "" - - debug = False - - def tearDown(self): - # print "tearDown", __file__ - # assure self.logger clear for next test - logger = LOG.getUnittestLogger() - logs = logger.getLogsAndClear() - # using assertNotIn() is too much verbose - self.assertFalse("ERROR" in logs) - self.assertFalse("CRITICAL" in logs) - - def test_000(self): - LOG.getUnittestLogger() - # one shot setUp() for this TestCase - if self.debug: - DBG.push_debug(True) - SAT.setNotLocale() # test english - - def test_999(self): - # one shot tearDown() for this TestCase - SAT.setLocale() # end test english - if self.debug: - DBG.pop_debug() - - def test_010(self): # TODO fix logger unittest - cmd = "sat --help" - res = SAT.launchSat(cmd) - self.assertTrue(res.isOk()) - out = res.getValue() - self.assertTrue(" - config" in out) - self.assertTrue(" - prepare" in out) - self.assertTrue(" - compile" in out) - - def test_011(self): - logger = LOG.getUnittestLogger() - cmd = "--help" - s = SAT.Sat(logger) - returnCode = s.execute_cli(cmd) - self.assertTrue(returnCode.isOk()) - logs = logger.getLogs() - DBG.write("test_011 logger", logs) - self.assertTrue(" - config" in logs) - self.assertTrue(" - prepare" in logs) - self.assertTrue(" - compile" in logs) - - def test_030(self): - cmd = "sat config --help" - returnCode = SAT.launchSat(cmd) - self.assertTrue(returnCode.isOk()) - out = returnCode.getValue() - DBG.write("test_030 stdout", out) - self.assertTrue("--value" in out) - - def test_031(self): - logger = LOG.getUnittestLogger() - cmd = "config --help" - s = SAT.Sat(logger) - returnCode = s.execute_cli(cmd) - self.assertTrue(returnCode.isOk()) - logs = logger.getLogs() - DBG.write("test_031 logger", logs) - self.assertTrue("--help" in logs) - - def test_032(self): - logger = LOG.getUnittestLogger() - cmd = "prepare --help" - s = SAT.Sat(logger) - returnCode = s.execute_cli(cmd) - self.assertTrue(returnCode.isOk()) - logs = logger.getLogs() - DBG.write("test_031 logger", logs) - self.assertTrue("--help" in logs) - - def test_040(self): - logger = LOG.getUnittestLogger() - cmd = "config --list" - s = SAT.Sat(logger) - returnCode = s.execute_cli(cmd) - self.assertTrue(returnCode.isOk()) - logs = logger.getLogs() - self.assertTrue("Applications" in logs) - - def test_050(self): - cmds = SAT.getCommandsList() - DBG.write("test_050 getCommandsList", cmds) - for c in cmds: - cmd = "sat %s --help" % c - DBG.write("test_050", cmd) - returnCode = SAT.launchSat(cmd) - if not returnCode.isOk(): - DBG.write("test_050 %s" % cmd, returnCode.getValue(), True) - self.assertTrue(returnCode.isOk()) - out = returnCode.getValue() - DBG.write("test_050 %s stdout" % c, out) - self.assertTrue("The %s command" % c in out) - self.assertTrue("Available options" in out) - - def test_051(self): - logger = LOG.getUnittestLogger() - cmds = SAT.getCommandsList() - for c in cmds: - cmd = "%s --help" % c - DBG.write("test_051", cmd) - s = SAT.Sat(logger) - returnCode = s.execute_cli(cmd) - self.assertTrue(returnCode.isOk()) - logs = logger.getLogsAndClear() - DBG.write(cmd, logs) - self.assertTrue("The %s command" % c in logs) - self.assertTrue("Available options" in logs) - - -if __name__ == "__main__": - unittest.main(exit=False) diff --git a/test/test_500_APPLI_TEST.py b/test/test_500_APPLI_TEST.py deleted file mode 100755 index d928526..0000000 --- a/test/test_500_APPLI_TEST.py +++ /dev/null @@ -1,130 +0,0 @@ -#!/usr/bin/env python -# -*- coding:utf-8 -*- - -# Copyright (C) 2010-2018 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 initializeTest # set PATH etc for test - -import src.salomeTools as SAT -import src.debug as DBG # Easy print stderr (for DEBUG only) -import src.loggingSimple as LOG - - -class TestCase(unittest.TestCase): - "Test the sat commands on APPLI_TEST configuration pyconf etc. files" "" - - debug = False - - # see test_100, # commands are expected OK - TRG = "APPLI_TEST" # "SALOME-8.4.0" - satCommandsToTestOk = [ - "config -l", - "config -v .", - "config -g .", - "config %s --value ." % TRG, - "config %s --debug ." % TRG, - "config %s --info KERNEL" % TRG, - "config %s --show_patchs" % TRG, - ] - # see test_110, # commands are expected KO - satCommandsToTestKo = [ - "config %s --info oops" % TRG, - "config --oops", - ] - # see test_120, # commands are expected Raise, - # but if fixed go to satCommandsToTestKo - satCommandsToTestRaise = [ - "oopsconfig --oops .", - ] - - def tearDown(self): - # print "tearDown", __file__ - # assure self.logger clear for next test - logger = LOG.getUnittestLogger() - logs = logger.getLogsAndClear() - # using assertNotIn() is too much verbose - self.assertFalse("ERROR ::" in logs) - self.assertFalse("CRITICAL ::" in logs) - - def test_000(self): - LOG.getUnittestLogger() - # one shot setUp() for this TestCase - if self.debug: - DBG.push_debug(True) - SAT.setNotLocale() # test english - - def test_999(self): - # one shot tearDown() for this TestCase - SAT.setLocale() # end test english - if self.debug: - DBG.pop_debug() - - def test_010(self): - logger = LOG.getUnittestLogger() - DBG.write("test_010 logger", logger.name) - cmd = "config -l" - s = SAT.Sat(logger) - DBG.write("s.getConfig()", s.getConfig()) # none - DBG.write("s.__dict__", s.__dict__) # have - returnCode = s.execute_cli(cmd) - DBG.write("test_010", returnCode) - logs = logger.getLogs() - DBG.write("test_010 logger", logs) - self.assertTrue(returnCode.isOk()) - - def xtest_100(self): # TODO - # test all satCommands expected OK - logger = LOG.getUnittestLogger() - dbg = self.debug - for cmd in self.satCommandsToTestOk: - s = SAT.Sat(logger) - returnCode = s.execute_cli(cmd) - DBG.write("test_100 'sat %s'" % cmd, str(returnCode), dbg) - logs = logger.getLogsAndClear() - DBG.write("logs", logs, dbg) - # using assertNotIn() is too much verbose - self.assertFalse("ERROR ::" in logs) - self.assertFalse("CRITICAL ::" in logs) - - def test_110(self): - # test all satCommands expected KO - logger = LOG.getUnittestLogger() - dbg = self.debug - for cmd in self.satCommandsToTestKo: - s = SAT.Sat(logger) - returnCode = s.execute_cli(cmd) - DBG.write("test_110 'sat %s'" % cmd, returnCode, dbg) - logs = logger.getLogsAndClear() - DBG.write("logs", logs, dbg) - - def test_120(self): - # test all satCommands expected raise - logger = LOG.getUnittestLogger() - dbg = self.debug - for cmd in self.satCommandsToTestRaise: - s = SAT.Sat(logger) - DBG.write("test_120 'sat %s'" % cmd, "expected raise", dbg) - with self.assertRaises(Exception): - s.execute_cli(cmd) - logs = logger.getLogsAndClear() - DBG.write("logs", logs, dbg) - - -if __name__ == "__main__": - unittest.main(exit=False) diff --git a/test/test_debug.py b/test/test_debug.py new file mode 100755 index 0000000..ce74613 --- /dev/null +++ b/test/test_debug.py @@ -0,0 +1,94 @@ +#!/usr/bin/env python + +import unittest +import src.debug as debug +import src.pyconf as pyconf +from test.init_test_env import init_test_env + +init_test_env() + +_EXAMPLES = { + 1: """\ + messages: + [ + { + stream : "sys.stderr" # modified + message: 111 # modified + name: 'Harry' + } + { + stream : $messages[0].stream + message: 1.23e4 # modified do not work 0.3.7 + name: 'Ruud' + } + { + stream : "HELLO " + $messages[0].stream + message: 'Bienvenue' + name: "Yves" + } + ] +""", + 2: """\ + aa: 111 + bb: $aa + 222 +""", + 3: """\ + aa: Yves + bb: "Herve" # avoid Hervé -> 'utf8' codec can't decode byte +""", + 4: """\ + aa: Yves + bb: "Hervé" # avoid Hervé -> 'utf8' codec can't decode byte +""", +} + + +class DebugTestCase(unittest.TestCase): + """ + test src/debug.py + """ + + def test_005(self): + res = debug.getLocalEnv() + self.assertTrue(len(res.split()) > 0) + self.assertTrue("USER :" in res) + self.assertTrue("LANG :" in res) + + def test_010(self): + inStream = debug.InStream(_EXAMPLES[1]) + self.assertEqual(inStream.getvalue(), _EXAMPLES[1]) + cfg = pyconf.Config(inStream) + self.assertEqual(len(cfg.messages), 3) + outStream = debug.OutStream() + debug.saveConfigStd(cfg, outStream) + res = outStream.value + debug.write("test_010 cfg std", res) + self.assertTrue("messages :" in res) + self.assertTrue("'sys.stderr'" in res) + + def test_020(self): + inStream = debug.InStream(_EXAMPLES[2]) + cfg = pyconf.Config(inStream) + res = debug.getStrConfigDbg(cfg) + debug.write("test_020 cfg dbg", res) + ress = res.split("\n") + self.assertTrue(".aa" in ress[0]) + self.assertTrue(": '111'" in ress[0]) + self.assertTrue(".bb" in ress[1]) + self.assertTrue(": $aa + 222 " in ress[1]) + self.assertTrue("--> '333'" in ress[1]) + + def test_025(self): + inStream = debug.InStream(_EXAMPLES[1]) + cfg = pyconf.Config(inStream) + outStream = debug.OutStream() + debug.saveConfigDbg(cfg, outStream) + res = outStream.value + debug.write("test_025 cfg dbg", res) + for i in range(len(cfg.messages)): + self.assertTrue("messages[%i].name" % i in res) + self.assertTrue("--> 'HELLO sys.stderr'" in res) + + +if __name__ == "__main__": + unittest.main() diff --git a/test/test_dummy_application.py b/test/test_dummy_application.py new file mode 100755 index 0000000..755235f --- /dev/null +++ b/test/test_dummy_application.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python +import unittest +import src.salomeTools as sat +import src.debug as debug # Easy print stderr (for DEBUG only) +import src.loggingSimple as log + +class DummyApplicationTestCase(unittest.TestCase): + "Test the sat commands on APPLI_TEST configuration pyconf etc. files" "" + + debug = False + + # see test_100, # commands are expected OK + TRG = "APPLI_TEST" # "SALOME-8.4.0" + satCommandsToTestOk = [ + "config -l", + "config -v .", + "config -g .", + "config %s --value ." % TRG, + "config %s --debug ." % TRG, + "config %s --info KERNEL" % TRG, + "config %s --show_patchs" % TRG, + ] + # see test_110, # commands are expected KO + satCommandsToTestKo = [ + "config %s --info oops" % TRG, + "config --oops", + ] + # see test_120, # commands are expected Raise, + # but if fixed go to satCommandsToTestKo + satCommandsToTestRaise = [ + "oopsconfig --oops .", + ] + + def tearDown(self): + # print "tearDown", __file__ + # assure self.logger clear for next test + logger = log.getUnittestLogger() + logs = logger.getLogsAndClear() + # using assertNotIn() is too much verbose + self.assertFalse("ERROR ::" in logs) + self.assertFalse("CRITICAL ::" in logs) + + def test_000(self): + log.getUnittestLogger() + if self.debug: + debug.push_debug(True) + sat.setNotLocale() # test english + + def test_999(self): + # one shot tearDown() for this TestCase + sat.setLocale() # end test english + + def test_010(self): + logger = log.getUnittestLogger() + debug.write("test_010 logger", logger.name) + cmd = "config -l" + s = sat.Sat(logger) + debug.write("s.getConfig()", s.getConfig()) # none + debug.write("s.__dict__", s.__dict__) # have + returnCode = s.execute_cli(cmd) + debug.write("test_010", returnCode) + logs = logger.getLogs() + debug.write("test_010 logger", logs) + self.assertTrue(returnCode.isOk()) + + def xtest_100(self): # TODO + # test all satCommands expected OK + logger = log.getUnittestLogger() + for cmd in self.satCommandsToTestOk: + s = sat.Sat(logger) + s.execute_cli(cmd) + logs = logger.getLogsAndClear() + self.assertFalse("ERROR ::" in logs) + self.assertFalse("CRITICAL ::" in logs) + + def test_110(self): + # test all satCommands expected KO + logger = log.getUnittestLogger() + for cmd in self.satCommandsToTestKo: + s = sat.Sat(logger) + _ = s.execute_cli(cmd) + + def test_120(self): + # test all satCommands expected raise + logger = log.getUnittestLogger() + dbg = self.debug + for cmd in self.satCommandsToTestRaise: + s = sat.Sat(logger) + with self.assertRaises(Exception): + s.execute_cli(cmd) + logs = logger.getLogsAndClear() + debug.write("logs", logs, dbg) + + +if __name__ == "__main__": + unittest.main() diff --git a/test/test_logging.py b/test/test_logging.py new file mode 100755 index 0000000..809ec1d --- /dev/null +++ b/test/test_logging.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python + +import unittest +import logging as log +import src.debug as debug +from test.init_test_env import init_test_env + +init_test_env() + +_TRACE = log.INFO - 2 # just below info + + +class LoggerSat(log.Logger): + """ + Elementary prototype for logger sat + add a level TRACE as log.trace(msg) + below log.info(msg) + above log.debug(msg) + to assume store long log asci in files txt under/outside files xml + + see: /usr/lib64/python2.7/logging/xxx__init__.py etc. + """ + + def __init__(self, name, level=log.INFO): + """ + Initialize the logger with a name and an optional level. + """ + super(LoggerSat, self).__init__(name, level) + log.addLevelName(_TRACE, "TRACE") + + def trace(self, msg, *args, **kwargs): + """ + Log 'msg % args' with severity '_TRACE'. + + To pass exception information, use the keyword argument exc_info with + a true value, e.g. + + logger.trace("Houston, we have a %s", "long trace to follow") + """ + if self.isEnabledFor(_TRACE): + self._log(_TRACE, msg, args, **kwargs) + + +class LoggingTestCase(unittest.TestCase): + "Test the debug.py" "" + + initialLoggerClass = [] # to keep clean module logging + + def test_000(self): + self.initialLoggerClass.append(log._loggerClass) + log.setLoggerClass(LoggerSat) + + def test_999(self): + log.setLoggerClass(self.initialLoggerClass[0]) + return + + def test_010(self): + name = "testLogging" + lgr = log.getLogger(name) # create it + lgr.setLevel("DEBUG") + self.assertEqual(lgr.__class__, LoggerSat) + self.assertEqual(lgr.name, name) + self.assertIn("trace", dir(lgr)) + self.assertEqual(log.getLevelName(log.INFO), "INFO") + self.assertEqual(log.getLevelName(_TRACE), "TRACE") + + # creation d'un handler pour chaque log sur la console + formatter = log.Formatter("%(levelname)-8s :: %(message)s") + # stream_handler = log.handlers.StreamHandler() # log outputs in console + stream_handler = log.handlers.BufferingHandler(1000) # log outputs in memory + stream_handler.setLevel(log.DEBUG) + stream_handler.setFormatter(formatter) + lgr.addHandler(stream_handler) + # print # skip one line if outputs in console + lgr.warning("!!! test warning") + lgr.info("!!! test info") + lgr.trace("!!! test trace") + lgr.debug("!!! test debug") + self.assertEqual(len(stream_handler.buffer), 4) + rec = stream_handler.buffer[-1] + self.assertEqual(rec.levelname, "DEBUG") + self.assertEqual(rec.msg, "!!! test debug") + self.assertEqual(stream_handler.get_name(), None) # what to serve ? + + +if __name__ == "__main__": + unittest.main() diff --git a/test/test_pyconf.py b/test/test_pyconf.py new file mode 100755 index 0000000..938279d --- /dev/null +++ b/test/test_pyconf.py @@ -0,0 +1,203 @@ +#!/usr/bin/env python +import unittest +import src.debug as debug +import src.pyconf as pyconf +from test.init_test_env import init_test_env + +init_test_env() + +_EXAMPLES = { + 1: """\ + messages: + [ + { + stream : "sys.stderr" # modified + message: 'Welcome' + name: 'Harry' + } + { + stream : "sys.stdout" # modified + message: 'Welkom' + name: 'Ruud' + } + { + stream : $messages[0].stream + message: 'Bienvenue' + name: "Yves" + } + ] +""", + 2: """\ + aa: 111 + bb: $aa + 222 +""", + 3: """\ + aa: Yves + bb: "Herve" # avoid Hervé -> 'utf8' codec can't decode byte +""", + 4: """\ + aa: Yves + bb: "Hervé" # avoid Hervé -> 'utf8' codec can't decode byte +""", + 5: """\ + aa: Yves + bb: "Herve" + cc: [ + cc1 + cc2 + cc3 + $bb + " hello" + ] + dd: { + d1 : dd11 + d2 : dd22 + d3 : dd33 + d4 : $bb + " bye" + } +""", + # error circular + 6: """\ + aa: Yves + bb: $cc + cc: $bb +""", + 7: """\ + aa: Yves + bb: $cc + cc: [ + cc1 + $bb + ] +""", + 8: """\ + aa: Yves + bb: $cc + cc: { + cc1: cc11 + cc2: $bb + } +""", +} + + +class PyconfTestCase(unittest.TestCase): + "Test the pyconf.py" "" + + def test_010(self): + # pyconf.py doc example 0.3.7 + # https://www.red-dove.com/config-doc/ is 0.3.9 ! + # which, when run, would yield the console output: + + expected = """\ +Welcome, Harry +Welkom, Ruud +Bienvenue, Yves +""" + inStream = debug.InStream(_EXAMPLES[1]) + cfg = pyconf.Config(inStream) + res = "" + for m in cfg.messages: + res += "%s, %s\n" % (m.message, m.name) + self.assertEqual(res, expected) + outStream = debug.OutStream() + cfg.__save__(outStream) # sat renamed save() in __save__() + res = outStream.value + debug.write("test_010 cfg", res) + self.assertTrue("name : 'Harry'" in res) + self.assertTrue("name : 'Ruud'" in res) + self.assertTrue("name : 'Yves'" in res) + + def test_020(self): + cfg = pyconf.Config() + self.assertEqual(str(cfg), "{}") + self.assertEqual(cfg.__repr__(), "{}") + cfg.aa = "1111" + self.assertEqual(str(cfg), "{'aa': '1111'}") + cfg.bb = 2222 + self.assertTrue("'bb': 2222" in str(cfg)) + self.assertTrue("'aa': '1111'" in str(cfg)) + cfg.cc = 3333.0 + self.assertTrue("'cc': 3333." in str(cfg)) + + def test_030(self): + inStream = debug.InStream(_EXAMPLES[2]) + cfg = pyconf.Config(inStream) + self.assertEqual(str(cfg), "{'aa': 111, 'bb': $aa + 222}") + self.assertEqual(cfg.aa, 111) + self.assertEqual(cfg.bb, 333) + + def test_040(self): + inStream = debug.InStream(_EXAMPLES[3]) + cfg = pyconf.Config(inStream) + self.assertEqual(cfg.aa, "Yves") + self.assertEqual(cfg.bb, "Herve") + self.assertEqual(type(cfg.bb), str) + cfg.bb = "Hervé" # try this + self.assertEqual(type(cfg.bb), str) + self.assertEqual(cfg.bb, "Hervé") + + def test_100(self): + inStream = debug.InStream(_EXAMPLES[5]) + outStream = debug.OutStream() + cfg = pyconf.Config(inStream) # KO + cfg.__save__(outStream) # sat renamed save() in __save__() + res = outStream.value + debug.write("test_100 cfg save", res) + debug.write("test_100 cfg debug", cfg) + debug.write("test_100 cfg.cc debug", cfg.cc) + + cc = cfg.cc + debug.write("test_100 cc", [cc.data[i] for i in range(len(cc))]) + + def test_105(self): + inStream = debug.InStream(_EXAMPLES[5]) + outStream = debug.OutStream() + cfg = pyconf.Config(inStream) # KO + cfg.__save__(outStream) # sat renamed save() in __save__() + res = outStream.value + debug.write("test_100 cfg save", res) + debug.write("test_100 cfg debug", cfg) + debug.write("test_100 cfg.cc debug", cfg.cc) + + cc = cfg.cc + debug.write("test_100 cc", [cc.data[i] for i in range(len(cc))]) + + def test_110(self): + inStream = debug.InStream(_EXAMPLES[6]) + outStream = debug.OutStream() + cfg = pyconf.Config(inStream) + cfg.__save__(outStream) + + res = outStream.value + debug.write("test_110 cfg save", res) + self.assertNotIn("ERROR", res) + + res = debug.getStrConfigDbg(cfg) + debug.write("test_110 cfg debug", res) + self.assertIn("ERROR", res) + self.assertIn("unable to evaluate $cc", res) + self.assertIn("unable to evaluate $bb", res) + + def test_120(self): + for ii in [7, 8]: + inStream = debug.InStream(_EXAMPLES[ii]) + outStream = debug.OutStream() + cfg = pyconf.Config(inStream) + cfg.__save__(outStream) + + res = outStream.value + debug.write("test_120 cfg save", res) + self.assertNotIn("ERROR", res) + + res = debug.getStrConfigDbg(cfg) + + debug.write("test_120 cfg debug", res) + res = cfg.bb + debug.write("test_120 cfg.bb debug", res) + + res = cfg.cc + debug.write("test_120 cfg.cc debug", res) + + +if __name__ == "__main__": + unittest.main() diff --git a/test/test_sat_help.py b/test/test_sat_help.py new file mode 100755 index 0000000..fd054f7 --- /dev/null +++ b/test/test_sat_help.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python + +import unittest +import src.salomeTools as sat +import src.debug as debug +import src.loggingSimple as log +from test.init_test_env import init_test_env + +init_test_env() + +class SatHelpTestCase(unittest.TestCase): + """ + test the sat --help commands + """ + + def tearDown(self): + logger = log.getUnittestLogger() + logs = logger.getLogsAndClear() + self.assertFalse("ERROR" in logs) + self.assertFalse("CRITICAL" in logs) + + def test_000(self): + log.getUnittestLogger() + sat.setNotLocale() # test english + + def test_999(self): + # one shot tearDown() for this TestCase + sat.setLocale() # end test english + + def test_010(self): + cmd = "sat --help" + res = sat.launchSat(cmd) + self.assertTrue(res.isOk()) + out = res.getValue() + self.assertTrue(" - config".encode() in out) + self.assertTrue(" - prepare".encode() in out) + self.assertTrue(" - compile".encode() in out) + + def test_011(self): + logger = log.getUnittestLogger() + cmd = "--help" + s = sat.Sat(logger) + returnCode = s.execute_cli(cmd) + self.assertTrue(returnCode.isOk()) + logs = logger.getLogs() + debug.write("test_011 logger", logs) + self.assertTrue(" - config" in logs) + self.assertTrue(" - prepare" in logs) + self.assertTrue(" - compile" in logs) + + def test_030(self): + cmd = "sat config --help" + returnCode = sat.launchSat(cmd) + self.assertTrue(returnCode.isOk()) + out = returnCode.getValue() + debug.write("test_030 stdout", out) + self.assertTrue("--value".encode() in out) + + def test_031(self): + logger = log.getUnittestLogger() + s = sat.Sat(logger) + returnCode = s.execute_cli("config --help") + self.assertTrue(returnCode.isOk()) + logs = logger.getLogs() + debug.write("test_031 logger", logs) + self.assertTrue("--help" in logs) + + def test_032(self): + logger = log.getUnittestLogger() + s = sat.Sat(logger) + returnCode = s.execute_cli("prepare --help") + self.assertTrue(returnCode.isOk()) + logs = logger.getLogs() + debug.write("test_031 logger", logs) + self.assertTrue("--help" in logs) + + def test_040(self): + logger = log.getUnittestLogger() + s = sat.Sat(logger) + returnCode = s.execute_cli("config --list") + self.assertTrue(returnCode.isOk()) + + def test_050(self): + cmds = sat.getCommandsList() + debug.write("test_050 getCommandsList", cmds) + for c in cmds: + cmd = f"sat {c} --help" + debug.write("test_050", cmd) + returnCode = sat.launchSat(cmd) + if not returnCode.isOk(): + debug.write("test_050 %s" % cmd, returnCode.getValue(), True) + self.assertTrue(returnCode.isOk()) + out = returnCode.getValue() + debug.write(f"test_050 {c} stdout", out) + self.assertTrue(f"The {c} command".encode() in out) + self.assertTrue("Available options".encode() in out) + + def test_051(self): + logger = log.getUnittestLogger() + cmds = sat.getCommandsList() + for c in cmds: + cmd = "%s --help" % c + debug.write("test_051", cmd) + s = sat.Sat(logger) + returnCode = s.execute_cli(cmd) + self.assertTrue(returnCode.isOk()) + logs = logger.getLogsAndClear() + debug.write(cmd, logs) + self.assertTrue(f"The {c} command" in logs) + self.assertTrue("Available options" in logs) + + +if __name__ == "__main__": + unittest.main() diff --git a/test/test_versionMinorMajor.py b/test/test_versionMinorMajor.py new file mode 100755 index 0000000..3a0d6af --- /dev/null +++ b/test/test_versionMinorMajor.py @@ -0,0 +1,219 @@ +#!/usr/bin/env python + +import unittest +import src.versionMinorMajorPatch as vmmp +from test.init_test_env import init_test_env + +init_test_env() + +class VersionMajorMinorTestCase(unittest.TestCase): + "Test the versionMajorMinorPatch.py" "" + + def test_010(self): + self.assertTrue(vmmp.only_numbers("") is None) + self.assertEqual(vmmp.only_numbers("1.2.3"), "123") + self.assertEqual(vmmp.only_numbers("\n11.12.13\n"), "111213") + self.assertEqual(vmmp.only_numbers(" \n 11.\t\n\t..12.13-rc2\n"), "1112132") + + def test_015(self): + res = "a_b_c" + self.assertEqual( + vmmp.remove_startswith("version_a_b_c", "version_".split()), res + ) + self.assertEqual(vmmp.remove_startswith("v_a_b_c", "version_ v_".split()), res) + self.assertEqual(vmmp.remove_startswith("va_b_c", "version_ v_ v".split()), res) + + ini = "version_a_b_c" + self.assertEqual(vmmp.remove_startswith(ini, "V".split()), ini) + self.assertEqual(vmmp.remove_startswith(ini, "_".split()), ini) + self.assertEqual(vmmp.remove_startswith(ini, "a_b_c".split()), ini) + self.assertEqual(vmmp.remove_startswith(ini, "VERSION".split()), ini) + + def test_020(self): + res = [11, 222, 3333] + self.assertEqual(vmmp.toList_majorMinorPatch("11.222.3333"), res) + self.assertEqual(vmmp.toList_majorMinorPatch("11_222_3333"), res) + self.assertEqual(vmmp.toList_majorMinorPatch("11.222_3333"), res) + self.assertEqual(vmmp.toList_majorMinorPatch(" 11. 222 . 3333 "), res) + self.assertEqual( + vmmp.toList_majorMinorPatch("\n 11 . 222 . 3333 \n"), res + ) + self.assertEqual( + vmmp.toList_majorMinorPatch(" \n11.\t222.\r3333\n "), res + ) # could be tricky + + self.assertEqual(vmmp.toList_majorMinorPatch("V11.222.3333"), res) + self.assertEqual(vmmp.toList_majorMinorPatch("Version11_222_3333"), res) + self.assertEqual(vmmp.toList_majorMinorPatch("Version_11_222_3333"), res) + + self.assertEqual(vmmp.toList_majorMinorPatch("11"), [11, 0, 0]) + self.assertEqual(vmmp.toList_majorMinorPatch("11.0"), [11, 0, 0]) + self.assertEqual(vmmp.toList_majorMinorPatch("11.2"), [11, 2, 0]) + self.assertEqual(vmmp.toList_majorMinorPatch("\n1 . 2 \n"), [1, 2, 0]) + + with self.assertRaises(Exception): + vmmp.toList_majorMinorPatch("") + with self.assertRaises(Exception): + vmmp.toList_majorMinorPatch("11.") + with self.assertRaises(Exception): + vmmp.toList_majorMinorPatch("11.2.") + with self.assertRaises(Exception): + vmmp.toList_majorMinorPatch("11.2.3.") + with self.assertRaises(Exception): + vmmp.toList_majorMinorPatch(".11") + with self.assertRaises(Exception): + vmmp.toList_majorMinorPatch("1_2_3_4") + with self.assertRaises(Exception): + vmmp.toList_majorMinorPatch("_1_2_3_") + with self.assertRaises(Exception): + vmmp.toList_majorMinorPatch(" \n 11...22.333-rc2\n") + with self.assertRaises(Exception): + vmmp.toList_majorMinorPatch(" \n 11...22.333-rc2\n") + with self.assertRaises(Exception): + vmmp.toList_majorMinorPatch(" \n 11...22.333-rc2\n") + + def test_040(self): + MMP = vmmp.MinorMajorPatch + v = [1, 2, 3] + self.assertEqual(MMP(v).__str__(), "1.2.3") + self.assertEqual(MMP(v).__str__(sep="_"), "1_2_3") + self.assertEqual(str(MMP(v)), "1.2.3") + + self.assertEqual(MMP(v).__repr__(), "version_1_2_3") + self.assertEqual(MMP(v).__repr__(sep="."), "version_1.2.3") + + self.assertEqual(MMP(v).strSalome(), "1_2_3") + self.assertEqual(MMP(v).strClassic(), "1.2.3") + + self.assertEqual(MMP([" 123 \n", 2, 10]).strClassic(), "123.2.10") + self.assertEqual(MMP([" 123 \n", 2, 10]).strSalome(), "123_2_10") + + with self.assertRaises(Exception): + MMP([-5, 2, 10]) + with self.assertRaises(Exception): + MMP([5, -2, 10]) + with self.assertRaises(Exception): + MMP([5, 2, -10]) + with self.assertRaises(Exception): + MMP(["-123", 2, 10]) + + def test_050(self): + MMP = vmmp.MinorMajorPatch + v000 = MMP("0.0.0") + v010 = MMP("0.1.0") + v100 = MMP("1.0.0") + v101 = MMP("1.0.1") + + va = v000 + vb = MMP("0.0.0") + self.assertTrue(va == vb) + self.assertTrue(va >= vb) + self.assertTrue(va <= vb) + self.assertFalse(va != vb) + self.assertFalse(va > vb) + self.assertFalse(va < vb) + + va = v000 + vb = v010 + self.assertFalse(va == vb) + self.assertFalse(va >= vb) + self.assertTrue(va <= vb) + self.assertTrue(va != vb) + self.assertFalse(va > vb) + self.assertTrue(va < vb) + + va = v101 + vb = v100 + self.assertFalse(va == vb) + self.assertTrue(va >= vb) + self.assertFalse(va <= vb) + self.assertTrue(va != vb) + self.assertTrue(va > vb) + self.assertFalse(va < vb) + + def test_060(self): + MMP = vmmp.MinorMajorPatch + v0 = MMP("0") + v1 = MMP("1") + v2 = MMP("2") + v123 = MMP("1.2.3") + v456 = MMP("4.5.6") + + tests = """\ +toto_from_1_to_2 + _from_1.0.0_to_2.0.0 +_from_1_0. 0_to_ 2.0_0 +_from_V1.0.0_to_2.0.0 +_from_version_1.0.0_to_2.0.0 +version_1.0.0_to_2.0.0 +VERSION_1.0.0_to_2.0.0""".split( + "\n" + ) + + for a in tests: + # print("test '%s'" % a) + r1, r2 = vmmp.getRange_majorMinorPatch(a) + self.assertEqual(r1, v1) + self.assertEqual(r2, v2) + + a = "toto_to_2" + r1, r2 = vmmp.getRange_majorMinorPatch(a) + self.assertEqual(r1, v0) + self.assertEqual(r2, v2) + + a = "toto_to_Version2" + r1, r2 = vmmp.getRange_majorMinorPatch(a) + self.assertEqual(r1, v0) + self.assertEqual(r2, v2) + + a = "toto_from_1.2.3_to_Version4_5_6" + r1, r2 = vmmp.getRange_majorMinorPatch(a) + self.assertEqual(r1, v123) + self.assertEqual(r2, v456) + + a = "toto_from_1.2.3_to_Version1_2_3" + r1, r2 = vmmp.getRange_majorMinorPatch(a) + self.assertEqual(r1, v123) + self.assertEqual(r2, v123) + + # _from_ without _to_ does not matter + tests = """\ + +toto +from +to +_from_ +toto_from_2""".split( + "\n" + ) + + for a in tests: + rx = vmmp.getRange_majorMinorPatch(a, verbose=False) + self.assertEqual(rx, None) + + # _to_ without _from_ does not matter, as implicit _from_ '0.0.0' + # empty _to_ raise error + with self.assertRaises(Exception): + vmmp.getRange_majorMinorPatch("_to_") + with self.assertRaises(Exception): + vmmp.getRange_majorMinorPatch("_from_to_") + with self.assertRaises(Exception): + vmmp.getRange_majorMinorPatch("_from__to_") + with self.assertRaises(Exception): + vmmp.getRange_majorMinorPatch("toto_from__to_") + with self.assertRaises(Exception): + vmmp.getRange_majorMinorPatch("toto_from_123_to_") + with self.assertRaises(Exception): + vmmp.getRange_majorMinorPatch("version_123_to_") + with self.assertRaises(Exception): + vmmp.getRange_majorMinorPatch("version_to_") + + # min > max does matter + with self.assertRaises(Exception): + vmmp.getRange_majorMinorPatch("_from_3_to_2") + with self.assertRaises(Exception): + vmmp.getRange_majorMinorPatch("_from_3.2.5_to_V2_1_1") + + +if __name__ == "__main__": + unittest.main()