Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
memsharded committed Feb 21, 2021
1 parent 387b0ce commit c26d153
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 32 deletions.
15 changes: 15 additions & 0 deletions conan/tools/env/environment.py
Expand Up @@ -107,6 +107,21 @@ def save_bat(self, filename, generate_deactivate=True, pathsep=os.pathsep):
content = "\n".join(result)
save(filename, content)

def save_ps1(self, filename, generate_deactivate=True, pathsep=os.pathsep):
# FIXME: This is broken and doesnt work
deactivate = ""
capture = textwrap.dedent("""\
{deactivate}
""").format(deactivate=deactivate if generate_deactivate else "")
result = [capture]
for varname, varvalues in self._values.items():
value = self._format_value(varname, varvalues, "$env:{name}", pathsep)
result.append('Write-Output "Error: whatever message {}"'.format(varname))
result.append('$env:{}={}'.format(varname, value))

content = "\n".join(result)
save(filename, content)

def save_sh(self, filename, pathsep=os.pathsep):
capture = textwrap.dedent("""\
echo Capturing current environment in deactivate_{filename}
Expand Down
71 changes: 39 additions & 32 deletions conans/test/unittests/tools/env/test_env.py
@@ -1,3 +1,4 @@
import os
import platform
import subprocess
import textwrap
Expand Down Expand Up @@ -133,46 +134,52 @@ def test_env_files():
echo MyPath4=$MyPath4!!
""")

def check(cmd_):
out, _ = subprocess.Popen(cmd_, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
env=prevenv, shell=True).communicate()
out = out.decode()
assert "MyVar=MyValue!!" in out
assert "MyVar1=MyValue1!!" in out
assert "MyVar2=OldVar2 MyValue2!!" in out
assert "MyVar3=MyValue3 OldVar3!!" in out
assert "MyVar4=!!" in out
assert "MyVar5=MyValue5 With Space5=More Space5;:More!!" in out
assert "MyVar6= MyValue6!!" in out # The previous is non existing, append has space
assert "MyPath1=/Some/Path1/!!" in out
assert "MyPath2=OldPath2:/Some/Path2/:/Other/Path2/!!" in out
assert "MyPath3=/Some/Path3/:OldPath3!!" in out
assert "MyPath4=!!" in out

# This should be output when deactivated
assert "MyVar=!!" in out
assert "MyVar1=OldVar1!!" in out
assert "MyVar2=OldVar2!!" in out
assert "MyVar3=OldVar3!!" in out
assert "MyVar4=OldVar4!!" in out
assert "MyVar5=!!" in out
assert "MyVar6=!!" in out
assert "MyPath1=OldPath1!!" in out
assert "MyPath2=OldPath2!!" in out
assert "MyPath3=OldPath3!!" in out
assert "MyPath4=OldPath4!!" in out

with chdir(folder):
if platform.system() == "Windows":
env.save_bat("test.bat", pathsep=":")
save("display.bat", display_bat)
cmd = "test.bat && display.bat && deactivate_test.bat && display.bat"

exe = None
check(cmd)
# FIXME: Powershell still not working
# env.save_ps1("test.ps1", pathsep=":")
# print(load("test.ps1"))
# cmd = r'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
# check(cmd)
else:
env.save_sh("test.sh")
save("display.sh", display_sh)
cmd = ". test.sh && . display.sh && . deactivate_test.sh && . display.sh"

exe = "/bin/bash"
out, _ = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
env=prevenv, shell=True, executable=exe).communicate()

out = out.decode()
assert "MyVar=MyValue!!" in out
assert "MyVar1=MyValue1!!" in out
assert "MyVar2=OldVar2 MyValue2!!" in out
assert "MyVar3=MyValue3 OldVar3!!" in out
assert "MyVar4=!!" in out
assert "MyVar5=MyValue5 With Space5=More Space5;:More!!" in out
assert "MyVar6= MyValue6!!" in out # The previous is non existing, append has space
assert "MyPath1=/Some/Path1/!!" in out
assert "MyPath2=OldPath2:/Some/Path2/:/Other/Path2/!!" in out
assert "MyPath3=/Some/Path3/:OldPath3!!" in out
assert "MyPath4=!!" in out

assert "MyVar=!!" in out
assert "MyVar1=OldVar1!!" in out
assert "MyVar2=OldVar2!!" in out
assert "MyVar3=OldVar3!!" in out
assert "MyVar4=OldVar4!!" in out
assert "MyVar5=!!" in out
assert "MyVar6=!!" in out
assert "MyPath1=OldPath1!!" in out
assert "MyPath2=OldPath2!!" in out
assert "MyPath3=OldPath3!!" in out
assert "MyPath4=OldPath4!!" in out
os.chmod("display.sh", 0o777)
cmd = 'bash -c ". test.sh && ./display.sh && . deactivate_test.sh && ./display.sh"'
check(cmd)


@pytest.mark.skipif(platform.system() != "Windows", reason="Requires Windows")
Expand Down

0 comments on commit c26d153

Please sign in to comment.