Skip to content

Commit

Permalink
Merge pull request #68 from scikit-build/publish_github_release-check…
Browse files Browse the repository at this point in the history
…-for-git-dir

publish_github_release: Raise an exception if git subdirectory is not found
  • Loading branch information
jcfr committed Jul 27, 2018
2 parents 70ba17c + 1ab0a6f commit d0176b0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 35 deletions.
37 changes: 29 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@

### PyCharm ###
.idea/

# Created by https://www.gitignore.io/api/python

### Python ###
Expand All @@ -11,7 +15,6 @@ __pycache__/

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
Expand All @@ -23,9 +26,11 @@ lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
Expand All @@ -45,8 +50,9 @@ htmlcov/
.cache
nosetests.xml
coverage.xml
*,cover
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
Expand All @@ -55,6 +61,7 @@ coverage.xml
# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
Expand All @@ -69,7 +76,7 @@ docs/_build/
# PyBuilder
target/

# IPython Notebook
# Jupyter Notebook
.ipynb_checkpoints

# pyenv
Expand All @@ -78,16 +85,30 @@ target/
# celery beat schedule file
celerybeat-schedule

# dotenv
.env
# SageMath parsed files
*.sage.py

# virtualenv
.venv/
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/

### Python Patch ###
.venv/
26 changes: 6 additions & 20 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ matrix:
# Used for testing
env: EXPECTED_PYTHON_VERSION=3.4.6

- os: linux
python: "3.3.5"
# Used for testing
env: EXPECTED_PYTHON_VERSION=3.3.5

- os: linux
python: "2.7"
# Used for testing
Expand All @@ -33,31 +28,22 @@ matrix:
language: generic
env:
# Used by pyenv
- PYTHON_VERSION=3.4.7
# Used for testing
- EXPECTED_PYTHON_VERSION=3.4.7

- os: osx
language: generic
env:
# Used by pyenv
- PYTHON_VERSION=3.3.7
- PYTHON_VERSION=3.4.8
# Used for testing
- EXPECTED_PYTHON_VERSION=3.3.7
- EXPECTED_PYTHON_VERSION=3.4.8

- os: osx
language: generic
env:
# Used by pyenv
- PYTHON_VERSION=2.7.14
- PYTHON_VERSION=2.7.15
# Used for testing
- EXPECTED_PYTHON_VERSION=2.7.14
- EXPECTED_PYTHON_VERSION=2.7.15

cache:
directories:
- $HOME/.pyenv/versions/3.4.5
- $HOME/.pyenv/versions/3.3.7
- $HOME/.pyenv/versions/2.7.14
- $HOME/.pyenv/versions/3.4.8
- $HOME/.pyenv/versions/2.7.15

install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then mkdir $HOME/bin; ln -s $(which pip2) $HOME/bin/pip; fi
Expand Down
34 changes: 27 additions & 7 deletions _tests/test_addon_publish_github_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ def test_packages_selection_minilanguage(


def test_missing_token(src_dir, mocker, capsys, monkeypatch):
monkeypatch.delenv("GITHUB_TOKEN")
if "GITHUB_TOKEN" in os.environ:
monkeypatch.delenv("GITHUB_TOKEN")

pgr = __import__('publish_github_release')

Expand All @@ -181,9 +182,9 @@ def test_missing_token(src_dir, mocker, capsys, monkeypatch):
"ci_addons", "anyci/publish_github_release",
"--prerelease-packages", "dist/*"
])
assert upload_prerelease.call_count == 0
assert upload_release.call_count == 0
assert excinfo.value.code == 1
assert upload_prerelease.call_count == 0
assert upload_release.call_count == 0
assert excinfo.value.code == 1

out, _ = capsys.readouterr()
assert "error: A token is expected." in out
Expand All @@ -194,9 +195,28 @@ def test_missing_token(src_dir, mocker, capsys, monkeypatch):
"--exit-success-if-missing-token",
"--prerelease-packages", "dist/*"
])
assert upload_prerelease.call_count == 0
assert upload_release.call_count == 0
assert excinfo.value.code == 0
assert upload_prerelease.call_count == 0
assert upload_release.call_count == 0
assert excinfo.value.code == 0

out, _ = capsys.readouterr()
assert "skipping: A token is expected." in out


@pytest.mark.parametrize("pgr_function", [
"get_tags",
"get_commit_date",
"get_commit_short_sha",
"get_commit_distance"
])
def test_missing_git_dir(pgr_function, tmpdir):
# Import addon to facilitate testing
sys.path.insert(0, os.path.join(ci_addons.home(), 'anyci'))

pgr = __import__('publish_github_release')

with push_dir(str(tmpdir)):
with pytest.raises(
RuntimeError,
match=r"Current directory is expected to contain a '.git' directory.*"):
getattr(pgr, pgr_function)()
20 changes: 20 additions & 0 deletions anyci/publish_github_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import subprocess
import textwrap


from functools import wraps

from github_release import (
gh_asset_delete,
gh_asset_upload,
Expand Down Expand Up @@ -73,6 +76,20 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,
return stdout, p.returncode


def git_dir_required(func):
"""This decorator raises :class:`RuntimeError` if the current
directory does not contain a ``.git`` subdirectory.
"""
@wraps(func)
def func_wrapper(*args, **kwargs):
if not os.path.exists(".git"):
raise RuntimeError(
"Current directory is expected to contain a '.git' directory: %s" % os.getcwd())
return func(*args, **kwargs)
return func_wrapper


@git_dir_required
def get_tags(ref="HEAD"):
"""If any, return all tags associated with `ref`.
Expand Down Expand Up @@ -106,6 +123,7 @@ def get_current_date():
return now.strftime("%Y-%m-%d %H:%m UTC")


@git_dir_required
def get_commit_date(ref="HEAD"):
# 2017-06-14 22:53:31 -0400
output, _ = run_command(
Expand All @@ -117,12 +135,14 @@ def get_commit_date(ref="HEAD"):
output, "%Y-%m-%d %H:%M:%S").strftime("%Y%m%d")


@git_dir_required
def get_commit_short_sha(ref="HEAD"):
output, _ = run_command(
GITS, ["rev-parse", "--short=7", str(ref)])
return output


@git_dir_required
def get_commit_distance(tag):
"""Return the distance to the given ``tag``. If ``tag`` is not found, it returns
the number of commits.
Expand Down

0 comments on commit d0176b0

Please sign in to comment.