From 9ceb46417add16fe2d9d143928c952626d1c8170 Mon Sep 17 00:00:00 2001 From: Guillaume Dequenne Date: Fri, 22 Sep 2023 16:22:00 +0200 Subject: [PATCH] SONARPY-1386 Avoid running Typeshed serializer tests when mvn has -DskipTests argument (#1577) --- python-frontend/pom.xml | 3 +++ .../typeshed_serializer/runners/tox_runner.py | 12 ++++++++-- .../tests/runners/test_tox_runner.py | 24 +++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/python-frontend/pom.xml b/python-frontend/pom.xml index 25a9ccb530..2e1daa9fd5 100644 --- a/python-frontend/pom.xml +++ b/python-frontend/pom.xml @@ -15,6 +15,7 @@ pom.xml,src/main/java,typeshed_serializer/serializer,typeshed_serializer/runners typeshed_serializer/cov.xml 3.9 + false @@ -172,6 +173,8 @@ python runners/tox_runner.py + --skip_tests + ${skipTests} true diff --git a/python-frontend/typeshed_serializer/runners/tox_runner.py b/python-frontend/typeshed_serializer/runners/tox_runner.py index 2d2cd178d2..fab2f829aa 100644 --- a/python-frontend/typeshed_serializer/runners/tox_runner.py +++ b/python-frontend/typeshed_serializer/runners/tox_runner.py @@ -28,6 +28,7 @@ from typing import Optional, Tuple from collections.abc import Callable import logging +import argparse CURRENT_PATH = os.path.dirname(__file__) CHECKSUM_FILE = os.path.join(CURRENT_PATH, '../checksum') @@ -117,7 +118,7 @@ def update_checksum(): file.writelines([f"{source_checksum}\n", binary_checksum]) -def main(): +def main(skip_tests=False): source_files = fetch_source_file_names(SERIALIZER_PATH) (previous_sources_checksum, previous_binaries_checksum) = read_previous_checksum(CHECKSUM_FILE) current_sources_checksum = compute_checksum(source_files, normalize_text_files) @@ -140,8 +141,15 @@ def main(): logger.info("SKIPPING TYPESHED SERIALIZATION") # At the moment we need to run the tests in order to not break the quality gate. # If the tests are skipped this could potentially result in missing coverage. + if skip_tests: + logger.info("SKIPPING TYPESHED SERIALIZER TESTS") + return subprocess.run(['tox', '-e', 'py39'], check=True) if __name__ == '__main__': - main() + parser = argparse.ArgumentParser() + parser.add_argument('--skip_tests') + args = parser.parse_args() + skip_tests = args.skip_tests == "true" + main(skip_tests) diff --git a/python-frontend/typeshed_serializer/tests/runners/test_tox_runner.py b/python-frontend/typeshed_serializer/tests/runners/test_tox_runner.py index 9e935cfb7a..abc0daccf5 100644 --- a/python-frontend/typeshed_serializer/tests/runners/test_tox_runner.py +++ b/python-frontend/typeshed_serializer/tests/runners/test_tox_runner.py @@ -235,3 +235,27 @@ def test_tox_runner_modified_checksum(self): mocked_previous_checksum.assert_called_with(tox_runner.CHECKSUM_FILE) mocked_checksum.assert_called_with(self.FILE_NAMES, tox_runner.normalize_text_files) mocked_subprocess.run.assert_called_with(['tox'], check=True) + + def test_skip_tests(self): + checksum = ('123', '456') + computed_checksum = ['123', '456'] + + def feed_checksum(_fn, _f): + return computed_checksum.pop(0) + + with mock.patch(self.READ_PREVIOUS_CHECKSUM_FUNCTION) as mocked_previous_checksum, \ + mock.patch(self.COMPUTE_CHECKSUM_FUNCTION) as mocked_checksum, \ + mock.patch(f'{self.MODULE_NAME}.fetch_source_file_names') as mock_files, \ + mock.patch(f'{self.MODULE_NAME}.fetch_binary_file_names') as mock_binary_files, \ + mock.patch(self.SUBPROCESS_CALL) as mocked_subprocess: + mocked_previous_checksum.return_value = checksum + mock_binary_files.return_value = self.FILE_NAMES + mock_files.return_value = self.FILE_NAMES + mocked_checksum.side_effect = feed_checksum + tox_runner.main(skip_tests=True) + mock_files.assert_called_once() + mock_binary_files.assert_called_once() + mocked_previous_checksum.assert_any_call(tox_runner.CHECKSUM_FILE) + mocked_checksum.assert_any_call(self.FILE_NAMES, tox_runner.normalize_text_files) + mocked_checksum.assert_any_call(self.FILE_NAMES, tox_runner.read_file) + mocked_subprocess.run.assert_not_called()