| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629 |
- import contextlib
- import io
- import os
- import sys
- from unittest.mock import patch
- import docx
- import pytest
- from openhands.runtime.plugins.agent_skills.file_ops.file_ops import (
- MSG_FILE_UPDATED,
- WINDOW,
- _print_window,
- append_file,
- create_file,
- edit_file_by_replace,
- find_file,
- goto_line,
- insert_content_at_line,
- open_file,
- scroll_down,
- scroll_up,
- search_dir,
- search_file,
- )
- from openhands.runtime.plugins.agent_skills.file_reader.file_readers import (
- parse_docx,
- parse_latex,
- parse_pdf,
- parse_pptx,
- )
- from openhands.runtime.plugins.agent_skills.utils.aider import Linter
- # CURRENT_FILE must be reset for each test
- @pytest.fixture(autouse=True)
- def reset_current_file():
- from openhands.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 _generate_ruby_test_file_with_lines(temp_path, num_lines) -> str:
- file_path = temp_path / 'test_file.rb'
- file_path.write_text('\n' * num_lines)
- return file_path
- def _calculate_window_bounds(current_line, total_lines, window_size):
- """Calculate the bounds of the window around the current line."""
- half_window = window_size // 2
- if current_line - half_window < 0:
- start = 1
- end = window_size
- else:
- start = current_line - half_window
- end = current_line + half_window
- return start, end
- def _generate_ruby_test_file_with_lines(temp_path, num_lines) -> str:
- file_path = temp_path / 'test_file.rb'
- file_path.write_text('\n' * num_lines)
- return file_path
- def _capture_file_operation_error(operation, expected_error_msg):
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- operation()
- result = buf.getvalue().strip()
- assert result == expected_error_msg
- SEP = '-' * 49 + '\n'
- # =============================================================================
- def test_open_file_unexist_path():
- _capture_file_operation_error(
- lambda: open_file('/unexist/path/a.txt'),
- 'ERROR: File /unexist/path/a.txt not found.',
- )
- 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'
- '(this is the beginning of the file)\n'
- '1|Line 1\n'
- '2|Line 2\n'
- '3|Line 3\n'
- '4|Line 4\n'
- '5|Line 5\n'
- '(this is the end of the file)\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'
- '(this is the beginning of the file)\n'
- '1|Line 1\n'
- '2| Line 2\n'
- '3|Line 3\n'
- '4|Line 4\n'
- '5|Line 5\n'
- '(this is the end of the file)\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'
- expected += '(this is the beginning of the file)\n'
- for i in range(1, 51):
- expected += f'{i}|Line {i}\n'
- expected += '(950 more lines below)\n'
- expected += '[Use `scroll_down` to view the next 100 lines of the file!]\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)
- cur_line = 100
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- open_file(str(temp_file_path), cur_line)
- result = buf.getvalue()
- assert result is not None
- expected = f'[File: {temp_file_path} (1000 lines total)]\n'
- # since 100 is < WINDOW and 100 - WINDOW//2 < 0, so it should show all lines from 1 to WINDOW
- start, end = _calculate_window_bounds(cur_line, 1000, WINDOW)
- if start == 1:
- expected += '(this is the beginning of the file)\n'
- else:
- expected += f'({start - 1} more lines above)\n'
- for i in range(start, end + 1):
- expected += f'{i}|Line {i}\n'
- if end == 1000:
- expected += '(this is the end of the file)\n'
- else:
- expected += f'({1000 - end} more lines below)\n'
- expected += '[Use `scroll_down` to view the next 100 lines of the file!]\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'
- '(this is the beginning of the file)\n'
- '1|\n'
- '(this is the end of the file)\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'
- total_lines = 1000
- content = '\n'.join([f'Line {i}' for i in range(1, total_lines + 1)])
- 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} ({total_lines} lines total)]\n'
- expected += '(this is the beginning of the file)\n'
- for i in range(1, WINDOW + 1):
- expected += f'{i}|Line {i}\n'
- expected += f'({total_lines - WINDOW} more lines below)\n'
- expected += '[Use `scroll_down` to view the next 100 lines of the file!]\n'
- assert result.split('\n') == expected.split('\n')
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- goto_line(500)
- result = buf.getvalue()
- assert result is not None
- cur_line = 500
- expected = f'[File: {temp_file_path} ({total_lines} lines total)]\n'
- start, end = _calculate_window_bounds(cur_line, total_lines, WINDOW)
- if start == 1:
- expected += '(this is the beginning of the file)\n'
- else:
- expected += f'({start - 1} more lines above)\n'
- for i in range(start, end + 1):
- expected += f'{i}|Line {i}\n'
- if end == total_lines:
- expected += '(this is the end of the file)\n'
- else:
- expected += f'({total_lines - end} 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))
- _capture_file_operation_error(
- lambda: goto_line(-1), 'ERROR: Line number must be between 1 and 4.'
- )
- 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, 10)])
- temp_file_path.write_text(content)
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- open_file(str(temp_file_path))
- _capture_file_operation_error(
- lambda: goto_line(100), 'ERROR: Line number must be between 1 and 9.'
- )
- def test_scroll_down(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- total_lines = 1000
- content = '\n'.join([f'Line {i}' for i in range(1, total_lines + 1)])
- 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} ({total_lines} lines total)]\n'
- start, end = _calculate_window_bounds(1, total_lines, WINDOW)
- if start == 1:
- expected += '(this is the beginning of the file)\n'
- else:
- expected += f'({start - 1} more lines above)\n'
- for i in range(start, end + 1):
- expected += f'{i}|Line {i}\n'
- if end == total_lines:
- expected += '(this is the end of the file)\n'
- else:
- expected += f'({total_lines - end} more lines below)\n'
- expected += '[Use `scroll_down` to view the next 100 lines of the file!]\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} ({total_lines} lines total)]\n'
- start = WINDOW + 1
- end = 2 * WINDOW + 1
- if start == 1:
- expected += '(this is the beginning of the file)\n'
- else:
- expected += f'({start - 1} more lines above)\n'
- for i in range(start, end + 1):
- expected += f'{i}|Line {i}\n'
- if end == total_lines:
- expected += '(this is the end of the file)\n'
- else:
- expected += f'({total_lines - end} more lines below)\n'
- assert result.split('\n') == expected.split('\n')
- def test_scroll_up(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- total_lines = 1000
- content = '\n'.join([f'Line {i}' for i in range(1, total_lines + 1)])
- temp_file_path.write_text(content)
- cur_line = 300
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- open_file(str(temp_file_path), cur_line)
- result = buf.getvalue()
- assert result is not None
- expected = f'[File: {temp_file_path} ({total_lines} lines total)]\n'
- start, end = _calculate_window_bounds(cur_line, total_lines, WINDOW)
- if start == 1:
- expected += '(this is the beginning of the file)\n'
- else:
- expected += f'({start - 1} more lines above)\n'
- for i in range(start, end + 1):
- expected += f'{i}|Line {i}\n'
- if end == total_lines:
- expected += '(this is the end of the file)\n'
- else:
- expected += f'({total_lines - end} more lines below)\n'
- expected += '[Use `scroll_down` to view the next 100 lines of the file!]\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
- cur_line = cur_line - WINDOW
- expected = f'[File: {temp_file_path} ({total_lines} lines total)]\n'
- start = cur_line
- end = cur_line + WINDOW
- if start == 1:
- expected += '(this is the beginning of the file)\n'
- else:
- expected += f'({start - 1} more lines above)\n'
- for i in range(start, end + 1):
- expected += f'{i}|Line {i}\n'
- if end == total_lines:
- expected += '(this is the end of the file)\n'
- else:
- expected += f'({total_lines - end} 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'
- expected += '(this is the beginning of the file)\n'
- for i in range(1, 10):
- expected += f'{i}|Line {i}\n'
- expected += '(this is the end of the file)\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_open_file_large_line_number(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, 1000):
- file.write(f'Line `{i}`\n')
- # Define the parameters for the test
- current_line = 800
- window = 100
- # 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)
- open_file(str(test_file_path), current_line, window)
- result = buf.getvalue()
- expected = f'[File: {test_file_path} (999 lines total)]\n'
- expected += '(749 more lines above)\n'
- for i in range(750, 850 + 1):
- expected += f'{i}|Line `{i}`\n'
- expected += '(149 more lines below)\n'
- expected += '[Use `scroll_down` to view the next 100 lines of the file!]\n'
- assert result == expected
- def test_edit_file_by_replace_window(tmp_path):
- with patch.dict(os.environ, {'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_by_replace(
- str(temp_file_path),
- to_replace=' assert any_int(1.0, 2, 3) == False',
- new_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'
- + SEP
- + '(this is the beginning of the file)\n'
- '1|def any_int(a, b, c):\n'
- '2| return isinstance(a, int) and isinstance(b, int) and isinstance(c, int)\n'
- '3|\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'
- '15| assert any_int(1.5, -2, 3) == False\n'
- '16| assert any_int(1, -2.5, 3) == False\n'
- '17|\n'
- '18|def check(any_int):\n'
- '19| # Check some simple cases\n'
- '20| assert any_int(2, 3, 1)==True, "This prints if this assert fails 1 (good for debugging!)"\n'
- '21| assert any_int(2.5, 2, 3)==False, "This prints if this assert fails 2 (good for debugging!)"\n'
- '(12 more lines below)\n' + SEP + '\n'
- '[This is the original code before your edit]\n'
- + SEP
- + '(this is the beginning of the file)\n'
- '1|def any_int(a, b, c):\n'
- '2| return isinstance(a, int) and isinstance(b, int) and isinstance(c, int)\n'
- '3|\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'
- '15| assert any_int(1.5, -2, 3) == False\n'
- '16| assert any_int(1, -2.5, 3) == False\n'
- '17|\n'
- '18|def check(any_int):\n'
- '19| # Check some simple cases\n'
- '20| assert any_int(2, 3, 1)==True, "This prints if this assert fails 1 (good for debugging!)"\n'
- '21| assert any_int(2.5, 2, 3)==False, "This prints if this assert fails 2 (good for debugging!)"\n'
- '(12 more lines below)\n'
- + SEP
- + '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_edit_file_by_replace_with_multiple_errors(tmp_path):
- # If the file has multiple errors, but the suggested modification can only fix one error, make sure it is applied.
- with patch.dict(os.environ, {'ENABLE_AUTO_LINT': 'True'}):
- content = """def Sum(a,b):
- try:
- answer = a + b
- return answer
- except Exception:
- answer = ANOTHER_CONSTANT
- return answer
- Sum(1,1)
- """
- temp_file_path = tmp_path / 'problematic-file-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_by_replace(
- str(temp_file_path),
- to_replace=' answer = a + b',
- new_content=' answer = a+b',
- )
- result = buf.getvalue()
- expected = (
- f'[File: {temp_file_path} (8 lines total after edit)]\n'
- '(this is the beginning of the file)\n'
- '1|def Sum(a,b):\n'
- '2| try:\n'
- '3| answer = a+b\n'
- '4| return answer\n'
- '5| except Exception:\n'
- '6| answer = ANOTHER_CONSTANT\n'
- '7| return answer\n'
- '8|Sum(1,1)\n'
- '(this is the end of the file)\n'
- + MSG_FILE_UPDATED.format(line_number=3)
- + '\n'
- )
- assert result.split('\n') == expected.split('\n')
- # ================================
- def test_edit_file_by_replace(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_by_replace(
- file_name=str(temp_file_path),
- to_replace='Line 1\nLine 2\nLine 3',
- new_content='REPLACE TEXT',
- )
- result = buf.getvalue()
- expected = (
- f'[File: {temp_file_path} (3 lines total after edit)]\n'
- '(this is the beginning of the file)\n'
- '1|REPLACE TEXT\n'
- '2|Line 4\n'
- '3|Line 5\n'
- '(this is the end of the file)\n'
- + MSG_FILE_UPDATED.format(line_number=1)
- + '\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_by_replace_sameline(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- content = 'Line 1\nLine 2\nLine 2\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_by_replace(
- file_name=str(temp_file_path),
- to_replace='Line 2\nLine 2',
- new_content='Line 2\nREPLACE TEXT',
- )
- result = buf.getvalue()
- expected = (
- f'[File: {temp_file_path} (5 lines total after edit)]\n'
- '(this is the beginning of the file)\n'
- '1|Line 1\n'
- '2|Line 2\n'
- '3|REPLACE TEXT\n'
- '4|Line 4\n'
- '5|Line 5\n'
- '(this is the end of the file)\n'
- + MSG_FILE_UPDATED.format(line_number=2)
- + '\n'
- )
- assert result.split('\n') == expected.split('\n')
- with open(temp_file_path, 'r') as file:
- lines = file.readlines()
- assert len(lines) == 5
- assert lines[0].rstrip() == 'Line 1'
- assert lines[1].rstrip() == 'Line 2'
- assert lines[2].rstrip() == 'REPLACE TEXT'
- assert lines[3].rstrip() == 'Line 4'
- assert lines[4].rstrip() == 'Line 5'
- def test_edit_file_by_replace_multiline(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- content = 'Line 1\nLine 2\nLine 2\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_by_replace(
- file_name=str(temp_file_path),
- to_replace='Line 2',
- new_content='REPLACE TEXT',
- )
- result = buf.getvalue()
- assert result.strip().startswith(
- 'ERROR: `to_replace` appears more than once, please include enough lines to make code in `to_replace` unique'
- )
- def test_edit_file_by_replace_no_diff(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- content = 'Line 1\nLine 2\nLine 2\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_by_replace(
- file_name=str(temp_file_path),
- to_replace='Line 1',
- new_content='Line 1',
- )
- result = buf.getvalue()
- assert result.strip().startswith(
- 'ERROR: `to_replace` and `new_content` must be different'
- )
- def test_edit_file_by_replace_toreplace_empty(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- content = 'Line 1\nLine 2\nLine 2\nLine 4\nLine 5'
- temp_file_path.write_text(content)
- open_file(str(temp_file_path))
- _capture_file_operation_error(
- lambda: edit_file_by_replace(
- file_name=str(temp_file_path),
- to_replace='',
- new_content='Line 1',
- ),
- 'ERROR: `to_replace` must not be empty.',
- )
- def test_edit_file_by_replace_unknown_file():
- _capture_file_operation_error(
- lambda: edit_file_by_replace(
- str('unknown file'),
- 'ORIGINAL TEXT',
- 'REPLACE TEXT',
- ),
- 'ERROR: File unknown file not found.',
- )
- def test_insert_content_at_line(tmp_path):
- temp_file_path = tmp_path / 'b.txt'
- content = 'Line 1\nLine 2\nLine 3'
- temp_file_path.write_text(content)
- open_file(str(temp_file_path))
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- insert_content_at_line(
- file_name=str(temp_file_path),
- line_number=2,
- content='Inserted Line',
- )
- result = buf.getvalue()
- expected = (
- f'[File: {temp_file_path} (4 lines total after edit)]\n'
- '(this is the beginning of the file)\n'
- '1|Line 1\n'
- '2|Inserted Line\n'
- '3|Line 2\n'
- '4|Line 3\n'
- '(this is the end of the file)\n'
- + MSG_FILE_UPDATED.format(line_number=2)
- + '\n'
- )
- assert result.split('\n') == expected.split('\n')
- with open(temp_file_path, 'r') as file:
- lines = file.readlines()
- assert len(lines) == 4
- assert lines[0].rstrip() == 'Line 1'
- assert lines[1].rstrip() == 'Inserted Line'
- assert lines[2].rstrip() == 'Line 2'
- assert lines[3].rstrip() == 'Line 3'
- def test_insert_content_at_line_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):
- insert_content_at_line(
- file_name=str(temp_file_path),
- line_number=1,
- content='REPLACE TEXT',
- )
- result = buf.getvalue()
- expected = (
- f'[File: {temp_file_path} (1 lines total after edit)]\n'
- '(this is the beginning of the file)\n'
- '1|REPLACE TEXT\n'
- '(this is the end of the file)\n'
- + MSG_FILE_UPDATED.format(line_number=1)
- + '\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_insert_content_at_line_from_scratch_emptyfile(tmp_path):
- temp_file_path = tmp_path / 'a.txt'
- with open(temp_file_path, 'w') as file:
- file.write('')
- open_file(str(temp_file_path))
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- insert_content_at_line(
- file_name=str(temp_file_path),
- line_number=1,
- content='REPLACE TEXT',
- )
- result = buf.getvalue()
- expected = (
- f'[File: {temp_file_path} (1 lines total after edit)]\n'
- '(this is the beginning of the file)\n'
- '1|REPLACE TEXT\n'
- '(this is the end of the file)\n'
- + MSG_FILE_UPDATED.format(line_number=1)
- + '\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_insert_content_at_line_emptyline(tmp_path):
- temp_file_path = tmp_path / 'b.txt'
- content = 'Line 1\n\n'
- temp_file_path.write_text(content)
- open_file(str(temp_file_path))
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- insert_content_at_line(
- file_name=str(temp_file_path),
- line_number=2,
- content='Inserted Line',
- )
- result = buf.getvalue()
- expected = (
- f'[File: {temp_file_path} (3 lines total after edit)]\n'
- '(this is the beginning of the file)\n'
- '1|Line 1\n'
- '2|Inserted Line\n'
- '3|\n'
- '(this is the end of the file)\n'
- + MSG_FILE_UPDATED.format(line_number=2)
- + '\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() == 'Inserted Line'
- def test_insert_content_at_line_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):
- insert_content_at_line(
- str(temp_file_path),
- 1,
- '`REPLACE TEXT1`\n`REPLACE TEXT2`\n`REPLACE TEXT3`',
- )
- result = buf.getvalue()
- expected = (
- f'[File: {temp_file_path} (3 lines total after edit)]\n'
- '(this is the beginning of the file)\n'
- '1|`REPLACE TEXT1`\n'
- '2|`REPLACE TEXT2`\n'
- '3|`REPLACE TEXT3`\n'
- '(this is the end of the file)\n'
- + MSG_FILE_UPDATED.format(line_number=1)
- + '\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_by_replace call
- assert '\\`' not in result
- # Perform a second edit
- with io.StringIO() as buf:
- with contextlib.redirect_stdout(buf):
- edit_file_by_replace(
- str(temp_file_path),
- '`REPLACE TEXT1`\n`REPLACE TEXT2`\n`REPLACE TEXT3`',
- '`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'
- '(this is the beginning of the file)\n'
- '1|`REPLACED TEXT1`\n'
- '2|`REPLACED TEXT2`\n'
- '3|`REPLACED TEXT3`\n'
- '(this is the end of the file)\n'
- + MSG_FILE_UPDATED.format(line_number=1)
- + '\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_by_replace call
- assert '\\`' not in second_result
- def test_insert_content_at_line_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):
- insert_content_at_line(
- str(temp_file_path),
- 1,
- content='REPLACE TEXT1\nREPLACE TEXT2\nREPLACE TEXT3',
- )
- result = buf.getvalue()
- expected = (
- f'[File: {temp_file_path} (3 lines total after edit)]\n'
- '(this is the beginning of the file)\n'
- '1|REPLACE TEXT1\n'
- '2|REPLACE TEXT2\n'
- '3|REPLACE TEXT3\n'
- '(this is the end of the file)\n'
- + MSG_FILE_UPDATED.format(line_number=1)
- + '\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_insert_content_at_line_not_opened():
- _capture_file_operation_error(
- lambda: insert_content_at_line(
- str('unknown file'),
- 1,
- 'REPLACE TEXT',
- ),
- 'ERROR: Invalid path or file name.',
- )
- 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'
- '(this is the beginning of the file)\n'
- '1|Line 1\n'
- '2|Line 2\n'
- '3|APPENDED TEXT\n'
- '(this is the end of the file)\n'
- + MSG_FILE_UPDATED.format(line_number=2)
- + '\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'
- '(this is the beginning of the file)\n'
- '1|APPENDED TEXT\n'
- '(this is the end of the file)\n'
- + MSG_FILE_UPDATED.format(line_number=1)
- + '\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'
- '(this is the beginning of the file)\n'
- '1|APPENDED TEXT1\n'
- '2|APPENDED TEXT2\n'
- '3|APPENDED TEXT3\n'
- '(this is the end of the file)\n'
- + MSG_FILE_UPDATED.format(line_number=1)
- + '\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():
- _capture_file_operation_error(
- lambda: append_file('unknown file', content='APPENDED TEXT'),
- 'ERROR: Invalid path or file name.',
- )
- 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():
- _capture_file_operation_error(
- lambda: search_file('Line 6', '/unexist/path/a.txt'),
- 'ERROR: File /unexist/path/a.txt not found.',
- )
- 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('nonexist.txt')
- result = buf.getvalue()
- assert result is not None
- expected = '[No matches found for "nonexist.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('nonexist.txt', str(tmp_path))
- result = buf.getvalue()
- assert result is not None
- expected = f'[No matches found for "nonexist.txt" in {tmp_path}]\n'
- assert result.split('\n') == expected.split('\n')
- def test_edit_lint_file_pass(tmp_path):
- # Enable linting
- with patch.dict(os.environ, {'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))
- insert_content_at_line(str(file_path), 1, "print('hello')\n")
- result = buf.getvalue()
- assert result is not None
- expected = (
- f'[File: {file_path} (1 lines total)]\n'
- '(this is the beginning of the file)\n'
- '1|\n'
- '(this is the end of the file)\n'
- f'[File: {file_path} (1 lines total after edit)]\n'
- '(this is the beginning of the file)\n'
- "1|print('hello')\n"
- '(this is the end of the file)\n'
- + MSG_FILE_UPDATED.format(line_number=1)
- + '\n'
- )
- assert result.split('\n') == expected.split('\n')
- def test_lint_file_fail_undefined_name(tmp_path, capsys):
- with patch.dict(os.environ, {'ENABLE_AUTO_LINT': 'True'}):
- current_line = 1
- file_path = _generate_test_file_with_lines(tmp_path, 1)
- open_file(str(file_path), current_line)
- insert_content_at_line(str(file_path), 1, 'undefined_name()\n')
- result = capsys.readouterr().out
- assert result is not None
- expected = (
- f'[File: {file_path} (1 lines total)]\n'
- '(this is the beginning of the file)\n'
- '1|\n'
- '(this is the end of the file)\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'
- + SEP
- + '(this is the beginning of the file)\n'
- '1|undefined_name()\n'
- '(this is the end of the file)\n'
- + SEP
- + '\n[This is the original code before your edit]\n'
- + SEP
- + '(this is the beginning of the file)\n'
- '1|\n'
- '(this is the end of the file)\n'
- + SEP
- + '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, capsys):
- with patch.dict(os.environ, {'ENABLE_AUTO_LINT': 'True'}):
- num_lines = 1000
- error_line = 500
- 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))
- insert_content_at_line(str(file_path), 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'
- '(this is the beginning of the file)\n'
- f'{open_lines}\n'
- f'({num_lines - WINDOW} more lines below)\n'
- f'[Use `scroll_down` to view the next 100 lines of the file!]\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'
- + SEP
- + '(489 more lines above)\n'
- + _numbered_test_lines(error_line - 10, error_line - 1)
- + '500|undefined_name()\n'
- + _numbered_test_lines(error_line + 1, error_line + 10)
- + '(491 more lines below)\n'
- + SEP
- + '\n[This is the original code before your edit]\n'
- + SEP
- + '(489 more lines above)\n'
- + _numbered_test_lines(error_line - 10, error_line + 10)
- + '(490 more lines below)\n'
- + SEP
- + '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, capsys):
- with patch.dict(os.environ, {'ENABLE_AUTO_LINT': 'False'}):
- file_path = _generate_test_file_with_lines(tmp_path, 1)
- open_file(str(file_path))
- insert_content_at_line(str(file_path), 1, 'undefined_name()\n')
- result = capsys.readouterr().out
- assert result is not None
- expected = (
- f'[File: {file_path} (1 lines total)]\n'
- '(this is the beginning of the file)\n'
- '1|\n'
- '(this is the end of the file)\n'
- f'[File: {file_path} (1 lines total after edit)]\n'
- '(this is the beginning of the file)\n'
- '1|undefined_name()\n'
- '(this is the end of the file)\n'
- + MSG_FILE_UPDATED.format(line_number=1)
- + '\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}'
- def test_lint_file_fail_non_python(tmp_path, capsys):
- with patch.dict(os.environ, {'ENABLE_AUTO_LINT': 'True'}):
- current_line = 1
- file_path = _generate_ruby_test_file_with_lines(tmp_path, 1)
- open_file(str(file_path), current_line)
- insert_content_at_line(
- str(file_path), 1, "def print_hello_world()\n puts 'Hello World'"
- )
- result = capsys.readouterr().out
- assert result is not None
- expected = (
- f'[File: {file_path} (1 lines total)]\n'
- '(this is the beginning of the file)\n'
- '1|\n'
- '(this is the end of the file)\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: Syntax error\n'
- '[This is how your edit would have looked if applied]\n'
- + SEP
- + '(this is the beginning of the file)\n'
- '1|def print_hello_world()\n'
- "2| puts 'Hello World'\n"
- '(this is the end of the file)\n'
- '-------------------------------------------------\n\n'
- '[This is the original code before your edit]\n'
- + SEP
- + '(this is the beginning of the file)\n'
- '1|\n'
- '(this is the end of the file)\n'
- + SEP
- + '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_typescript(tmp_path, capsys):
- linter = Linter()
- if not linter.ts_installed:
- return
- with patch.dict(os.environ, {'ENABLE_AUTO_LINT': 'True'}):
- current_line = 1
- file_path = tmp_path / 'test.ts'
- file_path.write_text('')
- open_file(str(file_path), current_line)
- insert_content_at_line(
- str(file_path),
- 1,
- "function greet(name: string) {\n console.log('Hello, ' + name)",
- )
- result = capsys.readouterr().out
- assert result is not None
- # Note: the tsc (typescript compiler) message is different from a
- # compared to a python linter message, like line and column in brackets:
- expected_lines = [
- f'[File: {file_path} (1 lines total)]',
- '(this is the beginning of the file)',
- '1|',
- '(this is the end of the file)',
- '[Your proposed edit has introduced new syntax error(s). Please understand the errors and retry your edit command.]',
- 'ERRORS:',
- f"{file_path}(3,1): error TS1005: '}}' expected.",
- '[This is how your edit would have looked if applied]',
- '-------------------------------------------------',
- '(this is the beginning of the file)',
- '1|function greet(name: string) {',
- "2| console.log('Hello, ' + name)",
- '(this is the end of the file)',
- '-------------------------------------------------',
- '',
- '[This is the original code before your edit]',
- '-------------------------------------------------',
- '(this is the beginning of the file)',
- '1|',
- '(this is the end of the file)',
- '-------------------------------------------------',
- 'Your changes have NOT been applied. Please fix your edit command and try again.',
- 'You either need to 1) Specify the correct start/end line arguments or 2) Correct your edit code.',
- 'DO NOT re-run the same failed edit command. Running it again will lead to the same error.',
- '',
- ]
- result_lines = result.split('\n')
- assert len(result_lines) == len(expected_lines), "Number of lines doesn't match"
- for i, (result_line, expected_line) in enumerate(
- zip(result_lines, expected_lines)
- ):
- assert result_line.lstrip('./') == expected_line.lstrip(
- './'
- ), f"Line {i+1} doesn't match"
|