Coverage for tests/test_enable_disable.py: 100%

1 import re

2 from random import choice

3

4 from sphinxlint.cli import main

5

6 CHECKER_LINE = re.compile(r"^\s*- ([^:]+):", flags=re.MULTILINE)

7

8

9 def parse_checkers(text):

10 """Given a --list output, returns a list of checkers names."""

11 return CHECKER_LINE.findall(text)

12

13

14 def count_checkers(text):

15 return len(parse_checkers(text))

16

17

18 def random_checker(text):

19 return choice(parse_checkers(text))

20

21

22 def test_default(capsys):

23 """Ensure that the output of `--list` includes at least 10 checkers."""

24 main(["sphinxlint", "--list"])

25 out, _err = capsys.readouterr()

26 assert count_checkers(out) > 10

27

28

29 def test_disable_all(capsys):

30 """Checks that disabling all checks actually disables them all."""

31 main(["sphinxlint", "--disable", "all", "--list"])

32 out, _err = capsys.readouterr()

33 assert out == "No checkers selected.\n"

34

35

36 def test_enable_all(capsys):

37 """Some checks are disabled by default, so enabling them all should

38 give more checks than the default list."""

39 main(["sphinxlint", "--list"])

40 default_out, _err = capsys.readouterr()

41 main(["sphinxlint", "--enable", "all", "--list"])

42 all_out, _err = capsys.readouterr()

43 assert count_checkers(default_out) < count_checkers(all_out)

44

45

46 def test_disable_one(capsys):

47 """Disabling a single check from the default set (any of them) should

48 give one check less than the default set."""

49 main(["sphinxlint", "--list"])

50 default_out, _err = capsys.readouterr()

51 one_to_disable = random_checker(default_out)

52 main(["sphinxlint", "--list", "--disable", one_to_disable])

53 disabled_out, _err = capsys.readouterr()

54 assert count_checkers(default_out) - 1 == count_checkers(disabled_out)

55

56

57 def test_enable_one(capsys):

58 """Enabling a single check not enabled by default should give one

59 check more than the default set."""

60 main(["sphinxlint", "--list"])

61 default_out, _err = capsys.readouterr()

62 main(["sphinxlint", "--list", "--enable", "all"])

63 all_out, _err = capsys.readouterr()

64 not_enabled_by_default = list(

65 set(parse_checkers(all_out)) - set(parse_checkers(default_out))

66 )

67 one_to_enable = choice(not_enabled_by_default)

68 main(["sphinxlint", "--list", "--enable", one_to_enable])

69 enabled_out, _err = capsys.readouterr()

70 assert count_checkers(default_out) + 1 == count_checkers(enabled_out)