| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082 |
- import contextlib
- import io
- import os
- import sys
- import docx
- import pytest
- from opendevin.runtime.plugins.agent_skills.agentskills import (
- MSG_FILE_UPDATED,
- _print_window,
- append_file,
- create_file,
- edit_file,
- find_file,
- goto_line,
- open_file,
- parse_docx,
- parse_latex,
- parse_pdf,
- parse_pptx,
- scroll_down,
- scroll_up,
- search_dir,
- search_file,
- )
- # CURRENT_FILE must be reset for each test
- @pytest.fixture(autouse=True)
- def reset_current_file():
- from opendevin.runtime.plugins.agent_skills import agentskills
- agentskills.CURRENT_FILE = None
- def _numbered_test_lines(start, end) -> str:
- return ('\n'.join(f'{i}|' for i in range(start, end + 1))) + '\n'
- def _generate_test_file_with_lines(temp_path, num_lines) -> str:
- file_path = temp_path / 'test_file.py'
- file_path.write_text('\n' * num_lines)
- return file_path
- def test_open_file_unexist_path():
- with pytest.raises(FileNotFoundError):
- open_file('/unexist/path/a.txt')
- def test_open_file(tmp_path):
- assert tmp_path is not None
- temp_file_path = tmp_path / 'a.txt'
- temp_file_path.write_text('Line 1\nLine 2\nLine 3\nLine 4\nLine 5')
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- open_file(str(temp_file_path))
- result = buf.getvalue()
- assert result is not None
- expected = (
- f'[File: {temp_file_path} (5 lines total)]\n'
- '1|Line 1\n'
- '2|Line 2\n'
- '3|Line 3\n'
- '4|Line 4\n'
- '5|Line 5\n'
- )
- assert result.split('\n') == expected.split('\n')
- def test_open_file_with_indentation(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- temp_file_path.write_text('Line 1\n Line 2\nLine 3\nLine 4\nLine 5')
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- open_file(str(temp_file_path))
- result = buf.getvalue()
- assert result is not None
- expected = (
- f'[File: {temp_file_path} (5 lines total)]\n'
- '1|Line 1\n'
- '2| Line 2\n'
- '3|Line 3\n'
- '4|Line 4\n'
- '5|Line 5\n'
- )
- assert result.split('\n') == expected.split('\n')
- def test_open_file_long(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- content = '\n'.join([f'Line {i}' for i in range(1, 1001)])
- temp_file_path.write_text(content)
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- open_file(str(temp_file_path), 1, 50)
- result = buf.getvalue()
- assert result is not None
- expected = f'[File: {temp_file_path} (1000 lines total)]\n'
- for i in range(1, 51):
- expected += f'{i}|Line {i}\n'
- expected += '(950 more lines below)\n'
- assert result.split('\n') == expected.split('\n')
- def test_open_file_long_with_lineno(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- content = '\n'.join([f'Line {i}' for i in range(1, 1001)])
- temp_file_path.write_text(content)
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- open_file(str(temp_file_path), 100)
- result = buf.getvalue()
- assert result is not None
- expected = f'[File: {temp_file_path} (1000 lines total)]\n'
- expected += '(49 more lines above)\n'
- for i in range(50, 151):
- expected += f'{i}|Line {i}\n'
- expected += '(850 more lines below)\n'
- assert result.split('\n') == expected.split('\n')
- def test_create_file_unexist_path():
- with pytest.raises(FileNotFoundError):
- create_file('/unexist/path/a.txt')
- def test_create_file(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- create_file(str(temp_file_path))
- result = buf.getvalue()
- expected = (
- f'[File: {temp_file_path} (1 lines total)]\n'
- '1|\n'
- f'[File {temp_file_path} created.]\n'
- )
- assert result.split('\n') == expected.split('\n')
- def test_goto_line(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- content = '\n'.join([f'Line {i}' for i in range(1, 1001)])
- temp_file_path.write_text(content)
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- open_file(str(temp_file_path))
- result = buf.getvalue()
- assert result is not None
- expected = f'[File: {temp_file_path} (1000 lines total)]\n'
- for i in range(1, 101):
- expected += f'{i}|Line {i}\n'
- expected += '(900 more lines below)\n'
- assert result.split('\n') == expected.split('\n')
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- goto_line(100)
- result = buf.getvalue()
- assert result is not None
- expected = f'[File: {temp_file_path} (1000 lines total)]\n'
- expected += '(49 more lines above)\n'
- for i in range(50, 151):
- expected += f'{i}|Line {i}\n'
- expected += '(850 more lines below)\n'
- assert result.split('\n') == expected.split('\n')
- def test_goto_line_negative(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- content = '\n'.join([f'Line {i}' for i in range(1, 5)])
- temp_file_path.write_text(content)
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- open_file(str(temp_file_path))
- with pytest.raises(ValueError):
- goto_line(-1)
- def test_goto_line_out_of_bound(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- content = '\n'.join([f'Line {i}' for i in range(1, 5)])
- temp_file_path.write_text(content)
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- open_file(str(temp_file_path))
- with pytest.raises(ValueError):
- goto_line(100)
- def test_scroll_down(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- content = '\n'.join([f'Line {i}' for i in range(1, 1001)])
- temp_file_path.write_text(content)
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- open_file(str(temp_file_path))
- result = buf.getvalue()
- assert result is not None
- expected = f'[File: {temp_file_path} (1000 lines total)]\n'
- for i in range(1, 101):
- expected += f'{i}|Line {i}\n'
- expected += '(900 more lines below)\n'
- assert result.split('\n') == expected.split('\n')
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- scroll_down()
- result = buf.getvalue()
- assert result is not None
- expected = f'[File: {temp_file_path} (1000 lines total)]\n'
- expected += '(50 more lines above)\n'
- for i in range(51, 152):
- expected += f'{i}|Line {i}\n'
- expected += '(849 more lines below)\n'
- assert result.split('\n') == expected.split('\n')
- def test_scroll_up(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- content = '\n'.join([f'Line {i}' for i in range(1, 1001)])
- temp_file_path.write_text(content)
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- open_file(str(temp_file_path), 300)
- result = buf.getvalue()
- assert result is not None
- expected = f'[File: {temp_file_path} (1000 lines total)]\n'
- expected += '(249 more lines above)\n'
- for i in range(250, 351):
- expected += f'{i}|Line {i}\n'
- expected += '(650 more lines below)\n'
- assert result.split('\n') == expected.split('\n')
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- scroll_up()
- result = buf.getvalue()
- assert result is not None
- expected = f'[File: {temp_file_path} (1000 lines total)]\n'
- expected += '(149 more lines above)\n'
- for i in range(150, 251):
- expected += f'{i}|Line {i}\n'
- expected += '(750 more lines below)\n'
- assert result.split('\n') == expected.split('\n')
- def test_scroll_down_edge(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- content = '\n'.join([f'Line {i}' for i in range(1, 10)])
- temp_file_path.write_text(content)
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- open_file(str(temp_file_path))
- result = buf.getvalue()
- assert result is not None
- expected = f'[File: {temp_file_path} (9 lines total)]\n'
- for i in range(1, 10):
- expected += f'{i}|Line {i}\n'
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- scroll_down()
- result = buf.getvalue()
- assert result is not None
- # expected should be unchanged
- assert result.split('\n') == expected.split('\n')
- def test_print_window_internal(tmp_path):
- test_file_path = tmp_path / 'a.txt'
- create_file(str(test_file_path))
- open_file(str(test_file_path))
- with open(test_file_path, 'w') as file:
- for i in range(1, 101):
- file.write(f'Line `{i}`\n')
- # Define the parameters for the test
- current_line = 50
- window = 2
- # Test _print_window especially with backticks
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- _print_window(str(test_file_path), current_line, window, return_str=False)
- result = buf.getvalue()
- expected = (
- '(48 more lines above)\n'
- '49|Line `49`\n'
- '50|Line `50`\n'
- '51|Line `51`\n'
- '(49 more lines below)\n'
- )
- assert result == expected
- def test_edit_file_window(tmp_path, monkeypatch):
- # Set environment variable via monkeypatch does NOT work!
- monkeypatch.setattr(
- 'opendevin.runtime.plugins.agent_skills.agentskills.ENABLE_AUTO_LINT', True
- )
- content = """def any_int(a, b, c):
- return isinstance(a, int) and isinstance(b, int) and isinstance(c, int)
- def test_any_int():
- assert any_int(1, 2, 3) == True
- assert any_int(1.5, 2, 3) == False
- assert any_int(1, 2.5, 3) == False
- assert any_int(1, 2, 3.5) == False
- assert any_int(1.0, 2, 3) == False
- assert any_int(1, 2.0, 3) == False
- assert any_int(1, 2, 3.0) == False
- assert any_int(0, 0, 0) == True
- assert any_int(-1, -2, -3) == True
- assert any_int(1, -2, 3) == True
- assert any_int(1.5, -2, 3) == False
- assert any_int(1, -2.5, 3) == False
- def check(any_int):
- # Check some simple cases
- assert any_int(2, 3, 1)==True, "This prints if this assert fails 1 (good for debugging!)"
- assert any_int(2.5, 2, 3)==False, "This prints if this assert fails 2 (good for debugging!)"
- assert any_int(1.5, 5, 3.5)==False, "This prints if this assert fails 3 (good for debugging!)"
- assert any_int(2, 6, 2)==False, "This prints if this assert fails 4 (good for debugging!)"
- assert any_int(4, 2, 2)==True, "This prints if this assert fails 5 (good for debugging!)"
- assert any_int(2.2, 2.2, 2.2)==False, "This prints if this assert fails 6 (good for debugging!)"
- assert any_int(-4, 6, 2)==True, "This prints if this assert fails 7 (good for debugging!)"
- # Check some edge cases that are easy to work out by hand.
- assert any_int(2,1,1)==True, "This prints if this assert fails 8 (also good for debugging!)"
- assert any_int(3,4,7)==True, "This prints if this assert fails 9 (also good for debugging!)"
- assert any_int(3.0,4,7)==False, "This prints if this assert fails 10 (also good for debugging!)"
- check(any_int)"""
- temp_file_path = tmp_path / 'error-test.py'
- temp_file_path.write_text(content)
- open_file(str(temp_file_path))
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- edit_file(
- str(temp_file_path),
- start=9,
- end=9,
- content=' assert any_int(1.0, 2, 3) == False',
- )
- result = buf.getvalue()
- expected = (
- '[Your proposed edit has introduced new syntax error(s). Please understand the errors and retry your edit command.]\n'
- 'ERRORS:\n'
- + str(temp_file_path)
- + ':9:9: '
- + 'E999 IndentationError: unexpected indent\n'
- '[This is how your edit would have looked if applied]\n'
- '-------------------------------------------------\n'
- '(3 more lines above)\n'
- '4|def test_any_int():\n'
- '5| assert any_int(1, 2, 3) == True\n'
- '6| assert any_int(1.5, 2, 3) == False\n'
- '7| assert any_int(1, 2.5, 3) == False\n'
- '8| assert any_int(1, 2, 3.5) == False\n'
- '9| assert any_int(1.0, 2, 3) == False\n'
- '10| assert any_int(1, 2.0, 3) == False\n'
- '11| assert any_int(1, 2, 3.0) == False\n'
- '12| assert any_int(0, 0, 0) == True\n'
- '13| assert any_int(-1, -2, -3) == True\n'
- '14| assert any_int(1, -2, 3) == True\n'
- '(19 more lines below)\n'
- '-------------------------------------------------\n'
- '\n'
- '[This is the original code before your edit]\n'
- '-------------------------------------------------\n'
- '(3 more lines above)\n'
- '4|def test_any_int():\n'
- '5| assert any_int(1, 2, 3) == True\n'
- '6| assert any_int(1.5, 2, 3) == False\n'
- '7| assert any_int(1, 2.5, 3) == False\n'
- '8| assert any_int(1, 2, 3.5) == False\n'
- '9| assert any_int(1.0, 2, 3) == False\n'
- '10| assert any_int(1, 2.0, 3) == False\n'
- '11| assert any_int(1, 2, 3.0) == False\n'
- '12| assert any_int(0, 0, 0) == True\n'
- '13| assert any_int(-1, -2, -3) == True\n'
- '14| assert any_int(1, -2, 3) == True\n'
- '(19 more lines below)\n'
- '-------------------------------------------------\n'
- 'Your changes have NOT been applied. Please fix your edit command and try again.\n'
- 'You either need to 1) Specify the correct start/end line arguments or 2) Correct your edit code.\n'
- 'DO NOT re-run the same failed edit command. Running it again will lead to the same error.\n'
- )
- assert result == expected
- def test_append_file(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- content = 'Line 1\nLine 2'
- temp_file_path.write_text(content)
- open_file(str(temp_file_path))
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- append_file(str(temp_file_path), content='APPENDED TEXT')
- result = buf.getvalue()
- expected = (
- f'[File: {temp_file_path} (3 lines total after edit)]\n'
- '1|Line 1\n'
- '2|Line 2\n'
- '3|APPENDED TEXT\n'
- '[File updated. Please review the changes and make sure they are correct (correct indentation, no duplicate lines, etc). Edit the file again if necessary.]\n'
- )
- assert result.split('\n') == expected.split('\n')
- with open(temp_file_path, 'r') as file:
- lines = file.readlines()
- assert len(lines) == 3
- assert lines[0].rstrip() == 'Line 1'
- assert lines[1].rstrip() == 'Line 2'
- assert lines[2].rstrip() == 'APPENDED TEXT'
- def test_append_file_from_scratch(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- create_file(str(temp_file_path))
- try:
- open_file(str(temp_file_path))
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- append_file(str(temp_file_path), content='APPENDED TEXT')
- result = buf.getvalue()
- expected = (
- f'[File: {temp_file_path} (1 lines total after edit)]\n'
- '1|APPENDED TEXT\n'
- '[File updated. Please review the changes and make sure they are correct (correct indentation, no duplicate lines, etc). Edit the file again if necessary.]\n'
- )
- assert result.split('\n') == expected.split('\n')
- with open(temp_file_path, 'r') as file:
- lines = file.readlines()
- assert len(lines) == 1
- assert lines[0].rstrip() == 'APPENDED TEXT'
- finally:
- os.remove(temp_file_path)
- def test_append_file_from_scratch_multiline(tmp_path):
- temp_file_path = tmp_path / 'a3.txt'
- create_file(str(temp_file_path))
- try:
- open_file(temp_file_path)
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- append_file(
- str(temp_file_path),
- content='APPENDED TEXT1\nAPPENDED TEXT2\nAPPENDED TEXT3',
- )
- result = buf.getvalue()
- expected = (
- f'[File: {temp_file_path} (3 lines total after edit)]\n'
- '1|APPENDED TEXT1\n'
- '2|APPENDED TEXT2\n'
- '3|APPENDED TEXT3\n'
- '[File updated. Please review the changes and make sure they are correct (correct indentation, no duplicate lines, etc). Edit the file again if necessary.]\n'
- )
- assert result.split('\n') == expected.split('\n')
- with open(temp_file_path, 'r') as file:
- lines = file.readlines()
- assert len(lines) == 3
- assert lines[0].rstrip() == 'APPENDED TEXT1'
- assert lines[1].rstrip() == 'APPENDED TEXT2'
- assert lines[2].rstrip() == 'APPENDED TEXT3'
- finally:
- os.remove(temp_file_path)
- def test_append_file_not_opened():
- with pytest.raises(FileNotFoundError):
- append_file(str('unknown file'), content='APPEND TEXT')
- def test_edit_file(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- content = 'Line 1\nLine 2\nLine 3\nLine 4\nLine 5'
- temp_file_path.write_text(content)
- open_file(str(temp_file_path))
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- edit_file(
- file_name=str(temp_file_path),
- start=1,
- end=3,
- content='REPLACE TEXT',
- )
- result = buf.getvalue()
- expected = (
- f'[File: {temp_file_path} (3 lines total after edit)]\n'
- '1|REPLACE TEXT\n'
- '2|Line 4\n'
- '3|Line 5\n' + MSG_FILE_UPDATED + '\n'
- )
- assert result.split('\n') == expected.split('\n')
- with open(temp_file_path, 'r') as file:
- lines = file.readlines()
- assert len(lines) == 3
- assert lines[0].rstrip() == 'REPLACE TEXT'
- assert lines[1].rstrip() == 'Line 4'
- assert lines[2].rstrip() == 'Line 5'
- def test_edit_file_from_scratch(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- create_file(str(temp_file_path))
- open_file(str(temp_file_path))
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- edit_file(
- str(temp_file_path),
- start=1,
- end=1,
- content='REPLACE TEXT',
- )
- result = buf.getvalue()
- expected = (
- f'[File: {temp_file_path} (1 lines total after edit)]\n'
- '1|REPLACE TEXT\n' + MSG_FILE_UPDATED + '\n'
- )
- assert result.split('\n') == expected.split('\n')
- with open(temp_file_path, 'r') as file:
- lines = file.readlines()
- assert len(lines) == 1
- assert lines[0].rstrip() == 'REPLACE TEXT'
- def test_edit_file_from_scratch_multiline_with_backticks_and_second_edit(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- create_file(str(temp_file_path))
- open_file(str(temp_file_path))
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- edit_file(
- str(temp_file_path),
- 1,
- 1,
- '`REPLACE TEXT1`\n`REPLACE TEXT2`\n`REPLACE TEXT3`',
- )
- result = buf.getvalue()
- expected = (
- f'[File: {temp_file_path} (3 lines total after edit)]\n'
- '1|`REPLACE TEXT1`\n'
- '2|`REPLACE TEXT2`\n'
- '3|`REPLACE TEXT3`\n' + MSG_FILE_UPDATED + '\n'
- )
- assert result.split('\n') == expected.split('\n')
- with open(temp_file_path, 'r') as file:
- lines = file.readlines()
- assert len(lines) == 3
- assert lines[0].rstrip() == '`REPLACE TEXT1`'
- assert lines[1].rstrip() == '`REPLACE TEXT2`'
- assert lines[2].rstrip() == '`REPLACE TEXT3`'
- # Check that no backticks are escaped in the edit_file call
- assert '\\`' not in result
- # Perform a second edit
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- edit_file(
- str(temp_file_path),
- 1,
- 3,
- '`REPLACED TEXT1`\n`REPLACED TEXT2`\n`REPLACED TEXT3`',
- )
- second_result = buf.getvalue()
- second_expected = (
- f'[File: {temp_file_path} (3 lines total after edit)]\n'
- '1|`REPLACED TEXT1`\n'
- '2|`REPLACED TEXT2`\n'
- '3|`REPLACED TEXT3`\n' + MSG_FILE_UPDATED + '\n'
- )
- assert second_result.split('\n') == second_expected.split('\n')
- with open(temp_file_path, 'r') as file:
- lines = file.readlines()
- assert len(lines) == 3
- assert lines[0].rstrip() == '`REPLACED TEXT1`'
- assert lines[1].rstrip() == '`REPLACED TEXT2`'
- assert lines[2].rstrip() == '`REPLACED TEXT3`'
- # Check that no backticks are escaped in the second edit_file call
- assert '\\`' not in second_result
- def test_edit_file_from_scratch_multiline(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- create_file(str(temp_file_path))
- open_file(temp_file_path)
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- edit_file(
- str(temp_file_path),
- 1,
- 1,
- content='REPLACE TEXT1\nREPLACE TEXT2\nREPLACE TEXT3',
- )
- result = buf.getvalue()
- expected = (
- f'[File: {temp_file_path} (3 lines total after edit)]\n'
- '1|REPLACE TEXT1\n'
- '2|REPLACE TEXT2\n'
- '3|REPLACE TEXT3\n' + MSG_FILE_UPDATED + '\n'
- )
- assert result.split('\n') == expected.split('\n')
- with open(temp_file_path, 'r') as file:
- lines = file.readlines()
- assert len(lines) == 3
- assert lines[0].rstrip() == 'REPLACE TEXT1'
- assert lines[1].rstrip() == 'REPLACE TEXT2'
- assert lines[2].rstrip() == 'REPLACE TEXT3'
- def test_edit_file_not_opened():
- with pytest.raises(FileNotFoundError):
- edit_file(
- str('unknown file'),
- 1,
- 3,
- 'REPLACE TEXT',
- )
- def test_search_dir(tmp_path):
- # create files with the search term "bingo"
- for i in range(1, 101):
- temp_file_path = tmp_path / f'a{i}.txt'
- with open(temp_file_path, 'w') as file:
- file.write('Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n')
- if i == 50:
- file.write('bingo')
- # test
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- search_dir('bingo', str(tmp_path))
- result = buf.getvalue()
- assert result is not None
- expected = (
- f'[Found 1 matches for "bingo" in {tmp_path}]\n'
- f'{tmp_path}/a50.txt (Line 6): bingo\n'
- f'[End of matches for "bingo" in {tmp_path}]\n'
- )
- assert result.split('\n') == expected.split('\n')
- def test_search_dir_not_exist_term(tmp_path):
- # create files with the search term "bingo"
- for i in range(1, 101):
- temp_file_path = tmp_path / f'a{i}.txt'
- with open(temp_file_path, 'w') as file:
- file.write('Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n')
- # test
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- search_dir('non-exist', str(tmp_path))
- result = buf.getvalue()
- assert result is not None
- expected = f'No matches found for "non-exist" in {tmp_path}\n'
- assert result.split('\n') == expected.split('\n')
- def test_search_dir_too_much_match(tmp_path):
- # create files with the search term "Line 5"
- for i in range(1, 1000):
- temp_file_path = tmp_path / f'a{i}.txt'
- with open(temp_file_path, 'w') as file:
- file.write('Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n')
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- search_dir('Line 5', str(tmp_path))
- result = buf.getvalue()
- assert result is not None
- expected = f'More than 999 files matched for "Line 5" in {tmp_path}. Please narrow your search.\n'
- assert result.split('\n') == expected.split('\n')
- def test_search_dir_cwd(tmp_path, monkeypatch):
- # Using pytest's monkeypatch to change directory without affecting other tests
- monkeypatch.chdir(tmp_path)
- # create files with the search term "bingo"
- for i in range(1, 101):
- temp_file_path = tmp_path / f'a{i}.txt'
- with open(temp_file_path, 'w') as file:
- file.write('Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n')
- if i == 50:
- file.write('bingo')
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- search_dir('bingo')
- result = buf.getvalue()
- assert result is not None
- expected = (
- '[Found 1 matches for "bingo" in ./]\n'
- './a50.txt (Line 6): bingo\n'
- '[End of matches for "bingo" in ./]\n'
- )
- assert result.split('\n') == expected.split('\n')
- def test_search_file(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- temp_file_path.write_text('Line 1\nLine 2\nLine 3\nLine 4\nLine 5')
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- search_file('Line 5', str(temp_file_path))
- result = buf.getvalue()
- assert result is not None
- expected = f'[Found 1 matches for "Line 5" in {temp_file_path}]\n'
- expected += 'Line 5: Line 5\n'
- expected += f'[End of matches for "Line 5" in {temp_file_path}]\n'
- assert result.split('\n') == expected.split('\n')
- def test_search_file_not_exist_term(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- temp_file_path.write_text('Line 1\nLine 2\nLine 3\nLine 4\nLine 5')
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- search_file('Line 6', str(temp_file_path))
- result = buf.getvalue()
- assert result is not None
- expected = f'[No matches found for "Line 6" in {temp_file_path}]\n'
- assert result.split('\n') == expected.split('\n')
- def test_search_file_not_exist_file():
- with pytest.raises(FileNotFoundError):
- search_file('Line 6', '/unexist/path/a.txt')
- def test_find_file(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- temp_file_path.write_text('Line 1\nLine 2\nLine 3\nLine 4\nLine 5')
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- find_file('a.txt', str(tmp_path))
- result = buf.getvalue()
- assert result is not None
- expected = f'[Found 1 matches for "a.txt" in {tmp_path}]\n'
- expected += f'{tmp_path}/a.txt\n'
- expected += f'[End of matches for "a.txt" in {tmp_path}]\n'
- assert result.split('\n') == expected.split('\n')
- def test_find_file_cwd(tmp_path, monkeypatch):
- monkeypatch.chdir(tmp_path)
- temp_file_path = tmp_path / 'a.txt'
- temp_file_path.write_text('Line 1\nLine 2\nLine 3\nLine 4\nLine 5')
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- find_file('a.txt')
- result = buf.getvalue()
- assert result is not None
- def test_find_file_not_exist_file():
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- find_file('unexist.txt')
- result = buf.getvalue()
- assert result is not None
- expected = '[No matches found for "unexist.txt" in ./]\n'
- assert result.split('\n') == expected.split('\n')
- def test_find_file_not_exist_file_specific_path(tmp_path):
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- find_file('unexist.txt', str(tmp_path))
- result = buf.getvalue()
- assert result is not None
- expected = f'[No matches found for "unexist.txt" in {tmp_path}]\n'
- assert result.split('\n') == expected.split('\n')
- def test_edit_lint_file_pass(tmp_path, monkeypatch):
- # Enable linting
- monkeypatch.setattr(
- 'opendevin.runtime.plugins.agent_skills.agentskills.ENABLE_AUTO_LINT', True
- )
- file_path = _generate_test_file_with_lines(tmp_path, 1)
- # Test linting functionality
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- open_file(str(file_path))
- edit_file(str(file_path), 1, 1, "print('hello')\n")
- result = buf.getvalue()
- assert result is not None
- expected = (
- f'[File: {file_path} (1 lines total)]\n'
- '1|\n'
- f'[File: {file_path} (1 lines total after edit)]\n'
- "1|print('hello')\n" + MSG_FILE_UPDATED + '\n'
- )
- assert result.split('\n') == expected.split('\n')
- def test_lint_file_fail_undefined_name(tmp_path, monkeypatch, capsys):
- # Enable linting
- monkeypatch.setattr(
- 'opendevin.runtime.plugins.agent_skills.agentskills.ENABLE_AUTO_LINT', True
- )
- num_lines = 1
- current_line = 1
- file_path = _generate_test_file_with_lines(tmp_path, 1)
- open_file(str(file_path), current_line)
- edit_file(str(file_path), current_line, num_lines, 'undefined_name()\n')
- result = capsys.readouterr().out
- assert result is not None
- expected = (
- f'[File: {file_path} (1 lines total)]\n'
- '1|\n'
- '[Your proposed edit has introduced new syntax error(s). Please understand the errors and retry your edit command.]\n'
- 'ERRORS:\n'
- f"{file_path}:1:1: F821 undefined name 'undefined_name'\n"
- '[This is how your edit would have looked if applied]\n'
- '-------------------------------------------------\n'
- '1|undefined_name()\n'
- '-------------------------------------------------\n\n'
- '[This is the original code before your edit]\n'
- '-------------------------------------------------\n'
- '1|\n'
- '-------------------------------------------------\n'
- 'Your changes have NOT been applied. Please fix your edit command and try again.\n'
- 'You either need to 1) Specify the correct start/end line arguments or 2) Correct your edit code.\n'
- 'DO NOT re-run the same failed edit command. Running it again will lead to the same error.\n'
- )
- assert result.split('\n') == expected.split('\n')
- def test_lint_file_fail_undefined_name_long(tmp_path, monkeypatch, capsys):
- # Enable linting
- monkeypatch.setattr(
- 'opendevin.runtime.plugins.agent_skills.agentskills.ENABLE_AUTO_LINT', True
- )
- num_lines = 1000
- current_line = 500
- error_line = 500
- window = 100
- file_path = _generate_test_file_with_lines(tmp_path, num_lines)
- error_message = f"{file_path}:{error_line}:1: F821 undefined name 'undefined_name'"
- open_file(str(file_path))
- edit_file(str(file_path), current_line, error_line, 'undefined_name()\n')
- result = capsys.readouterr().out
- assert result is not None
- open_lines = '\n'.join([f'{i}|' for i in range(1, window + 1)])
- expected = (
- f'[File: {file_path} ({num_lines} lines total)]\n'
- f'{open_lines}\n'
- '(900 more lines below)\n'
- '[Your proposed edit has introduced new syntax error(s). Please understand the errors and retry your edit command.]\n'
- f'ERRORS:\n{error_message}\n'
- '[This is how your edit would have looked if applied]\n'
- '-------------------------------------------------\n'
- '(494 more lines above)\n'
- + _numbered_test_lines(error_line - 5, error_line - 1)
- + '500|undefined_name()\n'
- + _numbered_test_lines(error_line + 1, error_line + 5)
- + '(495 more lines below)\n'
- + '-------------------------------------------------\n\n'
- '[This is the original code before your edit]\n'
- '-------------------------------------------------\n'
- '(494 more lines above)\n'
- + _numbered_test_lines(error_line - 5, error_line + 5)
- + '(495 more lines below)\n'
- + '-------------------------------------------------\n'
- 'Your changes have NOT been applied. Please fix your edit command and try again.\n'
- 'You either need to 1) Specify the correct start/end line arguments or 2) Correct your edit code.\n'
- 'DO NOT re-run the same failed edit command. Running it again will lead to the same error.\n'
- )
- assert result.split('\n') == expected.split('\n')
- def test_lint_file_disabled_undefined_name(tmp_path, monkeypatch, capsys):
- # Disable linting
- monkeypatch.setattr(
- 'opendevin.runtime.plugins.agent_skills.agentskills.ENABLE_AUTO_LINT', False
- )
- file_path = _generate_test_file_with_lines(tmp_path, 1)
- open_file(str(file_path))
- edit_file(str(file_path), 1, 1, 'undefined_name()\n')
- result = capsys.readouterr().out
- assert result is not None
- expected = (
- f'[File: {file_path} (1 lines total)]\n'
- '1|\n'
- f'[File: {file_path} (1 lines total after edit)]\n'
- '1|undefined_name()\n' + MSG_FILE_UPDATED + '\n'
- )
- assert result.split('\n') == expected.split('\n')
- def test_parse_docx(tmp_path):
- # Create a DOCX file with some content
- test_docx_path = tmp_path / 'test.docx'
- doc = docx.Document()
- doc.add_paragraph('Hello, this is a test document.')
- doc.add_paragraph('This is the second paragraph.')
- doc.save(str(test_docx_path))
- old_stdout = sys.stdout
- sys.stdout = io.StringIO()
- # Call the parse_docx function
- parse_docx(str(test_docx_path))
- # Capture the output
- output = sys.stdout.getvalue()
- sys.stdout = old_stdout
- # Check if the output is correct
- expected_output = (
- f'[Reading DOCX file from {test_docx_path}]\n'
- '@@ Page 1 @@\nHello, this is a test document.\n\n'
- '@@ Page 2 @@\nThis is the second paragraph.\n\n\n'
- )
- assert output == expected_output, f'Expected output does not match. Got: {output}'
- def test_parse_latex(tmp_path):
- # Create a LaTeX file with some content
- test_latex_path = tmp_path / 'test.tex'
- with open(test_latex_path, 'w') as f:
- f.write(r"""
- \documentclass{article}
- \begin{document}
- Hello, this is a test LaTeX document.
- \end{document}
- """)
- old_stdout = sys.stdout
- sys.stdout = io.StringIO()
- # Call the parse_latex function
- parse_latex(str(test_latex_path))
- # Capture the output
- output = sys.stdout.getvalue()
- sys.stdout = old_stdout
- # Check if the output is correct
- expected_output = (
- f'[Reading LaTex file from {test_latex_path}]\n'
- 'Hello, this is a test LaTeX document.\n'
- )
- assert output == expected_output, f'Expected output does not match. Got: {output}'
- def test_parse_pdf(tmp_path):
- # Create a PDF file with some content
- test_pdf_path = tmp_path / 'test.pdf'
- from reportlab.lib.pagesizes import letter
- from reportlab.pdfgen import canvas
- c = canvas.Canvas(str(test_pdf_path), pagesize=letter)
- c.drawString(100, 750, 'Hello, this is a test PDF document.')
- c.save()
- old_stdout = sys.stdout
- sys.stdout = io.StringIO()
- # Call the parse_pdf function
- parse_pdf(str(test_pdf_path))
- # Capture the output
- output = sys.stdout.getvalue()
- sys.stdout = old_stdout
- # Check if the output is correct
- expected_output = (
- f'[Reading PDF file from {test_pdf_path}]\n'
- '@@ Page 1 @@\n'
- 'Hello, this is a test PDF document.\n'
- )
- assert output == expected_output, f'Expected output does not match. Got: {output}'
- def test_parse_pptx(tmp_path):
- test_pptx_path = tmp_path / 'test.pptx'
- from pptx import Presentation
- pres = Presentation()
- slide1 = pres.slides.add_slide(pres.slide_layouts[0])
- title1 = slide1.shapes.title
- title1.text = 'Hello, this is the first test PPTX slide.'
- slide2 = pres.slides.add_slide(pres.slide_layouts[0])
- title2 = slide2.shapes.title
- title2.text = 'Hello, this is the second test PPTX slide.'
- pres.save(str(test_pptx_path))
- old_stdout = sys.stdout
- sys.stdout = io.StringIO()
- parse_pptx(str(test_pptx_path))
- output = sys.stdout.getvalue()
- sys.stdout = old_stdout
- expected_output = (
- f'[Reading PowerPoint file from {test_pptx_path}]\n'
- '@@ Slide 1 @@\n'
- 'Hello, this is the first test PPTX slide.\n\n'
- '@@ Slide 2 @@\n'
- 'Hello, this is the second test PPTX slide.\n\n'
- )
- assert output == expected_output, f'Expected output does not match. Got: {output}'
|