Skip to content

Commit

Permalink
SONARPY-1386 Avoid running Typeshed serializer tests when mvn has -Ds…
Browse files Browse the repository at this point in the history
…kipTests argument (#1577)
  • Loading branch information
guillaume-dequenne-sonarsource authored Sep 22, 2023
1 parent 1bc3a6f commit 9ceb464
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
3 changes: 3 additions & 0 deletions python-frontend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<sonar.sources>pom.xml,src/main/java,typeshed_serializer/serializer,typeshed_serializer/runners</sonar.sources>
<sonar.python.coverage.reportPaths>typeshed_serializer/cov.xml</sonar.python.coverage.reportPaths>
<sonar.python.version>3.9</sonar.python.version>
<skipTests>false</skipTests>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -172,6 +173,8 @@
<executable>python</executable>
<arguments>
<argument>runners/tox_runner.py</argument>
<argument>--skip_tests</argument>
<argument>${skipTests}</argument>
</arguments>
<useMavenLogger>true</useMavenLogger>
</configuration>
Expand Down
12 changes: 10 additions & 2 deletions python-frontend/typeshed_serializer/runners/tox_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 9ceb464

Please sign in to comment.