def _check_dir()

in tools/check_documentation.py [0:0]


def _check_dir(dir_name, exclude_files=None, files=False, show_extra=False):
  'Invoke tfdoc on folder, using the relevant options.'
  dir_path = BASEDIR / dir_name
  for readme_path in sorted(dir_path.glob('**/README.md')):
    if '.terraform' in str(readme_path):
      continue

    diff = None
    readme = readme_path.read_text()
    readme_rel = str(readme_path.relative_to(BASEDIR))
    current_doc = tfdoc.get_tfref_parts(readme)
    current_toc = tfdoc.get_toc_parts(readme)
    if current_doc or current_toc:
      new_doc = tfdoc.create_tfref(readme_path.parent, files, show_extra,
                                   exclude_files, readme)
      new_toc = tfdoc.create_toc(readme)
      newvars = new_doc.variables
      newouts = new_doc.outputs
      variables = [v.name for v in newvars if v.file.endswith('variables.tf')]
      outputs = [o.name for o in newouts if o.file.endswith('outputs.tf')]

      state = State.OK

      if current_doc and new_doc.content != current_doc['doc']:
        state = State.FAIL_STALE_README
        header = f'----- {readme_rel} diff -----\n'
        ndiff = difflib.ndiff(current_doc['doc'].splitlines(keepends=True),
                              new_doc.content.splitlines(keepends=True))
        diff = ''.join([header] + [x for x in ndiff if x[0] != ' '])

      elif current_toc and new_toc != current_toc['toc']:
        state = State.FAIL_STALE_TOC
        header = f'----- {readme_rel} diff -----\n'
        ndiff = difflib.ndiff(current_toc['toc'].splitlines(keepends=True),
                              new_toc.splitlines(keepends=True))
        diff = ''.join([header] + [x for x in ndiff if x[0] != ' '])

      elif empty := [v.name for v in newvars if not v.description]:
        state = state.FAIL_VARIABLE_DESCRIPTION
        diff = "\n".join([
            f'----- {readme_rel} variables missing description -----',
            ', '.join(empty),
        ])

      elif empty := [o.name for o in newouts if not o.description]:
        state = state.FAIL_VARIABLE_DESCRIPTION
        diff = "\n".join([
            f'----- {readme_rel} outputs missing description -----',
            ', '.join(empty),
        ])

      elif variables != sorted(variables):
        state = state.FAIL_UNSORTED_VARS
        diff = "\n".join([
            f'----- {readme_rel} variables -----',
            f'variables should be in this order: ',
            ', '.join(sorted(variables)),
        ])

      elif outputs != sorted(outputs):
        state = state.FAIL_UNSORTED_OUTPUTS
        diff = "\n".join([
            f'----- {readme_rel} outputs -----',
            f'outputs should be in this order: ',
            ', '.join(sorted(outputs)),
        ])

      elif nc := [v.name for v in newvars if not v.description.endswith('.')]:
        state = state.FAIL_VARIABLE_PERIOD
        diff = "\n".join([
            f'----- {readme_rel} variable descriptions missing ending period -----',
            ', '.join(nc),
        ])

      elif nc := [o.name for o in newouts if not o.description.endswith('.')]:
        state = state.FAIL_OUTPUT_PERIOD
        diff = "\n".join([
            f'----- {readme_rel} output descriptions missing ending period -----',
            ', '.join(nc),
        ])

      elif no_types := [v.name for v in newvars if not v.type]:
        state = state.FAIL_MISSING_TYPES
        diff = "\n".join([
            f'----- {readme_rel} variables without types -----',
            ', '.join(no_types),
        ])

      yield readme_rel, state, diff