diff --git a/conan/tools/apple/apple.py b/conan/tools/apple/apple.py index 8f51b8ef273..e3adf52805d 100644 --- a/conan/tools/apple/apple.py +++ b/conan/tools/apple/apple.py @@ -1,4 +1,5 @@ import os +from io import StringIO from conans.util.runners import check_output_runner from conan.tools.build import cmd_args_to_string @@ -85,7 +86,7 @@ def apple_min_version_flag(os_version, os_sdk, subsystem): return f"{flag}={os_version}" if flag else '' -class XCRun(object): +class XCRun: """ XCRun is a wrapper for the Apple **xcrun** tool used to get information for building. """ @@ -95,31 +96,28 @@ def __init__(self, conanfile, sdk=None, use_settings_target=False): :param conanfile: Conanfile instance. :param sdk: Will skip the flag when ``False`` is passed and will try to adjust the sdk it automatically if ``None`` is passed. - :param target_settings: Try to use ``settings_target`` in case they exist (``False`` by default) + :param use_settings_target: Try to use ``settings_target`` in case they exist (``False`` by default) """ - settings = None - if conanfile: - settings = conanfile.settings - if use_settings_target and conanfile.settings_target is not None: - settings = conanfile.settings_target + settings = conanfile.settings + if use_settings_target and conanfile.settings_target is not None: + settings = conanfile.settings_target - if sdk is None and settings: - sdk = settings.get_safe('os.sdk') + if sdk is None and settings: + sdk = settings.get_safe('os.sdk') + self._conanfile = conanfile self.settings = settings self.sdk = sdk def _invoke(self, args): - def cmd_output(cmd): - from conans.util.runners import check_output_runner - cmd_str = cmd_args_to_string(cmd) - return check_output_runner(cmd_str).strip() - command = ['xcrun'] if self.sdk: command.extend(['-sdk', self.sdk]) command.extend(args) - return cmd_output(command) + output = StringIO() + cmd_str = cmd_args_to_string(command) + self._conanfile.run(f"{cmd_str}", stdout=output, quiet=True) + return output.getvalue().strip() def find(self, tool): """find SDK tools (e.g. clang, ar, ranlib, lipo, codesign, etc.)""" diff --git a/conan/tools/scm/git.py b/conan/tools/scm/git.py index 252386bb25f..26a2c72c5d1 100644 --- a/conan/tools/scm/git.py +++ b/conan/tools/scm/git.py @@ -1,12 +1,12 @@ import os +from io import StringIO from conan.tools.files import chdir from conan.errors import ConanException from conans.util.files import mkdir -from conans.util.runners import check_output_runner -class Git(object): +class Git: """ Git is a wrapper for several common patterns used with *git* tool. """ @@ -25,7 +25,9 @@ def run(self, cmd): :return: The console output of the command. """ with chdir(self._conanfile, self.folder): - return check_output_runner("git {}".format(cmd)).strip() + output = StringIO() + self._conanfile.run(f"git {cmd}", stdout=output, quiet=True) + return output.getvalue().strip() def get_commit(self): """ diff --git a/conans/client/loader.py b/conans/client/loader.py index b6e3a2cb447..cbc1e33dcb1 100644 --- a/conans/client/loader.py +++ b/conans/client/loader.py @@ -202,6 +202,7 @@ def load_conanfile_txt(self, conan_txt_path): path, basename = os.path.split(conan_txt_path) display_name = basename conanfile = self._parse_conan_txt(contents, path, display_name) + conanfile._conan_helpers = self._conanfile_helpers conanfile._conan_is_consumer = True return conanfile diff --git a/conans/model/conan_file.py b/conans/model/conan_file.py index 33847dc1477..3899afb073c 100644 --- a/conans/model/conan_file.py +++ b/conans/model/conan_file.py @@ -325,8 +325,8 @@ def run(self, command, stdout=None, cwd=None, ignore_errors=False, env="", quiet envfiles_folder = self.generators_folder or os.getcwd() wrapped_cmd = command_env_wrapper(self, command, env, envfiles_folder=envfiles_folder) from conans.util.runners import conan_run - ConanOutput().writeln(f"{self.display_name}: RUN: {command if not quiet else '*hidden*'}", - fg=Color.BRIGHT_BLUE) + if not quiet: + ConanOutput().writeln(f"{self.display_name}: RUN: {command}", fg=Color.BRIGHT_BLUE) retcode = conan_run(wrapped_cmd, cwd=cwd, stdout=stdout, shell=shell) ConanOutput().writeln("") diff --git a/conans/test/functional/toolchains/meson/test_cross_compilation.py b/conans/test/functional/toolchains/meson/test_cross_compilation.py index 14d52bc6a5d..d5c72e93b46 100644 --- a/conans/test/functional/toolchains/meson/test_cross_compilation.py +++ b/conans/test/functional/toolchains/meson/test_cross_compilation.py @@ -7,7 +7,9 @@ from conan.tools.apple.apple import _to_apple_arch, XCRun from conans.test.assets.sources import gen_function_cpp, gen_function_h +from conans.test.utils.mocks import MockConanfile from conans.test.utils.tools import TestClient +from conans.util.runners import conan_run _conanfile_py = textwrap.dedent(""" from conan import ConanFile @@ -103,7 +105,8 @@ def test_apple_meson_toolchain_cross_compiling(arch, os_, os_version, os_sdk): demo = os.path.join(t.current_folder, "build", "demo") assert os.path.isfile(demo) is True - xcrun = XCRun(None, os_sdk) + conanfile = MockConanfile({}, runner=conan_run) + xcrun = XCRun(conanfile, os_sdk) lipo = xcrun.find('lipo') t.run_command('"%s" -info "%s"' % (lipo, libhello)) diff --git a/conans/test/functional/toolchains/meson/test_meson_and_objc.py b/conans/test/functional/toolchains/meson/test_meson_and_objc.py index 1141bc25bff..6e0629ca123 100644 --- a/conans/test/functional/toolchains/meson/test_meson_and_objc.py +++ b/conans/test/functional/toolchains/meson/test_meson_and_objc.py @@ -6,7 +6,9 @@ import pytest from conan.tools.apple.apple import _to_apple_arch, XCRun +from conans.test.utils.mocks import MockConanfile from conans.test.utils.tools import TestClient +from conans.util.runners import conan_run _conanfile_py = textwrap.dedent(""" from conan import ConanFile @@ -133,7 +135,8 @@ def test_apple_meson_toolchain_cross_compiling_and_objective_c(arch, os_, os_ver demo = os.path.join(t.current_folder, "build", "demo") assert os.path.isfile(demo) is True - xcrun = XCRun(None, sdk) + conanfile = MockConanfile({}, runner=conan_run) + xcrun = XCRun(conanfile, sdk) lipo = xcrun.find('lipo') t.run_command('"%s" -info "%s"' % (lipo, demo)) assert "architecture: %s" % _to_apple_arch(arch) in t.out diff --git a/conans/test/functional/tools/scm/test_git_get_commit.py b/conans/test/functional/tools/scm/test_git_get_commit.py index 40c718e1d18..1cd626744e9 100644 --- a/conans/test/functional/tools/scm/test_git_get_commit.py +++ b/conans/test/functional/tools/scm/test_git_get_commit.py @@ -6,6 +6,7 @@ from conans.test.utils.mocks import MockConanfile from conans.test.utils.tools import TestClient from conans.util.files import chdir +from conans.util.runners import conan_run @pytest.mark.tool("git") @@ -14,7 +15,6 @@ def test_change_branch_in_root_commit(): https://github.com/conan-io/conan/issues/10971#issuecomment-1089316912 """ c = TestClient() - conanfile = MockConanfile({}) c.save({"root.txt": "", "subfolder/subfolder.txt": ""}) c.run_command("git init .") c.run_command("git checkout -B master") @@ -29,6 +29,7 @@ def test_change_branch_in_root_commit(): c.run_command("git checkout master") c.run_command('git merge --no-ff change_branch -m "Merge branch"') + conanfile = MockConanfile({}, runner=conan_run) git = Git(conanfile, folder=c.current_folder) commit_conan = git.get_commit() @@ -40,7 +41,6 @@ def test_change_branch_in_root_commit(): @pytest.mark.tool("git") def test_multi_folder_repo(): c = TestClient() - conanfile = MockConanfile({}) c.save({"lib_a/conanfile.py": ""}) c.run_command("git init .") c.run_command('git config user.name myname') @@ -58,6 +58,7 @@ def test_multi_folder_repo(): c.run_command('git commit -m "root change"') # Git object for lib_a + conanfile = MockConanfile({}, runner=conan_run) git = Git(conanfile, folder=os.path.join(c.current_folder, "lib_a")) commit_libA = git.get_commit() @@ -108,7 +109,6 @@ def test_multi_folder_repo(): @pytest.mark.tool("git") def test_relative_folder_repo(): c = TestClient() - conanfile = MockConanfile({}) c.save({"lib_a/conanfile.py": ""}) c.run_command("git init .") c.run_command('git config user.name myname') @@ -125,6 +125,7 @@ def test_relative_folder_repo(): c.run_command("git add .") c.run_command('git commit -m "root change"') + conanfile = MockConanfile({}, runner=conan_run) # Relative paths for folders, from the current_folder with chdir(c.current_folder): git = Git(conanfile, folder="lib_a") @@ -174,7 +175,6 @@ def test_relative_folder_repo(): @pytest.mark.tool("git") def test_submodule_repo(): c = TestClient() - conanfile = MockConanfile({}) c.save({"conanfile.py": ""}) c.run_command("git init .") c.run_command('git config user.name myname') @@ -188,6 +188,7 @@ def test_submodule_repo(): c.run_command("git add .") c.run_command('git commit -m "root change"') + conanfile = MockConanfile({}, runner=conan_run) with chdir(c.current_folder): # default case git = Git(conanfile) diff --git a/conans/test/utils/mocks.py b/conans/test/utils/mocks.py index c3902f27d54..571df609ce8 100644 --- a/conans/test/utils/mocks.py +++ b/conans/test/utils/mocks.py @@ -106,7 +106,7 @@ def clear(self): def run(self, *args, **kwargs): if self.runner: - kwargs["output"] = None + kwargs.pop("quiet", None) self.runner(*args, **kwargs)