Skip to content

Commit

Permalink
Switch from nose (unmaintained) to nose2, use parametrized tests, fix…
Browse files Browse the repository at this point in the history
… flake8 nits
  • Loading branch information
dlenski committed Dec 19, 2021
1 parent 81f55d3 commit 027c610
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/test_and_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ jobs:
run: |
python -m pip install --upgrade pip
if [ -f requirements-test.txt ]; then pip install -r requirements-test.txt; fi
sudo apt update
sudo apt install -qq openjdk-11-jre # JDK is needed to run tests
- name: Test
run: |
python setup.py test
python setup.py build
nose2 -v --pretty-assert
# https://github.com/actions/starter-workflows/blob/main/ci/python-publish.yml
release:
Expand Down
2 changes: 1 addition & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
nose>=1.0
nose2>=0.10

pillow>=3.0,<6.0; python_version < '3.5'
pillow>=3.0,<8.0; python_version >= '3.5' and python_version < '3.6'
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def download_java_files(force=False):
},
install_requires=open('requirements.txt').readlines(),
tests_require=open('requirements-test.txt').readlines(),
test_suite='nose.collector',
test_suite='nose2.collector.collector',
license='LGPL v3 or later',
classifiers=[
'Development Status :: 4 - Beta',
Expand Down
20 changes: 10 additions & 10 deletions test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

from PIL import Image

from nose import with_setup
from nose.tools import raises
from nose2.tools.decorators import with_setup
from nose2.tools.such import helper

import zxing

Expand Down Expand Up @@ -119,27 +119,27 @@ def test_wrong_formats():
for filename, expected_format, expected_raw in test_barcodes)


@raises(zxing.BarCodeReaderException)
def test_bad_java():
test_reader = zxing.BarCodeReader(java=os.devnull)
test_reader.decode(test_barcodes[0][0])
with helper.assertRaises(zxing.BarCodeReaderException):
test_reader.decode(test_barcodes[0][0])


@raises(zxing.BarCodeReaderException)
def test_bad_classpath():
test_reader = zxing.BarCodeReader(classpath=mkdtemp())
test_reader.decode(test_barcodes[0][0])
with helper.assertRaises(zxing.BarCodeReaderException):
test_reader.decode(test_barcodes[0][0])


@raises(zxing.BarCodeReaderException)
@with_setup(setup_reader)
def test_nonexistent_file_error():
global test_reader
test_reader.decode(os.path.join(test_barcode_dir, 'nonexistent.png'))
with helper.assertRaises(zxing.BarCodeReaderException):
test_reader.decode(os.path.join(test_barcode_dir, 'nonexistent.png'))


@raises(zxing.BarCodeReaderException)
@with_setup(setup_reader)
def test_bad_file_format_error():
global test_reader
test_reader.decode(os.path.join(test_barcode_dir, 'bad_format.png'))
with helper.assertRaises(zxing.BarCodeReaderException):
test_reader.decode(os.path.join(test_barcode_dir, 'bad_format.png'))
6 changes: 3 additions & 3 deletions zxing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def file_uri_to_path(s):
raise ValueError(uri)
return urllib.parse.unquote_plus(uri.path)


class BarCodeReaderException(Exception):
def __init__(self, message, filename=None):
self.message, self.filename = message, filename
Expand Down Expand Up @@ -104,9 +103,10 @@ def decode(self, filenames, try_harder=False, possible_formats=None, pure_barcod
b'Exception in thread "main" java.lang.NoClassDefFoundError:')):
raise BarCodeReaderException("Java JARs not found in classpath (%s)" % self.classpath, self.classpath)
elif stdout.startswith((b'''Exception in thread "main" javax.imageio.IIOException: Can't get input stream from URL!''',
b'''Exception in thread "main" java.util.concurrent.ExecutionException: javax.imageio.IIOException: Can't get input stream from URL!''')):
b'''Exception in thread "main" java.util.concurrent.ExecutionException: javax.imageio.IIOException: Can't get input stream from URL!''')): # noqa: E501
# Find the line that looks like: "Caused by: java.io.FileNotFoundException: $FILENAME ({No such file or directory,Permission denied,*)"
fn, err = next((map(bytes.decode, l[42:].rsplit(b' (', 1)) for l in stdout.splitlines() if l.startswith(b"Caused by: java.io.FileNotFoundException: ")), ('', ''))
fn, err = next((map(bytes.decode, l[42:].rsplit(b' (', 1)) for l in stdout.splitlines()
if l.startswith(b"Caused by: java.io.FileNotFoundException: ")), ('', ''))
if err == 'No such file or directory)':
err = FileNotFoundError(fn)
elif err == 'Permission denied)':
Expand Down

0 comments on commit 027c610

Please sign in to comment.