test_agent_skill.py 51 KB

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