mirror of
https://github.com/NCBM/plyngent.git
synced 2026-11-07 15:37:55 +08:00
core/tools: restrict edit_lineno to lines read with line numbers
This commit is contained in:
@@ -22,6 +22,36 @@ def test_edit_lineno_replace_middle(workspace: object) -> None:
|
||||
assert call_sync(read_file, "a.txt") == "one\nTWO\nTHREE\nfour\n"
|
||||
|
||||
|
||||
def test_edit_lineno_requires_all_lines_read(workspace: object) -> None:
|
||||
del workspace
|
||||
_ = call_sync(write_file, "r.txt", "a\nb\nc\nd\ne\n")
|
||||
_ = reset_lineno_tracker()
|
||||
# Only lines 2-4 are read; editing line 1 or 5 must be rejected.
|
||||
_ = call_sync(read_file, "r.txt", offset=1, limit=3, with_lineno=True)
|
||||
out = call_sync(edit_lineno, "r.txt", 1, 2, "X\n")
|
||||
assert "not read" in out and "1" in out
|
||||
out = call_sync(edit_lineno, "r.txt", 4, 5, "Y\n")
|
||||
assert "not read" in out and "5" in out
|
||||
# Fully read range still edits.
|
||||
out = call_sync(edit_lineno, "r.txt", 2, 3, "B\nC\n")
|
||||
assert "replaced lines 2-3" in out
|
||||
|
||||
|
||||
def test_edit_lineno_append_requires_last_line_read(workspace: object) -> None:
|
||||
del workspace
|
||||
_ = call_sync(write_file, "ap.txt", "a\nb\nc\n")
|
||||
_ = reset_lineno_tracker()
|
||||
# Only lines 1-2 read; appending after line 3 requires line 3 read.
|
||||
_ = call_sync(read_file, "ap.txt", offset=0, limit=2, with_lineno=True)
|
||||
out = call_sync(edit_lineno, "ap.txt", 4, 4, "d\n")
|
||||
assert "not read" in out and "3" in out
|
||||
# Read the last line, then append succeeds.
|
||||
_ = call_sync(read_file, "ap.txt", offset=2, limit=1, with_lineno=True)
|
||||
out = call_sync(edit_lineno, "ap.txt", 4, 4, "d\n")
|
||||
assert "appended" in out
|
||||
assert call_sync(read_file, "ap.txt") == "a\nb\nc\nd\n"
|
||||
|
||||
|
||||
def test_edit_lineno_delete_range(workspace: object) -> None:
|
||||
del workspace
|
||||
_ = call_sync(write_file, "b.txt", "a\nb\nc\n")
|
||||
|
||||
Reference in New Issue
Block a user