test_agent_skill.py 54 KB

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