Skip to content

Commit

Permalink
Add unit tests in the build-matrix-from-impacted script
Browse files Browse the repository at this point in the history
  • Loading branch information
nineinchnick authored and hashhar committed Mar 16, 2022
1 parent 1170bd9 commit c862130
Showing 1 changed file with 92 additions and 1 deletion.
93 changes: 92 additions & 1 deletion .github/bin/build-matrix-from-impacted.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env python3

import argparse
import itertools
import json
import logging
import sys
import tempfile
import unittest

import yaml

Expand Down Expand Up @@ -44,9 +45,19 @@ def main():
default=logging.WARNING,
help="Print info level logs",
)
parser.add_argument(
"-t",
"--test",
action='store_true',
help="test this script instead of executing it",
)

args = parser.parse_args()
logging.basicConfig(level=args.loglevel)
if args.test:
sys.argv = [sys.argv[0]]
unittest.main()
return
build(args.matrix, args.impacted, args.output)


Expand Down Expand Up @@ -90,5 +101,85 @@ def check_modules(modules, impacted):
return ",".join(modules)


class TestBuild(unittest.TestCase):
def test_build(self):
cases = [
# basic test
(
{
"modules": ["a", "b"],
},
["a"],
{
"modules": ["a"],
},
),
# include adds a new entry
(
{
"modules": ["a", "b"],
"include": [
{"modules": "c"},
{"modules": "d"},
],
},
["a", "d"],
{
"modules": ["a"],
"include": [
{"modules": "d"},
],
},
),
# include entry overwrites one in matrix
(
{
"modules": ["a", "b"],
"include": [
{"modules": "a", "options": "-Pprofile"},
],
},
["a"],
{
"modules": ["a"],
"include": [
{"modules": "a", "options": "-Pprofile"},
],
},
),
# with excludes
(
{
"modules": ["a", "b"],
"exclude": ["b"],
},
["a"],
{
"modules": ["a"],
"exclude": ["b"],
},
),
]
for matrix, impacted, expected in cases:
with self.subTest():
# given
matrix_file = tempfile.TemporaryFile("w+")
yaml.dump(matrix, matrix_file)
matrix_file.seek(0)
impacted_file = tempfile.TemporaryFile("w+")
impacted_file.write("\n".join(impacted))
impacted_file.seek(0)
output_file = tempfile.TemporaryFile("w+")
# when
build(matrix_file, impacted_file, output_file)
output_file.seek(0)
output = json.load(output_file)
# then
self.assertEqual(output, expected)
matrix_file.close()
impacted_file.close()
output_file.close()


if __name__ == "__main__":
main()

0 comments on commit c862130

Please sign in to comment.