Skip to content

Commit

Permalink
Persist test-data when test-data dir set (#170)
Browse files Browse the repository at this point in the history
Can now set the directory into which test-data will be persisted (so
that re-download of test-data can be avoided) using the
`LAGTRAJ_TESTDATA_DIR` environment variable. Fix development notes to
indicate this new functionality.

Closes #166
  • Loading branch information
leifdenby authored Oct 18, 2022
1 parent 6ddfdb6 commit 1eea939
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
11 changes: 5 additions & 6 deletions docs/developing.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,13 @@ export PYTEST_ADDOPTS='--pdb --pdbcls=IPython.terminal.debugger:Pdb'

### Speeding up testing

If you are running the tests repeatedly it's a good idea to download the
testdata locally and point lagtraj to where it resides (otherwise lagtraj will
attempt to download the testdata every time the tests are run):
If you are running the tests repeatedly it's a good idea to ensure that the
test-data isn't downloaded anew every time the tests are run. To do this you
just neeed to set the environment variable `LAGTRAJ_TESTDATA_DIR` to set the
directory where you would like the test-data to be persisted (the directory
will be created for you if it doesn't already exist):

```bash
wget http://gws-access.ceda.ac.uk/public/eurec4auk/testdata/lagtraj.testdata.tar.gz
mkdir /tmp/lagtraj
tar zxvf lagtraj.testdata.tar.gz -C /tmp/lagtraj
export LAGTRAJ_TESTDATA_DIR=/tmp/lagtraj
```

Expand Down
18 changes: 17 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import shutil
import tarfile
import tempfile
import warnings
from pathlib import Path

import pytest
Expand All @@ -14,9 +15,21 @@

if os.environ.get("LAGTRAJ_TESTDATA_DIR", None):
TESTDATA_DIR = Path(os.environ["LAGTRAJ_TESTDATA_DIR"])
USING_PERSISTENT_TESTDIR = True
else:
tempdir = tempfile.TemporaryDirectory()
TESTDATA_DIR = Path(tempdir.name)
warnings.warn(
"The data required for testing is being downloaded to a temporary "
"directory and will be deleted once the tests have been run. This means "
"that the data will have to be re-downloaded when tests are next run. To "
"persist the test-data to a permanent directory please set the "
"LAGTRAJ_TESTDATA_DIR environment variable to the place where you would "
"like to store the test-data, for example run `export "
"LAGTRAJ_TESTDATA_DIR=/tmp/lagtraj` in your command prompt "
"(the directory will be created if it doesn't already exist)"
)
USING_PERSISTENT_TESTDIR = False


def _download_testdata():
Expand All @@ -30,8 +43,11 @@ def _download_testdata():


def ensure_testdata_available():
if not TESTDATA_DIR.exists():
if USING_PERSISTENT_TESTDIR:
TESTDATA_DIR.mkdir(exist_ok=True, parents=True)
elif not TESTDATA_DIR.exists():
raise Exception(f"Couldn't find test-data directory {TESTDATA_DIR}")

# Download testdata if it is not there yet
if len(list(TESTDATA_DIR.glob("**/*.nc"))) == 0:
print("Downloading testdata...")
Expand Down

0 comments on commit 1eea939

Please sign in to comment.