Skip to content

Commit

Permalink
Avoid reformatting hexadecimal integers (#4145)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored May 9, 2024
1 parent aaff090 commit c82bffd
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/release-drafter.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---
# see https://github.com/ansible/devtools
_extends: ansible/devtools
# see https://github.com/ansible/team-devtools
_extends: ansible/team-devtools
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
env:
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 859
PYTEST_REQPASS: 860
steps:
- uses: actions/checkout@v4
with:
Expand Down
9 changes: 7 additions & 2 deletions src/ansiblelint/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# To make the type checkers happy, we import from ruamel.yaml.main instead.
from ruamel.yaml.main import YAML
from ruamel.yaml.parser import ParserError
from ruamel.yaml.scalarint import ScalarInt
from ruamel.yaml.scalarint import HexInt, ScalarInt
from yamllint.config import YamlLintConfig

from ansiblelint.constants import (
Expand Down Expand Up @@ -541,7 +541,9 @@ def construct_yaml_int(self, node: ScalarNode) -> Any:
value_s = value_su.replace("_", "")
if value_s[0] in "+-":
value_s = value_s[1:]
if value_s[0] == "0":
if value_s[0:2] == "0x":
ret = HexInt(ret, width=len(value_s) - 2)
elif value_s[0] == "0":
# got an octal in YAML 1.1
ret = OctalIntYAML11(
ret,
Expand Down Expand Up @@ -630,6 +632,9 @@ def choose_scalar_style(self) -> Any:
and len(self.event.value) > 1
):
if self.event.tag == "tag:yaml.org,2002:int" and self.event.implicit[0]:
if self.event.value.startswith("0x"):
self.event.tag = "tag:yaml.org,2002:str"
return ""
# ensures that "0123" string does not lose its quoting
self.event.tag = "tag:yaml.org,2002:str"
self.event.implicit = (True, True, True)
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/formatting-after/fmt-hex.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
d: 0x123 # <-- hex
e: 0x0123
3 changes: 3 additions & 0 deletions test/fixtures/formatting-before/fmt-hex.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
d: 0x123 # <-- hex
e: 0x0123
3 changes: 3 additions & 0 deletions test/fixtures/formatting-prettier/fmt-hex.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
d: 0x123 # <-- hex
e: 0x0123
1 change: 1 addition & 0 deletions test/test_yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ def test_fmt(before: str, after: str, version: tuple[int, int] | None) -> None:
pytest.param("fmt-3.yml", (1, 1), id="3"),
pytest.param("fmt-4.yml", (1, 1), id="4"),
pytest.param("fmt-5.yml", (1, 1), id="5"),
pytest.param("fmt-hex.yml", (1, 1), id="hex"),
),
)
def test_formatted_yaml_loader_dumper(
Expand Down

0 comments on commit c82bffd

Please sign in to comment.