Skip to content

Commit

Permalink
using conanfile.run() so environment is injected (conan-io#14326)
Browse files Browse the repository at this point in the history
* using conanfile.run() so environment is injected

* fix tests

* fixes

* quiet output as before

* fix tests
  • Loading branch information
memsharded authored and mkmkme committed Jul 26, 2023
1 parent 658bfbd commit 2efaf71
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 27 deletions.
28 changes: 13 additions & 15 deletions conan/tools/apple/apple.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
"""
Expand All @@ -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.)"""
Expand Down
8 changes: 5 additions & 3 deletions conan/tools/scm/git.py
Original file line number Diff line number Diff line change
@@ -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.
"""
Expand All @@ -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):
"""
Expand Down
1 change: 1 addition & 0 deletions conans/client/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions conans/model/conan_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
9 changes: 5 additions & 4 deletions conans/test/functional/tools/scm/test_git_get_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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()

Expand All @@ -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')
Expand All @@ -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()

Expand Down Expand Up @@ -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')
Expand All @@ -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")
Expand Down Expand Up @@ -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')
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion conans/test/utils/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down

0 comments on commit 2efaf71

Please sign in to comment.