Skip to content

Commit

Permalink
Stop treating files with only line endings changed as doc-only
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackenmen committed Mar 28, 2023
1 parent 7592d2c commit 4db5717
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/label_doconly_changes/base_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ class HookOutputDict(TypedDict):

def _get_file_from_ref(*, ref: str, filename: str) -> str | None:
try:
# this can't use `check_output()`'s encoding or text kwarg
# instead of `.decode("utf-8")` because that forces universal newline behavior
return subprocess.check_output(
("git", "cat-file", "blob", f"{ref}:{filename}"),
stderr=subprocess.PIPE,
encoding="utf-8",
)
).decode("utf-8")
except subprocess.CalledProcessError as e:
prefixes = tuple(
f"fatal: path '{filename}' {error_msg} '"
Expand Down
5 changes: 4 additions & 1 deletion src/label_doconly_changes/hooks/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
_NodeT = TypeVar("_NodeT", bound=cst.CSTNode)
_ExprParentT = TypeVar("_ExprParentT")
_ExprT = TypeVar("_ExprT")
# ensure consistent default to avoid false positives
# caused by parser's detection mechanism for this value
PARSER_CONFIG = cst.PartialParserConfig(default_newline="\n")


class DocstringLocation(NamedTuple, Generic[_ExprParentT, _ExprT]):
Expand Down Expand Up @@ -69,7 +72,7 @@ def on_visit(self, node: cst.CSTNode) -> bool | None:

@classmethod
def from_contents(cls, contents: str) -> Self:
self = cls(cst.parse_module(contents))
self = cls(cst.parse_module(contents, PARSER_CONFIG))
self.base_node.visit(self)
return self

Expand Down
38 changes: 37 additions & 1 deletion tests/hooks/python_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def leave_List(

def simplify_list(nodes: list[cst.CSTNode], *, max_depth: int = 1) -> str:
visitor = _ListSimplifier(max_depth=max_depth)
return cst.parse_module(repr(nodes)).visit(visitor).code
return cst.parse_module(repr(nodes), python.PARSER_CONFIG).visit(visitor).code


def print_extractor_info(tracker: python.ModuleTracker) -> None:
Expand Down Expand Up @@ -72,3 +72,39 @@ def test_is_doc_only_false(contents_before: str, contents_after: str) -> None:
except Exception:
print_analyzer_info(analyzer)
raise


@pytest.mark.parametrize(
"contents_before,contents_after",
(
(
"x = 1\ny = 2\n",
"x = 1\r\ny = 2\r\n",
),
# LibCST's native parser panics with this one :D
#
# (
# "x = 1\ny = 2\n",
# "x = 1\ry = 2\r\n",
# ),
(
"x = 1\ny = 2\n",
"x = 1\ry = 2\r",
),
(
"x = 1\ny = 2\n",
"x = 1\ny = 2\r\n",
),
(
"x = 1\r\ny = 2\n",
"x = 1\ny = 2\r\n",
),
),
)
def test_different_line_endings(contents_before: str, contents_after: str) -> None:
analyzer = python.PythonAnalyzer(contents_before, contents_after)
try:
assert not analyzer.is_docstring_only()
except Exception:
print_analyzer_info(analyzer)
raise

0 comments on commit 4db5717

Please sign in to comment.