Skip to content

Commit 121930d

Browse files
committed
update to use TestBase instead
1 parent 18cddbc commit 121930d

File tree

8 files changed

+134
-130
lines changed

8 files changed

+134
-130
lines changed

crates/agent/src/agent/mod.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,15 +2171,11 @@ pub enum HookStage {
21712171
#[cfg(test)]
21722172
mod tests {
21732173
use super::*;
2174-
use crate::util::test::{
2175-
TestDir,
2176-
TestProvider,
2177-
};
2174+
use crate::util::test::TestBase;
21782175

21792176
#[tokio::test]
21802177
async fn test_collect_resources() {
2181-
let mut test_dir = TestDir::new();
2182-
let test_provider = TestProvider::new_with_base(test_dir.path());
2178+
let mut test_base = TestBase::new().await;
21832179

21842180
let files = [
21852181
(".amazonq/rules/first.md", "first"),
@@ -2188,10 +2184,10 @@ mod tests {
21882184
];
21892185

21902186
for file in files {
2191-
test_dir = test_dir.with_file_sys(file, &test_provider).await;
2187+
test_base = test_base.with_file(file).await;
21922188
}
21932189

2194-
let resources = collect_resources(["file://.amazonq/rules/**/*.md", "file://~/home.txt"], &test_provider).await;
2190+
let resources = collect_resources(["file://.amazonq/rules/**/*.md", "file://~/home.txt"], &test_base).await;
21952191

21962192
for file in files {
21972193
assert!(resources.iter().any(|r| r.content == file.1));

crates/agent/src/agent/tools/fs_read.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,7 @@ pub struct FileReadContext {}
196196
#[cfg(test)]
197197
mod tests {
198198
use super::*;
199-
use crate::agent::util::test::TestDir;
200-
use crate::util::test::{
201-
TestBase,
202-
TestProvider,
203-
};
199+
use crate::util::test::TestBase;
204200

205201
#[tokio::test]
206202
async fn test_fs_read_single_file() {
@@ -227,56 +223,56 @@ mod tests {
227223

228224
#[tokio::test]
229225
async fn test_fs_read_with_offset_and_limit() {
230-
let test_provider = TestProvider::new();
231-
let test_dir = TestDir::new()
232-
.with_file_sys(("test.txt", "line1\nline2\nline3\nline4\nline5"), &test_provider)
226+
let test_base = TestBase::new()
227+
.await
228+
.with_file(("test.txt", "line1\nline2\nline3\nline4\nline5"))
233229
.await;
234230

235231
let tool = FsRead {
236232
ops: vec![FsReadOp {
237-
path: test_dir.join("test.txt").to_string_lossy().to_string(),
233+
path: test_base.join("test.txt").to_string_lossy().to_string(),
238234
limit: Some(2),
239235
offset: Some(1),
240236
}],
241237
};
242238

243-
let result = tool.execute(&test_provider).await.unwrap();
239+
let result = tool.execute(&test_base).await.unwrap();
244240
if let ToolExecutionOutputItem::Text(content) = &result.items[0] {
245241
assert_eq!(content, "line2\nline3");
246242
}
247243
}
248244

249245
#[tokio::test]
250246
async fn test_fs_read_multiple_files() {
251-
let test_provider = TestProvider::new();
252-
let test_dir = TestDir::new()
253-
.with_file_sys(("file1.txt", "content1"), &test_provider)
247+
let test_base = TestBase::new()
248+
.await
249+
.with_file(("file1.txt", "content1"))
254250
.await
255-
.with_file_sys(("file2.txt", "content2"), &test_provider)
251+
.with_file(("file2.txt", "content2"))
256252
.await;
257253

258254
let tool = FsRead {
259255
ops: vec![
260256
FsReadOp {
261-
path: test_dir.join("file1.txt").to_string_lossy().to_string(),
257+
path: test_base.join("file1.txt").to_string_lossy().to_string(),
262258
limit: None,
263259
offset: None,
264260
},
265261
FsReadOp {
266-
path: test_dir.join("file2.txt").to_string_lossy().to_string(),
262+
path: test_base.join("file2.txt").to_string_lossy().to_string(),
267263
limit: None,
268264
offset: None,
269265
},
270266
],
271267
};
272268

273-
let result = tool.execute(&test_provider).await.unwrap();
269+
let result = tool.execute(&test_base).await.unwrap();
274270
assert_eq!(result.items.len(), 2);
275271
}
276272

277273
#[tokio::test]
278274
async fn test_fs_read_validate_nonexistent_file() {
279-
let test_provider = TestProvider::new();
275+
let test_base = TestBase::new().await;
280276
let tool = FsRead {
281277
ops: vec![FsReadOp {
282278
path: "/nonexistent/file.txt".to_string(),
@@ -285,22 +281,21 @@ mod tests {
285281
}],
286282
};
287283

288-
assert!(tool.validate(&test_provider).await.is_err());
284+
assert!(tool.validate(&test_base).await.is_err());
289285
}
290286

291287
#[tokio::test]
292288
async fn test_fs_read_validate_directory_path() {
293-
let test_provider = TestProvider::new();
294-
let test_dir = TestDir::new();
289+
let test_base = TestBase::new().await;
295290

296291
let tool = FsRead {
297292
ops: vec![FsReadOp {
298-
path: test_dir.join("").to_string_lossy().to_string(),
293+
path: test_base.join("").to_string_lossy().to_string(),
299294
limit: None,
300295
offset: None,
301296
}],
302297
};
303298

304-
assert!(tool.validate(&test_provider).await.is_err());
299+
assert!(tool.validate(&test_base).await.is_err());
305300
}
306301
}

crates/agent/src/agent/tools/fs_write.rs

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -354,148 +354,155 @@ impl FileLineTracker {
354354
#[cfg(test)]
355355
mod tests {
356356
use super::*;
357-
use crate::agent::util::test::TestDir;
358-
use crate::util::test::TestProvider;
357+
use crate::util::test::TestBase;
359358

360359
#[tokio::test]
361360
async fn test_create_file() {
362-
let test_provider = TestProvider::new();
363-
let test_dir = TestDir::new();
361+
let test_base = TestBase::new().await;
364362
let tool = FsWrite::Create(FileCreate {
365-
path: test_dir.join("new.txt").to_string_lossy().to_string(),
363+
path: test_base.join("new.txt").to_string_lossy().to_string(),
366364
content: "hello world".to_string(),
367365
});
368366

369-
assert!(tool.validate(&test_provider).await.is_ok());
370-
assert!(tool.execute(None, &test_provider).await.is_ok());
367+
assert!(tool.validate(&test_base).await.is_ok());
368+
assert!(tool.execute(None, &test_base).await.is_ok());
371369

372-
let content = tokio::fs::read_to_string(test_dir.join("new.txt")).await.unwrap();
370+
let content = tokio::fs::read_to_string(test_base.join("new.txt")).await.unwrap();
373371
assert_eq!(content, "hello world");
374372
}
375373

376374
#[tokio::test]
377375
async fn test_create_file_with_parent_dirs() {
378-
let test_provider = TestProvider::new();
379-
let test_dir = TestDir::new();
376+
let test_base = TestBase::new().await;
380377
let tool = FsWrite::Create(FileCreate {
381-
path: test_dir.join("nested/dir/file.txt").to_string_lossy().to_string(),
378+
path: test_base.join("nested/dir/file.txt").to_string_lossy().to_string(),
382379
content: "nested content".to_string(),
383380
});
384381

385-
assert!(tool.execute(None, &test_provider).await.is_ok());
382+
assert!(tool.execute(None, &test_base).await.is_ok());
386383

387-
let content = tokio::fs::read_to_string(test_dir.join("nested/dir/file.txt"))
384+
let content = tokio::fs::read_to_string(test_base.join("nested/dir/file.txt"))
388385
.await
389386
.unwrap();
390387
assert_eq!(content, "nested content");
391388
}
392389

393390
#[tokio::test]
394391
async fn test_str_replace_single_occurrence() {
395-
let test_provider = TestProvider::new();
396-
let test_dir = TestDir::new().with_file_sys(("test.txt", "hello world"), &test_provider).await;
392+
let test_base = TestBase::new()
393+
.await
394+
.with_file(("test.txt", "hello world"))
395+
.await;
397396

398397
let tool = FsWrite::StrReplace(StrReplace {
399-
path: test_dir.join("test.txt").to_string_lossy().to_string(),
398+
path: test_base.join("test.txt").to_string_lossy().to_string(),
400399
old_str: "world".to_string(),
401400
new_str: "rust".to_string(),
402401
replace_all: false,
403402
});
404403

405-
assert!(tool.execute(None, &test_provider).await.is_ok());
404+
assert!(tool.execute(None, &test_base).await.is_ok());
406405

407-
let content = tokio::fs::read_to_string(test_dir.join("test.txt")).await.unwrap();
406+
let content = tokio::fs::read_to_string(test_base.join("test.txt")).await.unwrap();
408407
assert_eq!(content, "hello rust");
409408
}
410409

411410
#[tokio::test]
412411
async fn test_str_replace_multiple_occurrences() {
413-
let test_provider = TestProvider::new();
414-
let test_dir = TestDir::new().with_file_sys(("test.txt", "foo bar foo"), &test_provider).await;
412+
let test_base = TestBase::new()
413+
.await
414+
.with_file(("test.txt", "foo bar foo"))
415+
.await;
415416

416417
let tool = FsWrite::StrReplace(StrReplace {
417-
path: test_dir.join("test.txt").to_string_lossy().to_string(),
418+
path: test_base.join("test.txt").to_string_lossy().to_string(),
418419
old_str: "foo".to_string(),
419420
new_str: "baz".to_string(),
420421
replace_all: true,
421422
});
422423

423-
assert!(tool.execute(None, &test_provider).await.is_ok());
424+
assert!(tool.execute(None, &test_base).await.is_ok());
424425

425-
let content = tokio::fs::read_to_string(test_dir.join("test.txt")).await.unwrap();
426+
let content = tokio::fs::read_to_string(test_base.join("test.txt")).await.unwrap();
426427
assert_eq!(content, "baz bar baz");
427428
}
428429

429430
#[tokio::test]
430431
async fn test_str_replace_no_match() {
431-
let test_provider = TestProvider::new();
432-
let test_dir = TestDir::new().with_file_sys(("test.txt", "hello world"), &test_provider).await;
432+
let test_base = TestBase::new()
433+
.await
434+
.with_file(("test.txt", "hello world"))
435+
.await;
433436

434437
let tool = FsWrite::StrReplace(StrReplace {
435-
path: test_dir.join("test.txt").to_string_lossy().to_string(),
438+
path: test_base.join("test.txt").to_string_lossy().to_string(),
436439
old_str: "missing".to_string(),
437440
new_str: "replacement".to_string(),
438441
replace_all: false,
439442
});
440443

441-
assert!(tool.execute(None, &test_provider).await.is_err());
444+
assert!(tool.execute(None, &test_base).await.is_err());
442445
}
443446

444447
#[tokio::test]
445448
async fn test_insert_at_line() {
446-
let test_provider = TestProvider::new();
447-
let test_dir = TestDir::new().with_file_sys(("test.txt", "line1\nline2\nline3"), &test_provider).await;
449+
let test_base = TestBase::new()
450+
.await
451+
.with_file(("test.txt", "line1\nline2\nline3"))
452+
.await;
448453

449454
let tool = FsWrite::Insert(Insert {
450-
path: test_dir.join("test.txt").to_string_lossy().to_string(),
455+
path: test_base.join("test.txt").to_string_lossy().to_string(),
451456
content: "inserted".to_string(),
452457
insert_line: Some(1),
453458
});
454459

455-
assert!(tool.execute(None, &test_provider).await.is_ok());
460+
assert!(tool.execute(None, &test_base).await.is_ok());
456461

457-
let content = tokio::fs::read_to_string(test_dir.join("test.txt")).await.unwrap();
462+
let content = tokio::fs::read_to_string(test_base.join("test.txt")).await.unwrap();
458463
assert_eq!(content, "line1\ninserted\nline2\nline3");
459464
}
460465

461466
#[tokio::test]
462467
async fn test_insert_append() {
463-
let test_provider = TestProvider::new();
464-
let test_dir = TestDir::new().with_file_sys(("test.txt", "existing"), &test_provider).await;
468+
let test_base = TestBase::new()
469+
.await
470+
.with_file(("test.txt", "existing"))
471+
.await;
465472

466473
let tool = FsWrite::Insert(Insert {
467-
path: test_dir.join("test.txt").to_string_lossy().to_string(),
474+
path: test_base.join("test.txt").to_string_lossy().to_string(),
468475
content: "appended".to_string(),
469476
insert_line: None,
470477
});
471478

472-
assert!(tool.execute(None, &test_provider).await.is_ok());
479+
assert!(tool.execute(None, &test_base).await.is_ok());
473480

474-
let content = tokio::fs::read_to_string(test_dir.join("test.txt")).await.unwrap();
481+
let content = tokio::fs::read_to_string(test_base.join("test.txt")).await.unwrap();
475482
assert_eq!(content, "existing\nappended");
476483
}
477484

478485
#[tokio::test]
479486
async fn test_fs_write_validate_empty_path() {
480-
let test_provider = TestProvider::new();
487+
let test_base = TestBase::new().await;
481488
let tool = FsWrite::Create(FileCreate {
482489
path: "".to_string(),
483490
content: "content".to_string(),
484491
});
485492

486-
assert!(tool.validate(&test_provider).await.is_err());
493+
assert!(tool.validate(&test_base).await.is_err());
487494
}
488495

489496
#[tokio::test]
490497
async fn test_fs_write_validate_nonexistent_file_for_replace() {
491-
let test_provider = TestProvider::new();
498+
let test_base = TestBase::new().await;
492499
let tool = FsWrite::StrReplace(StrReplace {
493500
path: "/nonexistent/file.txt".to_string(),
494501
old_str: "old".to_string(),
495502
new_str: "new".to_string(),
496503
replace_all: false,
497504
});
498505

499-
assert!(tool.validate(&test_provider).await.is_err());
506+
assert!(tool.validate(&test_base).await.is_err());
500507
}
501508
}

0 commit comments

Comments
 (0)