feat: use singleton electron app if an experimental flag is set
Some checks failed
Playwright Tests / test (push) Has been cancelled
Some checks failed
Playwright Tests / test (push) Has been cancelled
This commit is contained in:
parent
36903f0a37
commit
010d56a87d
@ -0,0 +1,22 @@
|
||||
import type { ElectronApplication } from "playwright"
|
||||
import {_electron as electron} from "playwright";
|
||||
import { addConsoleListener } from "./internal-util";
|
||||
|
||||
const singleton: {
|
||||
app: ElectronApplication | null
|
||||
} = {
|
||||
app: null,
|
||||
}
|
||||
|
||||
export async function getOrCreateApp(obsidianPath: string, uriArg?: string) {
|
||||
if (singleton.app) {
|
||||
return singleton.app;
|
||||
}
|
||||
const app = await electron.launch({
|
||||
timeout: 60000,
|
||||
args: [obsidianPath, uriArg].filter((a) => !!a) as string[],
|
||||
});
|
||||
addConsoleListener(app);
|
||||
singleton.app = app;
|
||||
return app;
|
||||
}
|
||||
@ -5,50 +5,13 @@ import { ObsidianTestFixtures } from "./fixtures.js";
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
||||
import { pageUtils, waitForIndexingComplete } from "./util.js";
|
||||
import { randomBytes } from "crypto";
|
||||
import { addConsoleListener, checkToy, getExe } from "./internal-util.js";
|
||||
import { getOrCreateApp } from "./electron-singleton.js";
|
||||
|
||||
export interface ObsidianTestingConfig {
|
||||
vault?: string;
|
||||
obsidianPath?: string;
|
||||
}
|
||||
|
||||
export function getExe(obsidianPath?: string): string {
|
||||
checkToy();
|
||||
if (obsidianPath) {
|
||||
return path.join(obsidianPath, "Resources", "app.asar");
|
||||
}
|
||||
if (process.platform == "win32") {
|
||||
const p = path.join(
|
||||
process.env.LOCALAPPDATA!,
|
||||
"Obsidian",
|
||||
"Resources",
|
||||
"app.asar"
|
||||
);
|
||||
if (existsSync(p)) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
const possibleDirs = [
|
||||
"/opt/Obsidian",
|
||||
"/usr/lib/Obsidian",
|
||||
"/opt/obsidian",
|
||||
"/usr/lib/obsidian",
|
||||
"/var/lib/flatpak/app/md.obsidian.Obsidian/current/active/files",
|
||||
"/snap/obsidian/current",
|
||||
];
|
||||
for (let i = 0; i < possibleDirs.length; i++) {
|
||||
if (existsSync(possibleDirs[i])) {
|
||||
// console.log(execSync(`ls -l ${possibleDirs[i]}`).toString());
|
||||
return path.join(possibleDirs[i], "resources", "app.asar");
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function checkToy() {
|
||||
if (process.platform == "darwin") {
|
||||
throw new Error("use a non-toy operating system, dumbass");
|
||||
}
|
||||
experimentalUseSingleton?: boolean;
|
||||
}
|
||||
|
||||
function generateVaultConfig(vault: string) {
|
||||
@ -87,7 +50,7 @@ function generateVaultConfig(vault: string) {
|
||||
writeFileSync(path.join(configLocation, `${vaultHash}.json`), "{}");
|
||||
return vaultHash;
|
||||
} else {
|
||||
return Object.entries(json.vaults).find(a => a[1].path === vault)![0];
|
||||
return Object.entries(json.vaults).find((a) => a[1].path === vault)![0];
|
||||
}
|
||||
}
|
||||
|
||||
@ -96,8 +59,9 @@ export const test = base.extend<ObsidianTestFixtures>({
|
||||
electronApp: [
|
||||
async ({ obsidian }, run) => {
|
||||
console.log("obsidian", obsidian);
|
||||
const {obsidianPath = undefined} = obsidian ?? {};
|
||||
const vault = <string> inject("vault")
|
||||
const { obsidianPath = undefined, experimentalUseSingleton = false } =
|
||||
obsidian ?? {};
|
||||
const vault = <string>inject("vault");
|
||||
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = "true";
|
||||
console.log("asar located at:", getExe(obsidianPath));
|
||||
let uriArg = "";
|
||||
@ -108,19 +72,23 @@ export const test = base.extend<ObsidianTestFixtures>({
|
||||
}
|
||||
}
|
||||
|
||||
const electronApp = await electron.launch({
|
||||
const electronApp = experimentalUseSingleton
|
||||
? await getOrCreateApp(
|
||||
getExe(obsidianPath),
|
||||
!!uriArg ? uriArg : undefined
|
||||
)
|
||||
: await electron.launch({
|
||||
timeout: 60000,
|
||||
args: [getExe(obsidianPath), uriArg].filter((a) => !!a) as string[],
|
||||
});
|
||||
electronApp.on("console", async (msg) => {
|
||||
console.log(
|
||||
...(await Promise.all(msg.args().map((a) => a.jsonValue())))
|
||||
);
|
||||
});
|
||||
addConsoleListener(electronApp);
|
||||
await electronApp.firstWindow();
|
||||
await run(electronApp);
|
||||
if (!experimentalUseSingleton) {
|
||||
await electronApp.close();
|
||||
}, {auto: true}
|
||||
}
|
||||
},
|
||||
{ auto: true },
|
||||
],
|
||||
page: [
|
||||
async ({ electronApp }, run) => {
|
||||
@ -136,16 +104,17 @@ export const test = base.extend<ObsidianTestFixtures>({
|
||||
for (let fn of Object.entries(pageUtils)) {
|
||||
await page.exposeFunction(fn[0], fn[1]);
|
||||
}
|
||||
page.on("pageerror", exc => {
|
||||
page.on("pageerror", (exc) => {
|
||||
console.error("EXCEPTION");
|
||||
console.error(exc);
|
||||
})
|
||||
});
|
||||
page.on("console", async (msg) => {
|
||||
console.log(
|
||||
...(await Promise.all(msg.args().map((a) => a.jsonValue())))
|
||||
);
|
||||
});
|
||||
await run(page);
|
||||
}, {auto: true}
|
||||
},
|
||||
{ auto: true },
|
||||
],
|
||||
});
|
||||
|
||||
51
packages/obsidian-testing-framework/src/internal-util.ts
Normal file
51
packages/obsidian-testing-framework/src/internal-util.ts
Normal file
@ -0,0 +1,51 @@
|
||||
import path from "path";
|
||||
import { existsSync } from "fs";
|
||||
import { ElectronApplication } from "playwright";
|
||||
|
||||
export function checkToy() {
|
||||
if (process.platform == "darwin") {
|
||||
throw new Error("use a non-toy operating system, dumbass");
|
||||
}
|
||||
}
|
||||
|
||||
export function getExe(obsidianPath?: string): string {
|
||||
checkToy();
|
||||
if (obsidianPath) {
|
||||
return path.join(obsidianPath, "Resources", "app.asar");
|
||||
}
|
||||
if (process.platform == "win32") {
|
||||
const p = path.join(
|
||||
process.env.LOCALAPPDATA!,
|
||||
"Obsidian",
|
||||
"Resources",
|
||||
"app.asar"
|
||||
);
|
||||
if (existsSync(p)) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
const possibleDirs = [
|
||||
"/opt/Obsidian",
|
||||
"/usr/lib/Obsidian",
|
||||
"/opt/obsidian",
|
||||
"/usr/lib/obsidian",
|
||||
"/var/lib/flatpak/app/md.obsidian.Obsidian/current/active/files",
|
||||
"/snap/obsidian/current",
|
||||
];
|
||||
for (let i = 0; i < possibleDirs.length; i++) {
|
||||
if (existsSync(possibleDirs[i])) {
|
||||
// console.log(execSync(`ls -l ${possibleDirs[i]}`).toString());
|
||||
return path.join(possibleDirs[i], "resources", "app.asar");
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
export function addConsoleListener(app: ElectronApplication) {
|
||||
app.on("console", async (msg) => {
|
||||
console.log(
|
||||
...(await Promise.all(msg.args().map((a) => a.jsonValue())))
|
||||
);
|
||||
});
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user