Skip to content

Commit

Permalink
Fix quotes converting free-form syntax to yaml (#4361)
Browse files Browse the repository at this point in the history
  • Loading branch information
cavcrosby authored Oct 30, 2024
1 parent 8fe1ea1 commit 7b97493
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ ruleset
runas
sarif
scalarint
scalarstring
scancode
schemafile
sdist
Expand Down
4 changes: 2 additions & 2 deletions examples/playbooks/transform-no-free-form.transformed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

- name: Example task with usage for '=' as module params
ansible.builtin.debug:
msg: "'Hello there world'"
msg: "Hello there world"
changed_when: false

- name: Task that has a non-debug string with spaces
ansible.builtin.set_fact:
foo: '"String with spaces"'
foo: "String with spaces"
4 changes: 2 additions & 2 deletions examples/playbooks/vars/strings.transformed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# Make sure that the Transformer does not mangle strings
# TODO: there is a bug in ruamel.yaml that discards some EOL comments

single: single # this is a comment
single: "single" # this is a comment
single_with_double: '"single" quoted' # this is a comment

single_multiline_with_octothorpe: "single over 160 char line to force wrapping. over 160 char line to force wrapping. over 160 char line to force wrapping. over 160\n
# this is not a comment"

double: double # this is a comment
double: "double" # this is a comment
double_with_single: "'double' quoted" # this is a comment

double_multiline_with_octothorpe: "double over 160 char line to force wrapping. over 160 char line to force wrapping. over 160 char line to force wrapping. over 160\n
Expand Down
13 changes: 11 additions & 2 deletions src/ansiblelint/rules/no_free_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
import sys
from typing import TYPE_CHECKING, Any

from ansiblelint.constants import INCLUSION_ACTION_NAMES, LINE_NUMBER_KEY
from ruamel.yaml.scalarstring import DoubleQuotedScalarString, SingleQuotedScalarString

from ansiblelint.constants import (
INCLUSION_ACTION_NAMES,
LINE_NUMBER_KEY,
)
from ansiblelint.rules import AnsibleLintRule, TransformMixin
from ansiblelint.rules.key_order import task_property_sorter

Expand Down Expand Up @@ -124,7 +129,11 @@ def filter_values(
# Keep quoted strings together
quote = v[0]
_, v, remainder = v.split(quote, 2)
v = f"{quote}{v}{quote}"
v = (
DoubleQuotedScalarString
if quote == '"'
else SingleQuotedScalarString
)(v)
else:
try:
v, remainder = v.split(" ", 1)
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,7 @@ def __init__( # pylint: disable=too-many-arguments
# This will only preserve quotes for strings read from the file.
# anything modified by the transform will use no quotes, preferred_quote,
# or the quote that results in the least amount of escaping.
self.preserve_quotes = True

# If needed, we can use this to change null representation to be explicit
# (see https://stackoverflow.com/a/44314840/1134951)
Expand Down
10 changes: 5 additions & 5 deletions test/fixtures/formatting-after/fmt-1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ vegetables: # indented sequence:
- carrot

quoting:
- that should have double quotes
- that should remain in single quotes
- a string with " inside
- "that should have double quotes"
- "that should remain in single quotes"
- 'a string with " inside'
# next line has some undesired trailing spaces:
- a string with ' inside
- "a string with ' inside"
- can't be sure!
# next line should be converted to use double quotes:
- [foo, bar]
- ["foo", "bar"]

inline-dictionary:
- { foo: bar } # should add some spacing between curly braces and content
Expand Down

0 comments on commit 7b97493

Please sign in to comment.