Skip to content

Commit

Permalink
Add test for Lintable.updated
Browse files Browse the repository at this point in the history
  • Loading branch information
cognifloyd committed Feb 13, 2022
1 parent 2b9004a commit d9d963c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/ansiblelint/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ def content(self) -> str:
def writable(self) -> TextIO:
"""Yield a writable stream that can be used to update ``lintable.content``.
The stream is pre-populated with ``lintable.content``.
Do not close this or use it in a ``with`` context or the updated content
will probably be lost.
"""
Expand Down
71 changes: 70 additions & 1 deletion test/test_file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
from argparse import Namespace
from pathlib import Path
from typing import Any, Dict, Union
from typing import Any, Dict, Optional, Union

import pytest
from _pytest.capture import CaptureFixture
Expand Down Expand Up @@ -197,3 +197,72 @@ def test_guess_project_dir(tmp_path: Path) -> None:
with cwd(str(tmp_path)):
result = guess_project_dir(None)
assert result == str(tmp_path)


@pytest.fixture
def temp_lintable(path: str, content: Optional[str], tmp_path: Path) -> Lintable:
"""Return a Lintable in a temporary directory that is safe to write to."""
lintable = Lintable(tmp_path / path, content)
return lintable


BASIC_PLAYBOOK = """
- name: "playbook"
tasks:
- name: hello
debug:
msg: 'world'
"""


@pytest.mark.parametrize(
("path", "content", "updated_content", "updated"),
(
pytest.param("a.yaml", BASIC_PLAYBOOK, BASIC_PLAYBOOK, False, id="no_change"),
pytest.param(
"b.yaml",
BASIC_PLAYBOOK,
BASIC_PLAYBOOK.replace('"', "'"),
True,
id="updated_quotes",
),
pytest.param(
"c.yaml", BASIC_PLAYBOOK, "# short file\n", True, id="shorten_file"
), # ??? what should this do?
pytest.param("d.yaml", None, BASIC_PLAYBOOK, True, id="new_file"),
pytest.param("e.yaml", BASIC_PLAYBOOK, None, False, id="write_none_fails"),
pytest.param(
"e.yaml",
BASIC_PLAYBOOK,
b"# bytes content\n",
False,
id="write_bytes_fails",
),
),
)
def test_lintable_updated(
temp_lintable: Lintable,
content: Optional[str],
updated_content: Optional[str],
updated: bool,
) -> None:
"""Validate ``Lintable.updated`` when using to ``Lintable.writable``."""
if content is None:
# Accessing content of a new file
with pytest.raises(FileNotFoundError):
_ = temp_lintable.content
else:
assert temp_lintable.content == content

stream = temp_lintable.writable

if not isinstance(updated_content, str):
with pytest.raises(TypeError):
stream.write(updated_content) # type: ignore # we're testing bad types
else:
# The stream is pre-populated with self.content
stream.truncate(0)
stream.write(updated_content)
assert temp_lintable.content == updated_content

assert temp_lintable.updated is updated

0 comments on commit d9d963c

Please sign in to comment.