|
6 | 6 | test shell invocation
|
7 | 7 | """
|
8 | 8 | from __future__ import print_function
|
| 9 | +import os |
9 | 10 |
|
| 11 | +from rez.config import config |
10 | 12 | from rez.shells import create_shell
|
11 | 13 | from rez.resolved_context import ResolvedContext
|
12 | 14 | from rez.tests.util import TestBase, TempdirMixin, per_available_shell
|
| 15 | +from rez.utils.filesystem import canonical_path |
13 | 16 | import unittest
|
14 | 17 | import subprocess
|
15 | 18 |
|
@@ -38,19 +41,111 @@ def _create_context(cls, pkgs):
|
38 | 41 |
|
39 | 42 | @per_available_shell()
|
40 | 43 | def test_shell_execution(self, shell):
|
| 44 | + """Test that a shell can be invoked.""" |
41 | 45 | sh = create_shell(shell)
|
42 | 46 | _, _, _, command = sh.startup_capabilities(command=True)
|
43 | 47 | if command:
|
44 | 48 | r = self._create_context(["shell"])
|
45 | 49 | 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") |
47 | 53 | self.assertEqual(p.returncode, 0)
|
48 | 54 |
|
49 | 55 | if p.returncode:
|
50 | 56 | raise RuntimeError(
|
51 | 57 | "The subprocess failed with exitcode %d" % p.returncode
|
52 | 58 | )
|
53 | 59 |
|
| 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 | + |
54 | 149 |
|
55 | 150 | if __name__ == "__main__":
|
56 | 151 | unittest.main()
|
0 commit comments