MCPcopy Index your code
hub / github.com/coder/coder / TestEditFiles

Function TestEditFiles

agent/agentfiles/files_test.go:481–1076  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

479}
480
481func 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",

Callers

nothing calls this directly

Calls 12

RoutesMethod · 0.95
NewAPIFunction · 0.92
MkdirAllMethod · 0.80
EncodeMethod · 0.80
newTestFsFunction · 0.70
TempDirMethod · 0.65
NewMethod · 0.65
RunMethod · 0.65
WriteFileMethod · 0.65
ReadFileMethod · 0.65
ServeHTTPMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected