Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

desktop: add support for running tests in parallel

Abdullah Attab30110a48a5b
diff --git a/apps/desktop/__tests__/utils.ts b/apps/desktop/__tests__/utils.ts
index aaaa9dc2f..658ab768f 100644
--- a/apps/desktop/__tests__/utils.ts
+++ b/apps/desktop/__tests__/utils.ts
@@ -18,13 +18,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { execSync } from "child_process";
-import { mkdir, rm, rmdir } from "fs/promises";
+import { cp, mkdir, readFile, rm, rmdir, writeFile } from "fs/promises";
import { fileURLToPath } from "node:url";
-import path from "path";
+import path, { join, resolve } from "path";
import { _electron as electron } from "playwright";
import slugify from "slugify";
import { test as vitestTest, TestContext } from "vitest";
import { patchBetterSQLite3 } from "../scripts/patch-better-sqlite3.mjs";
+import { existsSync } from "fs";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@@ -53,16 +54,14 @@ export const test = vitestTest.extend<Fixtures>({
ctx: async ({ options }, use) => {
const ctx = await buildAndLaunchApp(options);
await use(ctx);
- await ctx.app.close();
- await rmdir(ctx.userDataDir, { recursive: true });
- await rmdir(ctx.outputDir, { recursive: true });
}
});
export async function testCleanup(context: TestContext) {
+ const ctx = (context.task.context as unknown as Fixtures).ctx;
if (context.task.result?.state === "fail") {
await mkdir("test-results", { recursive: true });
- await (context.task.context as unknown as Fixtures).ctx.page.screenshot({
+ await ctx.page.screenshot({
path: path.join(
"test-results",
`${slugify(context.task.name)}-${process.platform}-${
@@ -71,6 +70,9 @@ export async function testCleanup(context: TestContext) {
)
});
}
+ await ctx.app.close();
+ await rmdir(ctx.userDataDir, { recursive: true });
+ await rmdir(ctx.outputDir, { recursive: true });
}
async function buildAndLaunchApp(options?: TestOptions): Promise<AppContext> {
@@ -78,11 +80,11 @@ async function buildAndLaunchApp(options?: TestOptions): Promise<AppContext> {
const outputDir = path.join("test-artifacts", `${productName}-output`);
const executablePath = await buildApp({
...options,
- productName,
outputDir
});
const { app, page, configPath, userDataDir } = await launchApp(
- executablePath
+ executablePath,
+ productName
);
const ctx: AppContext = {
app,
@@ -92,7 +94,8 @@ async function buildAndLaunchApp(options?: TestOptions): Promise<AppContext> {
outputDir,
relaunch: async () => {
const { app, page, configPath, userDataDir } = await launchApp(
- executablePath
+ executablePath,
+ productName
);
ctx.app = app;
ctx.page = page;
@@ -103,25 +106,31 @@ async function buildAndLaunchApp(options?: TestOptions): Promise<AppContext> {
return ctx;
}
-async function launchApp(executablePath: string) {
+async function launchApp(executablePath: string, packageName: string) {
+ const userDataDir = resolve(
+ __dirname,
+ "..",
+ "test-artifacts",
+ "user_data_dirs",
+ packageName
+ );
const app = await electron.launch({
executablePath,
args: IS_DEBUG ? [] : ["--hidden"],
- env:
- process.platform === "linux"
+ env: {
+ ...(process.platform === "linux"
? {
...(process.env as Record<string, string>),
APPIMAGE: "true"
}
- : (process.env as Record<string, string>)
+ : (process.env as Record<string, string>)),
+ CUSTOM_USER_DATA_DIR: userDataDir
+ }
});
const page = await app.firstWindow();
- const userDataDir = await app.evaluate((a) => {
- return a.app.getPath("userData");
- });
- const configPath = path.join(userDataDir, "config.json");
+ const configPath = path.join(userDataDir, "UserData", "config.json");
return {
app,
page,
@@ -132,54 +141,131 @@ async function launchApp(executablePath: string) {
async function buildApp({
version,
- productName,
outputDir
}: {
version?: string;
- productName: string;
outputDir: string;
}) {
- const args = [
- "electron-builder",
- "--dir",
- `--${process.arch}`,
- `--config electron-builder.config.js`,
- `--c.extraMetadata.productName=${productName}`,
- `--c.compression=store`,
- "--publish=never"
- ];
- if (version) args.push(`--c.extraMetadata.version=${version}`);
-
- execSync(`npx ${args.join(" ")}`, {
- stdio: IS_DEBUG ? "inherit" : "ignore",
- env: {
- ...process.env,
- NOTESNOOK_STAGING: "true",
- NN_PRODUCT_NAME: productName,
- NN_APP_ID: `com.notesnook.test.${productName}`,
- NN_OUTPUT_DIR: outputDir
- }
- });
+ const productName = `NotesnookTestHarness`;
+ const sourceDir = resolve("output", productName);
+ if (!existsSync(sourceDir)) {
+ const args = [
+ "electron-builder",
+ "--dir",
+ `--${process.arch}`,
+ `--config electron-builder.config.js`,
+ `--c.extraMetadata.productName=${productName}`,
+ `--c.compression=store`,
+ "--publish=never"
+ ];
+ if (version) args.push(`--c.extraMetadata.version=${version}`);
- await patchBetterSQLite3();
+ execSync(`npx ${args.join(" ")}`, {
+ stdio: IS_DEBUG ? "inherit" : "ignore",
+ env: {
+ ...process.env,
+ NOTESNOOK_STAGING: "true",
+ NN_PRODUCT_NAME: productName,
+ NN_APP_ID: `com.notesnook.test.${productName}`,
+ NN_OUTPUT_DIR: sourceDir
+ }
+ });
+ }
+ return process.platform === "win32"
+ ? await copyBuildWindows(sourceDir, outputDir, productName, version)
+ : process.platform === "darwin"
+ ? await copyBuildMacOS(sourceDir, outputDir, productName, version)
+ : await copyBuildLinux(sourceDir, outputDir, productName, version);
+}
- return path.join(
+async function copyBuildLinux(
+ sourceDir: string,
+ outputDir: string,
+ productName: string,
+ version?: string
+) {
+ const platformDir =
+ process.arch === "arm64" ? "linux-arm64-unpacked" : "linux-unpacked";
+ const appDir = await copyBuild(
+ sourceDir,
+ outputDir,
+ platformDir,
+ "resources",
+ version
+ );
+ return resolve(__dirname, "..", appDir, productName);
+}
+
+async function copyBuildWindows(
+ sourceDir: string,
+ outputDir: string,
+ productName: string,
+ version?: string
+) {
+ const platformDir =
+ process.arch === "arm64" ? "win-arm64-unpacked" : "win-unpacked";
+ const appDir = await copyBuild(
+ sourceDir,
+ outputDir,
+ platformDir,
+ "resources",
+ version
+ );
+ return resolve(__dirname, "..", appDir, `${productName}.exe`);
+}
+
+async function copyBuildMacOS(
+ sourceDir: string,
+ outputDir: string,
+ productName: string,
+ version?: string
+) {
+ const platformDir = process.arch === "arm64" ? "mac-arm64" : "mac";
+ const appDir = await copyBuild(
+ sourceDir,
+ outputDir,
+ platformDir,
+ join(`${productName}.app`, "Contents", "Resources"),
+ version
+ );
+ return resolve(
__dirname,
"..",
- outputDir,
- process.platform === "linux"
- ? process.arch === "arm64"
- ? "linux-arm64-unpacked"
- : "linux-unpacked"
- : process.platform === "darwin"
- ? process.arch === "arm64"
- ? `mac-arm64/${productName}.app/Contents/MacOS/`
- : `mac/${productName}.app/Contents/MacOS/`
- : "win-unpacked",
- process.platform === "win32" ? `${productName}.exe` : productName
+ appDir,
+ `${productName}.app`,
+ "Contents",
+ "MacOS",
+ productName
);
}
+async function copyBuild(
+ sourceDir: string,
+ outputDir: string,
+ platformDir: string,
+ resourcesDir: string,
+ version?: string
+) {
+ const appDir = outputDir;
+ await cp(join(sourceDir, platformDir), outputDir, {
+ recursive: true,
+ preserveTimestamps: true,
+ verbatimSymlinks: true,
+ dereference: false,
+ force: true
+ });
+
+ const packageJsonPath = join(appDir, resourcesDir, "app", "package.json");
+
+ const packageJson = JSON.parse(await readFile(packageJsonPath, "utf-8"));
+ if (version) {
+ packageJson.version = version;
+ await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));
+ }
+
+ return appDir;
+}
+
function makeid(length: number) {
let result = "";
const characters =
diff --git a/apps/desktop/package.json b/apps/desktop/package.json
index 0216fa02b..ef3edae32 100644
--- a/apps/desktop/package.json
+++ b/apps/desktop/package.json
@@ -8,7 +8,9 @@
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
- "sideEffects": false,
+ "sideEffects": [
+ "src/overrides.ts"
+ ],
"exports": {
".": {
"require": {
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index 83b78ac44..f4d44000a 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+import "./overrides";
import { app, BrowserWindow, nativeTheme, shell } from "electron";
import { isDevelopment } from "./utils";
import { registerProtocol, PROTOCOL_URL } from "./utils/protocol";
diff --git a/apps/desktop/src/overrides.ts b/apps/desktop/src/overrides.ts
new file mode 100644
index 000000000..721dbfb43
--- /dev/null
+++ b/apps/desktop/src/overrides.ts
@@ -0,0 +1,35 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+import { app } from "electron";
+import path from "path";
+
+if (process.env.CUSTOM_USER_DATA_DIR) {
+ app.setPath(
+ "appData",
+ path.join(process.env.CUSTOM_USER_DATA_DIR, "AppData")
+ );
+ app.setPath(
+ "userData",
+ path.join(process.env.CUSTOM_USER_DATA_DIR, "UserData")
+ );
+ app.setPath(
+ "documents",
+ path.join(process.env.CUSTOM_USER_DATA_DIR, "Documents")
+ );
+}
diff --git a/apps/desktop/vitest.config.ts b/apps/desktop/vitest.config.ts
index e3de1b3d9..eacce4391 100644
--- a/apps/desktop/vitest.config.ts
+++ b/apps/desktop/vitest.config.ts
@@ -23,7 +23,7 @@ export default defineConfig({
test: {
testTimeout: process.env.CI ? 120 * 1000 : 120 * 1000,
sequence: {
- concurrent: false,
+ concurrent: true,
shuffle: true
},
dir: "./__tests__/",