From cfd88466b281d4aecd93b7d2ad37e30f07b2a425 Mon Sep 17 00:00:00 2001 From: javrin Date: Wed, 21 Jun 2023 15:27:59 -0400 Subject: [PATCH] Keep unittests DRY, override `default_shell` in test decorator - Tests that use the the `per_available_shell` decorator were reporting false positives for most cases b/c it appears to have been assumed that execute_shell would execute the intended shell when it was actually picking up the system shell. If shell had been passed to `execute_shell` in most places then this change would not have been needed. After observing that most cases appear to depend on overriding the config I chose to move the override into the decorator to improve DRY-ness. Signed-off-by: javrin --- src/rez/tests/test_e2e_shells.py | 5 ----- src/rez/tests/test_shells.py | 12 ------------ src/rez/tests/test_suites.py | 2 -- src/rez/tests/util.py | 15 +++++++++++++++ 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/rez/tests/test_e2e_shells.py b/src/rez/tests/test_e2e_shells.py index 422945063..979c89266 100644 --- a/src/rez/tests/test_e2e_shells.py +++ b/src/rez/tests/test_e2e_shells.py @@ -7,7 +7,6 @@ """ from __future__ import print_function -from rez.config import config from rez.shells import create_shell from rez.resolved_context import ResolvedContext from rez.tests.util import TestBase, TempdirMixin, per_available_shell @@ -39,10 +38,6 @@ def _create_context(cls, pkgs): @per_available_shell() def test_shell_execution(self, shell): - config.override("default_shell", shell) - if shell == "gitbash": - config.override("enable_path_normalization", True) - sh = create_shell(shell) _, _, _, command = sh.startup_capabilities(command=True) if command: diff --git a/src/rez/tests/test_shells.py b/src/rez/tests/test_shells.py index c39261ae4..3d92e05e7 100644 --- a/src/rez/tests/test_shells.py +++ b/src/rez/tests/test_shells.py @@ -112,10 +112,6 @@ def test_aaa_shell_presence(self): @per_available_shell() def test_no_output(self, shell): - config.override("default_shell", shell) - if shell == "gitbash": - config.override("enable_path_normalization", True) - sh = create_shell(shell) _, _, _, command = sh.startup_capabilities(command=True) if command: @@ -310,8 +306,6 @@ def test_rex_code(self, shell): """Test that Rex code run in the shell creates the environment variable values that we expect. """ - config.override("default_shell", shell) - def _execute_code(func, expected_output): loc = inspect.getsourcelines(func)[0][1:] code = textwrap.dedent('\n'.join(loc)) @@ -481,8 +475,6 @@ def test_rex_code_alias(self, shell): the absolute path to doskey.exe before we modify PATH and continue to use the absolute path after the modifications. """ - config.override("default_shell", shell) - def _execute_code(func): loc = inspect.getsourcelines(func)[0][1:] code = textwrap.dedent('\n'.join(loc)) @@ -509,8 +501,6 @@ def test_alias_command(self, shell): This is important for Windows CMD shell because the doskey.exe isn't executed yet when the alias is being passed. """ - config.override("default_shell", shell) - def _make_alias(ex): ex.alias('hi', 'echo "hi"') @@ -529,8 +519,6 @@ def test_alias_command_with_args(self, shell): This is important for Windows CMD shell because the doskey.exe isn't executed yet when the alias is being passed. """ - config.override("default_shell", shell) - def _make_alias(ex): ex.alias('tell', 'echo') diff --git a/src/rez/tests/test_suites.py b/src/rez/tests/test_suites.py index e2ada77a5..ad76f4a99 100644 --- a/src/rez/tests/test_suites.py +++ b/src/rez/tests/test_suites.py @@ -158,8 +158,6 @@ def test_executable(self, shell): ``` """ - config.override("default_shell", shell) - c_pooh = ResolvedContext(["pooh"]) s = Suite() s.add_context("pooh", c_pooh) diff --git a/src/rez/tests/util.py b/src/rez/tests/util.py index ff04e4549..e607f0b4b 100644 --- a/src/rez/tests/util.py +++ b/src/rez/tests/util.py @@ -245,6 +245,21 @@ def decorator(func): def wrapper(self, shell=None): for shell in shells: print("\ntesting in shell: %s..." % shell) + # The default shell if none is configured is the system shell + # (bash or cmd ususally), so in order to test the other shells + # we need to override the default shell to the one we are + # testing to get a accurate results with more complete coverage. + # + # E.g. create_shell() will use the shell provided by this decorator + # but resoved_context.execute_shell() will use the default shell to + # execute a command if no shell is passed in. + config.override("default_shell", shell) + + # TODO: If & when path normalization is set to True by default, + # this should be removed. For now, we need to enable it for + # gitbash, because it requires path normalization. + if shell == "gitbash": + config.override("enable_path_normalization", True) try: func(self, shell=shell)