Explorar o código

Only run selected tests on py2

depot_tools codebase uses py3 and there's limited py2 usage, namely for
presubmits. This CL drops running most of py2 unit tests since there are
no longer useful.

Bug: 1357152
Change-Id: I49a3df503026bc6918362a9b5600f5714eb5c87a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3872425
Auto-Submit: Josip Sokcevic <sokcevic@google.com>
Reviewed-by: Gavin Mak <gavinmak@google.com>
Commit-Queue: Gavin Mak <gavinmak@google.com>
Josip Sokcevic %!s(int64=2) %!d(string=hai) anos
pai
achega
95c7da10c5
Modificáronse 3 ficheiros con 51 adicións e 58 borrados
  1. 27 40
      PRESUBMIT.py
  2. 17 9
      tests/PRESUBMIT.py
  3. 7 9
      tests/owners_client_test.py

+ 27 - 40
PRESUBMIT.py

@@ -10,6 +10,8 @@ details on the presubmit API built into depot_tools.
 
 PRESUBMIT_VERSION = '2.0.0'
 
+USE_PYTHON3 = True
+
 import fnmatch
 import os
 import sys
@@ -85,17 +87,16 @@ def CheckRecipes(input_api, output_api):
   return input_api.canned_checks.CheckJsonParses(input_api, output_api,
                                                  file_filter=file_filter)
 
-def CheckPythonVersion(input_api, output_api):
-  # The tests here are assuming this is not defined, so raise an error
-  # if it is.
-  if 'USE_PYTHON3' in globals():
-    return [output_api.PresubmitError(
-        'USE_PYTHON3 is defined; update the tests in //PRESUBMIT.py and '
-        '//tests/PRESUBMIT.py.')]
-  if sys.version_info.major != 2:
-    return [output_api.PresubmitError(
-        'Did not use Python2 for //PRESUBMIT.py by default.')]
-  return []
+
+def CheckUsePython3(input_api, output_api):
+  results = []
+
+  if sys.version_info.major != 3:
+    results.append(
+        output_api.PresubmitError(
+            'Did not use Python3 for //tests/PRESUBMIT.py.'))
+
+  return results
 
 
 def CheckJsonFiles(input_api, output_api):
@@ -104,9 +105,22 @@ def CheckJsonFiles(input_api, output_api):
 
 
 def CheckUnitTestsOnCommit(input_api, output_api):
-  # Do not run integration tests on upload since they are way too slow.
+  """ Do not run integration tests on upload since they are way too slow."""
+
   input_api.SetTimeout(TEST_TIMEOUT_S)
 
+  # We still support python2 presubmit tests, so we need to test them. We don't
+  # run py3 here as the code below will start those tests
+  tests = input_api.canned_checks.GetUnitTestsInDirectory(
+      input_api,
+      output_api,
+      'tests',
+      files_to_check=[
+          r'.*presubmit_unittest\.py$',
+      ],
+      run_on_python2=True,
+      run_on_python3=False)
+
   # Run only selected tests on Windows.
   test_to_run_list = [r'.*test\.py$']
   tests_to_skip_list = []
@@ -120,40 +134,13 @@ def CheckUnitTestsOnCommit(input_api, output_api):
         r'.*ninjalog_uploader_test\.py$',
         r'.*recipes_test\.py$',
     ])
-  py2_only_tests = [
-      'recipes_test.py',
-  ]
-
-  py3_only_tests = [
-      'autoninja_test.py',
-      'ninjalog_uploader_test.py',
-  ]
-
-  tests = input_api.canned_checks.GetUnitTestsInDirectory(
-      input_api,
-      output_api,
-      'tests',
-      files_to_check=test_to_run_list,
-      files_to_skip=tests_to_skip_list + py2_only_tests + py3_only_tests,
-      run_on_python3=True)
-
-  # TODO: once py3 compatbile, remove those tests
-  tests.extend(
-      input_api.canned_checks.GetUnitTestsInDirectory(
-          input_api,
-          output_api,
-          'tests',
-          files_to_check=py2_only_tests,
-          files_to_skip=tests_to_skip_list,
-          run_on_python3=False))
 
-  # TODO: use this for all tests when py2 support is dropped.
   tests.extend(
       input_api.canned_checks.GetUnitTestsInDirectory(
           input_api,
           output_api,
           'tests',
-          files_to_check=py3_only_tests,
+          files_to_check=test_to_run_list,
           files_to_skip=tests_to_skip_list,
           run_on_python3=True,
           run_on_python2=False))

+ 17 - 9
tests/PRESUBMIT.py

@@ -6,14 +6,22 @@ import sys
 
 PRESUBMIT_VERSION = '2.0.0'
 
-USE_PYTHON3 = True
+# This file can be removed once py2 presubmit is no longer supported. This is
+# an integration test to ensure py2 presubmit still works.
 
 
-def CheckUsePython3(input_api, output_api):
-  results = []
-
-  if sys.version_info.major != 3:
-    results.append(output_api.PresubmitError(
-        'Did not use Python3 for //tests/PRESUBMIT.py.'))
-
-  return results
+def CheckPythonVersion(input_api, output_api):
+  # The tests here are assuming this is not defined, so raise an error
+  # if it is.
+  if 'USE_PYTHON3' in globals():
+    return [
+        output_api.PresubmitError(
+            'USE_PYTHON3 is defined; update the tests in //PRESUBMIT.py and '
+            '//tests/PRESUBMIT.py.')
+    ]
+  if sys.version_info.major != 2:
+    return [
+        output_api.PresubmitError(
+            'Did not use Python2 for //PRESUBMIT.py by default.')
+    ]
+  return []

+ 7 - 9
tests/owners_client_test.py

@@ -1,3 +1,4 @@
+#!/usr/bin/env vpython3
 # Copyright (c) 2020 The Chromium Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
@@ -91,12 +92,12 @@ class GerritClientTest(unittest.TestCase):
             }
           ]
         }).start()
-    self.assertEquals(
+    self.assertEqual(
         ['approver@example.com', 'reviewer@example.com', 'missing@example.com'],
         self.client.ListOwners(os.path.join('bar', 'everyone', 'foo.txt')))
 
     # Result should be cached.
-    self.assertEquals(
+    self.assertEqual(
         ['approver@example.com', 'reviewer@example.com', 'missing@example.com'],
         self.client.ListOwners(os.path.join('bar', 'everyone', 'foo.txt')))
     # Always use slashes as separators.
@@ -130,12 +131,9 @@ class GerritClientTest(unittest.TestCase):
         },
       ]
     ).start()
-    self.assertEquals(
-        ['foo@example.com', self.client.EVERYONE],
-        self.client.ListOwners('foo.txt'))
-    self.assertEquals(
-        ['bar@example.com'],
-        self.client.ListOwners('bar.txt'))
+    self.assertEqual(['foo@example.com', self.client.EVERYONE],
+                     self.client.ListOwners('foo.txt'))
+    self.assertEqual(['bar@example.com'], self.client.ListOwners('bar.txt'))
 
 
 class TestClient(owners_client.OwnersClient):
@@ -280,7 +278,7 @@ class OwnersClientTest(unittest.TestCase):
         'bar/foo/': [bob, chris]
     }
 
-    self.assertEquals(
+    self.assertEqual(
         {
             'bar/everyone/foo.txt': [alice, bob],
             'bar/everyone/bar.txt': [bob],