Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix quotes converting free-form syntax to yaml #4361

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
@@ -319,6 +319,7 @@ ruleset
runas
sarif
scalarint
scalarstring
scancode
schemafile
sdist
4 changes: 2 additions & 2 deletions examples/playbooks/transform-no-free-form.transformed.yml
Original file line number Diff line number Diff line change
@@ -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
@@ -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
13 changes: 11 additions & 2 deletions src/ansiblelint/rules/no_free_form.py
Original file line number Diff line number Diff line change
@@ -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

@@ -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)
1 change: 1 addition & 0 deletions src/ansiblelint/yaml_utils.py
Original file line number Diff line number Diff line change
@@ -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
alisonlhart marked this conversation as resolved.
Show resolved Hide resolved

# If needed, we can use this to change null representation to be explicit
# (see https://stackoverflow.com/a/44314840/1134951)
10 changes: 5 additions & 5 deletions test/fixtures/formatting-after/fmt-1.yml
Original file line number Diff line number Diff line change
@@ -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
Loading