Skip to content

Commit 7914fe3

Browse files
committed
Better error message for borked JAVA_HOME environment variable
And corresponding test
1 parent c707794 commit 7914fe3

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

test/test_all.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ def test_bad_classpath():
162162
test_reader = zxing.BarCodeReader(classpath=mkdtemp())
163163

164164

165+
def test_wrong_JAVA_HOME():
166+
saved_JAVA_HOME = os.environ.get('JAVA_HOME')
167+
try:
168+
del os.environ['JAVA_HOME']
169+
test_reader = zxing.BarCodeReader()
170+
with helper.assertRaises(zxing.BarCodeReaderException):
171+
test_reader.decode(test_barcodes[0][0])
172+
finally:
173+
os.environ['JAVA_HOME'] = saved_JAVA_HOME
174+
175+
165176
@with_setup(setup_reader)
166177
def test_nonexistent_file_error():
167178
global test_reader

zxing/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ def decode(self, filenames, try_harder=False, possible_formats=None, pure_barcod
140140
raise BarCodeReaderException("Java library could not read image (is it in a supported format?)", fn)
141141
elif stdout.startswith(b'''Exception '''):
142142
raise BarCodeReaderException("Unknown Java exception", self.java) from sp.CalledProcessError(0, cmd, stdout)
143+
elif stdout.startswith(b'''The operation couldn't be completed. Unable to locate a Java Runtime.'''):
144+
raise BarCodeReaderException("Unable to locate Java runtime (check JAVA_HOME variable and other configuration)", self.java) from sp.CalledProcessError(p.returncode, cmd, stdout)
143145
elif p.returncode:
144146
raise BarCodeReaderException("Unexpected Java subprocess return code", self.java) from sp.CalledProcessError(p.returncode, cmd, stdout)
145147

0 commit comments

Comments
 (0)