Fix `is_pinned` property of `Requirement`

Previously, requirement version specs starting with `!=` were
incorrectly considered as pinned release requests because the
comparison was being made against a one-char string while the
operator is two-char. This patch changes the check to test against `!`
which is enough to detect this case.
pull/81812/head
Sviatoslav Sydorenko 8 months ago
parent 648a74baee
commit 15c029d5cc
No known key found for this signature in database
GPG Key ID: 9345E8FEA89CA455

@ -580,16 +580,15 @@ class _ComputedReqKindsMixin:
See https://github.com/ansible/ansible/pull/81606 for extra context.
"""
version_string = self.ver[0]
return version_string.isdigit() or not (
version_string == '*' or
version_string.startswith(('<', '>', '!='))
version_spec_start_char = self.ver.strip()[0]
return version_spec_start_char.isdigit() or not (
version_spec_start_char.startswith(('<', '>', '!', '*'))
)
@property
def is_pre_release(self) -> bool:
"""Return whether this candidate has a pre-release version."""
return is_pre_release(self.ver)
return self.is_pinned and is_pre_release(self.ver)
@property
def source_info(self):

@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
# Copyright: (c) 2023, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
"""Tests for depresolver dataclass objects."""
import pytest
from ansible.galaxy.dependency_resolution.dataclasses import Requirement
@pytest.mark.parametrize(
('collection_version_spec', 'expected_is_pinned_outcome'),
(
('1.2.3-dev4', True),
(' 1.2.3-dev4', True),
('=1.2.3', True),
('= 1.2.3', True),
(' = 1.2.3', True),
(' =1.2.3', True),
('==1.2.3', True),
('== 1.2.3', True),
(' == 1.2.3', True),
(' ==1.2.3', True),
('!=1.0.0', False),
('!= 1.0.0', False),
(' != 1.0.0', False),
(' !=1.0.0', False),
('>1.0.0', False),
('> 1.0.0', False),
(' > 1.0.0', False),
(' >1.0.0', False),
('>=1.0.0', False),
('>= 1.0.0', False),
(' >= 1.0.0', False),
(' >=1.0.0', False),
('<1.0.0', False),
('< 1.0.0', False),
(' < 1.0.0', False),
(' <1.0.0', False),
('*', False),
('* ', False),
(' * ', False),
(' *', False),
('=1.2.3,!=1.2.3rc5', True),
),
)
def test_requirement_is_pinned_logic(
collection_version_spec: str,
expected_is_pinned_outcome: bool,
) -> None:
"""Test how Requirement's is_pinned property detects pinned spec."""
assert Requirement(
'namespace.collection', collection_version_spec,
None, None, None,
).is_pinned is expected_is_pinned_outcome
Loading…
Cancel
Save