Bug 1413928 - [tryselect] Add python unittest for templates
This makes use of pytest's generation feature. To add a new
template test, just add a new entry containing the input and
expected output to the dict in test_templates.py
MozReview-Commit-ID: 4qMefYHMjAp
--- a/taskcluster/ci/source-test/python.yml
+++ b/taskcluster/ci/source-test/python.yml
@@ -152,8 +152,18 @@ reftest-harness:
./mach python-test --subsuite reftest
when:
files-changed:
- 'layout/tools/reftest/**'
- 'testing/mozbase/moztest/moztest/selftest/**'
- 'testing/mozharness/mozharness/base/log.py'
- 'testing/mozharness/mozharness/mozilla/structuredlog.py'
- 'testing/mozharness/mozharness/mozilla/testing/errors.py'
+
+tryselect:
+ description: tools/tryselect unit tests
+ treeherder:
+ symbol: py(try)
+ run:
+ mach: python-test --subsuite try
+ when:
+ files-changed:
+ - 'tools/tryselect/**'
--- a/tools/moz.build
+++ b/tools/moz.build
@@ -48,8 +48,12 @@ with Files("update-packaging/**"):
SPHINX_TREES['lint'] = 'lint/docs'
SPHINX_TREES['compare-locales'] = 'compare-locales/docs'
SPHINX_TREES['try'] = 'tryselect/docs'
CRAMTEST_MANIFESTS += [
'tryselect/test/cram.ini',
]
+
+PYTHON_UNITTEST_MANIFESTS += [
+ 'tryselect/test/python.ini',
+]
new file mode 100644
--- /dev/null
+++ b/tools/tryselect/test/conftest.py
@@ -0,0 +1,17 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+from __future__ import absolute_import, print_function, unicode_literals
+
+
+def pytest_generate_tests(metafunc):
+ if all(fixture in metafunc.fixturenames for fixture in ('template', 'args', 'expected')):
+ def load_tests():
+ for template, tests in metafunc.module.TEMPLATE_TESTS.items():
+ for args, expected in tests:
+ yield (template, args, expected)
+
+ tests = list(load_tests())
+ ids = ['{} {}'.format(t[0], ' '.join(t[1])).strip() for t in tests]
+ metafunc.parametrize('template,args,expected', tests, ids=ids)
new file mode 100644
--- /dev/null
+++ b/tools/tryselect/test/python.ini
@@ -0,0 +1,4 @@
+[DEFAULT]
+subsuite=try
+
+[test_templates.py]
new file mode 100644
--- /dev/null
+++ b/tools/tryselect/test/test_templates.py
@@ -0,0 +1,51 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+from __future__ import absolute_import, print_function, unicode_literals
+
+import inspect
+from argparse import ArgumentParser
+
+import mozunit
+import pytest
+
+from tryselect.templates import all_templates
+
+
+# templates have a list of tests of the form (input, expected)
+TEMPLATE_TESTS = {
+ 'artifact': [
+ (['--no-artifact'], None),
+ (['--artifact'], {'enabled': '1'}),
+ ],
+ 'env': [
+ ([], None),
+ (['--env', 'foo=bar', '--env', 'num=10'], {'foo': 'bar', 'num': '10'}),
+ ],
+ 'rebuild': [
+ ([], None),
+ (['--rebuild', '10'], 10),
+ (['--rebuild', '1'], SystemExit),
+ (['--rebuild', '21'], SystemExit),
+ ],
+}
+
+
+def test_templates(template, args, expected):
+ parser = ArgumentParser()
+
+ t = all_templates[template]()
+ t.add_arguments(parser)
+
+ if inspect.isclass(expected) and issubclass(expected, BaseException):
+ with pytest.raises(expected):
+ args = parser.parse_args(args)
+ t.context(**vars(args))
+ else:
+ args = parser.parse_args(args)
+ assert t.context(**vars(args)) == expected
+
+
+if __name__ == '__main__':
+ mozunit.main()