diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 5fd4dc3..15f58dc 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -35,8 +35,6 @@ jobs: run: make lint - name: Test type checkers run: make mypy - - name: Test security - run: make security - name: Test with pytest run: make test - name: Upload Coverage diff --git a/src/yamlfix/adapters.py b/src/yamlfix/adapters.py index ac2d0d4..dbb845b 100644 --- a/src/yamlfix/adapters.py +++ b/src/yamlfix/adapters.py @@ -579,15 +579,17 @@ def _fix_comments(self, source_code: str) -> str: for line in source_code.splitlines(): # Comment at the start of the line - if config.comments_require_starting_space and re.search(r"(^|\s)#\w", line): - line = line.replace("#", "# ") + if config.comments_require_starting_space: + line = re.sub(r"^(|[^\"']*\s)#(\w)", r"\1# \2", line) # Comment in the middle of the line, but it's not part of a string if ( config.comments_min_spaces_from_content > 1 and " #" in line and line[-1] not in ["'", '"'] ): - line = re.sub(r"(.+\S)(\s+?)#", rf"\1{comment_start}", line) + line = re.sub( + r"^([^\"']*[^\"' \t])(\s+?)#", rf"\1{comment_start}", line + ) fixed_source_lines.append(line) return "\n".join(fixed_source_lines) diff --git a/tests/unit/test_adapter_yaml.py b/tests/unit/test_adapter_yaml.py index 9ede42a..b56739d 100644 --- a/tests/unit/test_adapter_yaml.py +++ b/tests/unit/test_adapter_yaml.py @@ -80,6 +80,7 @@ def test_comment_spacing_config(self) -> None: --- # comment project_name: yamlfix #comment + "key #1": 'value #2' """ ) fixed_source = dedent( @@ -87,6 +88,7 @@ def test_comment_spacing_config(self) -> None: --- # comment project_name: yamlfix # comment + 'key #1': 'value #2' """ ) config = YamlfixConfig()