summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuis Martinez de Bartolome Izquierdo <lasote@gmail.com>2021-09-13 19:39:57 +0200
committerGitHub <noreply@github.com>2021-09-13 19:39:57 +0200
commit8494959914c81f653d8f72939aeb766575d297b5 (patch)
tree3558ad0d75065d4b5ac5bcb8c30b094e748fdc63
parent10dc5360a487d1da00b10c4e3c87694dc0171e0d (diff)
Use base_path as a relative folder from conanfile.source_folder (#9593)
-rw-r--r--conan/tools/files/patches.py7
-rw-r--r--conans/test/functional/tools/test_files.py30
-rw-r--r--conans/test/unittests/tools/files/test_patches.py13
3 files changed, 48 insertions, 2 deletions
diff --git a/conan/tools/files/patches.py b/conan/tools/files/patches.py
index 5c4db535..dfc6b356 100644
--- a/conan/tools/files/patches.py
+++ b/conan/tools/files/patches.py
@@ -25,9 +25,10 @@ class PatchLogHandler(logging.Handler):
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 2e58ed96..563c9814 100644
--- a/conans/test/functional/tools/test_files.py
+++ b/conans/test/functional/tools/test_files.py
@@ -158,6 +158,36 @@ def test_apply_conandata_patches(mock_patch_ng):
' 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 3f403788..ee1b9bcf 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")