diff --git a/test/sanity/code-smell/no-unwanted-characters.json b/test/sanity/code-smell/no-unwanted-characters.json new file mode 100644 index 00000000000..5648429eb04 --- /dev/null +++ b/test/sanity/code-smell/no-unwanted-characters.json @@ -0,0 +1,4 @@ +{ + "text": true, + "output": "path-line-column-message" +} diff --git a/test/sanity/code-smell/no-unwanted-characters.py b/test/sanity/code-smell/no-unwanted-characters.py new file mode 100644 index 00000000000..26e5912c546 --- /dev/null +++ b/test/sanity/code-smell/no-unwanted-characters.py @@ -0,0 +1,27 @@ +"""Disallow use of unwanted Unicode characters.""" +from __future__ import annotations + +import re +import sys + + +def main(): + """Main entry point.""" + for path in sys.argv[1:] or sys.stdin.read().splitlines(): + with open(path, 'rb') as path_fd: + for line, text in enumerate(path_fd.readlines()): + try: + text = text.decode('utf-8') + except UnicodeDecodeError as ex: + print('%s:%d:%d: UnicodeDecodeError: %s' % (path, line + 1, ex.start + 1, ex)) + continue + + match = re.search('(\u00a0)', text) + + if match: + print('%s:%d:%d: use an ASCII space instead of a Unicode no-break space' % ( + path, line + 1, match.start(1) + 1)) + + +if __name__ == '__main__': + main() diff --git a/test/sanity/ignore.txt b/test/sanity/ignore.txt index 57730176bfa..fe7a4ea358b 100644 --- a/test/sanity/ignore.txt +++ b/test/sanity/ignore.txt @@ -114,15 +114,19 @@ test/integration/targets/incidental_win_reboot/templates/post_reboot.ps1 pslint! test/integration/targets/json_cleanup/library/bad_json shebang test/integration/targets/lookup_csvfile/files/crlf.csv line-endings test/integration/targets/lookup_ini/lookup-8859-15.ini no-smart-quotes +test/integration/targets/lookup_ini/lookup-8859-15.ini no-unwanted-characters test/integration/targets/module_precedence/lib_with_extension/a.ini shebang test/integration/targets/module_precedence/lib_with_extension/ping.ini shebang test/integration/targets/module_precedence/roles_with_extension/foo/library/a.ini shebang test/integration/targets/module_precedence/roles_with_extension/foo/library/ping.ini shebang test/integration/targets/old_style_modules_posix/library/helloworld.sh shebang test/integration/targets/template/files/encoding_1252_utf-8.expected no-smart-quotes +test/integration/targets/template/files/encoding_1252_utf-8.expected no-unwanted-characters test/integration/targets/template/files/encoding_1252_windows-1252.expected no-smart-quotes +test/integration/targets/template/files/encoding_1252_windows-1252.expected no-unwanted-characters test/integration/targets/template/files/foo.dos.txt line-endings test/integration/targets/template/templates/encoding_1252.j2 no-smart-quotes +test/integration/targets/template/templates/encoding_1252.j2 no-unwanted-characters test/integration/targets/unicode/unicode.yml no-smart-quotes test/integration/targets/windows-minimal/library/win_ping_syntax_error.ps1 pslint!skip test/integration/targets/win_exec_wrapper/library/test_fail.ps1 pslint:PSCustomUseLiteralPath