Skip to content

Commit

Permalink
powershell: fix prepend for new variables and fix unsetenv for non-ex…
Browse files Browse the repository at this point in the history
…isting variables (#1477)

* Fix env prepend failing for new env vars @SitiSchu
* implement modify existing and add new test to exercise rex usage of 'unset' and 'prependenv' @maxnbk
* extend same prepend/append logic to powershell unset @maxnbk

---------

Signed-off-by: SitiSchu <admin@sitischu.com>
Signed-off-by: Stephen Mackenzie <maxnbk@users.noreply.github.com>
Co-authored-by: SitiSchu <admin@sitischu.com>
  • Loading branch information
maxnbk and SitiSchu authored Sep 9, 2023
1 parent 810c72a commit bb0d148
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
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

0 comments on commit bb0d148

Please sign in to comment.