diff --git a/src/lib/openFile.js b/src/lib/openFile.js index 3b8511467..c90b4f852 100644 --- a/src/lib/openFile.js +++ b/src/lib/openFile.js @@ -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; } diff --git a/src/sidebarApps/searchInFiles/index.js b/src/sidebarApps/searchInFiles/index.js index 5729f1e45..336fc3ef1 100644 --- a/src/sidebarApps/searchInFiles/index.js +++ b/src/sidebarApps/searchInFiles/index.js @@ -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; const regex = toRegex(search, options); if (!regex) return; diff --git a/tests/unit/openFileCancellation.test.js b/tests/unit/openFileCancellation.test.js index 80f4d4b0d..5e97d5b94 100644 --- a/tests/unit/openFileCancellation.test.js +++ b/tests/unit/openFileCancellation.test.js @@ -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", diff --git a/tests/unit/searchReplaceAll.test.js b/tests/unit/searchReplaceAll.test.js new file mode 100644 index 000000000..fdc77e158 --- /dev/null +++ b/tests/unit/searchReplaceAll.test.js @@ -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); + }); +});