From 85dba7faa87927e695b32ed30fd284f96862d203 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Wed, 24 Apr 2024 09:29:42 +0200 Subject: [PATCH 1/3] check for patches not referenced in conandata.yml reimplement part of https://github.com/ericLemanissier/conan-center-lint-patches/blob/main/lint_patches.py results are tracked in https://github.com/conan-io/conan-center-index/issues/21146 --- hooks/conan-center.py | 30 +++++++++++++++++-- .../conan-center/test_conan-center.py | 19 ++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/hooks/conan-center.py b/hooks/conan-center.py index 8cdbaef5..146efac7 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) + for patch in unused_patches: + out.error(f"Following patch files is not referenced in conandata.yml: patches/{patch}") + @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..a7cca7a8 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 files 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 files is not referenced in conandata.yml: patches/patch.diff", + output) + + From ceacaca17aaad048a5c6edd4f040ce266852058c Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Wed, 24 Apr 2024 23:34:09 +0200 Subject: [PATCH 2/3] typo --- hooks/conan-center.py | 2 +- tests/test_hooks/conan-center/test_conan-center.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/hooks/conan-center.py b/hooks/conan-center.py index 146efac7..82b1ea9d 100644 --- a/hooks/conan-center.py +++ b/hooks/conan-center.py @@ -965,7 +965,7 @@ def test(out): if patch_file_name in unused_patches: unused_patches.remove(patch_file_name) for patch in unused_patches: - out.error(f"Following patch files is not referenced in conandata.yml: patches/{patch}") + out.error(f"Following patch file is not referenced in conandata.yml: patches/{patch}") diff --git a/tests/test_hooks/conan-center/test_conan-center.py b/tests/test_hooks/conan-center/test_conan-center.py index a7cca7a8..df4a10a6 100644 --- a/tests/test_hooks/conan-center/test_conan-center.py +++ b/tests/test_hooks/conan-center/test_conan-center.py @@ -1258,12 +1258,12 @@ def test_dangling_patches(self): tools.save(os.path.join("all", "conandata.yml"), content=conandata) output = self.conan(['export', 'all', 'name/version@user/test']) - self.assertNotIn("Following patch files is not referenced in conandata.yml", + self.assertNotIn("Following patch file 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 files is not referenced in conandata.yml: patches/patch.diff", + self.assertIn("Following patch file is not referenced in conandata.yml: patches/patch.diff", output) From 2f1a8ec22e4bfbe3c0137b068dbd04def6a343b3 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Thu, 25 Apr 2024 10:43:35 +0200 Subject: [PATCH 3/3] group reports --- hooks/conan-center.py | 6 +++--- tests/test_hooks/conan-center/test_conan-center.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/hooks/conan-center.py b/hooks/conan-center.py index 82b1ea9d..0f8beaa9 100644 --- a/hooks/conan-center.py +++ b/hooks/conan-center.py @@ -964,9 +964,9 @@ def test(out): patch_file_name = patch_file_name[8:] if patch_file_name in unused_patches: unused_patches.remove(patch_file_name) - for patch in unused_patches: - out.error(f"Following patch file is not referenced in conandata.yml: patches/{patch}") - + 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 df4a10a6..09e6e91c 100644 --- a/tests/test_hooks/conan-center/test_conan-center.py +++ b/tests/test_hooks/conan-center/test_conan-center.py @@ -1258,12 +1258,12 @@ def test_dangling_patches(self): tools.save(os.path.join("all", "conandata.yml"), content=conandata) output = self.conan(['export', 'all', 'name/version@user/test']) - self.assertNotIn("Following patch file is not referenced in conandata.yml", + 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 is not referenced in conandata.yml: patches/patch.diff", + self.assertIn("Following patch file(s) are/is not referenced in conandata.yml: patches/patch.diff", output)