(t *testing.T)
| 479 | } |
| 480 | |
| 481 | func TestEditFiles(t *testing.T) { |
| 482 | t.Parallel() |
| 483 | |
| 484 | tmpdir := os.TempDir() |
| 485 | noPermsFilePath := filepath.Join(tmpdir, "no-perms-file") |
| 486 | failRenameFilePath := filepath.Join(tmpdir, "fail-rename") |
| 487 | logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug) |
| 488 | fs := newTestFs(afero.NewMemMapFs(), func(call, file string) error { |
| 489 | if file == noPermsFilePath { |
| 490 | return &os.PathError{ |
| 491 | Op: call, |
| 492 | Path: file, |
| 493 | Err: os.ErrPermission, |
| 494 | } |
| 495 | } else if file == failRenameFilePath && call == "rename" { |
| 496 | return xerrors.New("rename failed") |
| 497 | } |
| 498 | return nil |
| 499 | }) |
| 500 | api := agentfiles.NewAPI(logger, fs, nil) |
| 501 | |
| 502 | dirPath := filepath.Join(tmpdir, "directory") |
| 503 | err := fs.MkdirAll(dirPath, 0o755) |
| 504 | require.NoError(t, err) |
| 505 | |
| 506 | tests := []struct { |
| 507 | name string |
| 508 | contents map[string]string |
| 509 | edits []workspacesdk.FileEdits |
| 510 | expected map[string]string |
| 511 | errCode int |
| 512 | errors []string |
| 513 | }{ |
| 514 | { |
| 515 | name: "NoFiles", |
| 516 | errCode: http.StatusBadRequest, |
| 517 | errors: []string{"must specify at least one file"}, |
| 518 | }, |
| 519 | { |
| 520 | name: "NoPath", |
| 521 | errCode: http.StatusBadRequest, |
| 522 | edits: []workspacesdk.FileEdits{ |
| 523 | { |
| 524 | Edits: []workspacesdk.FileEdit{ |
| 525 | { |
| 526 | Search: "foo", |
| 527 | Replace: "bar", |
| 528 | }, |
| 529 | }, |
| 530 | }, |
| 531 | }, |
| 532 | errors: []string{"\"path\" is required"}, |
| 533 | }, |
| 534 | { |
| 535 | name: "RelativePathDotSlash", |
| 536 | edits: []workspacesdk.FileEdits{ |
| 537 | { |
| 538 | Path: "./relative", |
nothing calls this directly
no test coverage detected