test_python_linter.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from openhands.linter import DefaultLinter, LintResult
  2. from openhands.linter.languages.python import (
  3. PythonLinter,
  4. flake_lint,
  5. python_compile_lint,
  6. )
  7. def test_wrongly_indented_py_file(wrongly_indented_py_file):
  8. # Test Python linter
  9. linter = PythonLinter()
  10. assert '.py' in linter.supported_extensions
  11. result = linter.lint(wrongly_indented_py_file)
  12. print(result)
  13. assert isinstance(result, list) and len(result) == 1
  14. assert result[0] == LintResult(
  15. file=wrongly_indented_py_file,
  16. line=2,
  17. column=5,
  18. message='E999 IndentationError: unexpected indent',
  19. )
  20. print(result[0].visualize())
  21. assert result[0].visualize() == (
  22. '1|\n'
  23. '\033[91m2| def foo():\033[0m\n'
  24. ' ^ ERROR HERE: E999 IndentationError: unexpected indent\n'
  25. '3| print("Hello, World!")\n'
  26. '4|'
  27. )
  28. # General linter should have same result as Python linter
  29. # bc it uses PythonLinter under the hood
  30. general_linter = DefaultLinter()
  31. assert '.py' in general_linter.supported_extensions
  32. result = general_linter.lint(wrongly_indented_py_file)
  33. assert result == linter.lint(wrongly_indented_py_file)
  34. # Test flake8_lint
  35. assert result == flake_lint(wrongly_indented_py_file)
  36. # Test python_compile_lint
  37. compile_result = python_compile_lint(wrongly_indented_py_file)
  38. assert isinstance(compile_result, list) and len(compile_result) == 1
  39. assert compile_result[0] == LintResult(
  40. file=wrongly_indented_py_file, line=2, column=4, message='unexpected indent'
  41. )
  42. def test_simple_correct_py_file(simple_correct_py_file):
  43. linter = PythonLinter()
  44. assert '.py' in linter.supported_extensions
  45. result = linter.lint(simple_correct_py_file)
  46. assert result == []
  47. general_linter = DefaultLinter()
  48. assert '.py' in general_linter.supported_extensions
  49. result = general_linter.lint(simple_correct_py_file)
  50. assert result == linter.lint(simple_correct_py_file)
  51. # Test python_compile_lint
  52. compile_result = python_compile_lint(simple_correct_py_file)
  53. assert compile_result == []
  54. # Test flake_lint
  55. flake_result = flake_lint(simple_correct_py_file)
  56. assert flake_result == []
  57. def test_simple_correct_py_func_def(simple_correct_py_func_def):
  58. linter = PythonLinter()
  59. result = linter.lint(simple_correct_py_func_def)
  60. assert result == []
  61. general_linter = DefaultLinter()
  62. assert '.py' in general_linter.supported_extensions
  63. result = general_linter.lint(simple_correct_py_func_def)
  64. assert result == linter.lint(simple_correct_py_func_def)
  65. # Test flake_lint
  66. assert result == flake_lint(simple_correct_py_func_def)
  67. # Test python_compile_lint
  68. compile_result = python_compile_lint(simple_correct_py_func_def)
  69. assert compile_result == []