Skip to content

Commit b5ea435

Browse files
committed
Add testing script, derp, and python-based version test
1 parent 9a6dd30 commit b5ea435

File tree

6 files changed

+113
-13
lines changed

6 files changed

+113
-13
lines changed

.travis.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ install:
1818
- pip install -U -r test_requirements.txt
1919
- pip install --no-deps -e .
2020
script:
21-
- flake8 gemd
22-
- pytest --cov=gemd --cov-report term-missing:skip-covered --cov-config=tox.ini --no-cov-on-fail --cov-fail-under=100 gemd
21+
- bash scripts/run_tests.sh
2322
- cd docs; make html; cd ..;
2423
- touch ./docs/_build/html/.nojekyll
25-
- if [ "$TRAVIS_PULL_REQUEST" != "false" ] && [ "$TRAVIS_BRANCH" == "main" ]; then bash ./scripts/validate-version-bump.sh; fi
2624
deploy:
2725
- provider: pages
2826
skip_cleanup: true

CONTRIBUTING.md

+2-9
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,13 @@ Changes are gated on:
88
* PEP8 style compliance, with some exceptions in the [tox file](tox.ini)
99
* Incrementing the package version number in [setup.py](setup.py)
1010

11-
Check out the [.travis.yml](.travis.yml) file for the exact testing procedure.
11+
Travis runs the tests in `scripts/run_tests.sh`, which gives a convenient one-line invocation for testing.
1212

1313
As it can be easy to forget to verify these prior to pushing, it's possible to use [git hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) to enforce compliance during normal workflows.
1414
Consider editing `.git/hooks/pre-commit` or `.git/hooks/pre-push` (or adding them and marking them as executable: `chmod +x <file>`).
1515
For example, you could set your local `.git/hooks/pre-commit` to be
1616
```shell
17-
if [ "`git rev-parse --abbrev-ref HEAD`" == "main" ];
18-
then echo "On main branch";
19-
exit 1;
20-
fi &&
21-
scripts/validate-version-bump.sh &&
22-
flake8 gemd &&
23-
pytest --quiet --cov=gemd --cov-report term-missing:skip-covered \
24-
--cov-config=tox.ini --no-cov-on-fail --cov-fail-under=100 -x .
17+
scripts/run_tests.sh --quiet --exitfirst
2518
```
2619
to make sure you're not on the `main` branch, you've incremented the package version, you pass the linter and you have complete, passing tests.
2720

scripts/run_tests.sh

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bash
2+
3+
for i in "$@"; do
4+
case $i in
5+
-q|--quiet)
6+
QUIET="--quiet --no-cov-on-fail "
7+
shift # past argument=value
8+
;;
9+
-x|--exitfirst)
10+
EXITFIRST="--exitfirst "
11+
shift # past argument=value
12+
;;
13+
-*|--*)
14+
echo "Unknown option $i"
15+
exit 1
16+
;;
17+
*)
18+
;;
19+
esac
20+
done
21+
22+
REPO_DIR=`git rev-parse --show-toplevel`
23+
24+
# following https://docs.travis-ci.com/user/environment-variables/#convenience-variables
25+
# the pull request isn't set to true if its a PR; its just set to false if its not.
26+
if [ -z "$TRAVIS_PULL_REQUEST" ]; # Not Travis context
27+
then
28+
if [ "`git rev-parse --abbrev-ref HEAD`" == "main" ];
29+
then echo "On main branch"; exit 1;
30+
else python $REPO_DIR/scripts/validate_version_bump.py || exit 1;
31+
fi
32+
# only run the validate-version_bump.py script on PR builds against main,
33+
# since those have access to a "main" reference to git show the version
34+
elif [ "$TRAVIS_PULL_REQUEST" != "false" ] && [ "$TRAVIS_BRANCH" == "main" ];
35+
then python $REPO_DIR/scripts/validate_version_bump.py || exit 1;
36+
fi
37+
38+
flake8 $REPO_DIR/gemd || exit 1;
39+
derp $REPO_DIR $REPO_DIR/setup.py || exit 1;
40+
pytest $QUIET $EXITFIRST --cov=$REPO_DIR \
41+
--cov-report term-missing:skip-covered \
42+
--cov-config=$REPO_DIR/tox.ini --no-cov-on-fail --cov-fail-under=100 \
43+
$REPO_DIR/gemd || exit 1;

scripts/validate_version_bump.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!python
2+
from os import getcwd, popen
3+
from os.path import relpath
4+
from packaging.version import Version
5+
import re
6+
import sys
7+
from typing import TextIO
8+
9+
10+
def main():
11+
repo_dir = popen("git rev-parse --show-toplevel", mode="r").read().rstrip()
12+
version_path = relpath(f'{repo_dir}/setup.py', getcwd())
13+
14+
try:
15+
with open(version_path, "r") as fh:
16+
new_version = extract_version(fh)
17+
except Exception as e:
18+
raise ValueError(f"Couldn't extract version from {version_path}") from e
19+
20+
try:
21+
with popen(f"git show main:setup.py", mode="r") as fh:
22+
old_version = extract_version(fh)
23+
except Exception as e:
24+
raise ValueError(f"Couldn't extract version from main branch") from e
25+
26+
if new_version.major != old_version.major:
27+
number = "major"
28+
code = 1
29+
elif new_version.minor != old_version.minor:
30+
number = "minor"
31+
code = 2
32+
elif new_version.micro != old_version.micro:
33+
number = "patch"
34+
code = 3
35+
else:
36+
number = "other component of"
37+
code = 5
38+
39+
if new_version > old_version:
40+
print(f"{number} version bump")
41+
return 0
42+
elif new_version < old_version:
43+
print(f"error - {number} version decreased! {new_version} < {old_version}")
44+
return code
45+
else:
46+
print(f"error - version unchanged! {new_version} == {old_version}")
47+
return 4
48+
49+
50+
def extract_version(handle: TextIO) -> Version:
51+
text = handle.read()
52+
if not re.search(r"\S", text):
53+
raise ValueError(f"No content")
54+
match = re.search(r"""^\s*_*version_*\s*=\s*(['"])(\S+)\1""", text, re.MULTILINE)
55+
if match:
56+
return Version(match.group(2))
57+
else:
58+
raise ValueError(f"No version found\n{text}")
59+
60+
61+
if __name__ == "__main__":
62+
sys.tracebacklimit = 0
63+
result = main()
64+
if result != 0:
65+
sys.exit(result)

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
setup(name='gemd',
5-
version='1.12.2',
5+
version='1.12.3',
66
url='http://github.com/CitrineInformatics/gemd-python',
77
description="Python binding for Citrine's GEMD data model",
88
author='Citrine Informatics',

test_requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pydocstyle==3.0.0
55
pytest-cov==3.0.0
66
pytest-flake8==1.0.4
77
pandas>=1.1.5,<2
8+
derp==0.1.1

0 commit comments

Comments
 (0)