mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-03-22 05:09:12 +00:00
uv_python module: improve check mode for present state to fail when no patch version is available
This commit is contained in:
parent
debdce35e7
commit
5819c8eb7b
2 changed files with 29 additions and 13 deletions
|
|
@ -87,13 +87,17 @@ class UV:
|
|||
"""
|
||||
rc, out, _ = self._find_python("--show-version")
|
||||
if rc == 0:
|
||||
return False, out.split()[0]
|
||||
return False, out.split()[0], False
|
||||
if self.module.check_mode:
|
||||
return True, self.python_version_str
|
||||
_, versions_available, _ = self._list_python()
|
||||
# when uv does not find any available patch version the install command will fail
|
||||
if not json.loads(versions_available):
|
||||
return False, "", True
|
||||
return True, self.python_version_str, False
|
||||
|
||||
cmd = [self.module.get_bin_path("uv", required=True), self.subcommand, "install", self.python_version_str]
|
||||
self.module.run_command(cmd, check_rc=True)
|
||||
return True, self.python_version_str
|
||||
return True, self.python_version_str, False
|
||||
|
||||
def uninstall_python(self):
|
||||
"""
|
||||
|
|
@ -172,7 +176,7 @@ class UV:
|
|||
except ValueError:
|
||||
self.module.fail_json(
|
||||
msg=(
|
||||
f"Version {self.python_version_str} is not available. "
|
||||
f"Version {self.python_version_str} is not available."
|
||||
)
|
||||
)
|
||||
return latest_version
|
||||
|
|
@ -190,13 +194,14 @@ def main():
|
|||
result = dict(
|
||||
changed=False,
|
||||
msg="",
|
||||
failed=False
|
||||
)
|
||||
state = module.params["state"]
|
||||
|
||||
try:
|
||||
uv = UV(module)
|
||||
if state == "present":
|
||||
result["changed"], result["msg"] = uv.install_python()
|
||||
result["changed"], result["msg"], result["failed"] = uv.install_python()
|
||||
elif state == "absent":
|
||||
result["changed"], result["msg"] = uv.uninstall_python()
|
||||
elif state == "latest":
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@
|
|||
assert:
|
||||
that:
|
||||
- failed_install.failed is true
|
||||
- '"Failed to find required executable" in failed_install.msg'
|
||||
|
||||
- name: Install uv
|
||||
shell: |
|
||||
|
|
@ -73,8 +72,7 @@
|
|||
version: 3.15
|
||||
state: latest
|
||||
register: install_latest_python
|
||||
- debug:
|
||||
var: install_latest_python
|
||||
|
||||
- name: Verify python 3.15 installation
|
||||
assert:
|
||||
that:
|
||||
|
|
@ -201,6 +199,21 @@
|
|||
- upgrade_python.failed is false
|
||||
- '"3.13" in upgrade_python.msg'
|
||||
|
||||
- name: Install unexisting python version in check mode
|
||||
uv_python:
|
||||
version: 3.12.13
|
||||
state: present
|
||||
register: unexisting_python_check_mode
|
||||
ignore_errors: true
|
||||
check_mode: true
|
||||
|
||||
- name: Verify unexisting python 3.12.13 install in check mode
|
||||
assert:
|
||||
that:
|
||||
- unexisting_python_check_mode.changed is false
|
||||
- unexisting_python_check_mode.failed is true
|
||||
- 'unexisting_python_check_mode.msg == ""'
|
||||
|
||||
- name: Install unexisting python version
|
||||
uv_python:
|
||||
version: 3.12.13
|
||||
|
|
@ -208,23 +221,21 @@
|
|||
register: unexisting_python
|
||||
ignore_errors: true
|
||||
|
||||
- name: Verify python 3.12.13 deletion
|
||||
- name: Verify unexisting python 3.12.13 install
|
||||
assert:
|
||||
that:
|
||||
- unexisting_python.changed is false
|
||||
- unexisting_python.failed is true
|
||||
- '"No download found for request" in unexisting_python.msg'
|
||||
|
||||
- name: Install unexisting python version
|
||||
- name: Install unexisting python version with latest state
|
||||
uv_python:
|
||||
version: 3.12.13
|
||||
state: latest
|
||||
register: unexisting_python
|
||||
ignore_errors: true
|
||||
|
||||
- name: Verify python 3.12.13 deletion
|
||||
- name: Verify python 3.12.13 deletion with latest state
|
||||
assert:
|
||||
that:
|
||||
- unexisting_python.changed is false
|
||||
- unexisting_python.failed is true
|
||||
- '"Version 3.12.13 is not available" in unexisting_python.msg'
|
||||
Loading…
Add table
Add a link
Reference in a new issue