11"""Tests for the tool system."""
22
33import os
4+ import sys
45import tempfile
56from pathlib import Path
67
@@ -38,7 +39,7 @@ def test_bash_exit_code():
3839
3940def test_bash_timeout ():
4041 bash = get_tool ("bash" )
41- r = bash .execute (command = "sleep 10" , timeout = 1 )
42+ r = bash .execute (command = f'" { sys . executable } " -c "import time; time.sleep(10)"' , timeout = 1 )
4243 assert "timed out" in r
4344
4445
@@ -62,21 +63,19 @@ def test_bash_blocks_curl_pipe():
6263
6364def test_bash_truncates_long_output ():
6465 bash = get_tool ("bash" )
65- r = bash .execute (command = "python3 -c \ " print('x ' * 20000)\" " )
66+ r = bash .execute (command = f'" { sys . executable } " -c "print(\' x \ ' * 20000)"' )
6667 assert "truncated" in r
6768
6869
6970# --- read_file ---
7071
71- def test_read_file ():
72+ def test_read_file (tmp_path ):
7273 read = get_tool ("read_file" )
73- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".txt" , delete = False ) as f :
74- f .write ("line1\n line2\n line3\n " )
75- f .flush ()
76- r = read .execute (file_path = f .name )
77- assert "line1" in r
78- assert "line2" in r
79- os .unlink (f .name )
74+ path = tmp_path / "sample.txt"
75+ path .write_text ("line1\n line2\n line3\n " )
76+ r = read .execute (file_path = str (path ))
77+ assert "line1" in r
78+ assert "line2" in r
8079
8180
8281def test_read_file_not_found ():
@@ -85,14 +84,12 @@ def test_read_file_not_found():
8584 assert "not found" in r .lower () or "Error" in r
8685
8786
88- def test_read_file_offset_limit ():
87+ def test_read_file_offset_limit (tmp_path ):
8988 read = get_tool ("read_file" )
90- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".txt" , delete = False ) as f :
91- f .write ("\n " .join (f"line{ i } " for i in range (100 )))
92- f .flush ()
93- r = read .execute (file_path = f .name , offset = 10 , limit = 5 )
94- assert "line10" not in r or "line9" in r # offset is 1-based
95- os .unlink (f .name )
89+ path = tmp_path / "sample.txt"
90+ path .write_text ("\n " .join (f"line{ i } " for i in range (100 )))
91+ r = read .execute (file_path = str (path ), offset = 10 , limit = 5 )
92+ assert "line10" not in r or "line9" in r # offset is 1-based
9693
9794
9895# --- write_file ---
@@ -119,38 +116,32 @@ def test_write_file_creates_dirs():
119116
120117# --- edit_file ---
121118
122- def test_edit_file_basic ():
119+ def test_edit_file_basic (tmp_path ):
123120 edit = get_tool ("edit_file" )
124- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".py" , delete = False ) as f :
125- f .write ("def foo():\n return 42\n " )
126- f .flush ()
127- r = edit .execute (file_path = f .name , old_string = "return 42" , new_string = "return 99" )
128- assert "Edited" in r
129- assert "---" in r # unified diff
130- content = Path (f .name ).read_text ()
131- assert "return 99" in content
132- assert "return 42" not in content
133- os .unlink (f .name )
134-
135-
136- def test_edit_file_not_found_string ():
121+ path = tmp_path / "sample.py"
122+ path .write_text ("def foo():\n return 42\n " )
123+ r = edit .execute (file_path = str (path ), old_string = "return 42" , new_string = "return 99" )
124+ assert "Edited" in r
125+ assert "---" in r # unified diff
126+ content = path .read_text ()
127+ assert "return 99" in content
128+ assert "return 42" not in content
129+
130+
131+ def test_edit_file_not_found_string (tmp_path ):
137132 edit = get_tool ("edit_file" )
138- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".py" , delete = False ) as f :
139- f .write ("hello\n " )
140- f .flush ()
141- r = edit .execute (file_path = f .name , old_string = "NONEXISTENT" , new_string = "x" )
142- assert "not found" in r .lower ()
143- os .unlink (f .name )
133+ path = tmp_path / "sample.py"
134+ path .write_text ("hello\n " )
135+ r = edit .execute (file_path = str (path ), old_string = "NONEXISTENT" , new_string = "x" )
136+ assert "not found" in r .lower ()
144137
145138
146- def test_edit_file_duplicate_string ():
139+ def test_edit_file_duplicate_string (tmp_path ):
147140 edit = get_tool ("edit_file" )
148- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".py" , delete = False ) as f :
149- f .write ("dup\n dup\n " )
150- f .flush ()
151- r = edit .execute (file_path = f .name , old_string = "dup" , new_string = "x" )
152- assert "2 times" in r
153- os .unlink (f .name )
141+ path = tmp_path / "sample.py"
142+ path .write_text ("dup\n dup\n " )
143+ r = edit .execute (file_path = str (path ), old_string = "dup" , new_string = "x" )
144+ assert "2 times" in r
154145
155146
156147# --- glob ---
0 commit comments