Unit tests: make set_module_args() a context manager, and remove copies of it in some tests (#9838)

Make set_module_args() a context manager, and remove copies of set_module_args().

Prepares for Data Tagging.
This commit is contained in:
Felix Fontein 2025-03-07 07:21:03 +01:00 committed by GitHub
parent 402f725424
commit a1781d09dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
84 changed files with 4043 additions and 4302 deletions

View file

@ -55,14 +55,13 @@ class TestGem(ModuleTestCase):
return self.mocker.patch(target)
def test_fails_when_user_install_and_install_dir_are_combined(self):
set_module_args({
with set_module_args({
'name': 'dummy',
'user_install': True,
'install_dir': '/opt/dummy',
})
with pytest.raises(AnsibleFailJson) as exc:
gem.main()
}):
with pytest.raises(AnsibleFailJson) as exc:
gem.main()
result = exc.value.args[0]
assert result['failed']
@ -75,18 +74,17 @@ class TestGem(ModuleTestCase):
# test mocks. The only thing that matters is the assertion that this 'gem install' is
# invoked with '--install-dir'.
set_module_args({
with set_module_args({
'name': 'dummy',
'user_install': False,
'install_dir': '/opt/dummy',
})
}):
self.patch_rubygems_version()
self.patch_installed_versions([])
run_command = self.patch_run_command()
self.patch_rubygems_version()
self.patch_installed_versions([])
run_command = self.patch_run_command()
with pytest.raises(AnsibleExitJson) as exc:
gem.main()
with pytest.raises(AnsibleExitJson) as exc:
gem.main()
result = exc.value.args[0]
assert result['changed']
@ -98,20 +96,19 @@ class TestGem(ModuleTestCase):
# XXX: This test is also extremely fragile because of mocking.
# If this breaks, the only that matters is to check whether '--install-dir' is
# in the run command, and that GEM_HOME is passed to the command.
set_module_args({
with set_module_args({
'name': 'dummy',
'user_install': False,
'install_dir': '/opt/dummy',
'state': 'absent',
})
}):
self.patch_rubygems_version()
self.patch_installed_versions(['1.0.0'])
self.patch_rubygems_version()
self.patch_installed_versions(['1.0.0'])
run_command = self.patch_run_command()
run_command = self.patch_run_command()
with pytest.raises(AnsibleExitJson) as exc:
gem.main()
with pytest.raises(AnsibleExitJson) as exc:
gem.main()
result = exc.value.args[0]
@ -124,17 +121,16 @@ class TestGem(ModuleTestCase):
assert update_environ.get('GEM_HOME') == '/opt/dummy'
def test_passes_add_force_option(self):
set_module_args({
with set_module_args({
'name': 'dummy',
'force': True,
})
}):
self.patch_rubygems_version()
self.patch_installed_versions([])
run_command = self.patch_run_command()
self.patch_rubygems_version()
self.patch_installed_versions([])
run_command = self.patch_run_command()
with pytest.raises(AnsibleExitJson) as exc:
gem.main()
with pytest.raises(AnsibleExitJson) as exc:
gem.main()
result = exc.value.args[0]
assert result['changed']