diff --git a/conan/cli/commands/create.py b/conan/cli/commands/create.py index a81d99ce2cc..3e08b77c892 100644 --- a/conan/cli/commands/create.py +++ b/conan/cli/commands/create.py @@ -9,7 +9,7 @@ from conan.cli.printers import print_profiles from conan.cli.printers.graph import print_graph_packages, print_graph_basic from conan.errors import ConanException -from conans.util.files import mkdir +from conans.util.files import mkdir, load @conan_command(group="Creator", formatters={"json": format_graph_json}) @@ -98,7 +98,9 @@ def create(conan_api, parser, *args): if test_conanfile_path: # TODO: We need arguments for: # - decide update policy "--test_package_update" - tested_python_requires = ref.repr_notime() if is_python_require else None + # If it is a string, it will be injected always, if it is a RecipeReference, then it will + # be replaced only if ``python_requires = "tested_reference_str"`` + tested_python_requires = ref.repr_notime() if is_python_require else ref from conan.cli.commands.test import run_test # The test_package do not make the "conan create" command return a different graph or # produce a different lockfile. The result is always the same, irrespective of test_package @@ -124,16 +126,16 @@ def _check_tested_reference_matches(deps_graph, tested_ref, out): "tested is '{}'".format(missmatch[0], tested_ref)) -def test_package(conan_api, deps_graph, test_conanfile_path, tested_python_requires=None): +def test_package(conan_api, deps_graph, test_conanfile_path): out = ConanOutput() out.title("Testing the package") # TODO: Better modeling when we are testing a python_requires - if len(deps_graph.nodes) == 1 and not tested_python_requires: + conanfile = deps_graph.root.conanfile + if len(deps_graph.nodes) == 1 and not hasattr(conanfile, "python_requires"): raise ConanException("The conanfile at '{}' doesn't declare any requirement, " "use `self.tested_reference_str` to require the " "package being created.".format(test_conanfile_path)) conanfile_folder = os.path.dirname(test_conanfile_path) - conanfile = deps_graph.root.conanfile # To make sure the folders are correct conanfile.folders.set_base_folders(conanfile_folder, output_folder=None) if conanfile.build_folder and conanfile.build_folder != conanfile.source_folder: diff --git a/conan/cli/commands/test.py b/conan/cli/commands/test.py index d8f2093fb78..eda4ebafc75 100644 --- a/conan/cli/commands/test.py +++ b/conan/cli/commands/test.py @@ -38,7 +38,7 @@ def test(conan_api, parser, *args): print_profiles(profile_host, profile_build) deps_graph = run_test(conan_api, path, ref, profile_host, profile_build, remotes, lockfile, - args.update, build_modes=args.build) + args.update, build_modes=args.build, tested_python_requires=ref) lockfile = conan_api.lockfile.update_lockfile(lockfile, deps_graph, args.lockfile_packages, clean=args.lockfile_clean) conan_api.lockfile.save_lockfile(lockfile, args.lockfile_out, cwd) @@ -55,6 +55,11 @@ def run_test(conan_api, path, ref, profile_host, profile_build, remotes, lockfil update=update, lockfile=lockfile, tested_python_requires=tested_python_requires) + if isinstance(tested_python_requires, str): # create python-require + conanfile = root_node.conanfile + if not getattr(conanfile, "python_requires", None) == "tested_reference_str": + ConanOutput().warning("test_package/conanfile.py should declare 'python_requires" + " = \"tested_reference_str\"'", warn_tag="deprecated") out = ConanOutput() out.title("Launching test_package") @@ -74,5 +79,5 @@ def run_test(conan_api, path, ref, profile_host, profile_build, remotes, lockfil conan_api.install.install_binaries(deps_graph=deps_graph, remotes=remotes) _check_tested_reference_matches(deps_graph, ref, out) - test_package(conan_api, deps_graph, path, tested_python_requires) + test_package(conan_api, deps_graph, path) return deps_graph diff --git a/conans/client/loader.py b/conans/client/loader.py index 28be532d387..66086f71197 100644 --- a/conans/client/loader.py +++ b/conans/client/loader.py @@ -54,7 +54,10 @@ def load_basic_module(self, conanfile_path, graph_lock=None, display="", remotes try: module, conanfile = _parse_conanfile(conanfile_path) - if tested_python_requires: + if isinstance(tested_python_requires, RecipeReference): + if getattr(conanfile, "python_requires", None) == "tested_reference_str": + conanfile.python_requires = tested_python_requires.repr_notime() + elif tested_python_requires: conanfile.python_requires = tested_python_requires if self._pyreq_loader: diff --git a/conans/test/integration/py_requires/python_requires_test.py b/conans/test/integration/py_requires/python_requires_test.py index ef86ee11765..9b2342bef17 100644 --- a/conans/test/integration/py_requires/python_requires_test.py +++ b/conans/test/integration/py_requires/python_requires_test.py @@ -1013,6 +1013,7 @@ class Common(ConanFile): from conan import ConanFile class Tool(ConanFile): + python_requires = "tested_reference_str" def test(self): self.output.info("{}!!!".format(self.python_requires["common"].module.mycommon())) """) @@ -1021,6 +1022,9 @@ def test(self): c.run("create .") assert "common/0.1 (test package): 42!!!" in c.out + c.run("test test_package common/0.1") + assert "common/0.1 (test package): 42!!!" in c.out + def test_test_package_python_requires_configs(self): """ test how to test_package a python_require with various configurations """ @@ -1048,6 +1052,7 @@ def test(self): "test_package/conanfile.py": test}) c.run("create . ") assert "common/0.1 (test package): RELEASEOK!!!" in c.out + assert "WARN: deprecated: test_package/conanfile.py should declare 'python_requires" in c.out c.run("create . -s build_type=Debug") assert "common/0.1 (test package): DEBUGOK!!!" in c.out