Skip to content

Commit

Permalink
add remove bool flag for googet glazier action (defaults to False)
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 602524560
  • Loading branch information
mjoliver authored and copybara-github committed Jan 30, 2024
1 parent 410eb25 commit ea98032
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 19 deletions.
21 changes: 17 additions & 4 deletions glazier/lib/googet.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,25 @@ def _GooGet(self) -> str:
else:
return str(constants.SYS_GOOGETROOT)

def LaunchGooGet(self, pkg: str, retries: int, sleep: int,
build_info: 'buildinfo.BuildInfo', **kwargs):
def LaunchGooGet(
self,
pkg: str,
retries: int,
sleep: int,
build_info: 'buildinfo.BuildInfo',
remove: bool = False, # TODO(b/322877528) update docs once tested)
**kwargs,
):
"""Launch the GooGet executable with arguments.
Args:
pkg: package name
retries: number of times to retry a failed GooGet installation
sleep: number of seconds between retry attempts
build_info: current build information - used to get active release branch
remove: uninstall the packages (defaults to installing the package)
**kwargs: optional parameters such as path to GooGet binary, -reinstall,
and/or -sources
and/or -sources
Raises:
GooGetBinaryNotFoundError: If googet.exe cannot be found.
Expand All @@ -169,7 +177,12 @@ def LaunchGooGet(self, pkg: str, retries: int, sleep: int,
# Pass -root as GOOGETROOT environmental variable may not exist
root = '-root=' + root_path

cmd = ['-noconfirm', root, 'install']
cmd = ['-noconfirm', root]

if remove:
cmd.append('remove')
else:
cmd.append('install')

if kwargs['flags']:
cmd.extend(self._AddFlags(kwargs['flags'], build_info.Branch()))
Expand Down
115 changes: 100 additions & 15 deletions glazier/lib/googet_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,91 @@ def test_launch_goo_get(
pkg = 'test_package_v1'
retries = 5
sleep_dur = 30
remove = True
mock_branch.return_value = 'example'

# Use hosts paths
mock_check_winpe.return_value = False

mock_execute_binary.return_value = 0

# Command called successfully
# Uninstall Command called successfully
self.install.LaunchGooGet(
pkg,
retries,
sleep_dur,
self.buildinfo,
remove,
path=path,
flags=[('http://example.com/team-unstable, '
flags=[
(
'http://example.com/team-unstable, '
'http://example.co.uk/secure-unstable, '
'https://example.jp/unstable/'), '-reinstall', 'whatever'])
'https://example.jp/unstable/'
),
'whatever',
],
)
cmd = [
'-noconfirm', f'-root={os.path.dirname(path)}', 'install', '-sources',
('http://example.com/team-unstable, '
'http://example.co.uk/secure-unstable, '
'https://example.jp/unstable/'), '-reinstall', 'whatever'
'-noconfirm',
f'-root={os.path.dirname(path)}',
'remove',
'-sources',
(
'http://example.com/team-unstable, '
'http://example.co.uk/secure-unstable, '
'https://example.jp/unstable/'
),
'whatever',
]
cmd.extend([pkg])
mock_execute_binary.assert_called_with(path, cmd)

remove = False
# Install Command called successfully
self.install.LaunchGooGet(
pkg,
retries,
sleep_dur,
self.buildinfo,
remove,
path=path,
flags=[
(
'http://example.com/team-unstable, '
'http://example.co.uk/secure-unstable, '
'https://example.jp/unstable/'
),
'-reinstall',
'whatever',
],
)
cmd = [
'-noconfirm',
f'-root={os.path.dirname(path)}',
'install',
'-sources',
(
'http://example.com/team-unstable, '
'http://example.co.uk/secure-unstable, '
'https://example.jp/unstable/'
),
'-reinstall',
'whatever',
]
cmd.extend([pkg])
mock_execute_binary.assert_called_with(path, cmd)

# String replacement of sources flag was successful
self.install.LaunchGooGet(
pkg, retries, sleep_dur, self.buildinfo, path=path, flags=self.flags)
pkg,
retries,
sleep_dur,
self.buildinfo,
remove,
path=path,
flags=self.flags,
)
cmd = [
'-noconfirm',
f'-root={os.path.dirname(path)}',
Expand All @@ -95,14 +151,22 @@ def test_launch_goo_get(

# Only pkg
self.install.LaunchGooGet(
pkg, retries, sleep_dur, self.buildinfo, path=None, flags=None)
pkg, retries, sleep_dur, self.buildinfo, remove, path=None, flags=None
)
cmd = ['-noconfirm', f'-root={constants.SYS_GOOGETROOT}', 'install']
cmd.extend([pkg])
mock_execute_binary.assert_called_with(path, cmd)

# No Path
self.install.LaunchGooGet(
pkg, retries, sleep_dur, self.buildinfo, path=None, flags=self.flags)
pkg,
retries,
sleep_dur,
self.buildinfo,
remove,
path=None,
flags=self.flags,
)
cmd = [
'-noconfirm', f'-root={constants.SYS_GOOGETROOT}', 'install',
'-sources',
Expand All @@ -115,7 +179,8 @@ def test_launch_goo_get(

# No flags
self.install.LaunchGooGet(
pkg, retries, sleep_dur, self.buildinfo, path=path, flags=None)
pkg, retries, sleep_dur, self.buildinfo, remove, path=path, flags=None
)
cmd = ['-noconfirm', f'-root={constants.SYS_GOOGETROOT}', 'install']
cmd.extend([pkg])
mock_execute_binary.assert_called_with(path, cmd)
Expand All @@ -124,22 +189,42 @@ def test_launch_goo_get(
with self.assertRaisesRegex(
googet.Error, 'Cannot find path of GooGet binary*'):
self.install.LaunchGooGet(
pkg, retries, sleep_dur, self.buildinfo, path='C:\\abc\\def\\ghi',
flags=self.flags)
pkg,
retries,
sleep_dur,
self.buildinfo,
remove,
path='C:\\abc\\def\\ghi',
flags=self.flags,
)

# Empty Package Name
with self.assertRaisesRegex(
googet.Error, 'Missing package name for GooGet install.'):
self.install.LaunchGooGet(
'', retries, sleep_dur, self.buildinfo, path=path, flags=self.flags)
'',
retries,
sleep_dur,
self.buildinfo,
remove,
path=path,
flags=self.flags,
)

# Non zero return value
mock_execute_binary.side_effect = googet.execute.ExecError('some_command')
with self.assertRaisesRegex(
googet.Error,
'GooGet command failed after ' + str(retries) + ' attempts'):
self.install.LaunchGooGet(
pkg, retries, sleep_dur, self.buildinfo, path=path, flags=self.flags)
pkg,
retries,
sleep_dur,
self.buildinfo,
remove,
path=path,
flags=self.flags,
)

def test_add_flags(self):
branch = 'example'
Expand Down

0 comments on commit ea98032

Please sign in to comment.