From 1c5c5dd1c1a706f254fff332d0256d4bec0e29f1 Mon Sep 17 00:00:00 2001 From: James Date: Mon, 18 Sep 2023 09:19:17 +0200 Subject: [PATCH] fix regresion in Git.run() when win_bash (#14756) --- conan/tools/scm/git.py | 9 +++-- conans/test/functional/tools/scm/test_git.py | 42 ++++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/conan/tools/scm/git.py b/conan/tools/scm/git.py index 26a2c72c5d1..c7ceb0438d7 100644 --- a/conan/tools/scm/git.py +++ b/conan/tools/scm/git.py @@ -1,9 +1,9 @@ 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: @@ -25,9 +25,10 @@ def run(self, cmd): :return: The console output of the command. """ with chdir(self._conanfile, self.folder): - output = StringIO() - self._conanfile.run(f"git {cmd}", stdout=output, quiet=True) - return output.getvalue().strip() + # We tried to use self.conanfile.run(), but it didn't work: + # - when using win_bash, crashing because access to .settings (forbidden in source()) + # - the ``conan source`` command, not passing profiles, buildenv not injected + return check_output_runner("git {}".format(cmd)).strip() def get_commit(self): """ diff --git a/conans/test/functional/tools/scm/test_git.py b/conans/test/functional/tools/scm/test_git.py index 876b7a9c762..4a638ee5f9b 100644 --- a/conans/test/functional/tools/scm/test_git.py +++ b/conans/test/functional/tools/scm/test_git.py @@ -253,6 +253,48 @@ def source(self): assert "pkg/0.1: MYCMAKE: mycmake" in c.out assert "pkg/0.1: MYFILE: myheader!" in c.out + @pytest.mark.tool("msys2") + def test_clone_msys2_win_bash(self): + # To avoid regression in https://github.com/conan-io/conan/issues/14754 + folder = os.path.join(temp_folder(), "myrepo") + url, commit = create_local_git_repo(files={"src/myfile.h": "myheader!", + "CMakeLists.txt": "mycmake"}, folder=folder) + + c = TestClient() + conanfile_win_bash = textwrap.dedent(""" + import os + from conan import ConanFile + from conan.tools.scm import Git + from conan.tools.files import load + + class Pkg(ConanFile): + name = "pkg" + version = "0.1" + win_bash = True + + def layout(self): + self.folders.source = "source" + + def source(self): + git = Git(self) + git.clone(url="{url}", target=".") + git.checkout(commit="{commit}") + self.output.info("MYCMAKE: {{}}".format(load(self, "CMakeLists.txt"))) + self.output.info("MYFILE: {{}}".format(load(self, "src/myfile.h"))) + """) + c.save({"conanfile.py": conanfile_win_bash.format(url=url, commit=commit)}) + conf = "-c tools.microsoft.bash:subsystem=msys2 -c tools.microsoft.bash:path=bash.exe" + c.run(f"create . {conf}") + assert "pkg/0.1: MYCMAKE: mycmake" in c.out + assert "pkg/0.1: MYFILE: myheader!" in c.out + + # It also works in local flow, not running in msys2 at all + c.run(f"source .") + assert "conanfile.py (pkg/0.1): MYCMAKE: mycmake" in c.out + assert "conanfile.py (pkg/0.1): MYFILE: myheader!" in c.out + assert c.load("source/src/myfile.h") == "myheader!" + assert c.load("source/CMakeLists.txt") == "mycmake" + @pytest.mark.tool("git") class TestGitShallowClone: