test_agent_skill.py 54 KB

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