Skip to content

Commit

Permalink
version test - improve message when value is empty (ansible#74754)
Browse files Browse the repository at this point in the history
When an empty value is provided, no `version` attribute will exist on the `LooseVersion` or
`StrictVersion` object. We catch and handle this, but it's not immediatebly clear that an
AttributeError means an empty value was provided.

Specifically handle the case where value or version are empty and add more
helpful error messages.

Add integration tests.
  • Loading branch information
samdoran authored May 20, 2021
1 parent 83010f6 commit 71e33d2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelogs/fragments/version_compare-error-on-empty.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- version test - improve error message when an empty version is provided
6 changes: 6 additions & 0 deletions lib/ansible/plugins/test/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ def version_compare(value, version, operator='eq', strict=None, version_type=Non
if strict is not None and version_type is not None:
raise errors.AnsibleFilterError("Cannot specify both 'strict' and 'version_type'")

if not value:
raise errors.AnsibleFilterError("Input version value cannot be empty")

if not version:
raise errors.AnsibleFilterError("Version parameter to compare against cannot be empty")

Version = LooseVersion
if strict:
Version = StrictVersion
Expand Down
26 changes: 25 additions & 1 deletion test/integration/targets/test_core/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,24 @@
ignore_errors: yes
register: version_bad_semver

- name: Try version with empty input value
debug:
msg: "{{ '' is version('1.0', '>') }}"
ignore_errors: yes
register: version_empty_input

- name: Try version with empty comparison value
debug:
msg: "{{ '1.0' is version('', '>') }}"
ignore_errors: yes
register: version_empty_comparison

- name: Try version with empty input and comparison values
debug:
msg: "{{ '' is version('', '>') }}"
ignore_errors: yes
register: version_empty_both

- name: Assert version tests work
assert:
that:
Expand All @@ -258,6 +276,12 @@
- version_strict_version_type is failed
- version_bad_version_type is failed
- version_bad_semver is failed
- version_empty_input is failed
- version_empty_input is search('version value cannot be empty')
- version_empty_comparison is failed
- version_empty_comparison is search('to compare against cannot be empty')
- version_empty_both is failed
- version_empty_both is search('version value cannot be empty')

- name: Assert any tests work
assert:
Expand Down Expand Up @@ -288,7 +312,7 @@
- '"off" is not truthy(convert_bool=True)'
- '"fred" is truthy(convert_bool=True)'
- '{} is not truthy'
- '{"key": "value"} is truthy'
- '{"key": "value"} is truthy'

- name: Assert falsy tests work
assert:
Expand Down

0 comments on commit 71e33d2

Please sign in to comment.