Skip to content

Commit

Permalink
Merge pull request #5 from effigies/autodata2
Browse files Browse the repository at this point in the history
CI/TEST: Separate build, test and publish stages
  • Loading branch information
TheChymera authored Aug 25, 2022
2 parents 9c7a877 + b9e526f commit c763bb0
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 124 deletions.
192 changes: 103 additions & 89 deletions .github/workflows/schemacode_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,92 +5,81 @@ on:
branches:
- "master"
- "autodata2"
paths:
- "tools/schemacode/**"
- "src/schema/**"
tags:
- "schema-*"
pull_request:
branches:
- "*"
paths:
- "tools/schemacode/**"
- "src/schema/**"

jobs:
defaults:
run:
shell: bash

run_tests:
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: ["ubuntu-latest"]
python-version: ["3.10"]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: "Install build dependencies"
run: pip install --upgrade build twine
- name: "Install test dependencies on tag"
run: pip install --upgrade pytest pyyaml pandas tabulate
if: ${{ startsWith(github.ref, 'refs/tags/schema-') }}
- name: "Build archive on tag"
run: pytest tools/schemacode/bidsschematools -k make_archive
env:
BIDSSCHEMATOOLS_RELEASE: 1
if: ${{ startsWith(github.ref, 'refs/tags/schema-') }}
- name: "Build source distribution and wheel"
run: python -m build tools/schemacode
- name: "Check distribution metadata"
run: twine check tools/schemacode/dist/*
- uses: actions/upload-artifact@v3
with:
name: dist
path: tools/schemacode/dist/

test:
runs-on: ${{ matrix.os }}
needs: [build]
strategy:
fail-fast: false
matrix:
os: ["ubuntu-latest"]
python-version: ["3.7", "3.8", "3.9", "3.10"]
fail-fast: false
matrix:
os: ["ubuntu-latest"]
python-version: ["3.7", "3.8", "3.9", "3.10"]
name: ${{ matrix.os }} with Python ${{ matrix.python-version }}
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v3

- name: "Set up Python"
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
python-version: ${{ matrix.python-version }}

- name: "Display Python version"
shell: bash {0}
run: python -c "import sys; print(sys.version)"

- name: "Set Release Variable based on tag"
shell: bash {0}
run: |
MY_TAG="$(git describe --tags)"
if [ "${MY_TAG:0:7}" = "schema-" ]; then
echo "BIDSSCHEMATOOLS_RELEASE=1" >> $GITHUB_ENV
fi
- name: "Fetch packages"
uses: actions/download-artifact@v3
with:
name: dist
path: dist/

- name: "Install the schemacode package"
shell: bash {0}
- name: "Install package"
run: |
python -m pip install --progress-bar off --upgrade pip
python -m pip install ./tools/schemacode[all]
pip install $( ls dist/*.whl )[all]
- name: "Run tests"
shell: bash {0}
run: |
echo "Running test suite with BIDSSCHEMATOOLS_RELEASE set to $BIDSSCHEMATOOLS_RELEASE"
if [ -v BIDSSCHEMATOOLS_RELEASE ]; then
echo "This will save bundled data into test/data/bundled ."
fi
pushd tools/schemacode
python -m pytest -vvs --pyargs bidsschematools -m "not validate_schema" --cov-append --cov-report=xml --cov=bidsschematools
popd
- name: "Test upload"
shell: bash {0}
id: testpypi_upload
run: |
if [ -v BIDSSCHEMATOOLS_RELEASE ]; then
python -m pip install --upgrade build twine wheel
echo "BIDSSCHEMATOOLS_RELEASE is set to $BIDSSCHEMATOOLS_RELEASE"
pushd tools/schemacode
python -m build
twine upload -u __token__ -p ${{secrets.BIDS_TOKEN_TEST}} --repository testpypi dist/*
popd
fi
- name: "Real upload"
shell: bash {0}
if: steps.testpypi_upload.outputs.exit_code == 0
run: |
if [ -v BIDSSCHEMATOOLS_RELEASE ]; then
pushd tools/schemacode
echo "BIDSSCHEMATOOLS_RELEASE is set to $BIDSSCHEMATOOLS_RELEASE"
twine upload -u __token__ -p ${{secrets.BIDS_TOKEN_TEST}} dist/*
popd
fi
python -m pytest -vs --pyargs bidsschematools -m "not validate_schema" \
--cov-append --cov-report=xml --cov=bidsschematools
- name: Upload artifacts
uses: actions/upload-artifact@v3
Expand All @@ -99,6 +88,34 @@ jobs:
path: coverage.xml
if: success()

publish:
runs-on: ${{ matrix.os }}
name: Publish Python Package
needs: [test]
strategy:
matrix:
os: ["ubuntu-latest"]
python-version: ["3.10"]
steps:
- name: "Fetch packages"
uses: actions/download-artifact@v3
with:
name: dist
path: dist/
- name: "Test PyPI upload"
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
repository_url: https://test.pypi.org/legacy/
skip_existing: true
- name: "Upload to PyPI (on tags)"
if: ${{ startsWith(github.ref, 'refs/tags/schema-') }}
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}

flake8-lint:
runs-on: ubuntu-latest
name: Lint schemacode
Expand All @@ -112,56 +129,53 @@ jobs:
python-version: "3.7"

- name: Install the schemacode package
shell: bash {0}
run: |
python -m pip install --progress-bar off --upgrade pip setuptools wheel
python -m pip install -e ./tools/schemacode[all]
- name: Run flake8
working-directory: ./tools/schemacode/
shell: bash {0}
run: |
flake8 .
validate_schema:
runs-on: ubuntu-latest
name: Validate schema
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v3

- name: Set Up Python environment
uses: actions/setup-python@v4
with:
python-version: "3.9"
- name: Set Up Python environment
uses: actions/setup-python@v4
with:
python-version: "3.9"

- name: Install the schemacode package
shell: bash {0}
run: |
python -m pip install --progress-bar off --upgrade pip setuptools wheel
python -m pip install -e ./tools/schemacode[all]
- name: Install the schemacode package
run: |
python -m pip install --progress-bar off --upgrade pip setuptools wheel
python -m pip install -e ./tools/schemacode[all]
- name: Run schema validation tests
run: python -m pytest --pyargs bidsschematools -m "validate_schema" --cov-append --cov-report=xml --cov=bidsschematools
- name: Run schema validation tests
run: python -m pytest --pyargs bidsschematools -m "validate_schema" --cov-append --cov-report=xml --cov=bidsschematools

- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: schema_validation
path: coverage.xml
if: success()
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: schema_validation
path: coverage.xml
if: success()

upload_to_codecov:
runs-on: "ubuntu-latest"
name: Upload coverage
needs: [run_tests,validate_schema]
needs: [test, validate_schema]
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Checkout
uses: actions/checkout@v2

- name: Download artifacts
uses: actions/download-artifact@v3
- name: Download artifacts
uses: actions/download-artifact@v3

- name: Upload to CodeCov
uses: codecov/codecov-action@v3
with:
fail_ci_if_error: true
- name: Upload to CodeCov
uses: codecov/codecov-action@v3
with:
fail_ci_if_error: true
28 changes: 14 additions & 14 deletions scripts/test_tag
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#!/usr/bin/env bash

# Execute this script from its containing directory to push to a new unique testing tag.
# Execute this script from anywhere in the repository to push to a new unique testing tag.
# This is a script which automates creation and pushing of unique tags for github actions.
# It is particularly useful when debugging the upload hooks for pypi.
# The workflow for that is to edit `.github/workflows/schemacode_ci.yml` and add a secondary branch name to the `on` section right after master.
# This will allow you to keep pushing to the branch as if it were master and unix-time tagged archives being uploaded to test.pypi.

VERSION_FILE="src/schema/SCHEMA_VERSION"
EPOCH="$(date +%s)"

pushd ..
export MY_BIDS_DATE="$(date +%s)"
REAL_VERSION=$(cat "${VERSION_FILE}")
DUMMY_VERSION="0.0.${MY_BIDS_DATE}-dev"
echo "${DUMMY_VERSION}" > "${VERSION_FILE}"
git commit -a -m "debug"
git tag -a "schema-$DUMMY_VERSION" -m "debug"
git push origin
git push origin "schema-$DUMMY_VERSION"
echo "${REAL_VERSION}" > "${VERSION_FILE}"
popd
unset MY_BIDS_DATE
GIT_DIR=$( git rev-parse --git-dir )
REPO_ROOT=$( ls -d ${GIT_DIR%.git} )

VERSION_FILE="$REPO_ROOT/src/schema/SCHEMA_VERSION"

REAL_VERSION=$(cat "${VERSION_FILE}")
DUMMY_VERSION="0.0.${EPOCH}-dev"

echo "${DUMMY_VERSION}" > "${VERSION_FILE}"
git commit -a -m "Test release $DUMMY_VERSION"
git tag -a "schema-$DUMMY_VERSION" -m "Test release $DUMMY_VERSION"

echo "${REAL_VERSION}" > "${VERSION_FILE}"
30 changes: 17 additions & 13 deletions tools/schemacode/bidsschematools/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import logging
import os
import tempfile
from subprocess import run

import pytest
try:
from importlib.resources import as_file, files
except ImportError: # PY<3.9
from importlib_resources import as_file, files

from bidsschematools import schema, utils
import pytest

lgr = logging.getLogger()

Expand Down Expand Up @@ -35,23 +37,21 @@ def get_gitrepo_fixture(url, whitelist):
@pytest.fixture(scope="session")
def fixture():
archive_name = url.rsplit("/", 1)[-1]
testdata_dir = os.path.join(os.path.dirname(__file__), "data", "bundled", archive_name)
testdata_dir = os.path.abspath(testdata_dir)
if os.path.isdir(testdata_dir):
testdata_dir = files("bidsschematools.tests.data") / archive_name
if testdata_dir.is_dir():
lgr.info(
"Found static testdata archive under `%s`. "
"Not downloading latest data from version control.",
testdata_dir,
f"Found static testdata archive under `{testdata_dir}`. "
"Not downloading latest data from version control."
)
yield testdata_dir
with as_file(testdata_dir) as path:
yield path
else:
lgr.info(
"No static testdata available under `%s`. "
"Attempting to fetch live data from version control.",
testdata_dir,
)
with tempfile.TemporaryDirectory() as path:
assert os.path.exists(path)
lgr.debug("Cloning %r into %r", url, path)
runout = run(
[
Expand Down Expand Up @@ -80,14 +80,18 @@ def fixture():
@pytest.fixture(scope="session")
def schema_dir():
"""Path to the schema housed in the bids-specification repo."""
from bidsschematools import utils

bids_schema = utils.get_schema_path()
return bids_schema


@pytest.fixture(scope="session")
def schema_obj(schema_dir):
def schema_obj():
"""Schema object."""
return schema.load_schema(schema_dir)
from bidsschematools import schema

return schema.load_schema()


bids_examples = get_gitrepo_fixture(
Expand Down
Empty file.
17 changes: 9 additions & 8 deletions tools/schemacode/bidsschematools/tests/test_make_testdata.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os

# import tarfile
import shutil

import pytest
try:
from importlib.resources import files
except ImportError: # PY<3.9
from importlib_resources import files

# from .. import __version__
import pytest


def require_env(var):
Expand All @@ -32,10 +33,10 @@ def test_make_archive(bids_examples, bids_error_examples):
* Archives will be generated under `/tmp/bidsschematools-testdata-SCHEMA_VERSION.tar.gz`
"""

target_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "data", "bundled"))
os.makedirs(target_dir, exist_ok=True)
shutil.copytree(bids_examples, f"{target_dir}/bids-examples")
shutil.copytree(bids_error_examples, f"{target_dir}/bids-error-examples")
testdata_dir = files("bidsschematools.tests.data")
ignore_git = shutil.ignore_patterns(".git*")
shutil.copytree(bids_examples, testdata_dir / "bids-examples", ignore=ignore_git)
shutil.copytree(bids_error_examples, testdata_dir / "bids-error-examples", ignore=ignore_git)

# Keeping this for now, it would be really nice to have a separate archive someday.
# archive_name = f"bidsschematools-testdata-{__version__}"
Expand Down

0 comments on commit c763bb0

Please sign in to comment.