Skip to content

Commit 5d4809d

Browse files
committed
Add more e2e shell tests
Signed-off-by: javrin <[email protected]>
1 parent 88fcd51 commit 5d4809d

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

src/rez/data/tests/packages/shell/1.0.0/package.py

+3
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55

66

77
def commands():
8+
import os
9+
810
env.PATH.append("{root}")
11+
env.PYTHONPATH.append(os.path.join("{root}", "src"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

src/rez/tests/test_e2e_shells.py

+96-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
test shell invocation
77
"""
88
from __future__ import print_function
9+
import os
910

11+
from rez.config import config
1012
from rez.shells import create_shell
1113
from rez.resolved_context import ResolvedContext
1214
from rez.tests.util import TestBase, TempdirMixin, per_available_shell
15+
from rez.utils.filesystem import canonical_path
1316
import unittest
1417
import subprocess
1518

@@ -38,19 +41,111 @@ def _create_context(cls, pkgs):
3841

3942
@per_available_shell()
4043
def test_shell_execution(self, shell):
44+
"""Test that a shell can be invoked."""
4145
sh = create_shell(shell)
4246
_, _, _, command = sh.startup_capabilities(command=True)
4347
if command:
4448
r = self._create_context(["shell"])
4549
p = r.execute_shell(command="echo asd", stdout=subprocess.PIPE, text=True)
46-
_, _ = p.communicate()
50+
stdout, _ = p.communicate()
51+
52+
self.assertEqual(stdout, "asd\n")
4753
self.assertEqual(p.returncode, 0)
4854

4955
if p.returncode:
5056
raise RuntimeError(
5157
"The subprocess failed with exitcode %d" % p.returncode
5258
)
5359

60+
@per_available_shell()
61+
def test_shell_root_path_normalization(self, shell):
62+
"""Test {root} path is normalized to a platform native canonical path."""
63+
pkg = "shell"
64+
sh = create_shell(shell)
65+
_, _, stdin, _ = sh.startup_capabilities(command=None, stdin=True)
66+
if stdin:
67+
r = self._create_context([pkg])
68+
p = r.execute_shell(
69+
command="echo $REZ_SHELL_ROOT", stdout=subprocess.PIPE, text=True
70+
)
71+
stdout, _ = p.communicate()
72+
version = str(r.get_key("version")[pkg][-1])
73+
expected_result = os.path.join(
74+
self.settings.get("packages_path")[0], pkg, version
75+
)
76+
self.assertEqual(stdout.strip(), canonical_path(expected_result))
77+
78+
def test_shell_pythonpath_normalization(self, shell="gitbash"):
79+
"""Test PYTHONPATHs are being normalized by the shell."""
80+
config.override("default_shell", shell)
81+
config.override("enable_path_normalization", True)
82+
83+
sh = create_shell(shell)
84+
r = self._create_context(["shell"])
85+
p = r.execute_shell(
86+
command="echo $PYTHONPATH", stdout=subprocess.PIPE, text=True
87+
)
88+
stdout, _ = p.communicate()
89+
env = r.get_environ()
90+
self.assertEqual(stdout.strip(), sh.as_shell_path(env["PYTHONPATH"]))
91+
92+
def test_shell_disabled_normalization(self, shell="gitbash"):
93+
"""Test disabled normalization."""
94+
config.override("default_shell", shell)
95+
config.override("enable_path_normalization", False)
96+
97+
sh = create_shell(shell)
98+
r = self._create_context(["shell"])
99+
p = r.execute_shell(
100+
command="echo $PYTHONPATH", stdout=subprocess.PIPE, text=True
101+
)
102+
stdout, _ = p.communicate()
103+
env = r.get_environ()
104+
self.assertEqual(stdout.strip(), sh.as_shell_path(env["PYTHONPATH"]))
105+
106+
p = r.execute_shell(
107+
command="echo $PATH", stdout=subprocess.PIPE, text=True
108+
)
109+
stdout, _ = p.communicate()
110+
self.assertNotEqual(
111+
stdout.strip().split(os.pathsep)[0],
112+
sh.normalize_path(env["PATH"].split(os.pathsep)[0])
113+
)
114+
115+
@per_available_shell()
116+
def test_shell_invoking_script(self, shell):
117+
"""Test script used to invoke the shell."""
118+
sh = create_shell(shell)
119+
120+
_, _, stdin, _ = sh.startup_capabilities(command=None, stdin=True)
121+
if not stdin:
122+
return
123+
124+
r = self._create_context(["shell"])
125+
p = r.execute_shell(
126+
stdin=subprocess.PIPE,
127+
stdout=subprocess.PIPE,
128+
text=True,
129+
)
130+
_, stderr = p.communicate()
131+
self.assertEqual(p.returncode, 0)
132+
assert stderr is None
133+
134+
exec_script = p.args[-1].split()[-1]
135+
lines = []
136+
with open(exec_script, "r") as f:
137+
lines = f.readlines()
138+
139+
self.assertNotEqual(lines, [])
140+
if sh.name() == "gitbash":
141+
self.assertEqual(
142+
lines[0].strip(), "#!/usr/bin/env {}".format(sh.executable_name())
143+
)
144+
assert any(
145+
l.strip() == "'{}' {}".format(sh.executable_filepath(), sh.stdin_arg)
146+
for l in lines
147+
)
148+
54149

55150
if __name__ == "__main__":
56151
unittest.main()

0 commit comments

Comments
 (0)