From 748b41762603cb3f634119beaf87ca4baeb04e90 Mon Sep 17 00:00:00 2001 From: Juan <35701596+juansblanco@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:37:42 +0100 Subject: [PATCH] Add deactivate_conanvcvars file with message (#15557) * Add deactivate_conanvcvars file with message * Add deactivate_conanvcvars test * make function private * change ps check * add powershell test --- conan/tools/microsoft/visual.py | 23 ++++++++++--- .../toolchains/microsoft/vcvars_test.py | 32 +++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/conan/tools/microsoft/visual.py b/conan/tools/microsoft/visual.py index deed79f5257..0ad6519d474 100644 --- a/conan/tools/microsoft/visual.py +++ b/conan/tools/microsoft/visual.py @@ -6,9 +6,9 @@ from conan.errors import ConanException, ConanInvalidConfiguration from conan.tools.scm import Version from conan.tools.intel.intel_cc import IntelCC +from conans.util.files import save -CONAN_VCVARS_BAT = "conanvcvars.bat" -CONAN_VCVARS_PS1 = "conanvcvars.ps1" +CONAN_VCVARS = "conanvcvars" def check_min_vs(conanfile, version, raise_invalid=True): @@ -151,7 +151,9 @@ def generate(self, scope="build"): {vcvars} """) from conan.tools.env.environment import create_env_script - create_env_script(conanfile, content, CONAN_VCVARS_BAT, scope) + conan_vcvars_bat = f"{CONAN_VCVARS}.bat" + create_env_script(conanfile, content, conan_vcvars_bat, scope) + _create_deactivate_vcvars_file(conanfile, conan_vcvars_bat) is_ps1 = conanfile.conf.get("tools.env.virtualenv:powershell", check_type=bool, default=False) if is_ps1: @@ -167,10 +169,23 @@ def generate(self, scope="build"): Pop-Location write-host conanvcvars.ps1: Activated environment}} """) - create_env_script(conanfile, content_ps1, CONAN_VCVARS_PS1, scope) + conan_vcvars_ps1 = f"{CONAN_VCVARS}.ps1" + create_env_script(conanfile, content_ps1, conan_vcvars_ps1, scope) + _create_deactivate_vcvars_file(conanfile, conan_vcvars_ps1) +def _create_deactivate_vcvars_file(conanfile, filename): + deactivate_filename = f"deactivate_{filename}" + message = f"[{deactivate_filename}]: vcvars env cannot be deactivated" + is_ps1 = filename.endswith(".ps1") + if is_ps1: + content = f"Write-Host {message}" + else: + content = f"echo {message}" + path = os.path.join(conanfile.generators_folder, deactivate_filename) + save(path, content) + def vs_ide_version(conanfile): """ Gets the VS IDE version as string. It'll use the ``compiler.version`` (if exists) and/or the diff --git a/conans/test/integration/toolchains/microsoft/vcvars_test.py b/conans/test/integration/toolchains/microsoft/vcvars_test.py index d17f42cee1a..2cffd01fad3 100644 --- a/conans/test/integration/toolchains/microsoft/vcvars_test.py +++ b/conans/test/integration/toolchains/microsoft/vcvars_test.py @@ -126,3 +126,35 @@ class TestConan(ConanFile): vcvars = client.load("conanvcvars.bat") assert 'vcvarsall.bat" amd64 8.1 -vcvars_ver=14.3' in vcvars + +@pytest.mark.skipif(platform.system() != "Windows", reason="Requires Windows") +def test_deactivate_vcvars_message(): + client = TestClient() + conanfile = textwrap.dedent(""" + from conan import ConanFile + class TestConan(ConanFile): + generators = "VCVars" + settings = "os", "compiler", "arch", "build_type" + """) + client.save({"conanfile.py": conanfile}) + client.run('install . -s compiler.version=193') + client.run_command(r'conanbuild.bat') + assert "[vcvarsall.bat] Environment initialized" in client.out + client.run_command(r'deactivate_conanvcvars.bat') + assert "vcvars env cannot be deactivated" in client.out + +@pytest.mark.skipif(platform.system() != "Windows", reason="Requires Windows Powershell") +def test_deactivate_vcvars_with_powershell(): + client = TestClient() + conanfile = textwrap.dedent(""" + from conan import ConanFile + class TestConan(ConanFile): + generators = "VCVars" + settings = "os", "compiler", "arch", "build_type" + """) + client.save({"conanfile.py": conanfile}) + client.run('install . -c tools.env.virtualenv:powershell=True') + client.run_command(r'powershell.exe ".\conanbuild.ps1"') + assert "conanvcvars.ps1: Activated environment" in client.out + client.run_command(r'powershell.exe ".\deactivate_conanvcvars.ps1"') + assert "vcvars env cannot be deactivated" in client.out