From f8e731b82259f55889361217982c28fc39b3baf9 Mon Sep 17 00:00:00 2001 From: Luis Date: Mon, 13 Sep 2021 09:39:53 +0200 Subject: [PATCH] Use base_path as a relative folder from conanfile.source_folder --- conan/tools/files/patches.py | 7 +++-- conans/test/functional/tools/test_files.py | 30 +++++++++++++++++++ .../unittests/tools/files/test_patches.py | 13 ++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/conan/tools/files/patches.py b/conan/tools/files/patches.py index 5c4db5352ee..dfc6b356877 100644 --- a/conan/tools/files/patches.py +++ b/conan/tools/files/patches.py @@ -25,9 +25,10 @@ def emit(self, record): self._output.info("%s: %s" % (self.patchname, logstr)) -def patch(conanfile, patch_file=None, patch_string=None, strip=0, fuzz=False, **kwargs): +def patch(conanfile, base_path=None, patch_file=None, patch_string=None, strip=0, fuzz=False, **kwargs): """ Applies a diff from file (patch_file) or string (patch_string) in base_path directory or current dir if None + :param base_path: Base path where the patch should be applied. :param patch_file: Patch file that should be applied. :param patch_string: Patch string that should be applied. :param strip: Number of folders to be stripped from the path. @@ -56,7 +57,8 @@ def patch(conanfile, patch_file=None, patch_string=None, strip=0, fuzz=False, ** if not patchset: raise ConanException("Failed to parse patch: %s" % (patch_file if patch_file else "string")) - if not patchset.apply(root=conanfile.source_folder, strip=strip, fuzz=fuzz): + root = os.path.join(conanfile.source_folder, base_path) if base_path else conanfile.source_folder + if not patchset.apply(root, strip=strip, fuzz=fuzz): raise ConanException("Failed to apply patch: %s" % patch_file) @@ -71,6 +73,7 @@ def apply_conandata_patches(conanfile): ``` patches: - patch_file: "patches/0001-buildflatbuffers-cmake.patch" + base_path: "source_subfolder" - patch_file: "patches/0002-implicit-copy-constructor.patch" patch_type: backport patch_source: https://github.com/google/flatbuffers/pull/5650 diff --git a/conans/test/functional/tools/test_files.py b/conans/test/functional/tools/test_files.py index 2e58ed96361..563c9814598 100644 --- a/conans/test/functional/tools/test_files.py +++ b/conans/test/functional/tools/test_files.py @@ -158,6 +158,36 @@ def build(self): ' clang compilers.' in str(client.out) +def test_apply_conandata_patches_relative_base_path(mock_patch_ng): + conanfile = textwrap.dedent(""" + from conans import ConanFile + from conan.tools.files import apply_conandata_patches + + class Pkg(ConanFile): + name = "mypkg" + version = "1.11.0" + + def layout(self): + self.folders.source = "source_subfolder" + + def build(self): + apply_conandata_patches(self) + """) + conandata_yml = textwrap.dedent(""" + patches: + "1.11.0": + - patch_file: "patches/0001-buildflatbuffers-cmake.patch" + base_path: "relative_dir" + """) + + client = TestClient() + client.save({'conanfile.py': conanfile, + 'conandata.yml': conandata_yml}) + client.run('create .') + + assert mock_patch_ng.apply_args[0].endswith(os.path.join('source_subfolder', "relative_dir")) + assert mock_patch_ng.apply_args[1:] == (0, False) + def test_no_patch_file_entry(): conanfile = textwrap.dedent(""" diff --git a/conans/test/unittests/tools/files/test_patches.py b/conans/test/unittests/tools/files/test_patches.py index 3f403788cc0..ee1b9bcf35d 100644 --- a/conans/test/unittests/tools/files/test_patches.py +++ b/conans/test/unittests/tools/files/test_patches.py @@ -1,3 +1,5 @@ +import os + import patch_ng import pytest @@ -44,6 +46,17 @@ def test_single_patch_file(mock_patch_ng): assert len(str(conanfile.output)) == 0 +def test_base_path(mock_patch_ng): + conanfile = ConanFileMock() + conanfile.folders.set_base_source("my_source") + conanfile.display_name = 'mocked/ref' + patch(conanfile, patch_file='patch-file', base_path="subfolder") + assert mock_patch_ng.filename == 'patch-file' + assert mock_patch_ng.string is None + assert mock_patch_ng.apply_args == (os.path.join("my_source", "subfolder"), 0, False) + assert len(str(conanfile.output)) == 0 + + def test_single_patch_string(mock_patch_ng): conanfile = ConanFileMock() conanfile.folders.set_base_source("my_folder")