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

proposal for testing python_requires explicitly #15485

Merged
merged 5 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
17 changes: 12 additions & 5 deletions conan/cli/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -98,13 +98,20 @@ 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
run_test(conan_api, test_conanfile_path, ref, profile_host, profile_build, remotes, lockfile,
update=False, build_modes=args.build, build_modes_test=args.build_test,
tested_python_requires=tested_python_requires, tested_graph=deps_graph)
if is_python_require:
if 'python_requires = "tested_reference_str"' not in load(test_conanfile_path):
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
ConanOutput().warning("test_package/conanfile.py should declare "
"'python_requires = \"tested_reference_str\"'",
warn_tag="deprecated")

conan_api.lockfile.save_lockfile(lockfile, args.lockfile_out, cwd)
return {"graph": deps_graph,
Expand All @@ -124,16 +131,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:
Expand Down
4 changes: 2 additions & 2 deletions conan/cli/commands/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -74,5 +74,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
5 changes: 4 additions & 1 deletion conans/client/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions conans/test/integration/py_requires/python_requires_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
""")
Expand All @@ -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
"""
Expand Down Expand Up @@ -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

Expand Down