@@ -2175,5 +2175,80 @@ def test_grep_win_git_path_unaffected(self):
21752175 self .assertIn ("hello" , out )
21762176
21772177
2178+ class TestTildeExpansion (unittest .TestCase ):
2179+ """All filesystem tools must expand ~ in user-provided paths."""
2180+
2181+ def setUp (self ):
2182+ self .tmp = tempfile .TemporaryDirectory ()
2183+ self .home = os .path .join (self .tmp .name , "home" )
2184+ os .makedirs (self .home )
2185+ self .orig_home = os .environ .get ("HOME" )
2186+ os .environ ["HOME" ] = self .home
2187+
2188+ def tearDown (self ):
2189+ self .tmp .cleanup ()
2190+ if self .orig_home is not None :
2191+ os .environ ["HOME" ] = self .orig_home
2192+ else :
2193+ os .environ .pop ("HOME" , None )
2194+
2195+ def test_read_expands_tilde (self ):
2196+ p = os .path .join (self .home , "file.txt" )
2197+ with open (p , "w" ) as f :
2198+ f .write ("content\n " )
2199+ out = Read ().run ({"file_path" : "~/file.txt" }, ToolContext ())
2200+ self .assertIn ("content" , out )
2201+
2202+ def test_write_expands_tilde (self ):
2203+ out = Write ().run (
2204+ {"path" : "~/sub" , "filename" : "out.txt" , "content" : "hi\n " },
2205+ ToolContext (),
2206+ )
2207+ self .assertIn ("Created" , out )
2208+ with open (os .path .join (self .home , "sub" , "out.txt" )) as f :
2209+ self .assertEqual (f .read (), "hi\n " )
2210+
2211+ def test_edit_expands_tilde (self ):
2212+ p = os .path .join (self .home , "edit.txt" )
2213+ with open (p , "w" ) as f :
2214+ f .write ("old\n " )
2215+ ctx , _ = make_ctx ()
2216+ out = edit_tool ().run ({"path" : "~/edit.txt" , "old_str" : "old" , "new_str" : "new" }, ctx )
2217+ self .assertIn ("Successfully" , out )
2218+ with open (p ) as f :
2219+ self .assertEqual (f .read (), "new\n " )
2220+
2221+ def test_insert_expands_tilde (self ):
2222+ p = os .path .join (self .home , "insert.txt" )
2223+ with open (p , "w" ) as f :
2224+ f .write ("line1\n line2\n " )
2225+ out = Insert ().run (
2226+ {"path" : "~/insert.txt" , "line_number" : 1 , "new_str" : "inserted\n " },
2227+ ToolContext (),
2228+ )
2229+ self .assertIn ("inserted text" , out )
2230+ with open (p ) as f :
2231+ self .assertEqual (f .read (), "line1\n inserted\n line2\n " )
2232+
2233+ def test_mkdir_expands_tilde (self ):
2234+ out = Mkdir ().run ({"parent" : "~" , "name" : "newdir" }, ToolContext ())
2235+ self .assertIn ("created/verified" , out )
2236+ self .assertTrue (os .path .isdir (os .path .join (self .home , "newdir" )))
2237+
2238+ def test_glob_expands_tilde (self ):
2239+ os .makedirs (os .path .join (self .home , "proj" ))
2240+ with open (os .path .join (self .home , "proj" , "a.py" ), "w" ) as f :
2241+ f .write ("x\n " )
2242+ out = GlobTool ().run ({"pattern" : "*.py" , "path" : "~/proj" }, ToolContext ())
2243+ self .assertIn ("a.py" , out )
2244+
2245+ def test_grep_expands_tilde (self ):
2246+ os .makedirs (os .path .join (self .home , "proj" ))
2247+ with open (os .path .join (self .home , "proj" , "a.py" ), "w" ) as f :
2248+ f .write ("needle\n " )
2249+ out = Grep ().run ({"regex" : "needle" , "path" : "~/proj" }, ToolContext ())
2250+ self .assertIn ("needle" , out )
2251+
2252+
21782253if __name__ == "__main__" :
21792254 unittest .main ()
0 commit comments