Bug 1583353 - [moztest] Convert test_resolve.py to the pytest format, r=egao
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Thu, 31 Oct 2019 14:43:56 +0000
changeset 500242 f6d096dd754d0df5490193da0414c747dab13aec
parent 500241 bca7369cc0d346bc1a3ba57e63ff5832f4d5a218
child 500243 c7b6e1aef3a054068e1f4bf24a7cb97181ce7abc
push id99475
push userahalberstadt@mozilla.com
push dateFri, 01 Nov 2019 20:35:12 +0000
treeherderautoland@c7b6e1aef3a0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersegao
bugs1583353
milestone72.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 1583353 - [moztest] Convert test_resolve.py to the pytest format, r=egao Depends on D51174 Differential Revision: https://phabricator.services.mozilla.com/D51175
testing/mozbase/moztest/tests/test_resolve.py
--- a/testing/mozbase/moztest/tests/test_resolve.py
+++ b/testing/mozbase/moztest/tests/test_resolve.py
@@ -5,18 +5,18 @@
 
 from __future__ import absolute_import, print_function, unicode_literals
 
 import cPickle as pickle
 import os
 import re
 import shutil
 import tempfile
-import unittest
 
+import pytest
 import mozpack.path as mozpath
 import mozunit
 from mozbuild.base import MozbuildObject
 from mozfile import NamedTemporaryFile
 
 from moztest.resolve import (
     TestResolver,
     TEST_SUITES,
@@ -192,246 +192,217 @@ TASK_LABELS = [
     'test-linux64/opt-web-platform-tests-1',
     'test-linux64/opt-web-platform-test-e10s-1',
     'test-linux64/opt-xpcshell',
     'test-linux64/opt-xpcshell-1',
     'test-linux64/opt-xpcshell-2',
 ]
 
 
-class TestTestResolver(unittest.TestCase):
-    FAKE_TOPSRCDIR = '/firefox'
+@pytest.fixture
+def resolver(tmpdir):
+    topobjdir = tmpdir.mkdir("objdir").strpath
 
-    def setUp(self):
-        self._temp_files = []
-        self._temp_dirs = []
+    with open(os.path.join(topobjdir, 'all-tests.pkl'), 'wb') as fh:
+        pickle.dump(ALL_TESTS, fh)
+    with open(os.path.join(topobjdir, 'test-defaults.pkl'), 'wb') as fh:
+        pickle.dump(TEST_DEFAULTS, fh)
 
-    def tearDown(self):
-        for f in self._temp_files:
-            del f
+    o = MozbuildObject('/firefox', None, None, topobjdir=topobjdir)
 
-        for d in self._temp_dirs:
-            shutil.rmtree(d)
-
-        self._temp_files = []
-        self._temp_dirs = []
+    # Monkey patch the test resolver to avoid tests failing to find make
+    # due to our fake topscrdir.
+    TestResolver._run_make = lambda *a, **b: None
+    resolver = o._spawn(TestResolver)
+    resolver._puppeteer_loaded = True
+    resolver._wpt_loaded = True
+    return resolver
 
-    def get_resolver(self):
-        topobjdir = tempfile.mkdtemp()
-        self._temp_dirs.append(topobjdir)
 
-        with open(os.path.join(topobjdir, 'all-tests.pkl'), 'wb') as fh:
-            pickle.dump(ALL_TESTS, fh)
-        with open(os.path.join(topobjdir, 'test-defaults.pkl'), 'wb') as fh:
-            pickle.dump(TEST_DEFAULTS, fh)
+def test_load(resolver):
+    assert len(resolver.tests_by_path) == 8
 
-        o = MozbuildObject(self.FAKE_TOPSRCDIR, None, None, topobjdir=topobjdir)
+    assert len(resolver.tests_by_flavor['xpcshell']) == 3
+    assert len(resolver.tests_by_flavor['mochitest-plain']) == 0
+
 
-        # Monkey patch the test resolver to avoid tests failing to find make
-        # due to our fake topscrdir.
-        TestResolver._run_make = lambda *a, **b: None
-        resolver = o._spawn(TestResolver)
-        resolver._puppeteer_loaded = True
-        resolver._wpt_loaded = True
-        return resolver
+def test_resolve_all(resolver):
+    assert len(list(resolver._resolve())) == 9
+
 
-    def test_load(self):
-        r = self.get_resolver()
-        self.assertEqual(len(r.tests_by_path), 8)
+def test_resolve_filter_flavor(resolver):
+    assert len(list(resolver._resolve(flavor='xpcshell'))) == 4
+
 
-        self.assertEqual(len(r.tests_by_flavor['xpcshell']), 3)
-        self.assertEqual(len(r.tests_by_flavor['mochitest-plain']), 0)
+def test_resolve_by_dir(resolver):
+    assert len(list(resolver._resolve(paths=['services/common']))) == 2
 
-    def test_resolve_all(self):
-        r = self.get_resolver()
-        self.assertEqual(len(list(r._resolve())), 9)
 
-    def test_resolve_filter_flavor(self):
-        r = self.get_resolver()
-        self.assertEqual(len(list(r._resolve(flavor='xpcshell'))), 4)
+def test_resolve_under_path(resolver):
+    assert len(list(resolver._resolve(under_path='services'))) == 2
+    assert len(list(resolver._resolve(flavor='xpcshell', under_path='services'))) == 2
+
 
-    def test_resolve_by_dir(self):
-        r = self.get_resolver()
-        self.assertEqual(len(list(r._resolve(paths=['services/common']))), 2)
+def test_resolve_multiple_paths(resolver):
+    result = list(resolver.resolve_tests(paths=['services', 'toolkit']))
+    assert len(result) == 4
+
 
-    def test_resolve_under_path(self):
-        r = self.get_resolver()
-        self.assertEqual(len(list(r._resolve(under_path='services'))), 2)
+def test_resolve_support_files(resolver):
+    expected_support_files = "\ndata/**\nxpcshell_updater.ini"
+    result = list(resolver.resolve_tests(paths=['toolkit']))
+    assert len(result) == 2
 
-        self.assertEqual(len(list(r._resolve(flavor='xpcshell',
-            under_path='services'))), 2)
+    for test in result:
+        assert test['support-files'] == expected_support_files
 
-    def test_resolve_multiple_paths(self):
-        r = self.get_resolver()
-        result = list(r.resolve_tests(paths=['services', 'toolkit']))
-        self.assertEqual(len(result), 4)
 
-    def test_resolve_support_files(self):
-        expected_support_files = "\ndata/**\nxpcshell_updater.ini"
-        r = self.get_resolver()
-        result = list(r.resolve_tests(paths=['toolkit']))
-        self.assertEqual(len(result), 2)
+def test_resolve_path_prefix(resolver):
+    result = list(resolver._resolve(paths=['image']))
+    assert len(result) == 1
+
 
-        for test in result:
-            self.assertEqual(test['support-files'],
-                             expected_support_files)
+def test_cwd_children_only(resolver):
+    """If cwd is defined, only resolve tests under the specified cwd."""
+    # Pretend we're under '/services' and ask for 'common'. This should
+    # pick up all tests from '/services/common'
+    tests = list(resolver.resolve_tests(paths=['common'], cwd=os.path.join(resolver.topsrcdir,
+        'services')))
 
-    def test_resolve_path_prefix(self):
-        r = self.get_resolver()
-        result = list(r._resolve(paths=['image']))
-        self.assertEqual(len(result), 1)
+    assert len(tests) == 2
 
-    def test_cwd_children_only(self):
-        """If cwd is defined, only resolve tests under the specified cwd."""
-        r = self.get_resolver()
+    # Tests should be rewritten to objdir.
+    for t in tests:
+        assert t['here'] == mozpath.join(resolver.topobjdir,
+                                         '_tests/xpcshell/services/common/tests/unit')
 
-        # Pretend we're under '/services' and ask for 'common'. This should
-        # pick up all tests from '/services/common'
-        tests = list(r.resolve_tests(paths=['common'], cwd=os.path.join(r.topsrcdir,
-            'services')))
+def test_various_cwd(resolver):
+    """Test various cwd conditions are all equal."""
+    expected = list(resolver.resolve_tests(paths=['services']))
+    actual = list(resolver.resolve_tests(paths=['services'], cwd='/'))
+    assert actual == expected
 
-        self.assertEqual(len(tests), 2)
-
-        # Tests should be rewritten to objdir.
-        for t in tests:
-            self.assertEqual(t['here'], mozpath.join(r.topobjdir,
-                '_tests/xpcshell/services/common/tests/unit'))
+    actual = list(resolver.resolve_tests(paths=['services'], cwd=resolver.topsrcdir))
+    assert actual == expected
 
-    def test_various_cwd(self):
-        """Test various cwd conditions are all equal."""
+    actual = list(resolver.resolve_tests(paths=['services'], cwd=resolver.topobjdir))
+    assert actual == expected
 
-        r = self.get_resolver()
-
-        expected = list(r.resolve_tests(paths=['services']))
-        actual = list(r.resolve_tests(paths=['services'], cwd='/'))
-        self.assertEqual(actual, expected)
 
-        actual = list(r.resolve_tests(paths=['services'], cwd=r.topsrcdir))
-        self.assertEqual(actual, expected)
+def test_subsuites(resolver):
+    """Test filtering by subsuite."""
+    tests = list(resolver.resolve_tests(paths=['mobile']))
+    assert len(tests) == 2
 
-        actual = list(r.resolve_tests(paths=['services'], cwd=r.topobjdir))
-        self.assertEqual(actual, expected)
+    tests = list(resolver.resolve_tests(paths=['mobile'], subsuite='browser'))
+    assert len(tests) == 1
+    assert tests[0]['name'] == 'src/TestDistribution.java'
 
-    def test_subsuites(self):
-        """Test filtering by subsuite."""
+    tests = list(resolver.resolve_tests(paths=['mobile'], subsuite='background'))
+    assert len(tests) == 1
+    assert tests[0]['name'] == 'src/common/TestAndroidLogWriters.java'
 
-        r = self.get_resolver()
-
-        tests = list(r.resolve_tests(paths=['mobile']))
-        self.assertEqual(len(tests), 2)
-
-        tests = list(r.resolve_tests(paths=['mobile'], subsuite='browser'))
-        self.assertEqual(len(tests), 1)
-        self.assertEqual(tests[0]['name'], 'src/TestDistribution.java')
 
-        tests = list(r.resolve_tests(paths=['mobile'], subsuite='background'))
-        self.assertEqual(len(tests), 1)
-        self.assertEqual(tests[0]['name'], 'src/common/TestAndroidLogWriters.java')
-
-    def test_wildcard_patterns(self):
-        """Test matching paths by wildcard."""
+def test_wildcard_patterns(resolver):
+    """Test matching paths by wildcard."""
+    tests = list(resolver.resolve_tests(paths=['mobile/**']))
+    assert len(tests) == 2
+    for t in tests:
+        assert t['file_relpath'].startswith('mobile')
 
-        r = self.get_resolver()
+    tests = list(resolver.resolve_tests(paths=['**/**.js', 'accessible/**']))
+    assert len(tests) == 7
+    for t in tests:
+        path = t['file_relpath']
+        assert path.startswith('accessible') or path.endswith('.js')
 
-        tests = list(r.resolve_tests(paths=['mobile/**']))
-        self.assertEqual(len(tests), 2)
-        for t in tests:
-            self.assertTrue(t['file_relpath'].startswith('mobile'))
 
-        tests = list(r.resolve_tests(paths=['**/**.js', 'accessible/**']))
-        self.assertEqual(len(tests), 7)
-        for t in tests:
-            path = t['file_relpath']
-            self.assertTrue(path.startswith('accessible') or path.endswith('.js'))
-
-    def test_resolve_metadata(self):
-        """Test finding metadata from outgoing files."""
-        r = self.get_resolver()
+def test_resolve_metadata(resolver):
+    """Test finding metadata from outgoing files."""
+    suites, tests = resolver.resolve_metadata(['bc'])
+    assert suites == {'mochitest-browser-chrome'}
+    assert tests == []
 
-        suites, tests = r.resolve_metadata(['bc'])
-        assert suites == {'mochitest-browser-chrome'}
-        assert tests == []
+    suites, tests = resolver.resolve_metadata(['mochitest-a11y', '/browser', 'xpcshell'])
+    assert suites == {'mochitest-a11y', 'xpcshell'}
+    assert sorted(t['file_relpath'] for t in tests) == [
+        'devtools/client/markupview/test/browser_markupview_copy_image_data.js',
+        'image/test/browser/browser_bug666317.js',
+        'mobile/android/tests/browser/junit3/src/TestDistribution.java',
+    ]
 
-        suites, tests = r.resolve_metadata(['mochitest-a11y', '/browser', 'xpcshell'])
-        assert suites == {'mochitest-a11y', 'xpcshell'}
-        assert sorted(t['file_relpath'] for t in tests) == [
-            'devtools/client/markupview/test/browser_markupview_copy_image_data.js',
-            'image/test/browser/browser_bug666317.js',
-            'mobile/android/tests/browser/junit3/src/TestDistribution.java',
-        ]
-
-    def test_task_regexes(self):
-        """Test the task_regexes defined in TEST_SUITES."""
+def test_task_regexes():
+    """Test the task_regexes defined in TEST_SUITES."""
 
-        test_cases = {
-            'mochitest-browser-chrome': [
-                'test-linux64/opt-mochitest-browser-chrome',
-                'test-linux64/opt-mochitest-browser-chrome-e10s',
-            ],
-            'mochitest-chrome': [
-                'test-linux64/opt-mochitest-chrome',
-            ],
-            'mochitest-devtools-chrome': [
-                'test-linux64/opt-mochitest-devtools-chrome',
-            ],
-            'mochitest-media': [
-                'test-linux64/opt-mochitest-media-e10s-1',
-            ],
-            'mochitest-plain': [
-                'test-linux64/opt-mochitest',
-                'test-linux64/debug-mochitest-e10s',
-                # this isn't a real task but the regex would match it if it were
-                'test-linux64/opt-mochitest-plain',
-            ],
-            'mochitest-plain-gpu': [
-                'test-linux64/opt-mochitest-gpu',
-                'test-linux64/opt-mochitest-gpu-e10s',
-            ],
-            'mochitest-browser-chrome-screenshots': [
-                'test-linux64/opt-browser-screenshots-1',
-                'test-linux64/opt-browser-screenshots-e10s-1',
-            ],
-            'reftest': [
-                'test-linux64/opt-reftest',
-                'test-linux64/debug-reftest-e10s-1',
-            ],
-            'robocop': [
-                'test-linux64/opt-robocop',
-                'test-linux64/opt-robocop-1',
-                'test-linux64/opt-robocop-e10s',
-                'test-linux64/opt-robocop-e10s-1',
-            ],
-            'web-platform-tests': [
-                'test-linux64/opt-web-platform-tests-e10s-1',
-                'test-linux64/opt-web-platform-tests-reftests-e10s-1',
-                'test-linux64/opt-web-platform-tests-reftest-e10s-1',
-                'test-linux64/opt-web-platform-tests-wdspec-e10s-1',
-                'test-linux64/opt-web-platform-tests-1',
-            ],
-            'web-platform-tests-testharness': [
-                'test-linux64/opt-web-platform-tests-e10s-1',
-                'test-linux64/opt-web-platform-tests-1',
-            ],
-            'web-platform-tests-reftest': [
-                'test-linux64/opt-web-platform-tests-reftests-e10s-1',
-            ],
-            'web-platform-tests-wdspec': [
-                'test-linux64/opt-web-platform-tests-wdspec-e10s-1',
-            ],
-            'xpcshell': [
-                'test-linux64/opt-xpcshell',
-                'test-linux64/opt-xpcshell-1',
-            ],
-        }
+    test_cases = {
+        'mochitest-browser-chrome': [
+            'test-linux64/opt-mochitest-browser-chrome',
+            'test-linux64/opt-mochitest-browser-chrome-e10s',
+        ],
+        'mochitest-chrome': [
+            'test-linux64/opt-mochitest-chrome',
+        ],
+        'mochitest-devtools-chrome': [
+            'test-linux64/opt-mochitest-devtools-chrome',
+        ],
+        'mochitest-media': [
+            'test-linux64/opt-mochitest-media-e10s-1',
+        ],
+        'mochitest-plain': [
+            'test-linux64/opt-mochitest',
+            'test-linux64/debug-mochitest-e10s',
+            # this isn't a real task but the regex would match it if it were
+            'test-linux64/opt-mochitest-plain',
+        ],
+        'mochitest-plain-gpu': [
+            'test-linux64/opt-mochitest-gpu',
+            'test-linux64/opt-mochitest-gpu-e10s',
+        ],
+        'mochitest-browser-chrome-screenshots': [
+            'test-linux64/opt-browser-screenshots-1',
+            'test-linux64/opt-browser-screenshots-e10s-1',
+        ],
+        'reftest': [
+            'test-linux64/opt-reftest',
+            'test-linux64/debug-reftest-e10s-1',
+        ],
+        'robocop': [
+            'test-linux64/opt-robocop',
+            'test-linux64/opt-robocop-1',
+            'test-linux64/opt-robocop-e10s',
+            'test-linux64/opt-robocop-e10s-1',
+        ],
+        'web-platform-tests': [
+            'test-linux64/opt-web-platform-tests-e10s-1',
+            'test-linux64/opt-web-platform-tests-reftests-e10s-1',
+            'test-linux64/opt-web-platform-tests-reftest-e10s-1',
+            'test-linux64/opt-web-platform-tests-wdspec-e10s-1',
+            'test-linux64/opt-web-platform-tests-1',
+        ],
+        'web-platform-tests-testharness': [
+            'test-linux64/opt-web-platform-tests-e10s-1',
+            'test-linux64/opt-web-platform-tests-1',
+        ],
+        'web-platform-tests-reftest': [
+            'test-linux64/opt-web-platform-tests-reftests-e10s-1',
+        ],
+        'web-platform-tests-wdspec': [
+            'test-linux64/opt-web-platform-tests-wdspec-e10s-1',
+        ],
+        'xpcshell': [
+            'test-linux64/opt-xpcshell',
+            'test-linux64/opt-xpcshell-1',
+        ],
+    }
 
-        regexes = []
+    regexes = []
 
-        def match_task(task):
-            return any(re.search(pattern, task) for pattern in regexes)
+    def match_task(task):
+        return any(re.search(pattern, task) for pattern in regexes)
 
-        for suite, expected in sorted(test_cases.items()):
-            print(suite)
-            regexes = TEST_SUITES[suite]['task_regex']
-            assert set(filter(match_task, TASK_LABELS)) == set(expected)
+    for suite, expected in sorted(test_cases.items()):
+        print(suite)
+        regexes = TEST_SUITES[suite]['task_regex']
+        assert set(filter(match_task, TASK_LABELS)) == set(expected)
 
 
 if __name__ == '__main__':
     mozunit.main()