Skip to content

Commit

Permalink
Add typos validation (DataDog#9902)
Browse files Browse the repository at this point in the history
* Use codespell to check for typos

* save list of allowed words

* Test validation

* don't change cisco key ex

* Skip directories

* add metric names that should not be corrected

* try one line

* split multiline

* Use config file

* Create a validation command for typos

* Add annotations

* catch none

* Remove

* Fix

* Put ignore words in a file
  • Loading branch information
ChristineTChen authored Aug 20, 2021
1 parent bc0b241 commit 48e7754
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .codespell/ignore_words.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ACI
aci
creat
hist
idae
Mor
mor
nd
unparseable
6 changes: 6 additions & 0 deletions .codespell/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[codespell]
skip = *.txt,*.json,*/.tox/*,*.sql,*/site/*,*/metrics.yaml,*/CHANGELOG.md,*/.git/*,*/*.egg-info/*,*/build/*,*/vendor/*,*/tests/*,*/.tox/*
count =
quiet-level = 3
ignore-words = .codespell/ignore_words.txt
ignore-regex = PullRequest|PostgresSQL
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .recommended_monitors import recommended_monitors
from .saved_views import saved_views
from .service_checks import service_checks
from .typos import typos

ALL_COMMANDS = (
agent_reqs,
Expand All @@ -48,6 +49,7 @@
recommended_monitors,
saved_views,
service_checks,
typos,
)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# (C) Datadog, Inc. 2021-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import os
import re

import click

from datadog_checks.dev.tooling.annotations import annotate_warning
from datadog_checks.dev.tooling.constants import get_root

from ....subprocess import run_command
from ...utils import complete_valid_checks
from ..console import CONTEXT_SETTINGS, abort, echo_failure, echo_info, echo_success, echo_warning

FILE_PATH_REGEX = r'(.+\/[^\/]+)\:(\d+)\:\s+(\w+\s+\=\=\>\s+\w+)'


@click.command(context_settings=CONTEXT_SETTINGS, short_help='Validate spelling')
@click.argument('check', autocompletion=complete_valid_checks, required=False)
@click.option('--fix', is_flag=True, help='Apply suggested fix')
@click.pass_context
def typos(ctx, check, fix):
"""Validate spelling in the source code.
If `check` is specified, only the directory is validated.
Use codespell command line tool to detect spelling errors.
"""
root = get_root()

if check:
path = os.path.join(root, check)
else:
path = root

cmd = f"codespell {path} --config={root}/.codespell/setup.cfg"

if fix:
cmd += " -w"

try:
output, err, code = run_command(cmd, capture=True)
if code == 0:
echo_success("All files are valid!")
abort()
annotate_typos(output)
except Exception as e:
echo_info(f"Encountered error validating spell check: {e}")


def annotate_typos(output):
echo_failure("Typo validation failed, please fix typos")
parse_errors = output.split('\n')
for line in parse_errors:
m = re.match(FILE_PATH_REGEX, line)
if m is None:
continue

file, num, suggestion = m.groups()
annotate_warning(file, f"Detected typo: {suggestion}", line=num)
echo_warning(line)
1 change: 1 addition & 0 deletions datadog_checks_dev/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
'atomicwrites',
'beautifulsoup4>=4.9.3',
'click~=8.0',
'codespell',
'colorama',
'datamodel-code-generator~=0.11.4',
'docker-compose>=1.25',
Expand Down

0 comments on commit 48e7754

Please sign in to comment.