From 5a549e4dd3ea928afe658b4f1832cd538958f29c Mon Sep 17 00:00:00 2001 From: Remy Prechelt <prechelt@hawaii.edu> Date: Sat, 3 Oct 2020 09:14:14 -1000 Subject: [PATCH] Move 'read_hist' into 'corsika' and new CI. --- .gitlab-ci.yml | 22 +++++++++--- Python/.gitignore | 4 +++ Python/corsika/io/hist.py | 2 +- Python/tests/__init__.py | 56 +++++++++++++++++++++++++++++ Python/tests/io/__init__.py | 9 +++++ Python/tests/io/test_hist.py | 70 ++++++++++++++++++++++++++++++++++++ Python/tests/test_hist.py | 36 ------------------- 7 files changed, 158 insertions(+), 41 deletions(-) create mode 100644 Python/tests/__init__.py create mode 100644 Python/tests/io/__init__.py create mode 100644 Python/tests/io/test_hist.py delete mode 100644 Python/tests/test_hist.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index bf5ccd361..bc2df1b0c 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -621,22 +621,33 @@ sanity: script: - cd ${CI_PROJECT_DIR}/Python # change into the Python directory - pip install --user -e '.[test]' # install the package + test deps - - make all 2&>1 | tee python-test.log # this runs all of the Python tests + - python -m mypy corsika + - python -m isort --atomic corsika tests + - python -m black -t py37 corsika tests + - python -m flake8 corsika tests + - python -m pytest --cov=corsika tests - cd ${CI_PROJECT_DIR} # reset the directory coverage: '/^TOTAL\s*\d+\s*\d+\s*(.*\%)/' +<<<<<<< HEAD artifacts: when: always expire_in: 1 year paths: - ${CI_PROJECT_DIR}/Python/python-test.log allow_failure: true +======= +>>>>>>> 927acbbd... Move 'read_hist' into 'corsika' and new CI. -# we now configure the jobs for the three -# supported Python versions +# we now configure the jobs for the three supported Python versions. We run +# Python 3.7 for all commits but only run Python 3.6/3.8 before merging or if +# the MR is an explict Python MR with the 'Python' tag. python-3.6: extends: .python image: python:3.6 - + rules: + - if: '$CI_MERGE_REQUEST_LABELS =~ /Python/' + - if: '$CI_MERGE_REQUEST_LABELS =~ /Code review finished/' + python-3.7: extends: .python image: python:3.7 @@ -644,3 +655,6 @@ python-3.7: python-3.8: extends: .python image: python:3.8 + rules: + - if: '$CI_MERGE_REQUEST_LABELS =~ /Python/' + - if: '$CI_MERGE_REQUEST_LABELS =~ /Code review finished/' diff --git a/Python/.gitignore b/Python/.gitignore index d82fa7a96..7b9e44cc9 100644 --- a/Python/.gitignore +++ b/Python/.gitignore @@ -3,6 +3,10 @@ __pycache__/ *.py[cod] *$py.class +# ignore any generated output files +*.npz +*.dat + # C extensions *.so diff --git a/Python/corsika/io/hist.py b/Python/corsika/io/hist.py index e12dcfd1b..615db0fae 100644 --- a/Python/corsika/io/hist.py +++ b/Python/corsika/io/hist.py @@ -14,7 +14,7 @@ import numpy as np def read_hist(filename: str) -> bh.Histogram: """ - Read a histogram produced with CORSIKA8's `save_hist()` function. + Read a histogram produced with CORSIKA8's `SaveBoostHistogram()` function. Parameters ---------- diff --git a/Python/tests/__init__.py b/Python/tests/__init__.py new file mode 100644 index 000000000..c7d9b672b --- /dev/null +++ b/Python/tests/__init__.py @@ -0,0 +1,56 @@ +""" + Tests for `corsika` + + (c) Copyright 2018 CORSIKA Project, corsika-project@lists.kit.edu + + This software is distributed under the terms of the GNU General Public + Licence version 3 (GPL Version 3). See file LICENSE for a full version of + the license. +""" + +import os +import os.path as op + +__all__ = ["build_directory"] + + +def find_build_directory() -> str: + """ + Return the absolute path to the CORSIKA8 build directory. + + Returns + ------- + str + The absolute path to the build directory. + + Raises + ------ + RuntimeError + If the build directory cannot be found or it is empty. + """ + + # check if we are running on Gitlab + gitlab_build_dir = os.getenv("CI_BUILDS_DIR") + + # if we are running on Gitlab + if gitlab_build_dir is not None: + build_dir = op.abspath(gitlab_build_dir) + else: # otherwise, we are running locally + build_dir = op.abspath( + op.join( + op.dirname(__file__), + op.pardir, + op.pardir, + "build", + ) + ) + + # check that the build directory contains 'CMakeCache.txt' + if not op.exists(op.join(build_dir, "CMakeCache.txt")): + raise RuntimeError("Python tests cannot find C8 build directory.") + + return build_dir + + +# find the build_directory once +build_directory = find_build_directory() diff --git a/Python/tests/io/__init__.py b/Python/tests/io/__init__.py new file mode 100644 index 000000000..0d08be944 --- /dev/null +++ b/Python/tests/io/__init__.py @@ -0,0 +1,9 @@ +""" + Tests for `corsika.io` + + (c) Copyright 2018 CORSIKA Project, corsika-project@lists.kit.edu + + This software is distributed under the terms of the GNU General Public + Licence version 3 (GPL Version 3). See file LICENSE for a full version of + the license. +""" diff --git a/Python/tests/io/test_hist.py b/Python/tests/io/test_hist.py new file mode 100644 index 000000000..7cf5b38fb --- /dev/null +++ b/Python/tests/io/test_hist.py @@ -0,0 +1,70 @@ +""" + Tests for `corsika.io.hist` + + (c) Copyright 2018 CORSIKA Project, corsika-project@lists.kit.edu + + This software is distributed under the terms of the GNU General Public + Licence version 3 (GPL Version 3). See file LICENSE for a full version of + the license. +""" +import os +import os.path as op +import subprocess + +import corsika + +from .. import build_directory + +# the directory containing 'testSaveBoostHistogram' +bindir = op.join(build_directory, "Framework", "Utilities") + + +def generate_hist() -> str: + """ + Generate a test with `testSaveBoostHistogram`. + + Returns + ------- + str + The path to the generated histogram. + bool + If True, this file was regenerated. + """ + + # we construct the name of the bin + bin = op.join(bindir, "testSaveBoostHistogram") + + # check if a histogram already exists + if op.exists(op.join(bin, "hist.npz")): + return op.join(bin, "hist.npz"), False + else: + # run the program - this generates "hist.npz" in the CWD + subprocess.call(bin) + + return op.join(os.getcwd(), "hist.npz"), True + + +def test_corsika_io() -> None: + """ + Test I can corsika.io without a further import. + """ + corsika.io.read_hist + + +def test_corsika_read_hist() -> None: + """ + Check that I can read in the test histograms with `read_hist`. + """ + + # generate a test histogram + filename, delete = generate_hist() + + # and try and read in a histogram + h = corsika.io.read_hist(filename) + + # and delete the generated histogram + if delete: + os.remove(filename) + + # and check that it isn't empty + assert not h.empty() diff --git a/Python/tests/test_hist.py b/Python/tests/test_hist.py deleted file mode 100644 index 0ad70b734..000000000 --- a/Python/tests/test_hist.py +++ /dev/null @@ -1,36 +0,0 @@ -""" - Tests for `corsika.io.hist` - - (c) Copyright 2018 CORSIKA Project, corsika-project@lists.kit.edu - - This software is distributed under the terms of the GNU General Public - Licence version 3 (GPL Version 3). See file LICENSE for a full version of - the license. -""" -import pytest - -import corsika - - -def test_corsika_io() -> None: - """ - Test I can corsika.io without a further import. - """ - corsika.io.read_hist - - -def test_corsika_read_hist() -> None: - """ - Check that I can read in the test histograms with `read_hist`. - """ - - # try and read in a continuous histogram - - # try and read in a discrete histogram - - -def test_corsika_read_hist_fail() -> None: - """ - Check that an exception is thrown when reading - an incorrectly formatted histogram. - """ -- GitLab