Skip to content

Commit

Permalink
Add: parameter on trim_conandata to block the raise an exception wh…
Browse files Browse the repository at this point in the history
…en conandata.yml file doesn't exist (#15829)

* add: parameter on trim_conan data to block exception

* add: warning msg on trim_conandata

* add: trim_conan_data test parametrized
  • Loading branch information
ErniGH authored Mar 7, 2024
1 parent e226805 commit 4af94e3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
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"

0 comments on commit 4af94e3

Please sign in to comment.