/ dev / clint / tests / rules / test_markdown_link.py
test_markdown_link.py
  1  from pathlib import Path
  2  
  3  from clint.config import Config
  4  from clint.index import SymbolIndex
  5  from clint.linter import Position, Range, lint_file
  6  from clint.rules.markdown_link import MarkdownLink
  7  
  8  
  9  def test_markdown_link(index: SymbolIndex) -> None:
 10      code = '''
 11  # Bad
 12  def function_with_markdown_link():
 13      """
 14      This function has a [markdown link](https://example.com).
 15      """
 16  
 17  async def async_function_with_markdown_link():
 18      """
 19      This async function has a [markdown link](https://example.com).
 20      """
 21  
 22  class MyClass:
 23      """
 24      Class with [another markdown link](https://test.com).
 25      """
 26  
 27  # Good
 28  def function_with_rest_link():
 29      """
 30      This function has a `reST link <https://example.com>`_.
 31      """
 32  '''
 33  
 34      config = Config(select={MarkdownLink.name})
 35      violations = lint_file(Path("test.py"), code, config, index)
 36      assert len(violations) == 3
 37      assert all(isinstance(v.rule, MarkdownLink) for v in violations)
 38      assert violations[0].range == Range(Position(3, 4))
 39      assert violations[1].range == Range(Position(8, 4))
 40      assert violations[2].range == Range(Position(13, 4))
 41  
 42  
 43  def test_markdown_link_disable_on_end_line(index: SymbolIndex) -> None:
 44      code = '''
 45  def func():
 46      """
 47      Docstring with [markdown link](url).
 48      """  # clint: disable=markdown-link
 49      pass
 50  
 51  async def async_func():
 52      """
 53      Async docstring with [markdown link](url).
 54      """  # clint: disable=markdown-link
 55      pass
 56  
 57  class MyClass:
 58      """
 59      Class docstring with [markdown link](url).
 60      """  # clint: disable=markdown-link
 61      pass
 62  
 63  # This should still be detected (no disable comment)
 64  def func_without_disable():
 65      """
 66      Docstring with [markdown link](url).
 67      """
 68      pass
 69  '''
 70  
 71      config = Config(select={MarkdownLink.name})
 72      violations = lint_file(Path("test.py"), code, config, index)
 73      # Only the last function without disable comment should have a violation
 74      assert len(violations) == 1
 75      assert isinstance(violations[0].rule, MarkdownLink)
 76  
 77  
 78  def test_markdown_link_disable_multiple_rules(index: SymbolIndex) -> None:
 79      code = '''
 80  def func():
 81      """
 82      Docstring with [markdown link](url).
 83      """  # clint: disable=markdown-link,other-rule
 84      pass
 85  
 86  def func2():
 87      """
 88      Docstring with [markdown link](url).
 89      """  # clint: disable=other-rule, markdown-link
 90      pass
 91  
 92  # This should still be detected (markdown-link not in disable list)
 93  def func_without_markdown_disable():
 94      """
 95      Docstring with [markdown link](url).
 96      """  # clint: disable=some-other-rule
 97      pass
 98  '''
 99  
100      config = Config(select={MarkdownLink.name})
101      violations = lint_file(Path("test.py"), code, config, index)
102      # Only the last function should have a violation (markdown-link not disabled)
103      assert len(violations) == 1
104      assert isinstance(violations[0].rule, MarkdownLink)