Skip to content

Commit

Permalink
ci specific test-script setup (eqcorrscan#267)
Browse files Browse the repository at this point in the history
* ci specific setup

* debug circleci

* debug circleci

* correct path

* sort coverage

* Verbose

* Verbose

* Verbose

* Verbose

* install not develop

* Put doctests in the right place

* Put appveyor in the right place

* Do not runslow if runsusperslow

* Remove old docs

* Update paths for appveyor

* Do not install to avoid ImportMismatchError with doctest-modules

* line endings?

* not installed script

* use develop

* use develop

* use more recent pytest

* Just use pytest the second time

* don't use test script on windows

* don't use test script on windows

* Fix paths and don't use symlink for testing

* Typo

* use real path

* full path for doc files

* Remove extraneous path bit

* Add doctest option

* do not pass doc arg to pytest

* expand paths

* test-data paths in docs

* Rewrite coveragerc with correct paths

* fix paths a bit more

* Use ret

* Linux doesn't like multiple conftests

* posix is different

* make context manager revert properly

* changelog

* Don't check milli seconds in rid

* Update PKG_PATH in conftest

* Update PKG_PATH in conftest

* Update PKG_PATH in conftest
  • Loading branch information
calum-chamberlain authored Aug 25, 2018
1 parent ea3254e commit 9d5f241
Show file tree
Hide file tree
Showing 21 changed files with 239 additions and 79 deletions.
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ jobs:
command: |
. venv/bin/activate
python setup.py develop
# python setup.py build_ext --inplace
# run tests!
- run:
name: run tests
command: |
. venv/bin/activate
test_eqcorrscan.py -m "network" -n 2
test_eqcorrscan.py eqcorrscan/doc/tutorials/*.rst eqcorrscan/doc/submodules/*.rst --cov-append
test_eqcorrscan.py --ci -m "network" -n 2
test_eqcorrscan.py --ci --doc --cov-append
- run:
name: Upload to codecov
command: |
. venv/bin/activate
cp ~/.eqcorrscan/.coverage .
ls -a
codecov
Expand Down
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,24 @@ install:
- conda list
# done installing dependencies
- git version
# - python setup.py install
- python setup.py develop
# - python setup.py build_ext --inplace

script:
- export MPLBACKEND=Agg
- test_eqcorrscan.py --runslow -m "not serial and not network" -n 2
- test_eqcorrscan.py --ci --runslow -m "not serial and not network" -n 2
# - py.test --runslow -m "not serial and not network" -n 2
- export OMP_NUM_THREADS=2;
# These tests use multi-threading
# - py.test -m "serial and not network" --cov-append
- test_eqcorrscan.py -m "serial and not network" --cov-append
- test_eqcorrscan.py --ci -m "serial and not network" --cov-append
# - py.test --runsuperslow -s eqcorrscan/tests/tutorials_test.py --cov-append
- test_eqcorrscan.py --runsuperslow -s eqcorrscan/tests/tutorials_test.py --cov-append
- test_eqcorrscan.py --ci --runsuperslow -s --cov-append

after_success:
# Check how much code is actually tested and send this report to codecov
- ls -a
- mv .coverage ../.coverage.empty
- mv ~/.eqcorrscan/.coverage ../.coverage.empty
- cd ..
- coverage combine
- codecov
Expand Down
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## Current
* Make test-script more stable.
* Fix bug where `set_xcorr` as context manager did not correctly reset
stream_xcorr methods.
* Correct test-script (`test_eqcorrscan.py`) to find paths properly.

## 0.3.2
Expand Down
10 changes: 4 additions & 6 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ install:

# Install other pip dependancies
- "pip install -U future"
- "pip install pytest==3.2.4 pytest-pep8 pytest-cov pytest-xdist pytest-rerunfailures pytest-mpl codecov"
- "pip install pytest pytest-pep8 pytest-cov pytest-xdist pytest-rerunfailures pytest-mpl codecov"
# list package versions
- "conda list"
# Copy the windows fftw header file
Expand All @@ -62,15 +62,13 @@ install:
build: false

test_script:
- "%CMD_IN_ENV% python setup.py build_ext --inplace"
- "%CMD_IN_ENV% python setup.py develop"
- "%CMD_IN_ENV% test_eqcorrscan.py -n4 -m \"not serial and not network\""
- "%CMD_IN_ENV% test_eqcorrscan.py -m \"serial and not network\" --cov-append"
# - "%CMD_IN_ENV% test_eqcorrscan.py -n4 -m \"network\" --cov-append"
# - "%CMD_IN_ENV% python setup.py install"
- "%CMD_IN_ENV% py.test -n4 -m \"not serial and not network\""
- "%CMD_IN_ENV% py.test -m \"serial and not network\" --cov-append"

after_test:
# - "coverage combine"
# - "powershell copy-item .coverage ..\\.coverage.empty"
# - "cd .."
# - "coverage combine"
- "codecov"
8 changes: 6 additions & 2 deletions eqcorrscan/core/match_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2037,8 +2037,12 @@ def construct(self, method, name, lowcut, highcut, samp_rate, filt_order,
.. rubric:: Example
>>> sac_files = glob.glob(
... 'eqcorrscan/tests/test_data/SAC/2014p611252/*')
>>> # Get the path to the test data
>>> import eqcorrscan
>>> import os
>>> TEST_PATH = (
... os.path.dirname(eqcorrscan.__file__) + '/tests/test_data')
>>> sac_files = glob.glob(TEST_PATH + '/SAC/2014p611252/*')
>>> template = Template().construct(
... method='from_sac', name='test', lowcut=2.0, highcut=8.0,
... samp_rate=20.0, filt_order=4, prepick=0.1, swin='all',
Expand Down
24 changes: 18 additions & 6 deletions eqcorrscan/core/template_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,13 @@ def template_gen(method, lowcut, highcut, samp_rate, filt_order,
>>> from obspy import read
>>> from eqcorrscan.core.template_gen import template_gen
>>> st = read('eqcorrscan/tests/test_data/WAV/TEST_/' +
>>> # Get the path to the test data
>>> import eqcorrscan
>>> import os
>>> TEST_PATH = os.path.dirname(eqcorrscan.__file__) + '/tests/test_data'
>>> st = read(TEST_PATH + '/WAV/TEST_/' +
... '2013-09-01-0410-35.DFDPC_024_00')
>>> quakeml = 'eqcorrscan/tests/test_data/20130901T041115.xml'
>>> quakeml = TEST_PATH + '/20130901T041115.xml'
>>> templates = template_gen(
... method='from_meta_file', meta_file=quakeml, st=st, lowcut=2.0,
... highcut=9.0, samp_rate=20.0, filt_order=3, length=2, prepick=0.1,
Expand All @@ -212,7 +216,7 @@ def template_gen(method, lowcut, highcut, samp_rate, filt_order,
>>> from eqcorrscan.core.template_gen import template_gen
>>> import glob
>>> # Get all the SAC-files associated with one event.
>>> sac_files = glob.glob('eqcorrscan/tests/test_data/SAC/2014p611252/*')
>>> sac_files = glob.glob(TEST_PATH + '/SAC/2014p611252/*')
>>> templates = template_gen(
... method='from_sac', sac_files=sac_files, lowcut=2.0, highcut=10.0,
... samp_rate=25.0, filt_order=4, length=2.0, swin='all', prepick=0.1,
Expand Down Expand Up @@ -1130,8 +1134,12 @@ def from_sac(sac_files, lowcut, highcut, samp_rate, filt_order, length, swin,
>>> from eqcorrscan.core.template_gen import from_sac
>>> import glob
>>> # Get the path to the test data
>>> import eqcorrscan
>>> import os
>>> TEST_PATH = os.path.dirname(eqcorrscan.__file__) + '/tests/test_data'
>>> # Get all the SAC-files associated with one event.
>>> sac_files = glob.glob('eqcorrscan/tests/test_data/SAC/2014p611252/*')
>>> sac_files = glob.glob(TEST_PATH + '/SAC/2014p611252/*')
>>> templates = from_sac(sac_files=sac_files, lowcut=2.0, highcut=10.0,
... samp_rate=25.0, filt_order=4, length=2.0,
... swin='all', prepick=0.1, all_horiz=True)
Expand Down Expand Up @@ -1226,9 +1234,13 @@ def from_meta_file(meta_file, st, lowcut, highcut, samp_rate, filt_order,
>>> from obspy import read
>>> from eqcorrscan.core.template_gen import from_meta_file
>>> st = read('eqcorrscan/tests/test_data/WAV/TEST_/' +
>>> # Get the path to the test data
>>> import eqcorrscan
>>> import os
>>> TEST_PATH = os.path.dirname(eqcorrscan.__file__) + '/tests/test_data'
>>> st = read(TEST_PATH + '/WAV/TEST_/' +
... '2013-09-01-0410-35.DFDPC_024_00')
>>> quakeml = 'eqcorrscan/tests/test_data/20130901T041115.xml'
>>> quakeml = TEST_PATH + '/20130901T041115.xml'
>>> templates = from_meta_file(meta_file=quakeml, st=st, lowcut=2.0,
... highcut=9.0, samp_rate=20.0, filt_order=3,
... length=2, prepick=0.1, swin='S',
Expand Down
4 changes: 3 additions & 1 deletion eqcorrscan/doc/tutorials/clustering.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ in the tests directory.
>>> import glob
>>> import os
>>> from eqcorrscan.utils.clustering import cluster
>>> from eqcorrscan import tests
>>> # You will need to edit this line to the location of your eqcorrscan repo.
>>> testing_path = 'eqcorrscan/tests/test_data/similar_events'
>>> TEST_PATH = os.path.dirname(tests.__file__)
>>> testing_path = TEST_PATH + '/test_data/similar_events'
>>> stream_files = glob.glob(os.path.join(testing_path, '*'))
>>> stream_list = [(read(stream_file), i)
... for i, stream_file in enumerate(stream_files)]
Expand Down
11 changes: 8 additions & 3 deletions eqcorrscan/doc/tutorials/mag-calc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ This example requires data downloaded from the eqcorrscan github repository.
>>> from eqcorrscan.utils.mag_calc import amp_pick_event
>>> from obspy import read, read_events
>>> from obspy.io.nordic.core import readwavename
>>> from eqcorrscan import tests
>>> import os
>>> testing_path = 'eqcorrscan/tests/test_data'
>>> # Get the path for the test-data so we can test this
>>> testing_path = os.path.dirname(tests.__file__) + '/test_data'
>>> sfile = os.path.join(testing_path, 'REA', 'TEST_',
... '01-0411-15L.S201309')
>>> datapath = os.path.join(testing_path, 'WAV', 'TEST_')
Expand Down Expand Up @@ -50,8 +52,11 @@ This example requires data downloaded from the eqcorrscan github repository.
>>> import glob
>>> from eqcorrscan.utils.clustering import svd
>>> import numpy as np
>>> # Do the set-up
>>> testing_path = 'eqcorrscan/tests/test_data/similar_events'
>>> from eqcorrscan import tests
>>> import os
>>> # Get the path for the test-data so we can test this
>>> testing_path = os.path.dirname(
... tests.__file__) + '/test_data/similar_events'
>>> stream_files = glob.glob(os.path.join(testing_path, '*'))
>>> stream_list = [read(stream_file) for stream_file in stream_files]
>>> event_list = []
Expand Down
6 changes: 5 additions & 1 deletion eqcorrscan/doc/tutorials/matched-filter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ event:
>>> import glob
>>> from eqcorrscan.core.match_filter import Template
>>> sac_files = glob.glob('eqcorrscan/tests/test_data/SAC/2014p611252/*')
>>> import os
>>> from eqcorrscan import tests
>>> # Get the path for the test-data so we can test this
>>> TEST_PATH = os.path.dirname(tests.__file__)
>>> sac_files = glob.glob(TEST_PATH + '/test_data/SAC/2014p611252/*')
>>> # sac_files is now a list of all the SAC files for event id:2014p611252
>>> template = Template().construct(
... method='from_sac', name='test', lowcut=2.0, highcut=8.0,
Expand Down
6 changes: 5 additions & 1 deletion eqcorrscan/doc/tutorials/subspace.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ aligned (see clustering submodule for alignment methods).
>>> from obspy import read
>>> import glob
>>> wavefiles = glob.glob('eqcorrscan/tests/test_data/similar_events/*')
>>> import os
>>> from eqcorrscan import tests
>>> # Get the path for the test-data so we can test this
>>> TEST_PATH = os.path.dirname(tests.__file__)
>>> wavefiles = glob.glob(TEST_PATH + '/test_data/similar_events/*')
>>> wavefiles.sort() # Sort the wavefiles to ensure reproducibility
>>> streams = [read(w) for w in wavefiles[0:3]]
>>> # Channels must all be the same length
Expand Down
105 changes: 91 additions & 14 deletions eqcorrscan/scripts/test_eqcorrscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

VERSION = eqcorrscan.__version__
TEST_PATH = os.path.dirname(tests.__file__)
PKG_PATH = os.path.dirname((eqcorrscan.__file__))
PKG_PATH = os.path.dirname(eqcorrscan.__file__)
WORKING_DIR = os.path.join(os.path.expanduser("~"), ".eqcorrscan")
TAG_URL = "https://github.com/eqcorrscan/EQcorrscan/archive/{0}.zip".format(
VERSION)
Expand All @@ -55,16 +55,38 @@ def __exit__(self, etype, value, traceback):
os.chdir(self.savedPath)


def setup_ci():
if not os.path.isdir(WORKING_DIR):
os.makedirs(WORKING_DIR)
for f in ["pytest.ini", "conftest.py"]:
shutil.copy(
os.path.join(os.getcwd(), f), WORKING_DIR)
check_path_conftest(os.path.join(WORKING_DIR, "conftest.py"))
rewrite_coveragerc(os.path.join(os.getcwd(), ".coveragerc"),
os.path.join(WORKING_DIR, ".coveragerc"))
if os.path.isdir(TEST_DATA_PATH) and \
len(glob.glob(os.path.join(TEST_DATA_PATH, "*"))) == 0:
shutil.rmtree(TEST_DATA_PATH)
if not os.path.isdir(TEST_DATA_PATH):
test_data_path = os.path.join(
os.getcwd(), "eqcorrscan", "tests", "test_data")
shutil.copytree(test_data_path, TEST_DATA_PATH)
if os.path.isdir(os.path.join(PKG_PATH, "doc")) and \
len(glob.glob(os.path.join(PKG_PATH, "doc", "*"))) == 0:
shutil.rmtree(os.path.join(PKG_PATH, "doc"))
if not os.path.isdir(os.path.join(PKG_PATH, "doc")):
doc_path = os.path.join(
os.getcwd(), "eqcorrscan", "doc")
shutil.copytree(doc_path, os.path.join(PKG_PATH, "doc"))


def download_test_data():
"""Check if test data are installed, and if not, download them"""
test_data_downloaded = False
control_files = [
{"name": "pytest.ini", "downloaded": False},
{"name": "conftest.py", "downloaded": False},
{"name": ".coveragerc", "downloaded": False}]
pytest_ini_downloaded = False
conftest_downloaded = False
coveragerc_downloaded = False
if os.path.isdir(TEST_DATA_PATH):
if len(glob.glob(os.path.join(TEST_DATA_PATH, "*"))) > 0:
Logger.info("Found test data at: {0}".format(TEST_DATA_PATH))
Expand All @@ -80,16 +102,12 @@ def download_test_data():
os.makedirs(TEST_DATA_PATH)
if not os.path.isdir(WORKING_DIR):
os.makedirs(WORKING_DIR)
if not os.path.islink(os.path.join(WORKING_DIR, "eqcorrscan")):
# Make a symbolic link so that test data can be found
Logger.debug("Making symlink to install")
os.symlink(PKG_PATH, os.path.join(WORKING_DIR, "eqcorrscan"))

control_files_downloaded = all([c["downloaded"] for c in control_files])
if test_data_downloaded and control_files_downloaded:
return

Logger.info("Downloading test data from github")
Logger.info("Downloading necessary files from github")
with cd(TEST_DATA_PATH):
# Get the whole zip for this release
Logger.info("Downloading from {0}".format(TAG_URL))
Expand All @@ -107,7 +125,13 @@ def download_test_data():
contents.extract(extract_path, ".")
Logger.debug("Moving {0} to {1}".format(
extract_path, WORKING_DIR))
shutil.move(extract_path, WORKING_DIR)
if control_file["name"] == "conftest.py":
check_path_conftest(extract_path)
if control_file["name"] == ".coveragerc":
rewrite_coveragerc(
extract_path, os.path.join(WORKING_DIR, ".coveragerc"))
else:
shutil.move(extract_path, WORKING_DIR)

if not test_data_downloaded:
for file in contents.namelist():
Expand All @@ -122,21 +146,74 @@ def download_test_data():
return


def rewrite_coveragerc(infile, outfile):
with open(infile, "r") as f:
contents = f.read().split("\n")
with open(outfile, "w") as f:
for line in contents:
f.write(line.replace("eqcorrscan", PKG_PATH) + "\n")


def check_path_conftest(conftest):
lines_out = []
with open(conftest, "r") as f:
for line in f:
if line.startswith("PKG_PATH"):
lines_out.append("PKG_PATH = '{0}'".format(PKG_PATH))
else:
lines_out.append(line)
with open(conftest, "w") as f:
for line in lines_out:
f.write(line + "\n")


def run_tests(arg_list):
"""
Run the tests.
"""
arg_list.extend(["--doctest-modules", "--runslow"])
# Convert arguments to real paths
if "--doc" in arg_list:
doc_files = glob.glob(
os.path.join(PKG_PATH, "doc", "tutorials", "*.rst"))
doc_files.extend(glob.glob(
os.path.join(PKG_PATH, "doc", "submodules", "*.rst")))
arg_list.extend(doc_files)
arg_list.remove("--doc")
elif "--runsuperslow" in arg_list:
arg_list.append(os.path.join(PKG_PATH, "tests", "tutorials_test.py"))
else:
arg_list.append(PKG_PATH)
if "--runsuperslow" not in arg_list and "--runslow" not in arg_list:
arg_list.append("--runslow")
arg_list.extend(
["--ignore", "EGG-INFO", "--ignore", "eqcorrscan/utils/lib"])
["--ignore", "EGG-INFO", "--ignore", PKG_PATH + "/utils/lib",
"--doctest-modules", "--cov-config",
os.path.join(WORKING_DIR, ".coveragerc"), "--ignore=setup.py"])
# arg_list.append(PKG_PATH)
with cd(WORKING_DIR):
# If we are on windows, conftest.py has to be in the eqcorrscan dir :(
if sys.platform != "linux":
shutil.copy("pytest.ini", os.path.join(PKG_PATH, "pytest.ini"))
shutil.copy(".coveragerc", os.path.join(PKG_PATH, ".coveragerc"))
shutil.copy("conftest.py", os.path.join(PKG_PATH, "conftest.py"))
Logger.info("Working in {0}".format(WORKING_DIR))
Logger.info("Running tests from {0}".format(PKG_PATH))
Logger.info("pytest {0}".format(' '.join(arg_list)))
pytest.main(args=arg_list)
# Run the tests!
ret = pytest.main(args=arg_list)
if os.path.isfile(os.path.join(PKG_PATH, "conftest.py")):
os.remove(os.path.join(PKG_PATH, "pytest.ini"))
os.remove(os.path.join(PKG_PATH, ".coveragerc"))
os.remove(os.path.join(PKG_PATH, "conftest.py"))
if ret != 0:
raise SystemExit("Failed test")


if __name__ == "__main__":
arg_list = sys.argv[1:]
download_test_data()
if "--ci" in arg_list:
setup_ci()
arg_list.remove("--ci")
else:
download_test_data()
run_tests(arg_list=arg_list)
Loading

0 comments on commit 9d5f241

Please sign in to comment.