Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions scripts/aggregate_matrix_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import sys

PASSING_STATUS = 'success'
FAILING_STATUS = 'failure'
KNOWN_STATUSES = (PASSING_STATUS, FAILING_STATUS)


def split_os_arch(name):
Expand All @@ -18,6 +20,10 @@ def read_marker(path):
with open(path) as handle:
text = handle.read().strip()
name, _, status = text.partition(' ')
if not name or not status:
raise ValueError(f'malformed matrix marker: {path}')
if status not in KNOWN_STATUSES:
raise ValueError(f'unknown matrix status in {path}: {status}')
return name, status


Expand Down
17 changes: 14 additions & 3 deletions scripts/matrix_badge_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

GITHUB_PASSING_GREEN = '#28a745'
GITHUB_FAILING_RED = '#d73a49'
PASSING_TOKENS = ('true', 'success', 'passing', '1')
FAILING_TOKENS = ('false', 'failure', 'failing', '0')

NAMED_LOGO_BY_OS = {
'ubuntu': 'ubuntu',
Expand Down Expand Up @@ -35,15 +37,24 @@ def badge_json(os_name, arch, passed):


def parse_passed(token):
return token.strip().lower() in ('true', 'success', 'passing', '1')
normalized = token.strip().lower()
if normalized in PASSING_TOKENS:
return True
if normalized in FAILING_TOKENS:
return False
raise ValueError(f'unknown matrix status: {token}')


def main(argv):
if len(argv) != 4:
print('error: usage: matrix_badge_json.py <os> <arch> <passed:true|false>', file=sys.stderr)
return 2
print(badge_json(argv[1], argv[2], parse_passed(argv[3])))
return 0
try:
print(badge_json(argv[1], argv[2], parse_passed(argv[3])))
return 0
except ValueError as exc:
print(f'error: {exc}', file=sys.stderr)
return 2


if __name__ == '__main__':
Expand Down
13 changes: 13 additions & 0 deletions test/aggregate_matrix_status_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ def test_empty_directory_yields_empty_array(self):
with tempfile.TemporaryDirectory() as directory:
self.assertEqual(aggregate_matrix_status.aggregate(directory), '[]')

def test_unknown_status_fails_loudly(self):
with tempfile.TemporaryDirectory() as directory:
self._write(directory, 'ubuntu-amd64', 'cancelled')
with self.assertRaises(ValueError):
aggregate_matrix_status.aggregate(directory)

def test_malformed_marker_fails_loudly(self):
with tempfile.TemporaryDirectory() as directory:
with open(os.path.join(directory, 'ubuntu-amd64.txt'), 'w') as handle:
handle.write('ubuntu-amd64\n')
with self.assertRaises(ValueError):
aggregate_matrix_status.aggregate(directory)


if __name__ == '__main__':
unittest.main()
7 changes: 7 additions & 0 deletions test/matrix_badge_json_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ def test_unknown_os_falls_back_to_os_name_as_logo(self):
result = json.loads(matrix_badge_json.badge_json('freebsd', 'amd64', True))
self.assertEqual(result['namedLogo'], 'freebsd')

def test_parse_passed_rejects_unknown_status(self):
with self.assertRaises(ValueError):
matrix_badge_json.parse_passed('cancelled')

def test_main_returns_error_for_unknown_status(self):
self.assertEqual(matrix_badge_json.main(['matrix_badge_json.py', 'ubuntu', 'amd64', 'cancelled']), 2)


if __name__ == '__main__':
unittest.main()