Skip to content

Commit c63c4a9

Browse files
committed
Move edit length check above file open
1 parent 48a1956 commit c63c4a9

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

agent/files.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ func (a *agent) editFile(path string, edits []workspacesdk.FileEdit) (int, error
208208
return http.StatusBadRequest, xerrors.Errorf("file path must be absolute: %q", path)
209209
}
210210

211+
if len(edits) == 0 {
212+
return http.StatusBadRequest, xerrors.New("must specify at least one edit")
213+
}
214+
211215
f, err := a.filesystem.Open(path)
212216
if err != nil {
213217
status := http.StatusInternalServerError
@@ -230,10 +234,6 @@ func (a *agent) editFile(path string, edits []workspacesdk.FileEdit) (int, error
230234
return http.StatusBadRequest, xerrors.Errorf("open %s: not a file", path)
231235
}
232236

233-
if len(edits) == 0 {
234-
return http.StatusBadRequest, xerrors.New("must specify at least one edit")
235-
}
236-
237237
transforms := make([]transform.Transformer, len(edits))
238238
for i, edit := range edits {
239239
transforms[i] = replace.String(edit.Search, edit.Replace)

agent/files_test.go

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -435,30 +435,48 @@ func TestEditFile(t *testing.T) {
435435
error: "file path must be absolute",
436436
},
437437
{
438-
name: "NonExistent",
439-
path: filepath.Join(tmpdir, "does-not-exist"),
438+
name: "NoEdits",
439+
path: filepath.Join(tmpdir, "no-edits"),
440+
contents: "foo bar",
441+
errCode: http.StatusBadRequest,
442+
error: "must specify at least one edit",
443+
},
444+
{
445+
name: "NonExistent",
446+
path: filepath.Join(tmpdir, "does-not-exist"),
447+
edits: []workspacesdk.FileEdit{
448+
{
449+
Search: "foo",
450+
Replace: "bar",
451+
},
452+
},
440453
errCode: http.StatusNotFound,
441454
error: "file does not exist",
442455
},
443456
{
444-
name: "IsDir",
445-
path: dirPath,
457+
name: "IsDir",
458+
path: dirPath,
459+
edits: []workspacesdk.FileEdit{
460+
{
461+
Search: "foo",
462+
Replace: "bar",
463+
},
464+
},
446465
errCode: http.StatusBadRequest,
447466
error: "not a file",
448467
},
449468
{
450-
name: "NoPermissions",
451-
path: noPermsFilePath,
469+
name: "NoPermissions",
470+
path: noPermsFilePath,
471+
edits: []workspacesdk.FileEdit{
472+
{
473+
Search: "foo",
474+
Replace: "bar",
475+
},
476+
},
452477
errCode: http.StatusForbidden,
453478
error: "permission denied",
454479
},
455-
{
456-
name: "NoEdits",
457-
path: filepath.Join(tmpdir, "no-edits"),
458-
contents: "foo bar",
459-
errCode: http.StatusBadRequest,
460-
error: "must specify at least one edit",
461-
},
462480
{
463481
name: "FailRename",
464482
path: failRenameFilePath,

0 commit comments

Comments
 (0)