test_agent_skill.py 56 KB

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