Skip to content

Commit 9ceb464

Browse files
SONARPY-1386 Avoid running Typeshed serializer tests when mvn has -DskipTests argument (#1577)
1 parent 1bc3a6f commit 9ceb464

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

python-frontend/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<sonar.sources>pom.xml,src/main/java,typeshed_serializer/serializer,typeshed_serializer/runners</sonar.sources>
1616
<sonar.python.coverage.reportPaths>typeshed_serializer/cov.xml</sonar.python.coverage.reportPaths>
1717
<sonar.python.version>3.9</sonar.python.version>
18+
<skipTests>false</skipTests>
1819
</properties>
1920
<dependencies>
2021
<dependency>
@@ -172,6 +173,8 @@
172173
<executable>python</executable>
173174
<arguments>
174175
<argument>runners/tox_runner.py</argument>
176+
<argument>--skip_tests</argument>
177+
<argument>${skipTests}</argument>
175178
</arguments>
176179
<useMavenLogger>true</useMavenLogger>
177180
</configuration>

python-frontend/typeshed_serializer/runners/tox_runner.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from typing import Optional, Tuple
2929
from collections.abc import Callable
3030
import logging
31+
import argparse
3132

3233
CURRENT_PATH = os.path.dirname(__file__)
3334
CHECKSUM_FILE = os.path.join(CURRENT_PATH, '../checksum')
@@ -117,7 +118,7 @@ def update_checksum():
117118
file.writelines([f"{source_checksum}\n", binary_checksum])
118119

119120

120-
def main():
121+
def main(skip_tests=False):
121122
source_files = fetch_source_file_names(SERIALIZER_PATH)
122123
(previous_sources_checksum, previous_binaries_checksum) = read_previous_checksum(CHECKSUM_FILE)
123124
current_sources_checksum = compute_checksum(source_files, normalize_text_files)
@@ -140,8 +141,15 @@ def main():
140141
logger.info("SKIPPING TYPESHED SERIALIZATION")
141142
# At the moment we need to run the tests in order to not break the quality gate.
142143
# If the tests are skipped this could potentially result in missing coverage.
144+
if skip_tests:
145+
logger.info("SKIPPING TYPESHED SERIALIZER TESTS")
146+
return
143147
subprocess.run(['tox', '-e', 'py39'], check=True)
144148

145149

146150
if __name__ == '__main__':
147-
main()
151+
parser = argparse.ArgumentParser()
152+
parser.add_argument('--skip_tests')
153+
args = parser.parse_args()
154+
skip_tests = args.skip_tests == "true"
155+
main(skip_tests)

python-frontend/typeshed_serializer/tests/runners/test_tox_runner.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,27 @@ def test_tox_runner_modified_checksum(self):
235235
mocked_previous_checksum.assert_called_with(tox_runner.CHECKSUM_FILE)
236236
mocked_checksum.assert_called_with(self.FILE_NAMES, tox_runner.normalize_text_files)
237237
mocked_subprocess.run.assert_called_with(['tox'], check=True)
238+
239+
def test_skip_tests(self):
240+
checksum = ('123', '456')
241+
computed_checksum = ['123', '456']
242+
243+
def feed_checksum(_fn, _f):
244+
return computed_checksum.pop(0)
245+
246+
with mock.patch(self.READ_PREVIOUS_CHECKSUM_FUNCTION) as mocked_previous_checksum, \
247+
mock.patch(self.COMPUTE_CHECKSUM_FUNCTION) as mocked_checksum, \
248+
mock.patch(f'{self.MODULE_NAME}.fetch_source_file_names') as mock_files, \
249+
mock.patch(f'{self.MODULE_NAME}.fetch_binary_file_names') as mock_binary_files, \
250+
mock.patch(self.SUBPROCESS_CALL) as mocked_subprocess:
251+
mocked_previous_checksum.return_value = checksum
252+
mock_binary_files.return_value = self.FILE_NAMES
253+
mock_files.return_value = self.FILE_NAMES
254+
mocked_checksum.side_effect = feed_checksum
255+
tox_runner.main(skip_tests=True)
256+
mock_files.assert_called_once()
257+
mock_binary_files.assert_called_once()
258+
mocked_previous_checksum.assert_any_call(tox_runner.CHECKSUM_FILE)
259+
mocked_checksum.assert_any_call(self.FILE_NAMES, tox_runner.normalize_text_files)
260+
mocked_checksum.assert_any_call(self.FILE_NAMES, tox_runner.read_file)
261+
mocked_subprocess.run.assert_not_called()

0 commit comments

Comments
 (0)