Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

powershell: fix prepend for new variables and fix unsetenv for non-existing variables #1477

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/rez/tests/test_shells.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ def _rex_appending():
from rez.shells import create_shell
sh = create_shell()

env.FOO.unset()
env.FOO.append("hey")
info(sh.get_key_token("FOO"))
env.FOO.append(literal("$DAVE"))
Expand All @@ -434,6 +435,26 @@ def _rex_appending():

_execute_code(_rex_appending, expected_output)

def _rex_prepending():
from rez.shells import create_shell
sh = create_shell()

env.FOO.unset()
env.FOO.prepend("hey")
info(sh.get_key_token("FOO"))
env.FOO.prepend(literal("$DAVE"))
info(sh.get_key_token("FOO"))
env.FOO.prepend("Dave's not here man")
info(sh.get_key_token("FOO"))

expected_output = [
"hey",
sh.pathsep.join(["$DAVE", "hey"]),
sh.pathsep.join(["Dave's not here man", "$DAVE", "hey"])
]

_execute_code(_rex_prepending, expected_output)

@per_available_shell()
def test_rex_code_alias(self, shell):
"""Ensure PATH changes do not influence the alias command.
Expand Down
8 changes: 5 additions & 3 deletions src/rezplugins/shell/_utils/powershell_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ def prependenv(self, key, value):
# Be careful about ambiguous case in pwsh on Linux where pathsep is :
# so that the ${ENV:VAR} form has to be used to not collide.
self._addline(
'Set-Item -Path "Env:{0}" -Value ("{1}{2}" + (Get-ChildItem "Env:{0}").Value)'.format(
key, value, self.pathsep)
'Set-Item -Path "Env:{0}" -Value ("{1}{2}" + (Get-ChildItem -ErrorAction SilentlyContinue "Env:{0}").Value)'
.format(key, value, self.pathsep)
)

def appendenv(self, key, value):
Expand All @@ -280,7 +280,9 @@ def appendenv(self, key, value):
.format(key, os.path.pathsep, value))

def unsetenv(self, key):
self._addline(r"Remove-Item Env:\%s" % key)
self._addline(
'Remove-Item -ErrorAction SilentlyContinue "Env:{0}"'.format(key)
)

def resetenv(self, key, value, friends=None):
self._addline(self.setenv(key, value))
Expand Down
Loading