Skip to content

Commit 90b61d2

Browse files
Seppli11sonartech
authored andcommitted
SONARPY-4013 Fix checksum check (#1039)
GitOrigin-RevId: 6eb699c1de899e546cae5416f7e5bbb13970f9fb
1 parent d8070f4 commit 90b61d2

File tree

4 files changed

+134
-13
lines changed

4 files changed

+134
-13
lines changed

python-frontend/typeshed_serializer/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ tox -e py39 -- tests/runners/test_tox_runner.py::ToxRunnerTest::test_dry_run_unc
3232
python runners/tox_runner.py --dry_run true
3333
```
3434

35-
Can also be run in fail fast mode, to reflect behaviour in CI (where failStubGenerationFast is true)
35+
Can also be run in fail fast mode, to reflect the checksum validation used by the QA workflow
3636

3737
```bash
3838
python runners/tox_runner.py --dry_run true --fail_fast true
39-
```
39+
```
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
61e014719cf03716e393ae97da9e3d394f41cc9f4bf4f5ee9fe48b2004605cfd
2-
f8586ea766acbd86c5c0995ce3a7cc068ce433673cf057457eb8188fadc9ff39
1+
669032ff87c6a7027931d1987597a4371fc80b9da0f7817762518ec75d1fa357
2+
8a2fb0e2abba9b8e562913a29440ec31c7fd322f0c31d2d64d9a6c9591b444ed

python-frontend/typeshed_serializer/runners/tox_runner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ def detect_required_serializations(fail_fast=False) -> List[str]:
216216
binary_changed = compute_and_compare_checksums("BINARY", folder_name, binary_files, read_file, previous_binary_checksum)
217217

218218
if ressources_changed or binary_changed:
219-
# Check for binary inconsistency (binary changed but source didn't)
220-
if binary_changed and not ressources_changed and fail_fast:
221-
raise RuntimeError('INCONSISTENT RESOURCE FOLDER BINARY CHECKSUMS')
219+
# In CI fail-fast mode, the committed checksums must already match the tracked tree.
220+
if fail_fast:
221+
raise RuntimeError('INCONSISTENT RESOURCE FOLDER CHECKSUMS')
222222
ressources_that_needs_serialization.append(folder_config['serializer'])
223223

224224
return ressources_that_needs_serialization

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

Lines changed: 127 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,91 @@ def test_detect_required_serializations_fail_fast_binary_inconsistent(self):
617617
tox_runner.detect_required_serializations(fail_fast=True)
618618

619619
self.assertEqual(
620-
str(error.exception), "INCONSISTENT RESOURCE FOLDER BINARY CHECKSUMS"
620+
str(error.exception), "INCONSISTENT RESOURCE FOLDER CHECKSUMS"
621+
)
622+
623+
def test_detect_required_serializations_fail_fast_source_inconsistent(self):
624+
mock_resources_folders = {
625+
"custom": {
626+
"serializer": "custom",
627+
"source_path": "custom",
628+
}
629+
}
630+
631+
with (
632+
mock.patch(
633+
f"{self.MODULE_NAME}.RESOURCES_FOLDERS",
634+
mock_resources_folders,
635+
),
636+
mock.patch(
637+
f"{self.MODULE_NAME}.fetch_resources_subfolder_files"
638+
) as mock_resources_files,
639+
mock.patch(self.COMPUTE_CHECKSUM_FUNCTION) as mocked_checksum,
640+
mock.patch(
641+
f"{self.MODULE_NAME}.fetch_binary_files_for_folder"
642+
) as mock_binary_files,
643+
mock.patch(
644+
f"{self.MODULE_NAME}.read_previous_folder_checksum"
645+
) as mock_read_checksum,
646+
):
647+
mock_resources_files.return_value = [os.path.join("custom", "test.pyi")]
648+
mock_binary_files.return_value = [os.path.join("custom_protobuf", "test.protobuf")]
649+
mocked_checksum.side_effect = [
650+
"456",
651+
"123",
652+
]
653+
mock_read_checksum.return_value = (
654+
"999",
655+
"123",
656+
)
657+
658+
with self.assertRaises(RuntimeError) as error:
659+
tox_runner.detect_required_serializations(fail_fast=True)
660+
661+
self.assertEqual(
662+
str(error.exception), "INCONSISTENT RESOURCE FOLDER CHECKSUMS"
663+
)
664+
665+
def test_detect_required_serializations_fail_fast_source_and_binary_inconsistent(self):
666+
mock_resources_folders = {
667+
"custom": {
668+
"serializer": "custom",
669+
"source_path": "custom",
670+
}
671+
}
672+
673+
with (
674+
mock.patch(
675+
f"{self.MODULE_NAME}.RESOURCES_FOLDERS",
676+
mock_resources_folders,
677+
),
678+
mock.patch(
679+
f"{self.MODULE_NAME}.fetch_resources_subfolder_files"
680+
) as mock_resources_files,
681+
mock.patch(self.COMPUTE_CHECKSUM_FUNCTION) as mocked_checksum,
682+
mock.patch(
683+
f"{self.MODULE_NAME}.fetch_binary_files_for_folder"
684+
) as mock_binary_files,
685+
mock.patch(
686+
f"{self.MODULE_NAME}.read_previous_folder_checksum"
687+
) as mock_read_checksum,
688+
):
689+
mock_resources_files.return_value = [os.path.join("custom", "test.pyi")]
690+
mock_binary_files.return_value = [os.path.join("custom_protobuf", "test.protobuf")]
691+
mocked_checksum.side_effect = [
692+
"456",
693+
"789",
694+
]
695+
mock_read_checksum.return_value = (
696+
"999",
697+
"123",
698+
)
699+
700+
with self.assertRaises(RuntimeError) as error:
701+
tox_runner.detect_required_serializations(fail_fast=True)
702+
703+
self.assertEqual(
704+
str(error.exception), "INCONSISTENT RESOURCE FOLDER CHECKSUMS"
621705
)
622706

623707
def test_detect_required_serializations_no_fail_fast_binary_inconsistent(self):
@@ -665,8 +749,46 @@ def test_detect_required_serializations_no_fail_fast_binary_inconsistent(self):
665749
result = tox_runner.detect_required_serializations(fail_fast=False)
666750
self.assertEqual(result, ["custom"])
667751

668-
def test_main_fail_fast_resource_folder_binary_inconsistent(self):
669-
# Test main function fail-fast when resource folder has binary inconsistency
752+
def test_detect_required_serializations_no_fail_fast_source_and_binary_inconsistent(self):
753+
mock_resources_folders = {
754+
"custom": {
755+
"serializer": "custom",
756+
"source_path": "custom",
757+
}
758+
}
759+
760+
with (
761+
mock.patch(
762+
f"{self.MODULE_NAME}.RESOURCES_FOLDERS",
763+
mock_resources_folders,
764+
),
765+
mock.patch(
766+
f"{self.MODULE_NAME}.fetch_resources_subfolder_files"
767+
) as mock_resources_files,
768+
mock.patch(self.COMPUTE_CHECKSUM_FUNCTION) as mocked_checksum,
769+
mock.patch(
770+
f"{self.MODULE_NAME}.fetch_binary_files_for_folder"
771+
) as mock_binary_files,
772+
mock.patch(
773+
f"{self.MODULE_NAME}.read_previous_folder_checksum"
774+
) as mock_read_checksum,
775+
):
776+
mock_resources_files.return_value = [os.path.join("custom", "test.pyi")]
777+
mock_binary_files.return_value = [os.path.join("custom_protobuf", "test.protobuf")]
778+
mocked_checksum.side_effect = [
779+
"456",
780+
"789",
781+
]
782+
mock_read_checksum.return_value = (
783+
"999",
784+
"123",
785+
)
786+
787+
result = tox_runner.detect_required_serializations(fail_fast=False)
788+
self.assertEqual(result, ["custom"])
789+
790+
def test_main_fail_fast_resource_folder_checksum_inconsistent(self):
791+
# Test main function fail-fast when resource folder checksums are inconsistent
670792
source_checksum = "123"
671793

672794
with (
@@ -687,15 +809,15 @@ def test_main_fail_fast_resource_folder_binary_inconsistent(self):
687809

688810
# Resource folder check raises RuntimeError
689811
mock_check_changes.side_effect = RuntimeError(
690-
"INCONSISTENT RESOURCE FOLDER BINARY CHECKSUMS"
812+
"INCONSISTENT RESOURCE FOLDER CHECKSUMS"
691813
)
692814

693815
# Should propagate the RuntimeError
694816
with self.assertRaises(RuntimeError) as error:
695817
tox_runner.main(fail_fast=True)
696818

697819
self.assertEqual(
698-
str(error.exception), "INCONSISTENT RESOURCE FOLDER BINARY CHECKSUMS"
820+
str(error.exception), "INCONSISTENT RESOURCE FOLDER CHECKSUMS"
699821
)
700822
# Should not call subprocess
701823
mocked_subprocess.run.assert_not_called()
@@ -891,4 +1013,3 @@ def test_compute_and_compare_checksums_unchanged(self):
8911013

8921014
self.assertFalse(result)
8931015
mock_logger.info.assert_any_call('BINARY FILES UNCHANGED in folder: importer - Computed over 2 files - Checksum: same_checksum')
894-

0 commit comments

Comments
 (0)