Skip to content

Commit 9368fda

Browse files
committed
fix: use regex for go versions
1 parent 2d7e896 commit 9368fda

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

aws_lambda_builders/workflows/go_modules/validator.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import logging
6+
import re
67
import os
78
import subprocess
89

@@ -12,9 +13,9 @@
1213

1314

1415
class GoRuntimeValidator(object):
15-
1616
LANGUAGE = "go"
1717
SUPPORTED_RUNTIMES = {"go1.x"}
18+
GO_VERSION_REGEX = re.compile("go(\\d)\\.(x|\\d+)")
1819

1920
def __init__(self, runtime):
2021
self.runtime = runtime
@@ -29,13 +30,13 @@ def has_runtime(self):
2930
return self.runtime in self.SUPPORTED_RUNTIMES
3031

3132
@staticmethod
32-
def sanitize_go_version_part(version_part):
33-
sanitized_version_part = ""
34-
for char in version_part:
35-
if char.isalpha():
36-
break
37-
sanitized_version_part += char
38-
return sanitized_version_part
33+
def get_go_versions(version_string):
34+
parts = GoRuntimeValidator.GO_VERSION_REGEX.findall(version_string)
35+
try:
36+
# NOTE(sriram-mv): The version parts need to be a list with a major and minor version.
37+
return int(parts[0][0]), int(parts[0][1])
38+
except IndexError:
39+
return 0, 0
3940

4041
def validate(self, runtime_path):
4142
"""
@@ -51,19 +52,13 @@ def validate(self, runtime_path):
5152
min_expected_minor_version = 11 if expected_major_version == 1 else 0
5253

5354
p = subprocess.Popen([runtime_path, "version"], cwd=os.getcwd(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
54-
out, _ = p.communicate()
55+
version_string, _ = p.communicate()
5556

5657
if p.returncode == 0:
57-
out_parts = out.decode().split()
58-
if len(out_parts) >= 3:
59-
version_parts = [
60-
int(GoRuntimeValidator.sanitize_go_version_part(version_part))
61-
for version_part in out_parts[2].replace(self.LANGUAGE, "").split(".")
62-
]
63-
if len(version_parts) >= 2:
64-
if version_parts[0] == expected_major_version and version_parts[1] >= min_expected_minor_version:
65-
self._valid_runtime_path = runtime_path
66-
return self._valid_runtime_path
58+
major_version, minor_version = GoRuntimeValidator.get_go_versions(version_string.decode())
59+
if major_version == expected_major_version and minor_version >= min_expected_minor_version:
60+
self._valid_runtime_path = runtime_path
61+
return self._valid_runtime_path
6762

6863
# otherwise, raise mismatch exception
6964
raise MisMatchRuntimeError(language=self.LANGUAGE, required_runtime=self.runtime, runtime_path=runtime_path)

tests/unit/workflows/go_modules/test_validator.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def __init__(self, returncode, out=b"", err=b""):
1414
self.err = err
1515

1616
def communicate(self):
17-
return (self.out, self.err)
17+
return self.out, self.err
1818

1919

2020
class TestGoRuntimeValidator(TestCase):
@@ -30,38 +30,50 @@ def test_runtime_validate_unsupported_language_fail_open(self):
3030
validator = GoRuntimeValidator(runtime="go2.x")
3131
validator.validate(runtime_path="/usr/bin/go2")
3232

33+
@parameterized.expand(
34+
[
35+
("go1.11.2", (1, 11)),
36+
("go1.11rc.2", (1, 11)),
37+
("go1.16beta1", (1, 16)),
38+
("go%$", (0, 0)),
39+
("unknown", (0, 0)),
40+
]
41+
)
42+
def test_get_go_versions(self, version_string, version_parts):
43+
self.assertEqual(self.validator.get_go_versions(version_string), version_parts)
44+
3345
@parameterized.expand(
3446
[(b"go version go1.11.2 test",), (b"go version go1.11rc.2 test",), (b"go version go1.16beta1 test",)]
3547
)
3648
def test_runtime_validate_supported_version_runtime(self, go_version_output):
3749
with mock.patch("subprocess.Popen") as mock_subprocess:
3850
mock_subprocess.return_value = MockSubProcess(0, out=go_version_output)
3951
self.validator.validate(runtime_path="/usr/bin/go")
40-
self.assertTrue(mock_subprocess.call_count, 1)
52+
self.assertEqual(mock_subprocess.call_count, 1)
4153

4254
def test_runtime_validate_supported_higher_than_min_version_runtime(self):
4355
with mock.patch("subprocess.Popen") as mock_subprocess:
4456
mock_subprocess.return_value = MockSubProcess(0, out=b"go version go1.12 test")
4557
self.validator.validate(runtime_path="/usr/bin/go")
46-
self.assertTrue(mock_subprocess.call_count, 1)
58+
self.assertEqual(mock_subprocess.call_count, 1)
4759

4860
def test_runtime_validate_mismatch_nonzero_exit(self):
4961
with mock.patch("subprocess.Popen") as mock_subprocess:
5062
mock_subprocess.return_value = MockSubProcess(1)
5163
with self.assertRaises(MisMatchRuntimeError):
5264
self.validator.validate(runtime_path="/usr/bin/go")
53-
self.assertTrue(mock_subprocess.call_count, 1)
65+
self.assertEqual(mock_subprocess.call_count, 1)
5466

5567
def test_runtime_validate_mismatch_invalid_version(self):
5668
with mock.patch("subprocess.Popen") as mock_subprocess:
5769
mock_subprocess.return_value = MockSubProcess(0, out=b"go version")
5870
with self.assertRaises(MisMatchRuntimeError):
5971
self.validator.validate(runtime_path="/usr/bin/go")
60-
self.assertTrue(mock_subprocess.call_count, 1)
72+
self.assertEqual(mock_subprocess.call_count, 1)
6173

6274
def test_runtime_validate_mismatch_minor_version(self):
6375
with mock.patch("subprocess.Popen") as mock_subprocess:
6476
mock_subprocess.return_value = MockSubProcess(0, out=b"go version go1.10.2 test")
6577
with self.assertRaises(MisMatchRuntimeError):
6678
self.validator.validate(runtime_path="/usr/bin/go")
67-
self.assertTrue(mock_subprocess.call_count, 1)
79+
self.assertEqual(mock_subprocess.call_count, 1)

0 commit comments

Comments
 (0)