Skip to content

Commit

Permalink
Merge pull request #4 from byteskeptical/pubpy
Browse files Browse the repository at this point in the history
Publish Package
  • Loading branch information
byteskeptical committed Jul 23, 2022
2 parents 5a59bcb + bde56dc commit 96fc9e9
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 92 deletions.
14 changes: 9 additions & 5 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v3
with:
Expand All @@ -23,16 +26,17 @@ jobs:
python -m pip install --upgrade pip
pip install build
- name: Build package
run: python -m build --sdist --wheel --outdir /dist .
run: python -m build --sdist --wheel .
- name: Publish distribution to Test PyPI
uses: pypa/gh-action-pypi-publish@root
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
print_hash: true
repository_url: https://test.pypi.org/legacy/
user: __token__
- name: Publish distribution to PyPI
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@root
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}
print_hash: true
user: __token__
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Test

on:
push:
branches: [ master ]
branches: [ root ]
pull_request:
branches: [ master ]
branches: [ root ]

jobs:
build:
Expand All @@ -31,10 +31,10 @@ jobs:
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=80 --statistics
- name: Install retry
run: |
python -m pip install -e .
- name: Test with pytest
run: |
pytest
pytest -l -v --tb=long
168 changes: 85 additions & 83 deletions tests/test_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from logging import DEBUG, getLogger, StreamHandler
from retry import retry
from unittest import main, TestCase


class RetryableError(Exception):
Expand All @@ -17,111 +16,114 @@ class UnexpectedError(Exception):
pass


class RetryTestCase(TestCase):
@pytest.fixture(autouse=True)
def inject_fixtures(self, caplog):
self._caplog = caplog
def test_no_retry_required():
counter = 0

def test_no_retry_required(self):
self.counter = 0
@retry(RetryableError, tries=4, delay=0.1)
def succeeds():
nonlocal counter
counter += 1
return 'success'

@retry(RetryableError, tries=4, delay=0.1)
def succeeds():
self.counter += 1
return 'success'
r = succeeds()

r = succeeds()
assert r == 'success'
assert counter == 1

assert r == 'success'
assert self.counter == 1

def test_retries_once(self):
self.counter = 0
def test_retries_once():
counter = 0

@retry(RetryableError, tries=4, delay=0.1)
def fails_once():
self.counter += 1
if self.counter < 2:
raise RetryableError('failed')
else:
return 'success'
@retry(RetryableError, tries=4, delay=0.1)
def fails_once():
nonlocal counter
counter += 1
if counter < 2:
raise RetryableError('failed')
else:
return 'success'

r = fails_once()
r = fails_once()

assert r == 'success'
assert self.counter == 2
assert r == 'success'
assert counter == 2

def test_limit_is_reached(self):
self.counter = 0

@retry(RetryableError, tries=4, delay=0.1)
def always_fails():
self.counter += 1
raise RetryableError('failed')
def test_limit_is_reached():
counter = 0

with pytest.raises(RetryableError, match='failed'):
always_fails()
@retry(RetryableError, tries=4, delay=0.1)
def always_fails():
nonlocal counter
counter += 1
raise RetryableError('failed')

assert self.counter == 4
with pytest.raises(RetryableError, match='failed'):
always_fails()

def test_multiple_exception_types(self):
self.counter = 0
assert counter == 4

@retry((RetryableError, AnotherRetryableError), tries=4, delay=0.1)
def raise_multiple_exceptions():
self.counter += 1
if self.counter == 1:
raise RetryableError('a retryable error')
elif self.counter == 2:
raise AnotherRetryableError('another retryable error')
else:
return 'success'

r = raise_multiple_exceptions()
def test_multiple_exception_types():
counter = 0

assert r == 'success'
assert self.counter == 3
@retry((RetryableError, AnotherRetryableError), tries=4, delay=0.1)
def raise_multiple_exceptions():
nonlocal counter
counter += 1
if counter == 1:
raise RetryableError('a retryable error')
elif counter == 2:
raise AnotherRetryableError('another retryable error')
else:
return 'success'

def test_unexpected_exception_does_not_retry(self):
r = raise_multiple_exceptions()

@retry(RetryableError, tries=4, delay=0.1)
def raise_unexpected_error():
raise UnexpectedError('unexpected error')
assert r == 'success'
assert counter == 3

with pytest.raises(UnexpectedError, match='unexpected error'):
raise_unexpected_error()

def test_using_a_logger(self):
expected = {'DEBUG': 'success',
'ERROR': 'failed',
'WARNING': ('Retry (4/4):\nfailed\nRetrying in 0.1 '
'second(s)...')}
records = {}
self.counter = 0
def test_unexpected_exception_does_not_retry():

sh = StreamHandler()
log = getLogger(__name__)
log.addHandler(sh)
@retry(RetryableError, tries=4, delay=0.1)
def raise_unexpected_error():
raise UnexpectedError('unexpected error')

@retry(RetryableError, tries=4, delay=0.1, logger=log)
def fails_once():
self.counter += 1
if self.counter < 2:
log.error('failed')
raise RetryableError('failed')
else:
log.debug('success')
for record in self._caplog.records:
records[record.levelname] = record.message
return 'success'
with pytest.raises(UnexpectedError, match='unexpected error'):
raise_unexpected_error()

with self._caplog.at_level(DEBUG):
r = fails_once()

assert r == 'success'
assert self.counter == 2
assert expected == records
@pytest.fixture(autouse=True)
def test_using_a_logger(caplog):
_caplog = caplog
counter = 0
expected = {'DEBUG': 'success',
'ERROR': 'failed',
'WARNING': ('Retry (4/4):\nfailed\nRetrying in 0.1 '
'second(s)...')}
records = {}

sh = StreamHandler()
log = getLogger(__name__)
log.addHandler(sh)

@retry(RetryableError, tries=4, delay=0.1, logger=log)
def fails_once():
nonlocal counter
counter += 1
if counter < 2:
log.error('failed')
raise RetryableError('failed')
else:
log.debug('success')
for record in _caplog.records:
records[record.levelname] = record.message
return 'success'

with _caplog.at_level(DEBUG):
r = fails_once()

if __name__ == '__main__':
main()
assert r == 'success'
assert counter == 2
assert expected == records

0 comments on commit 96fc9e9

Please sign in to comment.