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
45 changes: 42 additions & 3 deletions src/engine/userScriptExecution.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TFile, type App } from "obsidian";
import { describe, expect, it, vi } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { IChoiceExecutor } from "../IChoiceExecutor";
import type QuickAdd from "../main";
import { CommandType } from "../types/macros/CommandType";
Expand Down Expand Up @@ -27,14 +27,14 @@ module.exports = {
};
`;

function createApp(secrets: Map<string, string>): App {
function createApp(secrets: Map<string, string>, source = SCRIPT_SOURCE): App {
const file = new TFile();
file.path = "scripts/todoist.js";
file.extension = "js";
return {
vault: {
getAbstractFileByPath: vi.fn(() => file),
read: vi.fn(async () => SCRIPT_SOURCE),
read: vi.fn(async () => source),
},
secretStorage: {
getSecret: vi.fn((id: string) => secrets.get(id) ?? null),
Expand Down Expand Up @@ -118,3 +118,42 @@ describe("executeUserScript settings with `Script::Export` member access", () =>
expect([...secrets.values()]).toEqual(["legacy-token"]);
});
});

describe("executeUserScript with a script that exports nothing", () => {
afterEach(() => vi.unstubAllGlobals());

// #2208: all of the script's work happens at the top level while it loads.
it("runs the top-level code and finishes without output", async () => {
const ran: string[] = [];
vi.stubGlobal("window", { require: () => ran });
const command: IUserScript = {
id: "side-effect-command",
name: "create_wp_folder",
type: CommandType.UserScript,
path: "scripts/todoist.js",
settings: {},
};

const { result } = await run(
command,
createApp(new Map(), "require('ran').push('top level');"),
);

expect(ran).toEqual(["top level"]);
expect(result).toBeUndefined();
});

it("still rejects a selected member that is an empty object", async () => {
const command: IUserScript = {
id: "empty-member-command",
name: "lib::start",
type: CommandType.UserScript,
path: "scripts/todoist.js",
settings: {},
};

await expect(
run(command, createApp(new Map(), "module.exports = { start: {} };")),
).rejects.toThrow("is an empty object");
});
});
6 changes: 5 additions & 1 deletion src/engine/userScriptExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { QuickAddApi } from "../quickAddApi";
import type QuickAdd from "../main";
import type { IChoiceExecutor } from "../IChoiceExecutor";
import type { IUserScript } from "../types/macros/IUserScript";
import { getUserScriptPreloadKey, type LoadedUserScript, loadUserScript } from "../utils/userScript";
import { getUserScriptMemberAccess, getUserScriptPreloadKey, type LoadedUserScript, loadUserScript } from "../utils/userScript";
import { initializeUserScriptSettings } from "../utils/userScriptSettings";
import { resolveScriptSettings } from "./userScriptSettings";
import { log } from "../logger/logManager";
Expand Down Expand Up @@ -120,6 +120,10 @@ export async function executeUserScript(
}
}

// Nothing exported: the script did its work at the top level while loading.
const drillsMember = (getUserScriptMemberAccess(command.name ?? "").memberAccess ?? []).length > 0;
if (!drillsMember && isRecord(userScript) && Object.keys(userScript).length === 0) return;

try {
return await delegate(userScript);
} catch (err) {
Expand Down
Loading