Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion cognite/neat/_utils/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def split_on_capitals(text: str) -> list[str]:
def quote_int_value_by_key_in_yaml(content: str, key: str) -> str:
"""Quote a value in a yaml string"""
# This pattern will match the key if it is not already quoted
pattern = rf"^(\s*-?\s*{key}:\s*)([\d_]+)(.*)$"
pattern = rf"^(\s*-?\s*{key}:\s*)([\d_]+)(\s*(?:#.*)?)$"
replacement = r'\1"\2"\3'

return re.sub(pattern, replacement, content, flags=re.MULTILINE)
66 changes: 66 additions & 0 deletions tests/tests_unit/test_utils/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,72 @@ def quote_key_in_yaml_test_cases() -> Iterable[tuple]:
id="Handle comment with quotes after version",
)

yield pytest.param(
"""space: my_space
externalID: myModel
version: 1.0
""",
"""space: my_space
externalID: myModel
version: 1.0
""",
id="version float stays unchanged",
)
yield pytest.param(
"""space: my_space
externalID: myModel
version: 1.0.0
""",
"""space: my_space
externalID: myModel
version: 1.0.0
""",
id="version dotted triple unchanged",
)
yield pytest.param(
"""version: 1.0 # release note""",
"""version: 1.0 # release note""",
id="version float with comment unchanged",
)
yield pytest.param(
"""space: my_space
externalID: myModel
version: v1
""",
"""space: my_space
externalID: myModel
version: v1
""",
id="version alphanumeric unchanged",
)
yield pytest.param(
"""space: my_space
externalID: myModel
version: 1.0.0-beta
""",
"""space: my_space
externalID: myModel
version: 1.0.0-beta
""",
id="version semver prerelease unchanged",
)
yield pytest.param(
"version: 1e2",
"version: 1e2",
id="version scientific notation unchanged",
)
yield pytest.param(
"""space: s
externalId: M
version:
""",
"""space: s
externalId: M
version:
""",
id="version empty same line null unchanged",
)


class TestQuoteKeyInYAML:
@pytest.mark.parametrize("raw, expected", list(quote_key_in_yaml_test_cases()))
Expand Down
Loading