From 398ad5a789147e09ee498121eb2d5e404b1481b5 Mon Sep 17 00:00:00 2001 From: Daniel Lenski Date: Sat, 18 Dec 2021 17:40:43 -0800 Subject: [PATCH] Switch from nose (unmaintained) to nose2, use parametrized tests, fix flake8 nits --- .github/workflows/test_and_release.yml | 4 ++-- requirements-test.txt | 2 +- setup.py | 2 +- test/test_all.py | 20 ++++++++++---------- zxing/__init__.py | 6 +++--- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/.github/workflows/test_and_release.yml b/.github/workflows/test_and_release.yml index f162651..a049fde 100644 --- a/.github/workflows/test_and_release.yml +++ b/.github/workflows/test_and_release.yml @@ -52,10 +52,10 @@ jobs: run: | python -m pip install --upgrade pip if [ -f requirements-test.txt ]; then pip install -r requirements-test.txt; fi - sudo apt install -qq openjdk-11-jre # JDK is needed to run tests + sudo apt update && sudo apt install -qq openjdk-11-jre # JDK is needed to run tests - name: Test run: | - python setup.py test + nose2 -v --pretty-assert # https://github.com/actions/starter-workflows/blob/main/ci/python-publish.yml release: diff --git a/requirements-test.txt b/requirements-test.txt index e56972b..924f917 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -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' diff --git a/setup.py b/setup.py index a6c5113..4039973 100644 --- a/setup.py +++ b/setup.py @@ -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', diff --git a/test/test_all.py b/test/test_all.py index dfa6415..06d1b64 100644 --- a/test/test_all.py +++ b/test/test_all.py @@ -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 @@ -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')) diff --git a/zxing/__init__.py b/zxing/__init__.py index e883fb2..101ed9c 100644 --- a/zxing/__init__.py +++ b/zxing/__init__.py @@ -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 @@ -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)':