test_doc.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # -*- coding: utf-8 -*-
  2. '''
  3. tests.unit.doc_test
  4. ~~~~~~~~~~~~~~~~~~~~
  5. '''
  6. # Import Python libs
  7. from __future__ import absolute_import
  8. import os
  9. import re
  10. import logging
  11. # Import Salt Testing libs
  12. from tests.support.unit import TestCase
  13. from tests.support.runtests import RUNTIME_VARS
  14. # Import Salt libs
  15. import salt.modules.cmdmod
  16. import salt.utils.platform
  17. log = logging.getLogger(__name__)
  18. class DocTestCase(TestCase):
  19. '''
  20. Unit test case for testing doc files and strings.
  21. '''
  22. def test_check_for_doc_inline_markup(self):
  23. '''
  24. We should not be using the ``:doc:`` inline markup option when
  25. cross-referencing locations. Use ``:ref:`` or ``:mod:`` instead.
  26. This test checks for reference to ``:doc:`` usage.
  27. See Issue #12788 for more information.
  28. https://github.com/saltstack/salt/issues/12788
  29. '''
  30. salt_dir = RUNTIME_VARS.CODE_DIR
  31. if salt.utils.platform.is_windows():
  32. if salt.utils.path.which('bash'):
  33. # Use grep from git-bash when it exists.
  34. cmd = 'bash -c \'grep -r :doc: ./salt/'
  35. grep_call = salt.modules.cmdmod.run_stdout(cmd=cmd, cwd=salt_dir).split(os.linesep)
  36. os_sep = '/'
  37. else:
  38. # No grep in Windows, use findstr
  39. # findstr in windows doesn't prepend 'Binary` to binary files, so
  40. # use the '/P' switch to skip files with unprintable characters
  41. cmd = 'findstr /C:":doc:" /S /P {0}\\*'.format(salt_dir)
  42. grep_call = salt.modules.cmdmod.run_stdout(cmd=cmd).split(os.linesep)
  43. os_sep = os.sep
  44. else:
  45. salt_dir += '/'
  46. cmd = 'grep -r :doc: ' + salt_dir
  47. grep_call = salt.modules.cmdmod.run_stdout(cmd=cmd).split(os.linesep)
  48. os_sep = os.sep
  49. test_ret = {}
  50. for line in grep_call:
  51. # Skip any .pyc files that may be present
  52. if line.startswith('Binary'):
  53. continue
  54. # Only split on colons not followed by a '\' as is the case with
  55. # Windows Drives
  56. regex = re.compile(r':(?!\\)')
  57. try:
  58. key, val = regex.split(line, 1)
  59. except ValueError:
  60. log.error("Could not split line: %s", line)
  61. continue
  62. # Don't test man pages, this file, the tox or nox virtualenv files,
  63. # the page that documents to not use ":doc:", the doc/conf.py file
  64. # or the artifacts directory on nox CI test runs
  65. if 'man' in key \
  66. or '.tox{}'.format(os_sep) in key \
  67. or '.nox{}'.format(os_sep) in key \
  68. or 'ext{}'.format(os_sep) in key \
  69. or 'artifacts{}'.format(os_sep) in key \
  70. or key.endswith('test_doc.py') \
  71. or key.endswith(os_sep.join(['doc', 'conf.py'])) \
  72. or key.endswith(os_sep.join(['conventions', 'documentation.rst'])) \
  73. or key.endswith(os_sep.join(['doc', 'topics', 'releases', '2016.11.2.rst'])) \
  74. or key.endswith(os_sep.join(['doc', 'topics', 'releases', '2016.11.3.rst'])) \
  75. or key.endswith(os_sep.join(['doc', 'topics', 'releases', '2016.3.5.rst'])):
  76. continue
  77. # Set up test return dict
  78. if test_ret.get(key) is None:
  79. test_ret[key] = [val.strip()]
  80. else:
  81. test_ret[key].append(val.strip())
  82. # Allow test results to show files with :doc: ref, rather than truncating
  83. self.maxDiff = None
  84. # test_ret should be empty, otherwise there are :doc: references present
  85. self.assertEqual(test_ret, {})