From 80625161e9a9bd9196eef57eac69608512ad5062 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Fri, 11 Mar 2016 23:48:36 +0100 Subject: [PATCH] Add cscripts Python module test. --- .gitignore | 1 + configure.ac | 2 ++ cscripts/Makefile.am | 17 +++++++++++++++++ cscripts/__init__.py.in | 6 +++--- cscripts/tests/__init__.py | 0 test/Makefile.am | 10 ++++++---- test/test_python.py | 7 +------ test/test_python_cscripts.py | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/test_python_cscripts.sh.in | 25 +++++++++++++++++++++++++ 9 files changed, 159 insertions(+), 13 deletions(-) create mode 100644 cscripts/tests/__init__.py create mode 100755 test/test_python_cscripts.py create mode 100755 test/test_python_cscripts.sh.in diff --git a/.gitignore b/.gitignore index d0f24c7..d30d653 100644 --- a/.gitignore +++ b/.gitignore @@ -80,6 +80,7 @@ test/model.reg test/obs.reg test/.coverage test/test_python.sh +test/test_python_cscripts.sh examples/*.fits examples/*.xml examples/*.dat diff --git a/configure.ac b/configure.ac index a3adf22..5410657 100644 --- a/configure.ac +++ b/configure.ac @@ -998,6 +998,7 @@ AC_CONFIG_FILES([Makefile test/Makefile test/test_cscripts.sh test/test_python.sh + test/test_python_cscripts.sh pyext/Makefile pyext/setup.py pyext/ctools/__init__.py @@ -1010,6 +1011,7 @@ AC_CONFIG_FILES([Makefile testconf.sh], [if test -f test/test_cscripts.sh; then chmod +x test/test_cscripts.sh; fi; if test -f test/test_python.sh; then chmod +x test/test_python.sh; fi; + if test -f test/test_python_cscripts.sh; then chmod +x test/test_python_cscripts.sh; fi; if test -f testconf.sh; then chmod +x testconf.sh; fi]) AC_OUTPUT diff --git a/cscripts/Makefile.am b/cscripts/Makefile.am index a025cd4..ccd898b 100644 --- a/cscripts/Makefile.am +++ b/cscripts/Makefile.am @@ -19,6 +19,17 @@ linkscripts = cspull cssens cstsdist cscaldb csresmap \ csobs2caldb \ csfindobs csiactcopy csiactdata csiactobs +# Test scripts to be distributed in $(pyexecdir)/cscripts/tests +testscripts = tests/__init__.py \ + ../test/test_python_cscripts.py \ + ../test/test_cscaldb.py + +# Test data to be distributed in $(pyexecdir)/cscripts/tests/data +testdata = + +# Test IRFs to be distributed in $(pyexecdir)/cscripts/tests/irf +testirf = + # Distribute scripts and parfiles EXTRA_DIST = $(binscripts) $(pythonscripts) binscriptsdir = $(bindir) @@ -27,6 +38,12 @@ pythonscriptsdir = $(pyexecdir)/cscripts pythonscripts_DATA = $(pythonscripts) pfilesdir = $(prefix)/syspfiles pfiles_DATA = $(parfiles) +testscriptsdir = $(pyexecdir)/cscripts/tests +testscripts_DATA = $(testscripts) +testdatadir = $(pyexecdir)/cscripts/tests/data +testdata_DATA = $(testdata) +testirfdir = $(pyexecdir)/cscripts/tests/irf +testirf_DATA = $(testirf) # Add hook rule to create symbolic links for cscripts install-data-hook: diff --git a/cscripts/__init__.py.in b/cscripts/__init__.py.in index 0516c9c..2a1064d 100644 --- a/cscripts/__init__.py.in +++ b/cscripts/__init__.py.in @@ -39,8 +39,7 @@ __all__ = [ "cstsdist", "cstsmapmerge", "csfindobs", - "obsutils", - "test", + "obsutils" ] if cur_version > req_version: __all__.extend(["csfindobs", @@ -80,7 +79,8 @@ def test(): """ Run cscripts tests. """ - print("No cscripts unit tests have been implemented so far.") + from cscripts.tests import test_python_cscripts + test_python_cscripts.test(installed=True) # Add version __version__ = '@PACKAGE_VERSION@' \ No newline at end of file diff --git a/cscripts/tests/__init__.py b/cscripts/tests/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/cscripts/tests/__init__.py diff --git a/test/Makefile.am b/test/Makefile.am index ca0c00a..ab2215a 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -6,9 +6,10 @@ MACOSX_DEPLOYMENT_TARGET=@CONFIGURE_MACOSX_DEPLOYMENT_TARGET@ # Optional python bindings if WITH_PYTHON - TEST_CSCRIPTS = test_cscripts.sh - TEST_PYTHON_SCRIPT = test_python.sh - TEST_PYTHON_ENV = PYTHONPATH=$(top_builddir):$(top_builddir)/pyext:$(top_builddir)/pyext/build/ctools/ctools:$(pythondir):$(pyexecdir):$(PYTHONPATH) + TEST_CSCRIPTS = test_cscripts.sh + TEST_PYTHON_CTOOLS = test_python.sh + TEST_PYTHON_CSCRIPTS = test_python_cscripts.sh + TEST_PYTHON_ENV = PYTHONPATH=$(top_builddir):$(top_builddir)/pyext:$(top_builddir)/pyext/build/ctools/ctools:$(pythondir):$(pyexecdir):$(PYTHONPATH) endif # Set test environment @@ -18,7 +19,8 @@ TESTS_ENVIRONMENT = @RUNSHARED@=$(top_builddir)/src/.libs$(TEST_ENV_DIR):$(LIBDI # Tests (these are the tests to be executed) TESTS = test_ctools.sh \ $(TEST_CSCRIPTS) \ - $(TEST_PYTHON_SCRIPT) + $(TEST_PYTHON_CTOOLS) \ + $(TEST_PYTHON_CSCRIPTS) # Add Valgrind rule valgrind: diff --git a/test/test_python.py b/test/test_python.py index ccb5b99..3d4aacd 100755 --- a/test/test_python.py +++ b/test/test_python.py @@ -2,7 +2,7 @@ # ========================================================================== # This scripts performs unit tests for the ctools package. # -# Copyright (C) 2012-2015 Juergen Knoedlseder +# Copyright (C) 2012-2016 Juergen Knoedlseder # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -37,7 +37,6 @@ import test_ctcubemask import test_ctbutterfly import test_ctulimit import test_cterror -import test_cscaldb import test_pipelines @@ -66,7 +65,6 @@ def test(installed=False): suite_ctbutterfly = test_ctbutterfly.Test() suite_ctulimit = test_ctulimit.Test() suite_cterror = test_cterror.Test() - suite_cscaldb = test_cscaldb.Test() suite_pipelines = test_pipelines.Test() # Setup unit tests @@ -84,7 +82,6 @@ def test(installed=False): suite_ctbutterfly.set() suite_ctulimit.set() suite_cterror.set() - suite_cscaldb.set() suite_pipelines.set() # Append tests to container @@ -102,7 +99,6 @@ def test(installed=False): suites.append(suite_ctbutterfly) suites.append(suite_ctulimit) suites.append(suite_cterror) - suites.append(suite_cscaldb) suites.append(suite_pipelines) # If we have an installed version then create a temporary @@ -133,7 +129,6 @@ def test(installed=False): # Copy over pfiles if not installed: os.system("cp -r ../src/*/*.par pfiles/") - os.system("cp -r ../cscripts/*.par pfiles/") else: os.system("cp -r %s/syspfiles/*.par pfiles/" % (os.environ['CTOOLS'])) diff --git a/test/test_python_cscripts.py b/test/test_python_cscripts.py new file mode 100755 index 0000000..9c57f1a --- /dev/null +++ b/test/test_python_cscripts.py @@ -0,0 +1,104 @@ +#! /usr/bin/env python +# ========================================================================== +# This scripts performs unit tests for cscripts used from Python. +# +# Copyright (C) 2016 Juergen Knoedlseder +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# ========================================================================== +import gammalib +import cscripts +import sys +import os +sys.path.append(os.path.dirname(os.path.abspath(__file__))) +import test_cscaldb + + +# ================== # +# Perform unit tests # +# ================== # +def test(installed=False): + """ + Perform unit testing for Python interface. + """ + # Allocate test suite container + suites = gammalib.GTestSuites("cscripts unit testing") + + # Allocate test suites and append them to the container + suite_cscaldb = test_cscaldb.Test() + + # Setup unit tests + suite_cscaldb.set() + + # Append tests to container + suites.append(suite_cscaldb) + + # If we have an installed version then create a temporary directory and + # copy over all information that is needed + if installed: + + # Create temporary working directory + import tempfile + path = tempfile.mkdtemp() + os.chdir(path) + + # Get test directory + import inspect + testdir = inspect.getfile(cscripts.tests) + dirname = os.path.dirname(testdir) + + # Copy over test data and irf + #os.system("cp -r %s %s" % (dirname+"/data", "data")) + #os.system("cp -r %s %s" % (dirname+"/irf", "irf")) + + # Set PFILES environment variable + try: + os.mkdir("pfiles") + except: + pass + os.environ['PFILES'] = "pfiles" + + # Copy over pfiles + if not installed: + os.system("cp -r ../cscripts/*.par pfiles/") + else: + os.system("cp -r %s/syspfiles/*.par pfiles/" % (os.environ['CTOOLS'])) + + # Run test suite + success = suites.run() + + # If we have a non-installed version then save test results + if not installed: + suites.save("reports/cscripts.xml") + + # Set return code + if success: + rc = 0 + else: + rc = 1 + + # Exit with return code + sys.exit(rc) + + +# ======================== # +# Main routine entry point # +# ======================== # +if __name__ == '__main__': + """ + Perform unit testing for cscripts used from Python. + """ + # Run tests + test() diff --git a/test/test_python_cscripts.sh.in b/test/test_python_cscripts.sh.in new file mode 100755 index 0000000..2d6f23c --- /dev/null +++ b/test/test_python_cscripts.sh.in @@ -0,0 +1,25 @@ +#! /bin/sh +# ========================================================================== +# This script tests the cscripts Python module +# +# Copyright (C) 2016 Juergen Knoedlseder +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# ========================================================================== + +# +# Run the Python test +# =================== +@PYTHON_COVERAGE@./test_python_cscripts.py -- libgit2 0.21.4