Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use base_path as a relative folder from conanfile.source_folder #9593

Merged
merged 1 commit into from Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions conan/tools/files/patches.py
Expand Up @@ -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.
Expand Down Expand Up @@ -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)


Expand All @@ -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
Expand Down
30 changes: 30 additions & 0 deletions conans/test/functional/tools/test_files.py
Expand Up @@ -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("""
Expand Down
13 changes: 13 additions & 0 deletions conans/test/unittests/tools/files/test_patches.py
@@ -1,3 +1,5 @@
import os

import patch_ng
import pytest

Expand Down Expand Up @@ -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")
Expand Down