Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib/openFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ export default async function openFile(file, options = {}) {
}
}

if (text) {
// If file is not opened and has unsaved text
if (text != null) {
// If file is not opened and has unsaved text (may be "" after a replace)
createEditor(true, text);
return;
}
Expand Down
3 changes: 2 additions & 1 deletion src/sidebarApps/searchInFiles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,8 @@ async function replaceAll() {
const search = $search.value;
const replace = $replace.value;
const options = getOptions();
if (!search || !replace) return;
// An empty replacement is valid: it deletes every match.
if (!search) return;
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
const regex = toRegex(search, options);
if (!regex) return;

Expand Down
8 changes: 8 additions & 0 deletions tests/unit/openFileCancellation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ describe("openFile cancellation", () => {
expect(recents.addFile).toHaveBeenCalledWith("target");
});

it("opens supplied empty text as unsaved instead of reading the file", async () => {
await openFile("target", { render: true, text: "" });
expect(createEditor).toHaveBeenCalledOnce();
expect(manager.activeFile.text).toBe("");
expect(manager.activeFile.isUnsaved).toBe(true);
expect(readFile).not.toHaveBeenCalled();
});

it.each([
"pdf",
"docx",
Expand Down
70 changes: 70 additions & 0 deletions tests/unit/searchReplaceAll.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { readFileSync } from "node:fs";
import vm from "node:vm";
import { describe, expect, it, vi } from "vitest";

const source = readFileSync(
`${process.cwd()}/src/sidebarApps/searchInFiles/index.js`,
"utf8",
);

function createReplaceAll({ search, replace, native = false }) {
const context = vm.createContext({
$search: { value: search },
$replace: { value: replace },
filesSearched: [{ url: "file:///a.js" }, { url: "file:///b.js" }],
filesReplaced: [],
replacing: false,
activeReplaceTasks: 0,
terminateWorker: vi.fn(),
getOptions: () => ({ regExp: false }),
toRegex: (value) => new RegExp(value, "g"),
supportsNativeSearch: () => native,
sendNativeSearch: vi.fn(),
sendMessage: vi.fn(),
});
vm.runInContext(
source.slice(
source.indexOf("async function replaceAll()"),
source.indexOf("function sendMessage("),
),
context,
);
return context;
}

describe("search in files replace all", () => {
it("replaces matches with an empty string", async () => {
const context = createReplaceAll({ search: "foo", replace: "" });
await vm.runInContext("replaceAll()", context);

expect(context.sendMessage).toHaveBeenCalledTimes(1);
const [action, files, , , replace] = context.sendMessage.mock.calls[0];
expect(action).toBe("replace-files");
expect(files).toHaveLength(2);
expect(replace).toBe("");
expect(context.replacing).toBe(true);
});

it("sends an empty replacement to the native search", async () => {
const context = createReplaceAll({
search: "foo",
replace: "",
native: true,
});
await vm.runInContext("replaceAll()", context);

expect(context.sendNativeSearch).toHaveBeenCalledTimes(1);
expect(context.sendNativeSearch.mock.calls[0][0]).toBe("replace");
expect(context.sendNativeSearch.mock.calls[0][4]).toBe("");
expect(context.sendMessage).not.toHaveBeenCalled();
});

it("does nothing without a search query", async () => {
const context = createReplaceAll({ search: "", replace: "bar" });
await vm.runInContext("replaceAll()", context);

expect(context.sendMessage).not.toHaveBeenCalled();
expect(context.sendNativeSearch).not.toHaveBeenCalled();
expect(context.replacing).toBe(false);
});
});
Loading