From 4211ec259d7d2e10e574fea3560b38aa0a8ad2c9 Mon Sep 17 00:00:00 2001 From: Scott K Logan Date: Tue, 30 Apr 2024 18:28:40 -0500 Subject: [PATCH] Fix several tests which require os.geteuid The code itself works on platforms which don't have os.geteuid, so the tests should too. --- test/test_rosdep_alpine.py | 9 ++++++--- test/test_rosdep_arch.py | 9 ++++++--- test/test_rosdep_debian.py | 9 ++++++--- test/test_rosdep_freebsd.py | 9 ++++++--- test/test_rosdep_gem.py | 9 ++++++--- test/test_rosdep_npm.py | 18 ++++++++++++------ test/test_rosdep_opensuse.py | 9 ++++++--- test/test_rosdep_pip.py | 9 ++++++--- test/test_rosdep_redhat.py | 18 ++++++++++++------ 9 files changed, 66 insertions(+), 33 deletions(-) diff --git a/test/test_rosdep_alpine.py b/test/test_rosdep_alpine.py index 7e78dff2a..a1aac7729 100644 --- a/test/test_rosdep_alpine.py +++ b/test/test_rosdep_alpine.py @@ -117,9 +117,12 @@ def test(expected_prefix, mock_method): assert val == expected, 'Result was: %s' % val try: - with patch('rosdep2.installers.os.geteuid', return_value=1): - test(['sudo', '-H']) - with patch('rosdep2.installers.os.geteuid', return_value=0): + if hasattr(os, 'geteuid'): + with patch('rosdep2.installers.os.geteuid', return_value=1): + test(['sudo', '-H']) + with patch('rosdep2.installers.os.geteuid', return_value=0): + test([]) + else: test([]) except AssertionError: traceback.print_exc() diff --git a/test/test_rosdep_arch.py b/test/test_rosdep_arch.py index 5c7ccc03c..219b6642f 100644 --- a/test/test_rosdep_arch.py +++ b/test/test_rosdep_arch.py @@ -58,9 +58,12 @@ def test(expected_prefix, mock_method): val = installer.get_install_command(['whatever'], interactive=True) assert val == expected, val try: - with patch('rosdep2.installers.os.geteuid', return_value=1): - test(['sudo', '-H']) - with patch('rosdep2.installers.os.geteuid', return_value=0): + if hasattr(os, 'geteuid'): + with patch('rosdep2.installers.os.geteuid', return_value=1): + test(['sudo', '-H']) + with patch('rosdep2.installers.os.geteuid', return_value=0): + test([]) + else: test([]) except AssertionError: traceback.print_exc() diff --git a/test/test_rosdep_debian.py b/test/test_rosdep_debian.py index 428b301f4..6c3ae4ec8 100644 --- a/test/test_rosdep_debian.py +++ b/test/test_rosdep_debian.py @@ -136,9 +136,12 @@ def test(expected_prefix, mock_get_packages_to_install, mock_read_stdout): val = installer.get_install_command(['whatever'], interactive=True) assert val == expected, val try: - with patch('rosdep2.installers.os.geteuid', return_value=1): - test(['sudo', '-H']) - with patch('rosdep2.installers.os.geteuid', return_value=0): + if hasattr(os, 'geteuid'): + with patch('rosdep2.installers.os.geteuid', return_value=1): + test(['sudo', '-H']) + with patch('rosdep2.installers.os.geteuid', return_value=0): + test([]) + else: test([]) except AssertionError: traceback.print_exc() diff --git a/test/test_rosdep_freebsd.py b/test/test_rosdep_freebsd.py index e2db8c6ad..bcda1ea3e 100644 --- a/test/test_rosdep_freebsd.py +++ b/test/test_rosdep_freebsd.py @@ -72,9 +72,12 @@ def test(expected_prefix, mock_method): val = installer.get_install_command(['whatever'], interactive=True) assert val == expected, val try: - with patch('rosdep2.installers.os.geteuid', return_value=1): - test(['sudo', '-H']) - with patch('rosdep2.installers.os.geteuid', return_value=0): + if hasattr(os, 'geteuid'): + with patch('rosdep2.installers.os.geteuid', return_value=1): + test(['sudo', '-H']) + with patch('rosdep2.installers.os.geteuid', return_value=0): + test([]) + else: test([]) except AssertionError: traceback.print_exc() diff --git a/test/test_rosdep_gem.py b/test/test_rosdep_gem.py index 1b6f9a43b..cb4b2d78a 100644 --- a/test/test_rosdep_gem.py +++ b/test/test_rosdep_gem.py @@ -106,9 +106,12 @@ def test(expected_prefix, mock_method, mock_is_gem_installed): val = installer.get_install_command(['whatever'], interactive=True) assert val == expected, val try: - with patch('rosdep2.installers.os.geteuid', return_value=1): - test(['sudo', '-H']) - with patch('rosdep2.installers.os.geteuid', return_value=0): + if hasattr(os, 'geteuid'): + with patch('rosdep2.installers.os.geteuid', return_value=1): + test(['sudo', '-H']) + with patch('rosdep2.installers.os.geteuid', return_value=0): + test([]) + else: test([]) except AssertionError: traceback.print_exc() diff --git a/test/test_rosdep_npm.py b/test/test_rosdep_npm.py index b89ce824f..a56c8caa7 100644 --- a/test/test_rosdep_npm.py +++ b/test/test_rosdep_npm.py @@ -91,7 +91,7 @@ def test_no_npm(mock_method): @patch('rosdep2.platforms.npm.is_npm_installed') @patch.object(NpmInstaller, 'get_packages_to_install') - def test(mock_method, mock_is_npm_installed): + def test(expected_prefix, mock_method, mock_is_npm_installed): mock_is_npm_installed.return_value = True installer = NpmInstaller() mock_method.return_value = [] @@ -99,12 +99,12 @@ def test(mock_method, mock_is_npm_installed): # no interactive option with NPM mock_method.return_value = ['a', 'b'] - expected = [['sudo', '-H', 'npm', 'install', '-g', 'a'], - ['sudo', '-H', 'npm', 'install', '-g', 'b']] + expected = [expected_prefix + ['npm', 'install', '-g', 'a'], + expected_prefix + ['npm', 'install', '-g', 'b']] val = installer.get_install_command(['whatever'], interactive=False) assert val == expected, val - expected = [['sudo', '-H', 'npm', 'install', '-g', 'a'], - ['sudo', '-H', 'npm', 'install', '-g', 'b']] + expected = [expected_prefix + ['npm', 'install', '-g', 'a'], + expected_prefix + ['npm', 'install', '-g', 'b']] val = installer.get_install_command(['whatever'], interactive=True) assert val == expected, val @@ -119,7 +119,13 @@ def test(mock_method, mock_is_npm_installed): val = installer.get_install_command(['whatever'], interactive=True) assert val == expected, val try: - test() + if hasattr(os, 'geteuid'): + with patch('rosdep2.installers.os.geteuid', return_value=1): + test(['sudo', '-H']) + with patch('rosdep2.installers.os.geteuid', return_value=0): + test([]) + else: + test([]) except AssertionError: traceback.print_exc() raise diff --git a/test/test_rosdep_opensuse.py b/test/test_rosdep_opensuse.py index fb0817654..42e44399a 100644 --- a/test/test_rosdep_opensuse.py +++ b/test/test_rosdep_opensuse.py @@ -58,9 +58,12 @@ def test(expected_prefix, mock_method): val = installer.get_install_command(['whatever'], interactive=True) assert val == expected, val try: - with patch('rosdep2.installers.os.geteuid', return_value=1): - test(['sudo', '-H']) - with patch('rosdep2.installers.os.geteuid', return_value=0): + if hasattr(os, 'geteuid'): + with patch('rosdep2.installers.os.geteuid', return_value=1): + test(['sudo', '-H']) + with patch('rosdep2.installers.os.geteuid', return_value=0): + test([]) + else: test([]) except AssertionError: traceback.print_exc() diff --git a/test/test_rosdep_pip.py b/test/test_rosdep_pip.py index 2b3a3ba88..2e34ffca5 100644 --- a/test/test_rosdep_pip.py +++ b/test/test_rosdep_pip.py @@ -105,9 +105,12 @@ def test(expected_prefix, mock_method, mock_get_pip_command): val = installer.get_install_command(['whatever'], interactive=True) assert val == expected, val try: - with patch('rosdep2.installers.os.geteuid', return_value=1): - test(['sudo', '-H']) - with patch('rosdep2.installers.os.geteuid', return_value=0): + if hasattr(os, 'geteuid'): + with patch('rosdep2.installers.os.geteuid', return_value=1): + test(['sudo', '-H']) + with patch('rosdep2.installers.os.geteuid', return_value=0): + test([]) + else: test([]) except AssertionError: traceback.print_exc() diff --git a/test/test_rosdep_redhat.py b/test/test_rosdep_redhat.py index 8a40da4ae..39607610e 100644 --- a/test/test_rosdep_redhat.py +++ b/test/test_rosdep_redhat.py @@ -103,9 +103,12 @@ def test(expected_prefix, mock_method): val = installer.get_install_command(['whatever'], interactive=True, quiet=False) assert val == expected, val + expected try: - with patch('rosdep2.installers.os.geteuid', return_value=1): - test(['sudo', '-H']) - with patch('rosdep2.installers.os.geteuid', return_value=0): + if hasattr(os, 'geteuid'): + with patch('rosdep2.installers.os.geteuid', return_value=1): + test(['sudo', '-H']) + with patch('rosdep2.installers.os.geteuid', return_value=0): + test([]) + else: test([]) except AssertionError: traceback.print_exc() @@ -136,9 +139,12 @@ def test(expected_prefix, mock_method): val = installer.get_install_command(['whatever'], interactive=True, quiet=False) assert val == expected, val + expected try: - with patch('rosdep2.installers.os.geteuid', return_value=1): - test(['sudo', '-H']) - with patch('rosdep2.installers.os.geteuid', return_value=0): + if hasattr(os, 'geteuid'): + with patch('rosdep2.installers.os.geteuid', return_value=1): + test(['sudo', '-H']) + with patch('rosdep2.installers.os.geteuid', return_value=0): + test([]) + else: test([]) except AssertionError: traceback.print_exc()