test_agent_skill.py 53 KB

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