Skip to content

Commit

Permalink
Merge branch 'fix-rex-env-contains' of https://github.com/davidlatwe/rez
Browse files Browse the repository at this point in the history
 into davidlatwe-fix-rex-env-contains
  • Loading branch information
nerdvegas committed Oct 19, 2021
2 parents 57498df + 658322f commit 59b3c57
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/rez/rex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,10 @@ def __repr__(self):
self.value())

def __nonzero__(self):
return bool(self.value())
try:
return bool(self.value())
except RexUndefinedVariableError:
return False

__bool__ = __nonzero__ # py3 compat

Expand Down
26 changes: 26 additions & 0 deletions src/rez/tests/test_rex.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,32 @@ def _rex2():
'BAH': 'omg',
'FOO': os.pathsep.join(['omg', '${BAH}', 'like']) + ', $SHE said, omg'})

def test_10(self):
"""Test env __contains__ and __bool__"""

def _test(func, env, expected):
ex = self._create_executor(env=env)
self.assertEqual(expected, ex.execute_function(func))

def _rex_1():
return {
"A": "A" in env.keys(),
"B": "B" in env.keys(),
}

def _rex_2():
return {
"A": "A" in env,
"B": "B" in env,
}

def _rex_3():
return env.get("B") or "not b"

_test(_rex_1, env={"A": "foo"}, expected={"A": True, "B": False})
_test(_rex_2, env={"A": "foo"}, expected={"A": True, "B": False})
_test(_rex_3, env={}, expected="not b")

def test_version_binding(self):
"""Test the Rex binding of the Version class."""
v = VersionBinding(Version("1.2.3alpha"))
Expand Down
3 changes: 3 additions & 0 deletions src/rez/utils/data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ def __setitem__(self, key, value):
def __delitem__(self, key):
del self._data[key]

def __contains__(self, key):
return key in self._data

def __iter__(self):
return iter(self._data)

Expand Down

0 comments on commit 59b3c57

Please sign in to comment.