test_agent_skill.py 54 KB

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