diff --git a/hooks/conan-center.py b/hooks/conan-center.py index 8cdbaef5..0f8beaa9 100644 --- a/hooks/conan-center.py +++ b/hooks/conan-center.py @@ -922,10 +922,12 @@ def test(out): conandata_yml = load_yml(conandata_path) if not conandata_yml: - return + conandata_yml = {} - if 'sources' not in conandata_yml or 'patches' not in conandata_yml: - return + if 'sources' not in conandata_yml: + conandata_yml['sources'] = {} + if 'patches' not in conandata_yml: + conandata_yml['patches'] = {} versions_conandata = conandata_yml['sources'].keys() versions_patches = conandata_yml['patches'].keys() @@ -943,6 +945,28 @@ def test(out): continue if not os.path.isfile(os.path.join(export_folder_path, patch['patch_file'])): out.error("The patch file '{}' does not exist.".format(patch['patch_file'])) + + patches_path = os.path.join(export_folder_path, "patches") + unused_patches: list[str] = [] + if os.path.isdir(patches_path): + unused_patches.extend( + os.path.join(root[len(patches_path) + 1:], f) + for root, _, files in os.walk(patches_path) for f in files) + unused_patches.sort() + for patches in conandata_yml["patches"].values(): + if not isinstance(patches, list): + continue + for patch in patches: + if 'patch_file' not in patch: + continue + patch_file_name = str(patch["patch_file"]) + patch_file_name = os.path.relpath(patch_file_name) # fixes the path (double slashes for example) + patch_file_name = patch_file_name[8:] + if patch_file_name in unused_patches: + unused_patches.remove(patch_file_name) + if unused_patches: + patches_str = ", ".join(f"patches/{patch}" for patch in unused_patches) + out.error(f"Following patch file(s) are/is not referenced in conandata.yml: {patches_str}") @raise_if_error_output diff --git a/tests/test_hooks/conan-center/test_conan-center.py b/tests/test_hooks/conan-center/test_conan-center.py index 111647b8..09e6e91c 100644 --- a/tests/test_hooks/conan-center/test_conan-center.py +++ b/tests/test_hooks/conan-center/test_conan-center.py @@ -1248,3 +1248,22 @@ def test_dangling_patches(self): output = self.conan(['export', 'all', 'name/version@user/test']) self.assertIn("The patch file 'patches/patch.diff' does not exist.", output) + + conandata = textwrap.dedent(""" + sources: + 1.0: + url: fakeurl + md5: 12323423423 + """) + + tools.save(os.path.join("all", "conandata.yml"), content=conandata) + output = self.conan(['export', 'all', 'name/version@user/test']) + self.assertNotIn("Following patch file(s) are/is not referenced in conandata.yml", + output) + + tools.save(os.path.join("all", "patches", "patch.diff"), content="") + output = self.conan(['export', 'all', 'name/version@user/test']) + self.assertIn("Following patch file(s) are/is not referenced in conandata.yml: patches/patch.diff", + output) + +