Skip to content

Commit

Permalink
Fix several tests which require os.geteuid (#957)
Browse files Browse the repository at this point in the history
The code itself works on platforms which don't have os.geteuid, so the
tests should too.
  • Loading branch information
cottsay authored May 3, 2024
1 parent 3dd3261 commit 70119e1
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 33 deletions.
9 changes: 6 additions & 3 deletions test/test_rosdep_alpine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions test/test_rosdep_arch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions test/test_rosdep_debian.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions test/test_rosdep_freebsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions test/test_rosdep_gem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 12 additions & 6 deletions test/test_rosdep_npm.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,20 @@ 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 = []
assert [] == installer.get_install_command(['fake'])

# 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

Expand All @@ -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
9 changes: 6 additions & 3 deletions test/test_rosdep_opensuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions test/test_rosdep_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 12 additions & 6 deletions test/test_rosdep_redhat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 70119e1

Please sign in to comment.