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

Add: parameter on trim_conandata to block the raise an exception when conandata.yml file doesn't exist #15829

Merged
merged 3 commits into from
Mar 7, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions conan/tools/files/conandata.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def recursive_dict_update(d, u):
save(path, new_content)


def trim_conandata(conanfile):
def trim_conandata(conanfile, raise_if_missing=True):
"""
Tool to modify the ``conandata.yml`` once it is exported, to limit it to the current version
only
Expand All @@ -50,7 +50,11 @@ def trim_conandata(conanfile):
raise ConanException("The 'trim_conandata()' can only be used in the 'export()' method")
path = os.path.join(conanfile.export_folder, "conandata.yml")
if not os.path.exists(path):
raise ConanException("conandata.yml file doesn't exist")
if raise_if_missing:
raise ConanException("conandata.yml file doesn't exist")
else:
conanfile.output.warning("conandata.yml file doesn't exist")
return

conandata = load(path)
conandata = yaml.safe_load(conandata)
Expand Down
20 changes: 20 additions & 0 deletions conans/test/integration/conanfile/conan_data_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import textwrap
import unittest

import pytest
import yaml

from conans.model.recipe_ref import RecipeReference
Expand Down Expand Up @@ -375,3 +376,22 @@ def post_export(conanfile):
assert "1.1" not in data2
assert data1 == data2
assert "pkg/1.0: Exported: pkg/1.0#03af39add1c7c9d68dcdb10b6968a14d" in c.out


@pytest.mark.parametrize("raise_if_missing", [True, False])
def test_trim_conandata_as_hook_without_conandata(raise_if_missing):
c = TestClient()
c.save_home({"extensions/hooks/hook_trim.py": textwrap.dedent(f"""
from conan.tools.files import trim_conandata

def post_export(conanfile):
trim_conandata(conanfile, raise_if_missing={raise_if_missing})
""")})

c.save({"conanfile.py": GenConanfile("pkg")})
if raise_if_missing:
with pytest.raises(Exception, match="conandata.yml file doesn't exist") as exception:
c.run("export . --version=1.0")
else:
c.run("export . --version=1.0")
assert c.exported_recipe_revision() == "a9ec2e5fbb166568d4670a9cd1ef4b26"