Skip to content

Commit

Permalink
Merge pull request #7 from ADACS-Australia/feature/docker
Browse files Browse the repository at this point in the history
Add testing with Docker
  • Loading branch information
gbpoole authored Aug 6, 2024
2 parents 4f19ead + ae88995 commit 1d5dca9
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
54 changes: 54 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#############
# Base image
#############
FROM python:3.11-buster

RUN pip install poetry==1.4.2

ENV POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_CREATE=0 \
POETRY_CACHE_DIR=/tmp/poetry_cache

ENV TEST_ROOT /test_template
ENV TEMPLATE_ROOT /template

WORKDIR ${TEST_ROOT}

################################################################
# Copy and install Poetry dependencies (but not the actual
# application, which will get installed by the entry_point
# script when we start the container)
################################################################
COPY pyproject.toml poetry.lock .
RUN poetry install --no-root && \
rm -rf ${POETRY_CACHE_DIR} && \
rm pyproject.toml && \
mv poetry.lock poetry.lock.image

##########################
# Set-up the entry point
##########################
RUN touch entry_script.sh
RUN chmod a+rx ${TEST_ROOT}/entry_script.sh
RUN echo '\n\
set -e \n\
\n\
_term() {\n\
echo "Caught SIGTERM signal!"\n\
kill -TERM "$child" 2>/dev/null\n\
}\n\
\n\
# trap _term SIGTERM\n\
\n\
if ! test -d ${TEMPLATE_ROOT} ; then \n\
echo The project directory has not been mounted. Please run the container with e.g. 'docker run -v \$PWD:${TEMPLATE_ROOT}' \n\
exit 1 \n\
fi \n\
if ! cmp -s ${TEST_ROOT}/poetry.lock.image ${TEMPLATE_ROOT}/poetry.lock ; then \n\
echo poetry.lock has been updated since the image was built. Please rebuild it and try again. \n\
exit 1 \n\
fi \n\
cd ${TEMPLATE_ROOT} \n\
poetry install --only-root \n\
pytest' >> ${TEST_ROOT}/entry_script.sh
ENTRYPOINT ["sh", "-c", "${TEST_ROOT}/entry_script.sh"]
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# The following are needed to make sure that running:
# $ make -f <path>/Makefile
# will work with this Makefile.
MAKEFILE_PATH = $(abspath $(lastword $(MAKEFILE_LIST)))
MAKEFILE_DIR = $(dir $(MAKEFILE_PATH))
PRJ_PATH=$(realpath ${MAKEFILE_DIR} )
PRJ_SLUG=$(notdir ${PRJ_PATH})

docker-build:
docker build -t ${PRJ_SLUG} .

docker-tests:
docker run --rm -v ${PRJ_PATH}:/template ${PRJ_SLUG}
13 changes: 12 additions & 1 deletion docs/content/template_maintenance.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@ the maintenance of this template.

When testing, temporary projects get created and need to be installed into an environment before tests can be run, documentation builds checked, etc. Be warned that this can both pollute your development environment and lead to unreliable tests due to the bleeding of state from past test runs to new runs.

To address this we will soon add the ability to run tests in a Docker container, where we can ensure fresh and reproducable test runs. This is not implemented yet but will be soon.
To address this, a `Dockerfile` and `Makefile` are provided for running tests in a container. Run the
following to build the container:

``` console
make docker-build
```

Run the following to run the tests in the container:

``` console
make docker-tests
```

## Some things to note about Cookiecutter templates

Expand Down
3 changes: 2 additions & 1 deletion hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

def git() -> None:
"""Create and configure a git repo for the rendered project"""
result = subprocess.run(["git", "init", "-q", "-b", "main"])
result = subprocess.run(["git", "init", "-q"])
result = subprocess.run(["git", "checkout", "-q", "-b", "main"])
result = subprocess.run(
["git", "config", "--local", "push.followTags", "true"])
result = subprocess.run(
Expand Down

0 comments on commit 1d5dca9

Please sign in to comment.