Bug 1413928 - [tryselect] Add python unittest for templates r=davehunt
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Tue, 21 Nov 2017 10:11:00 -0500
changeset 451539 7e910ce230d09b7380de3c8e5552e435cae14166
parent 451538 7ee4038b46285cd8111479ffef1313f0ee5bd8f4
child 451540 0c677fb018bee767363215edb0b6d0cb4ffb8111
push id8558
push userryanvm@gmail.com
push dateFri, 19 Jan 2018 15:34:56 +0000
treeherdermozilla-beta@ed0ef688fa64 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdavehunt
bugs1413928
milestone59.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1413928 - [tryselect] Add python unittest for templates r=davehunt 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
taskcluster/ci/source-test/python.yml
tools/moz.build
tools/tryselect/test/conftest.py
tools/tryselect/test/python.ini
tools/tryselect/test/test_templates.py
--- 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()