From: Christian Van Wambeke Date: Tue, 23 Jan 2018 09:23:25 +0000 (+0100) Subject: first src/test/satHelpTest.py ok X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=7798954d7ce7ac727dd4bc91ddce50bbb1d426cd;p=tools%2Fsat.git first src/test/satHelpTest.py ok --- diff --git a/.spyderproject b/.spyderproject new file mode 100644 index 0000000..cfa47ec Binary files /dev/null and b/.spyderproject differ diff --git a/commands/run.py b/commands/run.py index 8947485..f671344 100644 --- a/commands/run.py +++ b/commands/run.py @@ -21,6 +21,7 @@ import subprocess import src +parser = src.options.Options() # no options yet def description(): '''method that is called when salomeTools is called with --help option. @@ -28,8 +29,11 @@ def description(): :return: The text to display for the run command description. :rtype: str ''' - return _("This command runs the application launcher" - " with the given arguments.\n\nexample:\nsat run SALOME-master") + return _("""\ +This command runs the application launcher with the given arguments. + +example: +>> sat run SALOME-master""") def run(args, runner, logger): '''method that is called when salomeTools is called with run parameter. diff --git a/src/debug.py b/src/debug.py index 8ea680c..33dda56 100644 --- a/src/debug.py +++ b/src/debug.py @@ -26,10 +26,18 @@ import pprint as PP _debug = [False] #support push/pop for temporary active outputs +def indent(text, amount=2, ch=' '): + padding = amount * ch + return ''.join(padding + line for line in text.splitlines(True)) + def write(title, var="", force=None): """write sys.stderr a message if _debug[-1]==True or optionaly force=True""" - if _debug[-1] or force: - sys.stderr.write("\n#### DEBUG: %s:\n%s\n" % (title, PP.pformat(var))) + fmt = "\n#### DEBUG: %s:\n%s\n" + if _debug[-1] or force: + if type(var) is not str: + sys.stderr.write(fmt % (title, indent(PP.pformat(var)))) + else: + sys.stderr.write(fmt % (title, indent(var))) return def push_debug(aBool): @@ -41,5 +49,5 @@ def pop_debug(): if len(_debug) > 1: return _debug.pop() else: - sys.stderr.write("\n#### DEBUG: ERROR: too much pop_debug()") + sys.stderr.write("\nERROR: pop_debug: too much pop.") return None diff --git a/src/i18n/i18nTest.py b/src/i18n/i18nTest.py new file mode 100755 index 0000000..cf467da --- /dev/null +++ b/src/i18n/i18nTest.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +#-*- coding:utf-8 -*- + +# %% LICENSE_SALOME_CEA_BEGIN +# Copyright (C) 2008-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, or (at your option) any later version. +# +# 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://www.salome-platform.org or email : webmaster.salome@opencascade.com +# %% LICENSE_END + +import os +import gettext +import unittest + +verbose = False + +class TestCase(unittest.TestCase): + + def test_001(self): + # first load resources for internationalization + gettext.install('salomeTools', os.path.realpath(os.path.dirname(__file__))) + + def test_005(self): + res = _("Harvey writes '%(1)s' for %(2)s.") % {"1": "hello", "2": "test_005"} + if verbose: print(res) + self.assertEqual(res, "pour test_005 Hervé écrit 'hello'.") + + def test_010(self): + res = _("Harvey writes '%(1)s' for %(2)s.") % {"1": _("hello"), "2": "test_010"} + if verbose: print(res) + self.assertEqual(res, "pour test_010 Hervé écrit 'bonjour'.") + + def test_020(self): + # keep Ooops inexisting in salomeTools.po as no translation + res = _("Harvey writes '%(1)s' for %(2)s.") % {"1": _("Ooops"), "2": "test_020"} + if verbose: print(res) + self.assertEqual(res, "pour test_020 Hervé écrit 'Ooops'.") + +if __name__ == '__main__': + verbose = False + unittest.main() + pass diff --git a/src/i18n/test_i18n.py b/src/i18n/test_i18n.py deleted file mode 100755 index cf467da..0000000 --- a/src/i18n/test_i18n.py +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env python -#-*- coding:utf-8 -*- - -# %% LICENSE_SALOME_CEA_BEGIN -# Copyright (C) 2008-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, or (at your option) any later version. -# -# 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://www.salome-platform.org or email : webmaster.salome@opencascade.com -# %% LICENSE_END - -import os -import gettext -import unittest - -verbose = False - -class TestCase(unittest.TestCase): - - def test_001(self): - # first load resources for internationalization - gettext.install('salomeTools', os.path.realpath(os.path.dirname(__file__))) - - def test_005(self): - res = _("Harvey writes '%(1)s' for %(2)s.") % {"1": "hello", "2": "test_005"} - if verbose: print(res) - self.assertEqual(res, "pour test_005 Hervé écrit 'hello'.") - - def test_010(self): - res = _("Harvey writes '%(1)s' for %(2)s.") % {"1": _("hello"), "2": "test_010"} - if verbose: print(res) - self.assertEqual(res, "pour test_010 Hervé écrit 'bonjour'.") - - def test_020(self): - # keep Ooops inexisting in salomeTools.po as no translation - res = _("Harvey writes '%(1)s' for %(2)s.") % {"1": _("Ooops"), "2": "test_020"} - if verbose: print(res) - self.assertEqual(res, "pour test_020 Hervé écrit 'Ooops'.") - -if __name__ == '__main__': - verbose = False - unittest.main() - pass diff --git a/src/logger.py b/src/logger.py index cebe0e1..db300ee 100644 --- a/src/logger.py +++ b/src/logger.py @@ -25,12 +25,14 @@ import re import tempfile import src -from . import printcolors -from . import xmlManager + +from src import printcolors +from src import xmlManager log_macro_command_file_expression = "^[0-9]{8}_+[0-9]{6}_+.*\.xml$" log_all_command_file_expression = "^.*[0-9]{8}_+[0-9]{6}_+.*\.xml$" + class Logger(object): '''Class to handle log mechanism. ''' diff --git a/src/options.py b/src/options.py index 22c51f9..cce23d3 100644 --- a/src/options.py +++ b/src/options.py @@ -113,26 +113,33 @@ class Options(object): option['result'] = default self.options.append(option) - def print_help(self): - '''Method that display all options stored in self.options and there help + def get_help(self): + '''Method that returns all options stored in self.options + as help message colored string - :return: Nothing. + :return: colored string :rtype: N\A ''' + msg = "" # Do nothing if there are no options if len(self.options) == 0: - return + return _("No available options.") - # for all options, print its values. + # for all options, gets its values. # "shortname" is an optional field of the options - print(src.printcolors.printcHeader(_("Available options are:"))) + msg += src.printcolors.printcHeader(_("Available options are:")) + "\n" for option in self.options: if 'shortName' in option and len(option['shortName']) > 0: - print(" -%(shortName)1s, --%(longName)s" - " (%(optionType)s)\n\t%(helpString)s\n" % option) + msg += "\n -%(shortName)1s, --%(longName)s" \ + " (%(optionType)s)\n\t%(helpString)s\n" % option else: - print(" --%(longName)s (%(optionType)s)\n\t%(helpString)s\n" - % option) + msg += "\n --%(longName)s (%(optionType)s)\n\t%(helpString)s\n" \ + % option + return msg + + def print_help(self): + print(self.get_help()) + def parse_args(self, argList=None): '''Method that instantiates the class OptResult @@ -148,7 +155,7 @@ class Options(object): argList = sys.argv[1:] DBG.write("parse_args", argList) - DBG.write("options", self.options) + # DBG.write("options", self.options) # format shortNameOption and longNameOption # to make right arguments to getopt.getopt function shortNameOption = "" @@ -208,7 +215,7 @@ class Options(object): option['result'] = None self.results = {"optlist": optlist, "optResult": optResult, "args": args, "argList": argList} - DBG.write("options and results", self) + DBG.write("results", self.results) return optResult, args def __repr__(self): diff --git a/src/salomeTools.py b/src/salomeTools.py index f5ca674..4036061 100755 --- a/src/salomeTools.py +++ b/src/salomeTools.py @@ -28,7 +28,7 @@ import imp import types import gettext import traceback - +import subprocess as SP ################################# # NOT MAIN allowed @@ -48,7 +48,6 @@ srcdir = os.path.join(rootdir, "src") cmdsdir = os.path.join(rootdir, "commands") # load resources for internationalization -# DBG.write("TODO", "fix xxsalomeTools to avoid french", True) gettext.install('salomeTools', os.path.join(srcdir, 'i18n')) # salomeTools imports @@ -61,6 +60,7 @@ import commands.config C_PRE_HOOK = "pre" C_POST_HOOK = "post" +_LANG = os.environ["LANG"] # original locale def find_command_list(dirPath): '''Parse files in dirPath that end with .py : it gives commands list @@ -86,6 +86,33 @@ def getCommandsList(): """ return lCommand +def launchSat(command): + """launch sat as subprocess popen + command as string ('sat --help' for example) + used for unittest, or else... + returns tuple (stdout, stderr) + """ + if "sat" not in command.split()[0]: + raise Exception(_("Not a valid command for launchSat: '%s'") % command) + env = dict(os.environ) + env["PATH"] = rootdir + ":" + env["PATH"] + res =SP.Popen(command, shell=True, env=env, stdout=SP.PIPE, stderr=SP.PIPE).communicate() + return res + +def setNotLocale(): + """force english at any moment""" + os.environ["LANG"] = '' + gettext.install('salomeTools', os.path.join(srcdir, 'i18n')) + DBG.write("setNotLocale", os.environ["LANG"]) + +def setLocale(): + """reset initial locale at any moment + 'fr' or else 'TODO' from initial environment var '$LANG' + """ + os.environ["LANG"] = _LANG + gettext.install('salomeTools', os.path.join(srcdir, 'i18n')) + DBG.write("setLocale", os.environ["LANG"]) + # Define all possible option for salomeTools command : sat parser = src.options.Options() parser.add_option('h', 'help', 'boolean', 'help', @@ -102,24 +129,28 @@ parser.add_option('t', 'all_in_terminal', 'boolean', "all_in_terminal", _("all traces in the terminal (for example compilation logs).")) parser.add_option('l', 'logs_paths_in_file', 'string', "logs_paths_in_file", _("put the command result and paths to log files.")) - + class Sat(object): '''The main class that stores all the commands of salomeTools ''' def __init__(self, opt='', datadir=None): '''Initialization - :param opt str: The sat options + :param opt str or list: The sat options :param: datadir str : the directory that contain all the external data (like software pyconf and software scripts) ''' # Read the salomeTools prefixes options before the 'commands' tag # sat - # (the list of possible options is at the beginning of this file) + # (the list of possible options is at the beginning of this file) try: - options, args = parser.parse_args(opt) - DBG.write("Sat args", args) - DBG.write("Sat options", options) + if type(opt) is not list: # as string 'sat --help' for example' + opts = opt.split() + else: + opts = opt + options, args = parser.parse_args(opts) + # DBG.write("Sat args", args) + # DBG.write("Sat options", options) except Exception as exc: write_exception(exc) sys.exit(src.KOSYS) @@ -133,6 +164,8 @@ class Sat(object): # set the commands by calling the dedicated function self._setCommands(cmdsdir) + ''' + # done with execute_command, to avoid sys.exit # if the help option has been called, print help and exit if options.help: try: @@ -142,6 +175,7 @@ class Sat(object): write_exception(exc) DBG.write("args", args, True) sys.exit(src.KOSYS) + ''' def __getattr__(self, name): '''overwrite of __getattr__ function in order to display @@ -152,7 +186,7 @@ class Sat(object): if name in self.__dict__: return self.__dict__[name] else: - raise AttributeError(name + _(" is not a valid command")) + raise AttributeError("'%s'" % name + _(" is not a valid command")) def execute_command(self, opt=None): """select first argument as a command in directory 'commands', and launch on arguments @@ -164,9 +198,15 @@ class Sat(object): else: args = self.arguments + # print general help and returns if len(args) == 0: print_help() return src.OKSYS + + # if the help option has been called, print command help and returns + if self.options.help: + self.print_help(self.arguments) + return src.OKSYS # the command called command = args[0] @@ -375,7 +415,7 @@ class Sat(object): res = 1 # print the log file path if - # the maximum verbose mode is invoked + # the maximum verbose mode is invoked if not micro_command: logger_command.write("\nPath to the xml log file:\n", 5) logger_command.write("%s\n\n" % \ @@ -466,6 +506,7 @@ class Sat(object): if len(opt)==0: print_help() return + # get command name command = opt[0] # read the configuration from all the pyconf files @@ -476,20 +517,35 @@ class Sat(object): if not hasattr(self, command): raise src.SatException(_("Command '%s' does not exist") % command) - # Print salomeTools version - print_version() - # load the module module = self.get_module(command) - + + msg = self.get_module_help(module) + + if isStdoutPipe(): + # clean color if the terminal is redirected by user + # ex: sat compile appli > log.txt + msg = src.printcolors.cleancolor(msg) + print(msg) + return + + def get_module_help(self, module): + """get help for a command + as 'sat --help config' for example + """ + # get salomeTools version + msg = get_version() + "\n\n" + # print the description of the command that is done in the command file - if hasattr( module, "description" ) : - print(src.printcolors.printcHeader( _("Description:") )) - print(module.description() + '\n') + if hasattr( module, "description" ): + msg += src.printcolors.printcHeader( _("Description:") ) + "\n" + msg += module.description() + "\n\n" # print the description of the command options if hasattr( module, "parser" ) : - module.parser.print_help() + msg += module.parser.get_help() + "\n" + return msg + def get_module(self, module): '''Loads a command. Function called only by print_help @@ -504,6 +560,7 @@ class Sat(object): (file_, pathname, description) = imp.find_module(module, [cmdsdir]) module = imp.load_module(module, file_, pathname, description) return module + def get_text_from_options(options): text_options = "" @@ -518,42 +575,56 @@ def get_text_from_options(options): option_contain = "" text_options+= "--%s %s " % (attr, option_contain) return text_options - - -def print_version(): - '''prints salomeTools version (in src/internal_config/salomeTools.pyconf) - ''' - # read the config + + +def isStdoutPipe(): + """check if the terminal is redirected by user (elsewhere a tty) + example: + >> sat compile appli > log.txt + """ + return not ('isatty' in dir(sys.stdout) and sys.stdout.isatty()) + +def get_version(): + """get version colored string + """ cfgManager = commands.config.ConfigManager() cfg = cfgManager.get_config() # print the key corresponding to salomeTools version - print(src.printcolors.printcHeader( _("Version: ") ) + - cfg.INTERNAL.sat_version + '\n') - - -def print_help(): - '''prints salomeTools general help - - :param options str: the options - ''' - print_version() - - print(src.printcolors.printcHeader( _("Usage: ") ) + - "sat [sat_options] [product] [command_options]\n") - - parser.print_help() - - # display all the available commands. - print(src.printcolors.printcHeader(_("Available commands are:\n"))) + msg = src.printcolors.printcHeader( _("Version: ") ) + \ + cfg.INTERNAL.sat_version + return msg + +def get_help(): + """get general help colored string + """ + # read the config + msg = get_version() + "\n\n" + msg += src.printcolors.printcHeader(_("Usage: ")) + \ + "sat [sat_options] [product] [command_options]\n\n" + msg += parser.get_help() + "\n" + msg += src.printcolors.printcHeader(_("Available commands are:")) + "\n\n" for command in lCommand: - print(" - %s" % (command)) - + msg += " - %s\n" % (command) + msg += "\n" # Explain how to get the help for a specific command - print(src.printcolors.printcHeader(_("\nGetting the help for a specific" - " command: ")) + "sat --help \n") + msg += src.printcolors.printcHeader( + _("Getting the help for a specific command: ")) + \ + "sat --help \n" + return msg + +def print_help(): + """prints salomeTools general help + """ + msg = get_help() + if isStdoutPipe(): + # clean color if the terminal is redirected by user + # ex: sat compile appli > log.txt + msg = src.printcolors.cleancolor(msg) + print(msg) + return def write_exception(exc): - '''write exception in case of error in a command + '''write in stderr exception in case of error in a command :param exc exception: the exception to print ''' diff --git a/src/test/TOOLS.py b/src/test/TOOLS.py deleted file mode 100644 index 77e0a4e..0000000 --- a/src/test/TOOLS.py +++ /dev/null @@ -1,155 +0,0 @@ -#!/usr/bin/env python -#-*- coding:utf-8 -*- - -# ToolBox for test framework - -import os -import string -import subprocess - -""" -Exception class for test errors. -""" -class SatTestError(Exception): - def __init__(self, value): - self.value = value - def __str__(self): - return repr(self.value) - -class SatNotApplicableError(Exception): - def __init__(self, value): - self.value = value - def __str__(self): - return repr(self.value) - -def ERROR(message): - print message - raise SatTestError(message) - -def NOT_APPLICABLE(message): - print message - raise SatNotApplicableError(message) - -## -# Compares 2 numbers with tolerance tol. -def compFloat(f1, f2, tol=10e-10): - diff = abs(f1 - f2) - print "|f1-f2| = %s (tol=%s)" % (str(diff), str(tol)) - if diff <= tol: - comp = "OK" - else: - comp = "KO" - return comp - -## -# Compares 2 files. -def compFiles(f1, f2, tol=0): - assert os.path.exists(f1), "compFiles: file not found: %s" % f1 - assert os.path.exists(f2), "compFiles: file not found: %s" % f2 - diffLine = os.popen("diff -y --suppress-common-lines %s %s" % (f1, f2)) - diff = len(string.split(diffLine.read(), "\n")) - diffLine.close() - print "nb of diff lines = %s (tol=%s)" % (str(diff), str(tol)) - if diff <= tol: - comp = "OK" - else: - comp = "KO" - return comp - -## -# Uses mdump to dump a med file. -def mdump_med(med_file, dump_file, options): - assert isinstance(options, list), "Bad options for mdump: %s" % options - assert len(options) == 3, "Bad options for mdump: %s" % options - cmd = "mdump %s %s" % (med_file, " ".join(options)) - #print cmd - - df = open(dump_file, "w") - pdump = subprocess.Popen(cmd, shell=True, stdout=df) - st = pdump.wait() - df.close() - - return st - -## -# Compares 2 med files by using mdump. -def compMED(file1, file2, tol=0, diff_flags=""): - assert os.path.exists(file1), "compMED: file not found: %s" % file1 - assert os.path.exists(file2), "compMED: file not found: %s" % file2 - - print - print ">>>> compMED" - print " file1:", file1 - print " file2:", file2 - - def do_dump(med): - dump = os.path.join(os.environ['TT_TMP_RESULT'], os.path.basename(med) + ".mdump") - st = mdump_med(med, dump, ["1", "NODALE", "FULL_INTERLACE"]) - if st != 0 or not os.path.exists(dump): - raise Exception("Error mpdump %s" % med) - - # replace file name with "filename" - lines = open(dump, "r").readlines() - dumpfile = open(dump, "w") - for line in lines: - try: - line.index('Nom universel du maillage') - continue - except: - dumpfile.write(line.replace(med, 'filename')) - return dump - - dump1 = do_dump(file1) - dump2 = do_dump(file2) - - diff_cmd = "diff %s %s %s" % (diff_flags, dump1, dump2) - print " >" + diff_cmd - pdiff = subprocess.Popen(diff_cmd, shell=True, stdout=subprocess.PIPE) - status = pdiff.wait() - print " Diff =", status - if status != 0: - print pdiff.stdout.read() - - print "<<<< compMED" - print - - return status - - -class TOOLS_class: - def __init__(self, base_ressources_dir, tmp_dir, test_ressources_dir): - self.base_ressources_dir = base_ressources_dir - self.tmp_dir = tmp_dir - self.test_ressources_dir = test_ressources_dir - pass - - def init(self): - self.inFiles = [] - - def ERROR(self, message): - # Simulation d'un plantage - ERROR(message) - - def compMED(self, file1, file2, tol=0): - return compMED(file1, file2, tol, "--ignore-all-space") - - def compFloat(self, f1, f2, tol=10e-10): - return compFloat(f1, f2, tol) - - def compFiles(self, f1, f2, tol=0): - return compFiles(f1, f2, tol) - - def get_inFile(self, name=None): - if not name: - return self.base_ressources_dir - self.inFiles.append(name) - return os.path.join(self.base_ressources_dir, name) - - def get_outFile(self, name=None): - if not name: - return self.tmp_dir - return os.path.join(self.tmp_dir, name) - - def writeInFiles(self, pylog): - pylog.write('inFiles=%s\n' % str(self.inFiles)) - diff --git a/src/test/satHelpTest.py b/src/test/satHelpTest.py new file mode 100755 index 0000000..6eea8bd --- /dev/null +++ b/src/test/satHelpTest.py @@ -0,0 +1,82 @@ +#!/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 os +import sys +import unittest + +import src.salomeTools as SAT +import src +import src.debug as DBG # Easy print stderr (for DEBUG only) + +class TestCase(unittest.TestCase): + "Test the sat --help commands""" + + def test_000(self): + # one shot setUp() for this TestCase + # DBG.push_debug(True) + SAT.setNotLocale() # test english + + def test_010(self): + cmd = "sat --help" + stdout, stderr = SAT.launchSat(cmd) + self.assertEqual(stderr, "") + self.assertTrue(" - config" in stdout) + + def test_011(self): + cmd = "--help" + s = SAT.Sat(cmd) + exitCode = s.execute_command() + self.assertEqual(src.okToStr(exitCode), "OK") + + def test_030(self): + cmd = "sat --help config" + stdout, stderr = SAT.launchSat(cmd) + self.assertEqual(stderr, "") + self.assertTrue("--value" in stdout) + + def test_031(self): + cmd = "--help config" + s = SAT.Sat(cmd) + exitCode = s.execute_command() + self.assertEqual(src.okToStr(exitCode), "OK") + + def test_012(self): + cmd = "config -l" + s = SAT.Sat(cmd) + exitCode = s.execute_command() + self.assertEqual(src.okToStr(exitCode), "OK") + + def test_040(self): + cmds = SAT.getCommandsList() + for c in cmds: + cmd = "sat --help %s" % c + stdout, stderr = SAT.launchSat(cmd) + self.assertEqual(stderr, "") + # DBG.write("stdout '%s'" % cmd, stdout) + self.assertTrue("vailable options" in stdout) + + def test_999(self): + # one shot tearDown() for this TestCase + SAT.setLocale() # end test english + # DBG.pop_debug() + +if __name__ == '__main__': + unittest.main(exit=False) + pass diff --git a/src/test/scriptTemplate.py b/src/test/scriptTemplate.py deleted file mode 100644 index f3f7de4..0000000 --- a/src/test/scriptTemplate.py +++ /dev/null @@ -1,98 +0,0 @@ -#!/usr/bin/env python -#-*- coding:utf-8 -*- - -import os, sys, traceback -import os.path -import time as THEBIGTIME - -# set path -toolsWay = r'${toolsWay}' -resourcesWay = r'${resourcesWay}' -outWay = r'${sessionDir}' -tmpDir = r'${tmpDir}' - -listTest = ${listTest} -ignore = ${ignore} - -sys.path.append(toolsWay) -from TOOLS import TOOLS_class -my_tools = TOOLS_class(resourcesWay, tmpDir, toolsWay) - -from TOOLS import SatNotApplicableError - -# on set les variables d'environement -os.environ['TT_BASE_RESSOURCES'] = resourcesWay -sys.path.append(resourcesWay) - -exec_result = open(r'${resultFile}', 'w') -exec_result.write('Open\n') - -__stdout__ = sys.stdout -__stderr__ = sys.stderr - -for test in listTest: - pylog = open(os.path.join(outWay, test[:-3] + ".result.py"), "w") - testout = open(os.path.join(outWay, test[:-3] + ".out.py"), "w") - my_tools.init() - sys.stdout = testout - sys.stderr = testout - - pylog.write('#-*- coding:utf-8 -*-\n') - exec_result.write("Run %s " % test) - exec_result.flush() - - try: - timeStart = THEBIGTIME.time() - execfile(os.path.join(outWay, test), globals(), locals()) - timeTest = THEBIGTIME.time() - timeStart - except SatNotApplicableError, ex: - status = "NA" - reason = str(ex) - exec_result.write("NA\n") - timeTest = THEBIGTIME.time() - timeStart - pylog.write('status = "NA"\n') - pylog.write('time = "' + timeTest.__str__() + '"\n') - pylog.write('callback = "%s"\n' % reason) - except Exception, ex: - status = "KO" - reason = "" - if ignore.has_key(test): - status = "KF" - reason = "Known Failure = %s\n\n" % ignore[test] - exec_result.write("%s\n" % status) - timeTest = THEBIGTIME.time() - timeStart - pylog.write('status = "%s" \n' % status) - pylog.write('time = "' + timeTest.__str__() + '"\n') - pylog.write('callback="""' + reason) - exc_type, exc_value, exc_traceback = sys.exc_info() - traceback.print_exception(exc_type, - exc_value, - exc_traceback, - None, - file=pylog) - pylog.write('"""\n') - else: - exec_result.write("OK\n") - pylog.write('status = "OK"\n') - pylog.write('time = "' + timeTest.__str__() + '"\n') - - testout.close() - sys.stdout = __stdout__ - sys.stderr = __stderr__ - my_tools.writeInFiles(pylog) - pylog.close() - -exec_result.write('Close\n') -exec_result.close() - -if 'PY' not in '${sessionName}': - import salome_utils - #killScript = os.path.join(os.environ['KERNEL_ROOT_DIR'], - # 'bin', - # 'salome', - # 'killSalome.py') - #cmd = '{python} {killScript} {port}'.format(python=os.environ['PYTHONBIN'], - # killScript=killScript, - # port=salome_utils.getPortNumber()) - cmd = 'killSalome.py {port}'.format( port=salome_utils.getPortNumber()) - os.system(cmd) diff --git a/src/test_module.py b/src/test_module.py index ca90232..57a15fd 100755 --- a/src/test_module.py +++ b/src/test_module.py @@ -377,15 +377,15 @@ class Test: def generate_script(self, listTest, script_path, ignoreList): # open template file template_file = open(os.path.join(self.config.VARS.srcDir, - "test", - "scriptTemplate.py"), 'r') + "test_module", + "script_test_module.pyTemplate"), 'r') template = string.Template(template_file.read()) # create substitution dictionary d = dict() d['resourcesWay'] = os.path.join(self.currentDir, 'RESSOURCES') d['tmpDir'] = os.path.join(self.tmp_working_dir, 'WORK') - d['toolsWay'] = os.path.join(self.config.VARS.srcDir, "test") + d['toolsWay'] = os.path.join(self.config.VARS.srcDir, "test_module") d['sessionDir'] = os.path.join(self.currentDir, self.currentgrid, self.currentsession) diff --git a/src/test_module/script_test_module.pyTemplate b/src/test_module/script_test_module.pyTemplate new file mode 100644 index 0000000..b653544 --- /dev/null +++ b/src/test_module/script_test_module.pyTemplate @@ -0,0 +1,107 @@ +#!/usr/bin/env python +#-*- coding:utf-8 -*- + +# +# WARNING: this script is automaticaly generated from +# salomeTools-5/src/test_module/script_test_module.pyTemplate +# by salomeTools-5/src/test_module.py +# it could be overriden at any time +# +# TODO: change this fucking code +# + +import os, sys, traceback +import os.path +import time as THEBIGTIME + +# set path +toolsWay = r'${toolsWay}' +resourcesWay = r'${resourcesWay}' +outWay = r'${sessionDir}' +tmpDir = r'${tmpDir}' + +listTest = ${listTest} +ignore = ${ignore} + +sys.path.append(toolsWay) +from utils_test_module import UtilsTest +my_tools = UtilsTest(resourcesWay, tmpDir, toolsWay) + +from utils_test_module import SatNotApplicableError + +# on set les variables d'environement +os.environ['TT_BASE_RESSOURCES'] = resourcesWay +sys.path.append(resourcesWay) + +exec_result = open(r'${resultFile}', 'w') +exec_result.write('Open\n') + +__stdout__ = sys.stdout +__stderr__ = sys.stderr + +for test in listTest: + pylog = open(os.path.join(outWay, test[:-3] + ".result.py"), "w") + testout = open(os.path.join(outWay, test[:-3] + ".out.py"), "w") + my_tools.init() + sys.stdout = testout + sys.stderr = testout + + pylog.write('#-*- coding:utf-8 -*-\n') + exec_result.write("Run %s " % test) + exec_result.flush() + + try: + timeStart = THEBIGTIME.time() + execfile(os.path.join(outWay, test), globals(), locals()) + timeTest = THEBIGTIME.time() - timeStart + except SatNotApplicableError, ex: + status = "NA" + reason = str(ex) + exec_result.write("NA\n") + timeTest = THEBIGTIME.time() - timeStart + pylog.write('status = "NA"\n') + pylog.write('time = "' + timeTest.__str__() + '"\n') + pylog.write('callback = "%s"\n' % reason) + except Exception, ex: + status = "KO" + reason = "" + if ignore.has_key(test): + status = "KF" + reason = "Known Failure = %s\n\n" % ignore[test] + exec_result.write("%s\n" % status) + timeTest = THEBIGTIME.time() - timeStart + pylog.write('status = "%s" \n' % status) + pylog.write('time = "' + timeTest.__str__() + '"\n') + pylog.write('callback="""' + reason) + exc_type, exc_value, exc_traceback = sys.exc_info() + traceback.print_exception(exc_type, + exc_value, + exc_traceback, + None, + file=pylog) + pylog.write('"""\n') + else: + exec_result.write("OK\n") + pylog.write('status = "OK"\n') + pylog.write('time = "' + timeTest.__str__() + '"\n') + + testout.close() + sys.stdout = __stdout__ + sys.stderr = __stderr__ + my_tools.writeInFiles(pylog) + pylog.close() + +exec_result.write('Close\n') +exec_result.close() + +if 'PY' not in '${sessionName}': + import salome_utils + #killScript = os.path.join(os.environ['KERNEL_ROOT_DIR'], + # 'bin', + # 'salome', + # 'killSalome.py') + #cmd = '{python} {killScript} {port}'.format(python=os.environ['PYTHONBIN'], + # killScript=killScript, + # port=salome_utils.getPortNumber()) + cmd = 'killSalome.py {port}'.format( port=salome_utils.getPortNumber()) + os.system(cmd) diff --git a/src/test_module/utils_test_module.py b/src/test_module/utils_test_module.py new file mode 100644 index 0000000..2bf5fbb --- /dev/null +++ b/src/test_module/utils_test_module.py @@ -0,0 +1,155 @@ +#!/usr/bin/env python +#-*- coding:utf-8 -*- + +import os +import string +import subprocess + +""" +ToolBox for test framework salome +Exception class for test errors. +""" + +class SatTestError(Exception): + def __init__(self, value): + self.value = value + def __str__(self): + return repr(self.value) + +class SatNotApplicableError(Exception): + def __init__(self, value): + self.value = value + def __str__(self): + return repr(self.value) + +def ERROR(message): + print message + raise SatTestError(message) + +def NOT_APPLICABLE(message): + print message + raise SatNotApplicableError(message) + +## +# Compares 2 numbers with tolerance tol. +def compFloat(f1, f2, tol=10e-10): + diff = abs(f1 - f2) + print "|f1-f2| = %s (tol=%s)" % (str(diff), str(tol)) + if diff <= tol: + comp = "OK" + else: + comp = "KO" + return comp + +## +# Compares 2 files. +def compFiles(f1, f2, tol=0): + assert os.path.exists(f1), "compFiles: file not found: %s" % f1 + assert os.path.exists(f2), "compFiles: file not found: %s" % f2 + diffLine = os.popen("diff -y --suppress-common-lines %s %s" % (f1, f2)) + diff = len(string.split(diffLine.read(), "\n")) + diffLine.close() + print "nb of diff lines = %s (tol=%s)" % (str(diff), str(tol)) + if diff <= tol: + comp = "OK" + else: + comp = "KO" + return comp + +## +# Uses mdump to dump a med file. +def mdump_med(med_file, dump_file, options): + assert isinstance(options, list), "Bad options for mdump: %s" % options + assert len(options) == 3, "Bad options for mdump: %s" % options + cmd = "mdump %s %s" % (med_file, " ".join(options)) + #print cmd + + df = open(dump_file, "w") + pdump = subprocess.Popen(cmd, shell=True, stdout=df) + st = pdump.wait() + df.close() + + return st + +## +# Compares 2 med files by using mdump. +def compMED(file1, file2, tol=0, diff_flags=""): + assert os.path.exists(file1), "compMED: file not found: %s" % file1 + assert os.path.exists(file2), "compMED: file not found: %s" % file2 + + print + print ">>>> compMED" + print " file1:", file1 + print " file2:", file2 + + def do_dump(med): + dump = os.path.join(os.environ['TT_TMP_RESULT'], os.path.basename(med) + ".mdump") + st = mdump_med(med, dump, ["1", "NODALE", "FULL_INTERLACE"]) + if st != 0 or not os.path.exists(dump): + raise Exception("Error mpdump %s" % med) + + # replace file name with "filename" + lines = open(dump, "r").readlines() + dumpfile = open(dump, "w") + for line in lines: + try: + line.index('Nom universel du maillage') + continue + except: + dumpfile.write(line.replace(med, 'filename')) + return dump + + dump1 = do_dump(file1) + dump2 = do_dump(file2) + + diff_cmd = "diff %s %s %s" % (diff_flags, dump1, dump2) + print " >" + diff_cmd + pdiff = subprocess.Popen(diff_cmd, shell=True, stdout=subprocess.PIPE) + status = pdiff.wait() + print " Diff =", status + if status != 0: + print pdiff.stdout.read() + + print "<<<< compMED" + print + + return status + + +class UtilsTest: + def __init__(self, base_ressources_dir, tmp_dir, test_ressources_dir): + self.base_ressources_dir = base_ressources_dir + self.tmp_dir = tmp_dir + self.test_ressources_dir = test_ressources_dir + pass + + def init(self): + self.inFiles = [] + + def ERROR(self, message): + # Simulation d'un plantage + ERROR(message) + + def compMED(self, file1, file2, tol=0): + return compMED(file1, file2, tol, "--ignore-all-space") + + def compFloat(self, f1, f2, tol=10e-10): + return compFloat(f1, f2, tol) + + def compFiles(self, f1, f2, tol=0): + return compFiles(f1, f2, tol) + + def get_inFile(self, name=None): + if not name: + return self.base_ressources_dir + self.inFiles.append(name) + return os.path.join(self.base_ressources_dir, name) + + def get_outFile(self, name=None): + if not name: + return self.tmp_dir + return os.path.join(self.tmp_dir, name) + + def writeInFiles(self, pylog): + pylog.write('inFiles=%s\n' % str(self.inFiles)) +