test_aider_linter.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import os
  2. import pytest
  3. from opendevin.runtime.plugins.agent_skills.aider import Linter, LintResult
  4. @pytest.fixture
  5. def temp_file(tmp_path):
  6. # Fixture to create a temporary file
  7. temp_name = os.path.join(tmp_path, 'lint-test.py')
  8. with open(temp_name, 'w', encoding='utf-8') as tmp_file:
  9. tmp_file.write("""def foo():
  10. print("Hello, World!")
  11. foo()
  12. """)
  13. tmp_file.close()
  14. yield temp_name
  15. os.remove(temp_name)
  16. @pytest.fixture
  17. def temp_ruby_file_errors(tmp_path):
  18. # Fixture to create a temporary file
  19. temp_name = os.path.join(tmp_path, 'lint-test.rb')
  20. with open(temp_name, 'w', encoding='utf-8') as tmp_file:
  21. tmp_file.write("""def foo():
  22. print("Hello, World!")
  23. foo()
  24. """)
  25. tmp_file.close()
  26. yield temp_name
  27. os.remove(temp_name)
  28. @pytest.fixture
  29. def temp_ruby_file_errors_parentheses(tmp_path):
  30. # Fixture to create a temporary file
  31. temp_name = os.path.join(tmp_path, 'lint-test.rb')
  32. with open(temp_name, 'w', encoding='utf-8') as tmp_file:
  33. tmp_file.write("""def print_hello_world()\n puts 'Hello World'\n""")
  34. tmp_file.close()
  35. yield temp_name
  36. os.remove(temp_name)
  37. @pytest.fixture
  38. def temp_ruby_file_correct(tmp_path):
  39. # Fixture to create a temporary file
  40. temp_name = os.path.join(tmp_path, 'lint-test.rb')
  41. with open(temp_name, 'w', encoding='utf-8') as tmp_file:
  42. tmp_file.write("""def foo
  43. puts "Hello, World!"
  44. end
  45. foo
  46. """)
  47. tmp_file.close()
  48. yield temp_name
  49. os.remove(temp_name)
  50. @pytest.fixture
  51. def linter(tmp_path):
  52. return Linter(root=tmp_path)
  53. def test_get_rel_fname(linter, temp_file, tmp_path):
  54. # Test get_rel_fname method
  55. rel_fname = linter.get_rel_fname(temp_file)
  56. assert rel_fname == os.path.relpath(temp_file, tmp_path)
  57. def test_run_cmd(linter, temp_file):
  58. # Test run_cmd method with a simple command
  59. result = linter.run_cmd('echo', temp_file, '')
  60. assert result is None # echo command should return zero exit status
  61. def test_set_linter(linter):
  62. # Test set_linter method
  63. def custom_linter(fname, rel_fname, code):
  64. return LintResult(text='Custom Linter', lines=[1])
  65. linter.set_linter('custom', custom_linter)
  66. assert 'custom' in linter.languages
  67. assert linter.languages['custom'] == custom_linter
  68. def test_py_lint(linter, temp_file):
  69. # Test py_lint method
  70. result = linter.py_lint(
  71. temp_file, linter.get_rel_fname(temp_file), "print('Hello, World!')\n"
  72. )
  73. assert result is None # No lint errors expected for this simple code
  74. def test_py_lint_fail(linter, temp_file):
  75. # Test py_lint method
  76. result = linter.py_lint(
  77. temp_file, linter.get_rel_fname(temp_file), "print('Hello, World!')\n"
  78. )
  79. assert result is None
  80. def test_basic_lint(temp_file):
  81. from opendevin.runtime.plugins.agent_skills.aider.linter import basic_lint
  82. poorly_formatted_code = """
  83. def foo()
  84. print("Hello, World!")
  85. print("Wrong indent")
  86. foo(
  87. """
  88. result = basic_lint(temp_file, poorly_formatted_code)
  89. assert isinstance(result, LintResult)
  90. assert result.text == f'{temp_file}:2'
  91. assert 2 in result.lines
  92. def test_basic_lint_fail_returns_text_and_lines(temp_file):
  93. from opendevin.runtime.plugins.agent_skills.aider.linter import basic_lint
  94. poorly_formatted_code = """
  95. def foo()
  96. print("Hello, World!")
  97. print("Wrong indent")
  98. foo(
  99. """
  100. result = basic_lint(temp_file, poorly_formatted_code)
  101. assert isinstance(result, LintResult)
  102. assert result.text == f'{temp_file}:2'
  103. assert 2 in result.lines
  104. def test_lint_python_compile(temp_file):
  105. from opendevin.runtime.plugins.agent_skills.aider.linter import lint_python_compile
  106. result = lint_python_compile(temp_file, "print('Hello, World!')\n")
  107. assert result is None
  108. def test_lint_python_compile_fail_returns_text_and_lines(temp_file):
  109. from opendevin.runtime.plugins.agent_skills.aider.linter import lint_python_compile
  110. poorly_formatted_code = """
  111. def foo()
  112. print("Hello, World!")
  113. print("Wrong indent")
  114. foo(
  115. """
  116. result = lint_python_compile(temp_file, poorly_formatted_code)
  117. assert temp_file in result.text
  118. assert 1 in result.lines
  119. def test_lint(linter, temp_file):
  120. result = linter.lint(temp_file)
  121. assert result is None
  122. def test_lint_fail(linter, temp_file):
  123. # Test lint method
  124. with open(temp_file, 'w', encoding='utf-8') as lint_file:
  125. lint_file.write("""
  126. def foo()
  127. print("Hello, World!")
  128. print("Wrong indent")
  129. foo(
  130. """)
  131. errors = linter.lint(temp_file)
  132. assert errors is not None
  133. def test_lint_pass_ruby(linter, temp_ruby_file_correct):
  134. result = linter.lint(temp_ruby_file_correct)
  135. assert result is None
  136. def test_lint_fail_ruby(linter, temp_ruby_file_errors):
  137. errors = linter.lint(temp_ruby_file_errors)
  138. assert errors is not None
  139. def test_lint_fail_ruby_no_parentheses(linter, temp_ruby_file_errors_parentheses):
  140. errors = linter.lint(temp_ruby_file_errors_parentheses)
  141. assert errors is not None