Skip to content

Commit 2877637

Browse files
committed
Test Java version parsing
1 parent 0df2bdf commit 2877637

1 file changed

Lines changed: 29 additions & 8 deletions

File tree

‎download_lt.py‎

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,34 @@
3030
LATEST_VERSION = '2.7'
3131

3232

33+
def parse_java_version(version_text):
34+
"""Return Java version (major1, major2).
35+
36+
>>> parse_java_version('''java version "1.6.0_65"
37+
... Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
38+
... Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode))
39+
... ''')
40+
(1, 6)
41+
42+
>>> parse_java_version('''
43+
... openjdk version "1.8.0_60"
44+
... OpenJDK Runtime Environment (build 1.8.0_60-b27)
45+
... OpenJDK 64-Bit Server VM (build 25.60-b23, mixed mode))
46+
... ''')
47+
(1, 8)
48+
49+
"""
50+
match = re.search(
51+
r'^(?:java|openjdk) version "(?P<major1>\d+)\.(?P<major2>\d+)\.[^"]+"$',
52+
version_text,
53+
re.MULTILINE)
54+
if not match:
55+
raise SystemExit(
56+
'Could not parse Java version from """{}""".'.format(version_text))
57+
58+
return (int(match.group('major1')), int(match.group('major2')))
59+
60+
3361
def get_newest_possible_languagetool_version():
3462
"""Return newest compatible version.
3563
@@ -49,15 +77,8 @@ def get_newest_possible_languagetool_version():
4977
stderr=subprocess.STDOUT,
5078
universal_newlines=True)
5179

52-
match = re.search(
53-
r'^(?:java|openjdk) version "(?P<major1>\d+)\.(?P<major2>\d+)\.[^"]+"$',
54-
output,
55-
re.MULTILINE)
56-
if not match:
57-
raise SystemExit(
58-
'Could not parse Java version from """{}""".'.format(output))
80+
java_version = parse_java_version(output)
5981

60-
java_version = (int(match.group('major1')), int(match.group('major2')))
6182
if java_version >= (1, 7):
6283
return LATEST_VERSION
6384
elif java_version >= (1, 6):

0 commit comments

Comments
 (0)