test_agent_skill.py 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. import contextlib
  2. import io
  3. import os
  4. import sys
  5. import docx
  6. import pytest
  7. from opendevin.runtime.plugins.agent_skills.agentskills import (
  8. MSG_FILE_UPDATED,
  9. WINDOW,
  10. _print_window,
  11. append_file,
  12. create_file,
  13. edit_file_by_replace,
  14. find_file,
  15. goto_line,
  16. insert_content_at_line,
  17. open_file,
  18. parse_docx,
  19. parse_latex,
  20. parse_pdf,
  21. parse_pptx,
  22. scroll_down,
  23. scroll_up,
  24. search_dir,
  25. search_file,
  26. )
  27. # CURRENT_FILE must be reset for each test
  28. @pytest.fixture(autouse=True)
  29. def reset_current_file():
  30. from opendevin.runtime.plugins.agent_skills import agentskills
  31. agentskills.CURRENT_FILE = None
  32. def _numbered_test_lines(start, end) -> str:
  33. return ('\n'.join(f'{i}|' for i in range(start, end + 1))) + '\n'
  34. def _generate_test_file_with_lines(temp_path, num_lines) -> str:
  35. file_path = temp_path / 'test_file.py'
  36. file_path.write_text('\n' * num_lines)
  37. return file_path
  38. def _generate_ruby_test_file_with_lines(temp_path, num_lines) -> str:
  39. file_path = temp_path / 'test_file.rb'
  40. file_path.write_text('\n' * num_lines)
  41. return file_path
  42. def _calculate_window_bounds(current_line, total_lines, window_size):
  43. """Calculate the bounds of the window around the current line."""
  44. half_window = window_size // 2
  45. if current_line - half_window < 0:
  46. start = 1
  47. end = window_size
  48. else:
  49. start = current_line - half_window
  50. end = current_line + half_window
  51. return start, end
  52. def _generate_ruby_test_file_with_lines(temp_path, num_lines) -> str:
  53. file_path = temp_path / 'test_file.rb'
  54. file_path.write_text('\n' * num_lines)
  55. return file_path
  56. def test_open_file_unexist_path():
  57. with pytest.raises(FileNotFoundError):
  58. open_file('/unexist/path/a.txt')
  59. def test_open_file(tmp_path):
  60. assert tmp_path is not None
  61. temp_file_path = tmp_path / 'a.txt'
  62. temp_file_path.write_text('Line 1\nLine 2\nLine 3\nLine 4\nLine 5')
  63. with io.StringIO() as buf:
  64. with contextlib.redirect_stdout(buf):
  65. open_file(str(temp_file_path))
  66. result = buf.getvalue()
  67. assert result is not None
  68. expected = (
  69. f'[File: {temp_file_path} (5 lines total)]\n'
  70. '(this is the beginning of the file)\n'
  71. '1|Line 1\n'
  72. '2|Line 2\n'
  73. '3|Line 3\n'
  74. '4|Line 4\n'
  75. '5|Line 5\n'
  76. '(this is the end of the file)\n'
  77. )
  78. assert result.split('\n') == expected.split('\n')
  79. def test_open_file_with_indentation(tmp_path):
  80. temp_file_path = tmp_path / 'a.txt'
  81. temp_file_path.write_text('Line 1\n Line 2\nLine 3\nLine 4\nLine 5')
  82. with io.StringIO() as buf:
  83. with contextlib.redirect_stdout(buf):
  84. open_file(str(temp_file_path))
  85. result = buf.getvalue()
  86. assert result is not None
  87. expected = (
  88. f'[File: {temp_file_path} (5 lines total)]\n'
  89. '(this is the beginning of the file)\n'
  90. '1|Line 1\n'
  91. '2| Line 2\n'
  92. '3|Line 3\n'
  93. '4|Line 4\n'
  94. '5|Line 5\n'
  95. '(this is the end of the file)\n'
  96. )
  97. assert result.split('\n') == expected.split('\n')
  98. def test_open_file_long(tmp_path):
  99. temp_file_path = tmp_path / 'a.txt'
  100. content = '\n'.join([f'Line {i}' for i in range(1, 1001)])
  101. temp_file_path.write_text(content)
  102. with io.StringIO() as buf:
  103. with contextlib.redirect_stdout(buf):
  104. open_file(str(temp_file_path), 1, 50)
  105. result = buf.getvalue()
  106. assert result is not None
  107. expected = f'[File: {temp_file_path} (1000 lines total)]\n'
  108. expected += '(this is the beginning of the file)\n'
  109. for i in range(1, 51):
  110. expected += f'{i}|Line {i}\n'
  111. expected += '(950 more lines below)\n'
  112. assert result.split('\n') == expected.split('\n')
  113. def test_open_file_long_with_lineno(tmp_path):
  114. temp_file_path = tmp_path / 'a.txt'
  115. content = '\n'.join([f'Line {i}' for i in range(1, 1001)])
  116. temp_file_path.write_text(content)
  117. cur_line = 100
  118. with io.StringIO() as buf:
  119. with contextlib.redirect_stdout(buf):
  120. open_file(str(temp_file_path), cur_line)
  121. result = buf.getvalue()
  122. assert result is not None
  123. expected = f'[File: {temp_file_path} (1000 lines total)]\n'
  124. # since 100 is < WINDOW and 100 - WINDOW//2 < 0, so it should show all lines from 1 to WINDOW
  125. start, end = _calculate_window_bounds(cur_line, 1000, WINDOW)
  126. if start == 1:
  127. expected += '(this is the beginning of the file)\n'
  128. else:
  129. expected += f'({start - 1} more lines above)\n'
  130. for i in range(start, end + 1):
  131. expected += f'{i}|Line {i}\n'
  132. if end == 1000:
  133. expected += '(this is the end of the file)\n'
  134. else:
  135. expected += f'({1000 - end} more lines below)\n'
  136. assert result.split('\n') == expected.split('\n')
  137. def test_create_file_unexist_path():
  138. with pytest.raises(FileNotFoundError):
  139. create_file('/unexist/path/a.txt')
  140. def test_create_file(tmp_path):
  141. temp_file_path = tmp_path / 'a.txt'
  142. with io.StringIO() as buf:
  143. with contextlib.redirect_stdout(buf):
  144. create_file(str(temp_file_path))
  145. result = buf.getvalue()
  146. expected = (
  147. f'[File: {temp_file_path} (1 lines total)]\n'
  148. '(this is the beginning of the file)\n'
  149. '1|\n'
  150. '(this is the end of the file)\n'
  151. f'[File {temp_file_path} created.]\n'
  152. )
  153. assert result.split('\n') == expected.split('\n')
  154. def test_goto_line(tmp_path):
  155. temp_file_path = tmp_path / 'a.txt'
  156. total_lines = 1000
  157. content = '\n'.join([f'Line {i}' for i in range(1, total_lines + 1)])
  158. temp_file_path.write_text(content)
  159. with io.StringIO() as buf:
  160. with contextlib.redirect_stdout(buf):
  161. open_file(str(temp_file_path))
  162. result = buf.getvalue()
  163. assert result is not None
  164. expected = f'[File: {temp_file_path} ({total_lines} lines total)]\n'
  165. expected += '(this is the beginning of the file)\n'
  166. for i in range(1, WINDOW + 1):
  167. expected += f'{i}|Line {i}\n'
  168. expected += f'({total_lines - WINDOW} more lines below)\n'
  169. assert result.split('\n') == expected.split('\n')
  170. with io.StringIO() as buf:
  171. with contextlib.redirect_stdout(buf):
  172. goto_line(500)
  173. result = buf.getvalue()
  174. assert result is not None
  175. cur_line = 500
  176. expected = f'[File: {temp_file_path} ({total_lines} lines total)]\n'
  177. start, end = _calculate_window_bounds(cur_line, total_lines, WINDOW)
  178. if start == 1:
  179. expected += '(this is the beginning of the file)\n'
  180. else:
  181. expected += f'({start - 1} more lines above)\n'
  182. for i in range(start, end + 1):
  183. expected += f'{i}|Line {i}\n'
  184. if end == total_lines:
  185. expected += '(this is the end of the file)\n'
  186. else:
  187. expected += f'({total_lines - end} more lines below)\n'
  188. assert result.split('\n') == expected.split('\n')
  189. def test_goto_line_negative(tmp_path):
  190. temp_file_path = tmp_path / 'a.txt'
  191. content = '\n'.join([f'Line {i}' for i in range(1, 5)])
  192. temp_file_path.write_text(content)
  193. with io.StringIO() as buf:
  194. with contextlib.redirect_stdout(buf):
  195. open_file(str(temp_file_path))
  196. with pytest.raises(ValueError):
  197. goto_line(-1)
  198. def test_goto_line_out_of_bound(tmp_path):
  199. temp_file_path = tmp_path / 'a.txt'
  200. content = '\n'.join([f'Line {i}' for i in range(1, 5)])
  201. temp_file_path.write_text(content)
  202. with io.StringIO() as buf:
  203. with contextlib.redirect_stdout(buf):
  204. open_file(str(temp_file_path))
  205. with pytest.raises(ValueError):
  206. goto_line(100)
  207. def test_scroll_down(tmp_path):
  208. temp_file_path = tmp_path / 'a.txt'
  209. total_lines = 1000
  210. content = '\n'.join([f'Line {i}' for i in range(1, total_lines + 1)])
  211. temp_file_path.write_text(content)
  212. with io.StringIO() as buf:
  213. with contextlib.redirect_stdout(buf):
  214. open_file(str(temp_file_path))
  215. result = buf.getvalue()
  216. assert result is not None
  217. expected = f'[File: {temp_file_path} ({total_lines} lines total)]\n'
  218. start, end = _calculate_window_bounds(1, total_lines, WINDOW)
  219. if start == 1:
  220. expected += '(this is the beginning of the file)\n'
  221. else:
  222. expected += f'({start - 1} more lines above)\n'
  223. for i in range(start, end + 1):
  224. expected += f'{i}|Line {i}\n'
  225. if end == total_lines:
  226. expected += '(this is the end of the file)\n'
  227. else:
  228. expected += f'({total_lines - end} more lines below)\n'
  229. assert result.split('\n') == expected.split('\n')
  230. with io.StringIO() as buf:
  231. with contextlib.redirect_stdout(buf):
  232. scroll_down()
  233. result = buf.getvalue()
  234. assert result is not None
  235. expected = f'[File: {temp_file_path} ({total_lines} lines total)]\n'
  236. start, end = _calculate_window_bounds(WINDOW + 1, total_lines, WINDOW)
  237. if start == 1:
  238. expected += '(this is the beginning of the file)\n'
  239. else:
  240. expected += f'({start - 1} more lines above)\n'
  241. for i in range(start, end + 1):
  242. expected += f'{i}|Line {i}\n'
  243. if end == total_lines:
  244. expected += '(this is the end of the file)\n'
  245. else:
  246. expected += f'({total_lines - end} more lines below)\n'
  247. assert result.split('\n') == expected.split('\n')
  248. def test_scroll_up(tmp_path):
  249. temp_file_path = tmp_path / 'a.txt'
  250. total_lines = 1000
  251. content = '\n'.join([f'Line {i}' for i in range(1, total_lines + 1)])
  252. temp_file_path.write_text(content)
  253. cur_line = 300
  254. with io.StringIO() as buf:
  255. with contextlib.redirect_stdout(buf):
  256. open_file(str(temp_file_path), cur_line)
  257. result = buf.getvalue()
  258. assert result is not None
  259. expected = f'[File: {temp_file_path} ({total_lines} lines total)]\n'
  260. start, end = _calculate_window_bounds(cur_line, total_lines, WINDOW)
  261. if start == 1:
  262. expected += '(this is the beginning of the file)\n'
  263. else:
  264. expected += f'({start - 1} more lines above)\n'
  265. for i in range(start, end + 1):
  266. expected += f'{i}|Line {i}\n'
  267. if end == total_lines:
  268. expected += '(this is the end of the file)\n'
  269. else:
  270. expected += f'({total_lines - end} more lines below)\n'
  271. assert result.split('\n') == expected.split('\n')
  272. with io.StringIO() as buf:
  273. with contextlib.redirect_stdout(buf):
  274. scroll_up()
  275. result = buf.getvalue()
  276. assert result is not None
  277. cur_line = cur_line - WINDOW
  278. expected = f'[File: {temp_file_path} ({total_lines} lines total)]\n'
  279. start, end = _calculate_window_bounds(cur_line, total_lines, WINDOW)
  280. if start == 1:
  281. expected += '(this is the beginning of the file)\n'
  282. else:
  283. expected += f'({start - 1} more lines above)\n'
  284. for i in range(start, end + 1):
  285. expected += f'{i}|Line {i}\n'
  286. if end == total_lines:
  287. expected += '(this is the end of the file)\n'
  288. else:
  289. expected += f'({total_lines - end} more lines below)\n'
  290. assert result.split('\n') == expected.split('\n')
  291. def test_scroll_down_edge(tmp_path):
  292. temp_file_path = tmp_path / 'a.txt'
  293. content = '\n'.join([f'Line {i}' for i in range(1, 10)])
  294. temp_file_path.write_text(content)
  295. with io.StringIO() as buf:
  296. with contextlib.redirect_stdout(buf):
  297. open_file(str(temp_file_path))
  298. result = buf.getvalue()
  299. assert result is not None
  300. expected = f'[File: {temp_file_path} (9 lines total)]\n'
  301. expected += '(this is the beginning of the file)\n'
  302. for i in range(1, 10):
  303. expected += f'{i}|Line {i}\n'
  304. expected += '(this is the end of the file)\n'
  305. with io.StringIO() as buf:
  306. with contextlib.redirect_stdout(buf):
  307. scroll_down()
  308. result = buf.getvalue()
  309. assert result is not None
  310. # expected should be unchanged
  311. assert result.split('\n') == expected.split('\n')
  312. def test_print_window_internal(tmp_path):
  313. test_file_path = tmp_path / 'a.txt'
  314. create_file(str(test_file_path))
  315. open_file(str(test_file_path))
  316. with open(test_file_path, 'w') as file:
  317. for i in range(1, 101):
  318. file.write(f'Line `{i}`\n')
  319. # Define the parameters for the test
  320. current_line = 50
  321. window = 2
  322. # Test _print_window especially with backticks
  323. with io.StringIO() as buf:
  324. with contextlib.redirect_stdout(buf):
  325. _print_window(str(test_file_path), current_line, window, return_str=False)
  326. result = buf.getvalue()
  327. expected = (
  328. '(48 more lines above)\n'
  329. '49|Line `49`\n'
  330. '50|Line `50`\n'
  331. '51|Line `51`\n'
  332. '(49 more lines below)\n'
  333. )
  334. assert result == expected
  335. def test_open_file_large_line_number(tmp_path):
  336. test_file_path = tmp_path / 'a.txt'
  337. create_file(str(test_file_path))
  338. open_file(str(test_file_path))
  339. with open(test_file_path, 'w') as file:
  340. for i in range(1, 1000):
  341. file.write(f'Line `{i}`\n')
  342. # Define the parameters for the test
  343. current_line = 800
  344. window = 100
  345. # Test _print_window especially with backticks
  346. with io.StringIO() as buf:
  347. with contextlib.redirect_stdout(buf):
  348. # _print_window(str(test_file_path), current_line, window, return_str=False)
  349. open_file(str(test_file_path), current_line, window)
  350. result = buf.getvalue()
  351. expected = f'[File: {test_file_path} (999 lines total)]\n'
  352. expected += '(749 more lines above)\n'
  353. for i in range(750, 850 + 1):
  354. expected += f'{i}|Line `{i}`\n'
  355. expected += '(149 more lines below)\n'
  356. assert result == expected
  357. def test_open_file_large_line_number_consecutive_diff_window(tmp_path):
  358. test_file_path = tmp_path / 'a.txt'
  359. create_file(str(test_file_path))
  360. open_file(str(test_file_path))
  361. total_lines = 1000
  362. with open(test_file_path, 'w') as file:
  363. for i in range(1, total_lines + 1):
  364. file.write(f'Line `{i}`\n')
  365. # Define the parameters for the test
  366. current_line = 800
  367. cur_window = 300
  368. # Test _print_window especially with backticks
  369. with io.StringIO() as buf:
  370. with contextlib.redirect_stdout(buf):
  371. # _print_window(str(test_file_path), current_line, window, return_str=False)
  372. open_file(str(test_file_path), current_line, cur_window)
  373. result = buf.getvalue()
  374. expected = f'[File: {test_file_path} ({total_lines} lines total)]\n'
  375. start, end = _calculate_window_bounds(current_line, total_lines, cur_window)
  376. if start == 1:
  377. expected += '(this is the beginning of the file)\n'
  378. else:
  379. expected += f'({start - 1} more lines above)\n'
  380. for i in range(
  381. current_line - cur_window // 2, current_line + cur_window // 2 + 1
  382. ):
  383. expected += f'{i}|Line `{i}`\n'
  384. if end == total_lines:
  385. expected += '(this is the end of the file)\n'
  386. else:
  387. expected += f'({total_lines - end} more lines below)\n'
  388. assert result == expected
  389. # open_file **SHOULD NOT** Change the "window size" to 300
  390. # the window size should still be WINDOW
  391. current_line = current_line - WINDOW
  392. with io.StringIO() as buf:
  393. with contextlib.redirect_stdout(buf):
  394. scroll_up()
  395. result = buf.getvalue()
  396. expected = f'[File: {test_file_path} ({total_lines} lines total)]\n'
  397. start, end = _calculate_window_bounds(current_line, total_lines, WINDOW)
  398. if start == 1:
  399. expected += '(this is the beginning of the file)\n'
  400. else:
  401. expected += f'({start - 1} more lines above)\n'
  402. for i in range(start, end + 1):
  403. expected += f'{i}|Line `{i}`\n'
  404. if end == total_lines:
  405. expected += '(this is the end of the file)\n'
  406. else:
  407. expected += f'({total_lines - end} more lines below)\n'
  408. assert result == expected
  409. def test_edit_file_by_replace_window(tmp_path, monkeypatch):
  410. # Set environment variable via monkeypatch does NOT work!
  411. monkeypatch.setattr(
  412. 'opendevin.runtime.plugins.agent_skills.agentskills.ENABLE_AUTO_LINT', True
  413. )
  414. content = """def any_int(a, b, c):
  415. return isinstance(a, int) and isinstance(b, int) and isinstance(c, int)
  416. def test_any_int():
  417. assert any_int(1, 2, 3) == True
  418. assert any_int(1.5, 2, 3) == False
  419. assert any_int(1, 2.5, 3) == False
  420. assert any_int(1, 2, 3.5) == False
  421. assert any_int(1.0, 2, 3) == False
  422. assert any_int(1, 2.0, 3) == False
  423. assert any_int(1, 2, 3.0) == False
  424. assert any_int(0, 0, 0) == True
  425. assert any_int(-1, -2, -3) == True
  426. assert any_int(1, -2, 3) == True
  427. assert any_int(1.5, -2, 3) == False
  428. assert any_int(1, -2.5, 3) == False
  429. def check(any_int):
  430. # Check some simple cases
  431. assert any_int(2, 3, 1)==True, "This prints if this assert fails 1 (good for debugging!)"
  432. assert any_int(2.5, 2, 3)==False, "This prints if this assert fails 2 (good for debugging!)"
  433. assert any_int(1.5, 5, 3.5)==False, "This prints if this assert fails 3 (good for debugging!)"
  434. assert any_int(2, 6, 2)==False, "This prints if this assert fails 4 (good for debugging!)"
  435. assert any_int(4, 2, 2)==True, "This prints if this assert fails 5 (good for debugging!)"
  436. assert any_int(2.2, 2.2, 2.2)==False, "This prints if this assert fails 6 (good for debugging!)"
  437. assert any_int(-4, 6, 2)==True, "This prints if this assert fails 7 (good for debugging!)"
  438. # Check some edge cases that are easy to work out by hand.
  439. assert any_int(2,1,1)==True, "This prints if this assert fails 8 (also good for debugging!)"
  440. assert any_int(3,4,7)==True, "This prints if this assert fails 9 (also good for debugging!)"
  441. assert any_int(3.0,4,7)==False, "This prints if this assert fails 10 (also good for debugging!)"
  442. check(any_int)"""
  443. temp_file_path = tmp_path / 'error-test.py'
  444. temp_file_path.write_text(content)
  445. open_file(str(temp_file_path))
  446. with io.StringIO() as buf:
  447. with contextlib.redirect_stdout(buf):
  448. edit_file_by_replace(
  449. str(temp_file_path),
  450. to_replace=' assert any_int(1.0, 2, 3) == False',
  451. new_content=' assert any_int(1.0, 2, 3) == False',
  452. )
  453. result = buf.getvalue()
  454. expected = (
  455. '[Your proposed edit has introduced new syntax error(s). Please understand the errors and retry your edit command.]\n'
  456. 'ERRORS:\n'
  457. + str(temp_file_path)
  458. + ':9:9: '
  459. + 'E999 IndentationError: unexpected indent\n'
  460. '[This is how your edit would have looked if applied]\n'
  461. '-------------------------------------------------\n'
  462. '(this is the beginning of the file)\n'
  463. '1|def any_int(a, b, c):\n'
  464. '2| return isinstance(a, int) and isinstance(b, int) and isinstance(c, int)\n'
  465. '3|\n'
  466. '4|def test_any_int():\n'
  467. '5| assert any_int(1, 2, 3) == True\n'
  468. '6| assert any_int(1.5, 2, 3) == False\n'
  469. '7| assert any_int(1, 2.5, 3) == False\n'
  470. '8| assert any_int(1, 2, 3.5) == False\n'
  471. '9| assert any_int(1.0, 2, 3) == False\n'
  472. '10| assert any_int(1, 2.0, 3) == False\n'
  473. '11| assert any_int(1, 2, 3.0) == False\n'
  474. '12| assert any_int(0, 0, 0) == True\n'
  475. '13| assert any_int(-1, -2, -3) == True\n'
  476. '14| assert any_int(1, -2, 3) == True\n'
  477. '15| assert any_int(1.5, -2, 3) == False\n'
  478. '16| assert any_int(1, -2.5, 3) == False\n'
  479. '17|\n'
  480. '18|def check(any_int):\n'
  481. '19| # Check some simple cases\n'
  482. '20| assert any_int(2, 3, 1)==True, "This prints if this assert fails 1 (good for debugging!)"\n'
  483. '21| assert any_int(2.5, 2, 3)==False, "This prints if this assert fails 2 (good for debugging!)"\n'
  484. '(12 more lines below)\n'
  485. '-------------------------------------------------\n'
  486. '\n'
  487. '[This is the original code before your edit]\n'
  488. '-------------------------------------------------\n'
  489. '(this is the beginning of the file)\n'
  490. '1|def any_int(a, b, c):\n'
  491. '2| return isinstance(a, int) and isinstance(b, int) and isinstance(c, int)\n'
  492. '3|\n'
  493. '4|def test_any_int():\n'
  494. '5| assert any_int(1, 2, 3) == True\n'
  495. '6| assert any_int(1.5, 2, 3) == False\n'
  496. '7| assert any_int(1, 2.5, 3) == False\n'
  497. '8| assert any_int(1, 2, 3.5) == False\n'
  498. '9| assert any_int(1.0, 2, 3) == False\n'
  499. '10| assert any_int(1, 2.0, 3) == False\n'
  500. '11| assert any_int(1, 2, 3.0) == False\n'
  501. '12| assert any_int(0, 0, 0) == True\n'
  502. '13| assert any_int(-1, -2, -3) == True\n'
  503. '14| assert any_int(1, -2, 3) == True\n'
  504. '15| assert any_int(1.5, -2, 3) == False\n'
  505. '16| assert any_int(1, -2.5, 3) == False\n'
  506. '17|\n'
  507. '18|def check(any_int):\n'
  508. '19| # Check some simple cases\n'
  509. '20| assert any_int(2, 3, 1)==True, "This prints if this assert fails 1 (good for debugging!)"\n'
  510. '21| assert any_int(2.5, 2, 3)==False, "This prints if this assert fails 2 (good for debugging!)"\n'
  511. '(12 more lines below)\n'
  512. '-------------------------------------------------\n'
  513. 'Your changes have NOT been applied. Please fix your edit command and try again.\n'
  514. 'You either need to 1) Specify the correct start/end line arguments or 2) Correct your edit code.\n'
  515. 'DO NOT re-run the same failed edit command. Running it again will lead to the same error.\n'
  516. )
  517. assert result == expected
  518. # ================================
  519. def test_edit_file_by_replace(tmp_path):
  520. temp_file_path = tmp_path / 'a.txt'
  521. content = 'Line 1\nLine 2\nLine 3\nLine 4\nLine 5'
  522. temp_file_path.write_text(content)
  523. open_file(str(temp_file_path))
  524. with io.StringIO() as buf:
  525. with contextlib.redirect_stdout(buf):
  526. edit_file_by_replace(
  527. file_name=str(temp_file_path),
  528. to_replace='Line 1\nLine 2\nLine 3',
  529. new_content='REPLACE TEXT',
  530. )
  531. result = buf.getvalue()
  532. expected = (
  533. f'[File: {temp_file_path} (3 lines total after edit)]\n'
  534. '(this is the beginning of the file)\n'
  535. '1|REPLACE TEXT\n'
  536. '2|Line 4\n'
  537. '3|Line 5\n'
  538. '(this is the end of the file)\n'
  539. + MSG_FILE_UPDATED.format(line_number=1)
  540. + '\n'
  541. )
  542. assert result.split('\n') == expected.split('\n')
  543. with open(temp_file_path, 'r') as file:
  544. lines = file.readlines()
  545. assert len(lines) == 3
  546. assert lines[0].rstrip() == 'REPLACE TEXT'
  547. assert lines[1].rstrip() == 'Line 4'
  548. assert lines[2].rstrip() == 'Line 5'
  549. def test_edit_file_by_replace_sameline(tmp_path):
  550. temp_file_path = tmp_path / 'a.txt'
  551. content = 'Line 1\nLine 2\nLine 2\nLine 4\nLine 5'
  552. temp_file_path.write_text(content)
  553. open_file(str(temp_file_path))
  554. with io.StringIO() as buf:
  555. with contextlib.redirect_stdout(buf):
  556. edit_file_by_replace(
  557. file_name=str(temp_file_path),
  558. to_replace='Line 2\nLine 2',
  559. new_content='Line 2\nREPLACE TEXT',
  560. )
  561. result = buf.getvalue()
  562. expected = (
  563. f'[File: {temp_file_path} (5 lines total after edit)]\n'
  564. '(this is the beginning of the file)\n'
  565. '1|Line 1\n'
  566. '2|Line 2\n'
  567. '3|REPLACE TEXT\n'
  568. '4|Line 4\n'
  569. '5|Line 5\n'
  570. '(this is the end of the file)\n'
  571. + MSG_FILE_UPDATED.format(line_number=2)
  572. + '\n'
  573. )
  574. assert result.split('\n') == expected.split('\n')
  575. with open(temp_file_path, 'r') as file:
  576. lines = file.readlines()
  577. assert len(lines) == 5
  578. assert lines[0].rstrip() == 'Line 1'
  579. assert lines[1].rstrip() == 'Line 2'
  580. assert lines[2].rstrip() == 'REPLACE TEXT'
  581. assert lines[3].rstrip() == 'Line 4'
  582. assert lines[4].rstrip() == 'Line 5'
  583. def test_edit_file_by_replace_multiline(tmp_path):
  584. temp_file_path = tmp_path / 'a.txt'
  585. content = 'Line 1\nLine 2\nLine 2\nLine 4\nLine 5'
  586. temp_file_path.write_text(content)
  587. open_file(str(temp_file_path))
  588. with io.StringIO() as buf:
  589. with contextlib.redirect_stdout(buf):
  590. with pytest.raises(
  591. ValueError,
  592. match='`to_replace` appears more than once, please include enough lines to make code in `to_replace` unique',
  593. ):
  594. edit_file_by_replace(
  595. file_name=str(temp_file_path),
  596. to_replace='Line 2',
  597. new_content='REPLACE TEXT',
  598. )
  599. def test_edit_file_by_replace_no_diff(tmp_path):
  600. temp_file_path = tmp_path / 'a.txt'
  601. content = 'Line 1\nLine 2\nLine 2\nLine 4\nLine 5'
  602. temp_file_path.write_text(content)
  603. open_file(str(temp_file_path))
  604. with io.StringIO() as buf:
  605. with contextlib.redirect_stdout(buf):
  606. with pytest.raises(
  607. ValueError, match='`to_replace` and `new_content` must be different'
  608. ):
  609. edit_file_by_replace(
  610. file_name=str(temp_file_path),
  611. to_replace='Line 1',
  612. new_content='Line 1',
  613. )
  614. def test_edit_file_by_replace_toreplace_empty(tmp_path):
  615. temp_file_path = tmp_path / 'a.txt'
  616. content = 'Line 1\nLine 2\nLine 2\nLine 4\nLine 5'
  617. temp_file_path.write_text(content)
  618. open_file(str(temp_file_path))
  619. with io.StringIO() as buf:
  620. with contextlib.redirect_stdout(buf):
  621. with pytest.raises(ValueError, match='`to_replace` must not be empty.'):
  622. edit_file_by_replace(
  623. file_name=str(temp_file_path),
  624. to_replace=' ',
  625. new_content='Line 1',
  626. )
  627. def test_edit_file_by_replace_unknown_file():
  628. with pytest.raises(FileNotFoundError):
  629. edit_file_by_replace(
  630. str('unknown file'),
  631. 'ORIGINAL TEXT',
  632. 'REPLACE TEXT',
  633. )
  634. def test_insert_content_at_line(tmp_path):
  635. temp_file_path = tmp_path / 'b.txt'
  636. content = 'Line 1\nLine 2\nLine 3'
  637. temp_file_path.write_text(content)
  638. open_file(str(temp_file_path))
  639. with io.StringIO() as buf:
  640. with contextlib.redirect_stdout(buf):
  641. insert_content_at_line(
  642. file_name=str(temp_file_path),
  643. line_number=2,
  644. content='Inserted Line',
  645. )
  646. result = buf.getvalue()
  647. expected = (
  648. f'[File: {temp_file_path} (4 lines total after edit)]\n'
  649. '(this is the beginning of the file)\n'
  650. '1|Line 1\n'
  651. '2|Inserted Line\n'
  652. '3|Line 2\n'
  653. '4|Line 3\n'
  654. '(this is the end of the file)\n'
  655. + MSG_FILE_UPDATED.format(line_number=2)
  656. + '\n'
  657. )
  658. assert result.split('\n') == expected.split('\n')
  659. with open(temp_file_path, 'r') as file:
  660. lines = file.readlines()
  661. assert len(lines) == 4
  662. assert lines[0].rstrip() == 'Line 1'
  663. assert lines[1].rstrip() == 'Inserted Line'
  664. assert lines[2].rstrip() == 'Line 2'
  665. assert lines[3].rstrip() == 'Line 3'
  666. def test_insert_content_at_line_from_scratch(tmp_path):
  667. temp_file_path = tmp_path / 'a.txt'
  668. create_file(str(temp_file_path))
  669. open_file(str(temp_file_path))
  670. with io.StringIO() as buf:
  671. with contextlib.redirect_stdout(buf):
  672. insert_content_at_line(
  673. file_name=str(temp_file_path),
  674. line_number=1,
  675. content='REPLACE TEXT',
  676. )
  677. result = buf.getvalue()
  678. expected = (
  679. f'[File: {temp_file_path} (1 lines total after edit)]\n'
  680. '(this is the beginning of the file)\n'
  681. '1|REPLACE TEXT\n'
  682. '(this is the end of the file)\n'
  683. + MSG_FILE_UPDATED.format(line_number=1)
  684. + '\n'
  685. )
  686. assert result.split('\n') == expected.split('\n')
  687. with open(temp_file_path, 'r') as file:
  688. lines = file.readlines()
  689. assert len(lines) == 1
  690. assert lines[0].rstrip() == 'REPLACE TEXT'
  691. def test_insert_content_at_line_from_scratch_emptyfile(tmp_path):
  692. temp_file_path = tmp_path / 'a.txt'
  693. with open(temp_file_path, 'w') as file:
  694. file.write('')
  695. open_file(str(temp_file_path))
  696. with io.StringIO() as buf:
  697. with contextlib.redirect_stdout(buf):
  698. insert_content_at_line(
  699. file_name=str(temp_file_path),
  700. line_number=1,
  701. content='REPLACE TEXT',
  702. )
  703. result = buf.getvalue()
  704. expected = (
  705. f'[File: {temp_file_path} (1 lines total after edit)]\n'
  706. '(this is the beginning of the file)\n'
  707. '1|REPLACE TEXT\n'
  708. '(this is the end of the file)\n'
  709. + MSG_FILE_UPDATED.format(line_number=1)
  710. + '\n'
  711. )
  712. assert result.split('\n') == expected.split('\n')
  713. with open(temp_file_path, 'r') as file:
  714. lines = file.readlines()
  715. assert len(lines) == 1
  716. assert lines[0].rstrip() == 'REPLACE TEXT'
  717. def test_insert_content_at_line_emptyline(tmp_path):
  718. temp_file_path = tmp_path / 'b.txt'
  719. content = 'Line 1\n\n'
  720. temp_file_path.write_text(content)
  721. open_file(str(temp_file_path))
  722. with io.StringIO() as buf:
  723. with contextlib.redirect_stdout(buf):
  724. insert_content_at_line(
  725. file_name=str(temp_file_path),
  726. line_number=2,
  727. content='Inserted Line',
  728. )
  729. result = buf.getvalue()
  730. expected = (
  731. f'[File: {temp_file_path} (3 lines total after edit)]\n'
  732. '(this is the beginning of the file)\n'
  733. '1|Line 1\n'
  734. '2|Inserted Line\n'
  735. '3|\n'
  736. '(this is the end of the file)\n'
  737. + MSG_FILE_UPDATED.format(line_number=2)
  738. + '\n'
  739. )
  740. assert result.split('\n') == expected.split('\n')
  741. with open(temp_file_path, 'r') as file:
  742. lines = file.readlines()
  743. assert len(lines) == 3
  744. assert lines[0].rstrip() == 'Line 1'
  745. assert lines[1].rstrip() == 'Inserted Line'
  746. def test_insert_content_at_line_from_scratch_multiline_with_backticks_and_second_edit(
  747. tmp_path,
  748. ):
  749. temp_file_path = tmp_path / 'a.txt'
  750. create_file(str(temp_file_path))
  751. open_file(str(temp_file_path))
  752. with io.StringIO() as buf:
  753. with contextlib.redirect_stdout(buf):
  754. insert_content_at_line(
  755. str(temp_file_path),
  756. 1,
  757. '`REPLACE TEXT1`\n`REPLACE TEXT2`\n`REPLACE TEXT3`',
  758. )
  759. result = buf.getvalue()
  760. expected = (
  761. f'[File: {temp_file_path} (3 lines total after edit)]\n'
  762. '(this is the beginning of the file)\n'
  763. '1|`REPLACE TEXT1`\n'
  764. '2|`REPLACE TEXT2`\n'
  765. '3|`REPLACE TEXT3`\n'
  766. '(this is the end of the file)\n'
  767. + MSG_FILE_UPDATED.format(line_number=1)
  768. + '\n'
  769. )
  770. assert result.split('\n') == expected.split('\n')
  771. with open(temp_file_path, 'r') as file:
  772. lines = file.readlines()
  773. assert len(lines) == 3
  774. assert lines[0].rstrip() == '`REPLACE TEXT1`'
  775. assert lines[1].rstrip() == '`REPLACE TEXT2`'
  776. assert lines[2].rstrip() == '`REPLACE TEXT3`'
  777. # Check that no backticks are escaped in the edit_file_by_replace call
  778. assert '\\`' not in result
  779. # Perform a second edit
  780. with io.StringIO() as buf:
  781. with contextlib.redirect_stdout(buf):
  782. edit_file_by_replace(
  783. str(temp_file_path),
  784. '`REPLACE TEXT1`\n`REPLACE TEXT2`\n`REPLACE TEXT3`',
  785. '`REPLACED TEXT1`\n`REPLACED TEXT2`\n`REPLACED TEXT3`',
  786. )
  787. second_result = buf.getvalue()
  788. second_expected = (
  789. f'[File: {temp_file_path} (3 lines total after edit)]\n'
  790. '(this is the beginning of the file)\n'
  791. '1|`REPLACED TEXT1`\n'
  792. '2|`REPLACED TEXT2`\n'
  793. '3|`REPLACED TEXT3`\n'
  794. '(this is the end of the file)\n'
  795. + MSG_FILE_UPDATED.format(line_number=1)
  796. + '\n'
  797. )
  798. assert second_result.split('\n') == second_expected.split('\n')
  799. with open(temp_file_path, 'r') as file:
  800. lines = file.readlines()
  801. assert len(lines) == 3
  802. assert lines[0].rstrip() == '`REPLACED TEXT1`'
  803. assert lines[1].rstrip() == '`REPLACED TEXT2`'
  804. assert lines[2].rstrip() == '`REPLACED TEXT3`'
  805. # Check that no backticks are escaped in the second edit_file_by_replace call
  806. assert '\\`' not in second_result
  807. def test_insert_content_at_line_from_scratch_multiline(tmp_path):
  808. temp_file_path = tmp_path / 'a.txt'
  809. create_file(str(temp_file_path))
  810. open_file(temp_file_path)
  811. with io.StringIO() as buf:
  812. with contextlib.redirect_stdout(buf):
  813. insert_content_at_line(
  814. str(temp_file_path),
  815. 1,
  816. content='REPLACE TEXT1\nREPLACE TEXT2\nREPLACE TEXT3',
  817. )
  818. result = buf.getvalue()
  819. expected = (
  820. f'[File: {temp_file_path} (3 lines total after edit)]\n'
  821. '(this is the beginning of the file)\n'
  822. '1|REPLACE TEXT1\n'
  823. '2|REPLACE TEXT2\n'
  824. '3|REPLACE TEXT3\n'
  825. '(this is the end of the file)\n'
  826. + MSG_FILE_UPDATED.format(line_number=1)
  827. + '\n'
  828. )
  829. assert result.split('\n') == expected.split('\n')
  830. with open(temp_file_path, 'r') as file:
  831. lines = file.readlines()
  832. assert len(lines) == 3
  833. assert lines[0].rstrip() == 'REPLACE TEXT1'
  834. assert lines[1].rstrip() == 'REPLACE TEXT2'
  835. assert lines[2].rstrip() == 'REPLACE TEXT3'
  836. def test_insert_content_at_line_not_opened():
  837. with pytest.raises(FileNotFoundError):
  838. insert_content_at_line(
  839. str('unknown file'),
  840. 1,
  841. 'REPLACE TEXT',
  842. )
  843. def test_append_file(tmp_path):
  844. temp_file_path = tmp_path / 'a.txt'
  845. content = 'Line 1\nLine 2'
  846. temp_file_path.write_text(content)
  847. open_file(str(temp_file_path))
  848. with io.StringIO() as buf:
  849. with contextlib.redirect_stdout(buf):
  850. append_file(str(temp_file_path), content='APPENDED TEXT')
  851. result = buf.getvalue()
  852. expected = (
  853. f'[File: {temp_file_path} (3 lines total after edit)]\n'
  854. '(this is the beginning of the file)\n'
  855. '1|Line 1\n'
  856. '2|Line 2\n'
  857. '3|APPENDED TEXT\n'
  858. '(this is the end of the file)\n'
  859. + MSG_FILE_UPDATED.format(line_number=2)
  860. + '\n'
  861. )
  862. assert result.split('\n') == expected.split('\n')
  863. with open(temp_file_path, 'r') as file:
  864. lines = file.readlines()
  865. assert len(lines) == 3
  866. assert lines[0].rstrip() == 'Line 1'
  867. assert lines[1].rstrip() == 'Line 2'
  868. assert lines[2].rstrip() == 'APPENDED TEXT'
  869. def test_append_file_from_scratch(tmp_path):
  870. temp_file_path = tmp_path / 'a.txt'
  871. create_file(str(temp_file_path))
  872. try:
  873. open_file(str(temp_file_path))
  874. with io.StringIO() as buf:
  875. with contextlib.redirect_stdout(buf):
  876. append_file(str(temp_file_path), content='APPENDED TEXT')
  877. result = buf.getvalue()
  878. expected = (
  879. f'[File: {temp_file_path} (1 lines total after edit)]\n'
  880. '(this is the beginning of the file)\n'
  881. '1|APPENDED TEXT\n'
  882. '(this is the end of the file)\n'
  883. + MSG_FILE_UPDATED.format(line_number=1)
  884. + '\n'
  885. )
  886. assert result.split('\n') == expected.split('\n')
  887. with open(temp_file_path, 'r') as file:
  888. lines = file.readlines()
  889. assert len(lines) == 1
  890. assert lines[0].rstrip() == 'APPENDED TEXT'
  891. finally:
  892. os.remove(temp_file_path)
  893. def test_append_file_from_scratch_multiline(tmp_path):
  894. temp_file_path = tmp_path / 'a3.txt'
  895. create_file(str(temp_file_path))
  896. try:
  897. open_file(temp_file_path)
  898. with io.StringIO() as buf:
  899. with contextlib.redirect_stdout(buf):
  900. append_file(
  901. str(temp_file_path),
  902. content='APPENDED TEXT1\nAPPENDED TEXT2\nAPPENDED TEXT3',
  903. )
  904. result = buf.getvalue()
  905. expected = (
  906. f'[File: {temp_file_path} (3 lines total after edit)]\n'
  907. '(this is the beginning of the file)\n'
  908. '1|APPENDED TEXT1\n'
  909. '2|APPENDED TEXT2\n'
  910. '3|APPENDED TEXT3\n'
  911. '(this is the end of the file)\n'
  912. + MSG_FILE_UPDATED.format(line_number=1)
  913. + '\n'
  914. )
  915. assert result.split('\n') == expected.split('\n')
  916. with open(temp_file_path, 'r') as file:
  917. lines = file.readlines()
  918. assert len(lines) == 3
  919. assert lines[0].rstrip() == 'APPENDED TEXT1'
  920. assert lines[1].rstrip() == 'APPENDED TEXT2'
  921. assert lines[2].rstrip() == 'APPENDED TEXT3'
  922. finally:
  923. os.remove(temp_file_path)
  924. def test_append_file_not_opened():
  925. with pytest.raises(FileNotFoundError):
  926. append_file(str('unknown file'), content='APPEND TEXT')
  927. def test_search_dir(tmp_path):
  928. # create files with the search term "bingo"
  929. for i in range(1, 101):
  930. temp_file_path = tmp_path / f'a{i}.txt'
  931. with open(temp_file_path, 'w') as file:
  932. file.write('Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n')
  933. if i == 50:
  934. file.write('bingo')
  935. # test
  936. with io.StringIO() as buf:
  937. with contextlib.redirect_stdout(buf):
  938. search_dir('bingo', str(tmp_path))
  939. result = buf.getvalue()
  940. assert result is not None
  941. expected = (
  942. f'[Found 1 matches for "bingo" in {tmp_path}]\n'
  943. f'{tmp_path}/a50.txt (Line 6): bingo\n'
  944. f'[End of matches for "bingo" in {tmp_path}]\n'
  945. )
  946. assert result.split('\n') == expected.split('\n')
  947. def test_search_dir_not_exist_term(tmp_path):
  948. # create files with the search term "bingo"
  949. for i in range(1, 101):
  950. temp_file_path = tmp_path / f'a{i}.txt'
  951. with open(temp_file_path, 'w') as file:
  952. file.write('Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n')
  953. # test
  954. with io.StringIO() as buf:
  955. with contextlib.redirect_stdout(buf):
  956. search_dir('non-exist', str(tmp_path))
  957. result = buf.getvalue()
  958. assert result is not None
  959. expected = f'No matches found for "non-exist" in {tmp_path}\n'
  960. assert result.split('\n') == expected.split('\n')
  961. def test_search_dir_too_much_match(tmp_path):
  962. # create files with the search term "Line 5"
  963. for i in range(1, 1000):
  964. temp_file_path = tmp_path / f'a{i}.txt'
  965. with open(temp_file_path, 'w') as file:
  966. file.write('Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n')
  967. with io.StringIO() as buf:
  968. with contextlib.redirect_stdout(buf):
  969. search_dir('Line 5', str(tmp_path))
  970. result = buf.getvalue()
  971. assert result is not None
  972. expected = f'More than 999 files matched for "Line 5" in {tmp_path}. Please narrow your search.\n'
  973. assert result.split('\n') == expected.split('\n')
  974. def test_search_dir_cwd(tmp_path, monkeypatch):
  975. # Using pytest's monkeypatch to change directory without affecting other tests
  976. monkeypatch.chdir(tmp_path)
  977. # create files with the search term "bingo"
  978. for i in range(1, 101):
  979. temp_file_path = tmp_path / f'a{i}.txt'
  980. with open(temp_file_path, 'w') as file:
  981. file.write('Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n')
  982. if i == 50:
  983. file.write('bingo')
  984. with io.StringIO() as buf:
  985. with contextlib.redirect_stdout(buf):
  986. search_dir('bingo')
  987. result = buf.getvalue()
  988. assert result is not None
  989. expected = (
  990. '[Found 1 matches for "bingo" in ./]\n'
  991. './a50.txt (Line 6): bingo\n'
  992. '[End of matches for "bingo" in ./]\n'
  993. )
  994. assert result.split('\n') == expected.split('\n')
  995. def test_search_file(tmp_path):
  996. temp_file_path = tmp_path / 'a.txt'
  997. temp_file_path.write_text('Line 1\nLine 2\nLine 3\nLine 4\nLine 5')
  998. with io.StringIO() as buf:
  999. with contextlib.redirect_stdout(buf):
  1000. search_file('Line 5', str(temp_file_path))
  1001. result = buf.getvalue()
  1002. assert result is not None
  1003. expected = f'[Found 1 matches for "Line 5" in {temp_file_path}]\n'
  1004. expected += 'Line 5: Line 5\n'
  1005. expected += f'[End of matches for "Line 5" in {temp_file_path}]\n'
  1006. assert result.split('\n') == expected.split('\n')
  1007. def test_search_file_not_exist_term(tmp_path):
  1008. temp_file_path = tmp_path / 'a.txt'
  1009. temp_file_path.write_text('Line 1\nLine 2\nLine 3\nLine 4\nLine 5')
  1010. with io.StringIO() as buf:
  1011. with contextlib.redirect_stdout(buf):
  1012. search_file('Line 6', str(temp_file_path))
  1013. result = buf.getvalue()
  1014. assert result is not None
  1015. expected = f'[No matches found for "Line 6" in {temp_file_path}]\n'
  1016. assert result.split('\n') == expected.split('\n')
  1017. def test_search_file_not_exist_file():
  1018. with pytest.raises(FileNotFoundError):
  1019. search_file('Line 6', '/unexist/path/a.txt')
  1020. def test_find_file(tmp_path):
  1021. temp_file_path = tmp_path / 'a.txt'
  1022. temp_file_path.write_text('Line 1\nLine 2\nLine 3\nLine 4\nLine 5')
  1023. with io.StringIO() as buf:
  1024. with contextlib.redirect_stdout(buf):
  1025. find_file('a.txt', str(tmp_path))
  1026. result = buf.getvalue()
  1027. assert result is not None
  1028. expected = f'[Found 1 matches for "a.txt" in {tmp_path}]\n'
  1029. expected += f'{tmp_path}/a.txt\n'
  1030. expected += f'[End of matches for "a.txt" in {tmp_path}]\n'
  1031. assert result.split('\n') == expected.split('\n')
  1032. def test_find_file_cwd(tmp_path, monkeypatch):
  1033. monkeypatch.chdir(tmp_path)
  1034. temp_file_path = tmp_path / 'a.txt'
  1035. temp_file_path.write_text('Line 1\nLine 2\nLine 3\nLine 4\nLine 5')
  1036. with io.StringIO() as buf:
  1037. with contextlib.redirect_stdout(buf):
  1038. find_file('a.txt')
  1039. result = buf.getvalue()
  1040. assert result is not None
  1041. def test_find_file_not_exist_file():
  1042. with io.StringIO() as buf:
  1043. with contextlib.redirect_stdout(buf):
  1044. find_file('unexist.txt')
  1045. result = buf.getvalue()
  1046. assert result is not None
  1047. expected = '[No matches found for "unexist.txt" in ./]\n'
  1048. assert result.split('\n') == expected.split('\n')
  1049. def test_find_file_not_exist_file_specific_path(tmp_path):
  1050. with io.StringIO() as buf:
  1051. with contextlib.redirect_stdout(buf):
  1052. find_file('unexist.txt', str(tmp_path))
  1053. result = buf.getvalue()
  1054. assert result is not None
  1055. expected = f'[No matches found for "unexist.txt" in {tmp_path}]\n'
  1056. assert result.split('\n') == expected.split('\n')
  1057. def test_edit_lint_file_pass(tmp_path, monkeypatch):
  1058. # Enable linting
  1059. monkeypatch.setattr(
  1060. 'opendevin.runtime.plugins.agent_skills.agentskills.ENABLE_AUTO_LINT', True
  1061. )
  1062. file_path = _generate_test_file_with_lines(tmp_path, 1)
  1063. # Test linting functionality
  1064. with io.StringIO() as buf:
  1065. with contextlib.redirect_stdout(buf):
  1066. open_file(str(file_path))
  1067. insert_content_at_line(str(file_path), 1, "print('hello')\n")
  1068. result = buf.getvalue()
  1069. assert result is not None
  1070. expected = (
  1071. f'[File: {file_path} (1 lines total)]\n'
  1072. '(this is the beginning of the file)\n'
  1073. '1|\n'
  1074. '(this is the end of the file)\n'
  1075. f'[File: {file_path} (1 lines total after edit)]\n'
  1076. '(this is the beginning of the file)\n'
  1077. "1|print('hello')\n"
  1078. '(this is the end of the file)\n'
  1079. + MSG_FILE_UPDATED.format(line_number=1)
  1080. + '\n'
  1081. )
  1082. assert result.split('\n') == expected.split('\n')
  1083. def test_lint_file_fail_undefined_name(tmp_path, monkeypatch, capsys):
  1084. # Enable linting
  1085. monkeypatch.setattr(
  1086. 'opendevin.runtime.plugins.agent_skills.agentskills.ENABLE_AUTO_LINT', True
  1087. )
  1088. current_line = 1
  1089. file_path = _generate_test_file_with_lines(tmp_path, 1)
  1090. open_file(str(file_path), current_line)
  1091. insert_content_at_line(str(file_path), 1, 'undefined_name()\n')
  1092. result = capsys.readouterr().out
  1093. assert result is not None
  1094. expected = (
  1095. f'[File: {file_path} (1 lines total)]\n'
  1096. '(this is the beginning of the file)\n'
  1097. '1|\n'
  1098. '(this is the end of the file)\n'
  1099. '[Your proposed edit has introduced new syntax error(s). Please understand the errors and retry your edit command.]\n'
  1100. 'ERRORS:\n'
  1101. f"{file_path}:1:1: F821 undefined name 'undefined_name'\n"
  1102. '[This is how your edit would have looked if applied]\n'
  1103. '-------------------------------------------------\n'
  1104. '(this is the beginning of the file)\n'
  1105. '1|undefined_name()\n'
  1106. '(this is the end of the file)\n'
  1107. '-------------------------------------------------\n\n'
  1108. '[This is the original code before your edit]\n'
  1109. '-------------------------------------------------\n'
  1110. '(this is the beginning of the file)\n'
  1111. '1|\n'
  1112. '(this is the end of the file)\n'
  1113. '-------------------------------------------------\n'
  1114. 'Your changes have NOT been applied. Please fix your edit command and try again.\n'
  1115. 'You either need to 1) Specify the correct start/end line arguments or 2) Correct your edit code.\n'
  1116. 'DO NOT re-run the same failed edit command. Running it again will lead to the same error.\n'
  1117. )
  1118. assert result.split('\n') == expected.split('\n')
  1119. def test_lint_file_fail_undefined_name_long(tmp_path, monkeypatch, capsys):
  1120. # Enable linting
  1121. monkeypatch.setattr(
  1122. 'opendevin.runtime.plugins.agent_skills.agentskills.ENABLE_AUTO_LINT', True
  1123. )
  1124. num_lines = 1000
  1125. error_line = 500
  1126. file_path = _generate_test_file_with_lines(tmp_path, num_lines)
  1127. error_message = f"{file_path}:{error_line}:1: F821 undefined name 'undefined_name'"
  1128. open_file(str(file_path))
  1129. insert_content_at_line(str(file_path), error_line, 'undefined_name()\n')
  1130. result = capsys.readouterr().out
  1131. assert result is not None
  1132. open_lines = '\n'.join([f'{i}|' for i in range(1, WINDOW + 1)])
  1133. expected = (
  1134. f'[File: {file_path} ({num_lines} lines total)]\n'
  1135. '(this is the beginning of the file)\n'
  1136. f'{open_lines}\n'
  1137. f'({num_lines - WINDOW} more lines below)\n'
  1138. '[Your proposed edit has introduced new syntax error(s). Please understand the errors and retry your edit command.]\n'
  1139. f'ERRORS:\n{error_message}\n'
  1140. '[This is how your edit would have looked if applied]\n'
  1141. '-------------------------------------------------\n'
  1142. '(489 more lines above)\n'
  1143. + _numbered_test_lines(error_line - 10, error_line - 1)
  1144. + '500|undefined_name()\n'
  1145. + _numbered_test_lines(error_line + 1, error_line + 10)
  1146. + '(491 more lines below)\n'
  1147. + '-------------------------------------------------\n\n'
  1148. '[This is the original code before your edit]\n'
  1149. '-------------------------------------------------\n'
  1150. '(489 more lines above)\n'
  1151. + _numbered_test_lines(error_line - 10, error_line + 10)
  1152. + '(490 more lines below)\n'
  1153. + '-------------------------------------------------\n'
  1154. 'Your changes have NOT been applied. Please fix your edit command and try again.\n'
  1155. 'You either need to 1) Specify the correct start/end line arguments or 2) Correct your edit code.\n'
  1156. 'DO NOT re-run the same failed edit command. Running it again will lead to the same error.\n'
  1157. )
  1158. assert result.split('\n') == expected.split('\n')
  1159. def test_lint_file_disabled_undefined_name(tmp_path, monkeypatch, capsys):
  1160. # Disable linting
  1161. monkeypatch.setattr(
  1162. 'opendevin.runtime.plugins.agent_skills.agentskills.ENABLE_AUTO_LINT', False
  1163. )
  1164. file_path = _generate_test_file_with_lines(tmp_path, 1)
  1165. open_file(str(file_path))
  1166. insert_content_at_line(str(file_path), 1, 'undefined_name()\n')
  1167. result = capsys.readouterr().out
  1168. assert result is not None
  1169. expected = (
  1170. f'[File: {file_path} (1 lines total)]\n'
  1171. '(this is the beginning of the file)\n'
  1172. '1|\n'
  1173. '(this is the end of the file)\n'
  1174. f'[File: {file_path} (1 lines total after edit)]\n'
  1175. '(this is the beginning of the file)\n'
  1176. '1|undefined_name()\n'
  1177. '(this is the end of the file)\n'
  1178. + MSG_FILE_UPDATED.format(line_number=1)
  1179. + '\n'
  1180. )
  1181. assert result.split('\n') == expected.split('\n')
  1182. def test_parse_docx(tmp_path):
  1183. # Create a DOCX file with some content
  1184. test_docx_path = tmp_path / 'test.docx'
  1185. doc = docx.Document()
  1186. doc.add_paragraph('Hello, this is a test document.')
  1187. doc.add_paragraph('This is the second paragraph.')
  1188. doc.save(str(test_docx_path))
  1189. old_stdout = sys.stdout
  1190. sys.stdout = io.StringIO()
  1191. # Call the parse_docx function
  1192. parse_docx(str(test_docx_path))
  1193. # Capture the output
  1194. output = sys.stdout.getvalue()
  1195. sys.stdout = old_stdout
  1196. # Check if the output is correct
  1197. expected_output = (
  1198. f'[Reading DOCX file from {test_docx_path}]\n'
  1199. '@@ Page 1 @@\nHello, this is a test document.\n\n'
  1200. '@@ Page 2 @@\nThis is the second paragraph.\n\n\n'
  1201. )
  1202. assert output == expected_output, f'Expected output does not match. Got: {output}'
  1203. def test_parse_latex(tmp_path):
  1204. # Create a LaTeX file with some content
  1205. test_latex_path = tmp_path / 'test.tex'
  1206. with open(test_latex_path, 'w') as f:
  1207. f.write(r"""
  1208. \documentclass{article}
  1209. \begin{document}
  1210. Hello, this is a test LaTeX document.
  1211. \end{document}
  1212. """)
  1213. old_stdout = sys.stdout
  1214. sys.stdout = io.StringIO()
  1215. # Call the parse_latex function
  1216. parse_latex(str(test_latex_path))
  1217. # Capture the output
  1218. output = sys.stdout.getvalue()
  1219. sys.stdout = old_stdout
  1220. # Check if the output is correct
  1221. expected_output = (
  1222. f'[Reading LaTex file from {test_latex_path}]\n'
  1223. 'Hello, this is a test LaTeX document.\n'
  1224. )
  1225. assert output == expected_output, f'Expected output does not match. Got: {output}'
  1226. def test_parse_pdf(tmp_path):
  1227. # Create a PDF file with some content
  1228. test_pdf_path = tmp_path / 'test.pdf'
  1229. from reportlab.lib.pagesizes import letter
  1230. from reportlab.pdfgen import canvas
  1231. c = canvas.Canvas(str(test_pdf_path), pagesize=letter)
  1232. c.drawString(100, 750, 'Hello, this is a test PDF document.')
  1233. c.save()
  1234. old_stdout = sys.stdout
  1235. sys.stdout = io.StringIO()
  1236. # Call the parse_pdf function
  1237. parse_pdf(str(test_pdf_path))
  1238. # Capture the output
  1239. output = sys.stdout.getvalue()
  1240. sys.stdout = old_stdout
  1241. # Check if the output is correct
  1242. expected_output = (
  1243. f'[Reading PDF file from {test_pdf_path}]\n'
  1244. '@@ Page 1 @@\n'
  1245. 'Hello, this is a test PDF document.\n'
  1246. )
  1247. assert output == expected_output, f'Expected output does not match. Got: {output}'
  1248. def test_parse_pptx(tmp_path):
  1249. test_pptx_path = tmp_path / 'test.pptx'
  1250. from pptx import Presentation
  1251. pres = Presentation()
  1252. slide1 = pres.slides.add_slide(pres.slide_layouts[0])
  1253. title1 = slide1.shapes.title
  1254. title1.text = 'Hello, this is the first test PPTX slide.'
  1255. slide2 = pres.slides.add_slide(pres.slide_layouts[0])
  1256. title2 = slide2.shapes.title
  1257. title2.text = 'Hello, this is the second test PPTX slide.'
  1258. pres.save(str(test_pptx_path))
  1259. old_stdout = sys.stdout
  1260. sys.stdout = io.StringIO()
  1261. parse_pptx(str(test_pptx_path))
  1262. output = sys.stdout.getvalue()
  1263. sys.stdout = old_stdout
  1264. expected_output = (
  1265. f'[Reading PowerPoint file from {test_pptx_path}]\n'
  1266. '@@ Slide 1 @@\n'
  1267. 'Hello, this is the first test PPTX slide.\n\n'
  1268. '@@ Slide 2 @@\n'
  1269. 'Hello, this is the second test PPTX slide.\n\n'
  1270. )
  1271. assert output == expected_output, f'Expected output does not match. Got: {output}'
  1272. def test_lint_file_fail_non_python(tmp_path, monkeypatch, capsys):
  1273. monkeypatch.setattr(
  1274. 'opendevin.runtime.plugins.agent_skills.agentskills.ENABLE_AUTO_LINT', True
  1275. )
  1276. current_line = 1
  1277. file_path = _generate_ruby_test_file_with_lines(tmp_path, 1)
  1278. open_file(str(file_path), current_line)
  1279. insert_content_at_line(
  1280. str(file_path), 1, "def print_hello_world()\n puts 'Hello World'"
  1281. )
  1282. result = capsys.readouterr().out
  1283. assert result is not None
  1284. expected = (
  1285. f'[File: {file_path} (1 lines total)]\n'
  1286. '(this is the beginning of the file)\n'
  1287. '1|\n'
  1288. '(this is the end of the file)\n'
  1289. '[Your proposed edit has introduced new syntax error(s). Please understand the errors and retry your edit command.]\n'
  1290. 'ERRORS:\n'
  1291. f'{file_path}:1\n'
  1292. '[This is how your edit would have looked if applied]\n'
  1293. '-------------------------------------------------\n'
  1294. '(this is the beginning of the file)\n'
  1295. '1|def print_hello_world()\n'
  1296. "2| puts 'Hello World'\n"
  1297. '(this is the end of the file)\n'
  1298. '-------------------------------------------------\n\n'
  1299. '[This is the original code before your edit]\n'
  1300. '-------------------------------------------------\n'
  1301. '(this is the beginning of the file)\n'
  1302. '1|\n'
  1303. '(this is the end of the file)\n'
  1304. '-------------------------------------------------\n'
  1305. 'Your changes have NOT been applied. Please fix your edit command and try again.\n'
  1306. 'You either need to 1) Specify the correct start/end line arguments or 2) Correct your edit code.\n'
  1307. 'DO NOT re-run the same failed edit command. Running it again will lead to the same error.\n'
  1308. )
  1309. assert result.split('\n') == expected.split('\n')