another commit

This commit is contained in:
parent 61e71a562c
commit fc5a65e9f7
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C
11 changed files with 122 additions and 149 deletions

BIN
.yarn/install-state.gz vendored

Binary file not shown.

@ -2,6 +2,9 @@
"name": "obsidian-testing-framework",
"packageManager": "yarn@4.5.1",
"dependencies": {
"@codemirror/language": "https://github.com/lishid/cm-language",
"@codemirror/state": "^6.0.1",
"@codemirror/view": "^6.0.1",
"typescript": "^5.6.3"
},
"version": "",
@ -17,6 +20,10 @@
"./utils": {
"default": "./lib/utils.js",
"types": "./lib/utils.d.ts"
},
"./fixture": {
"default": "./lib/fixtures.js",
"types": "./lib/fixtures.d.ts"
}
},
"main": "./lib/index.js",
@ -35,5 +42,6 @@
},
"workspaces": [
"./*"
]
],
"type": "module"
}

@ -1,6 +1,6 @@
import { App } from "obsidian";
import { test as base } from "@playwright/test";
import { _electron as electron, ElectronApplication, Page } from "playwright";
import { _electron as electron } from "playwright";
import { Fixtures } from "@playwright/test";
import { execSync } from "child_process";
import path from "path";

@ -1,114 +0,0 @@
import {
_electron,
_electron as electron,
ElectronApplication,
Page,
} from "playwright";
import {expect} from "vitest"
import { App, TFile, Vault } from "obsidian";
import { execSync } from "child_process";
import path from "path";
import { EOL } from "os";
type RawOptions = NonNullable<Parameters<typeof _electron.launch>[0]>;
export type TestOptions = Omit<RawOptions, "executablePath"> & {
vault: string;
};
export class ObsidianTester {
#loadPromise: Promise<void>;
public electronApp: ElectronApplication | null = null;
public page: Page;
public app: App;
constructor(options: Partial<TestOptions & { vault: string }>) {
const args = [...(options.args || [])];
if (options.vault)
args.push(`obsidian://open?vault=${encodeURI(options.vault)}`);
this.#loadPromise = new Promise((res, rej) => {
electron
.launch({
...options,
args,
executablePath: ObsidianTester.getExe(),
})
.then((v) => {
this.postInit(v).then(() => {
res();
});
})
.catch((e) => rej(e));
});
}
public get vault(): Vault {
return this.app.vault
}
public async doWithApp(
callback: (app: App) => Promise<unknown>
): Promise<unknown> {
await this.#loadPromise;
return await callback(this.app);
}
public async doWithVault(
callback: (vault: Vault) => Promise<unknown>
): Promise<unknown> {
await this.#loadPromise;
return await callback(this.app.vault);
}
public async assertFileEquals(path: string, expectedContent: string, cached: boolean = true) {
await this.#loadPromise;
const fileContent = await this.readFile(path, cached);
expect(fileContent).toEqual(this.normalizeEOL(expectedContent));
}
public async assertLineEquals(path: string, lineNumber: number, expectedContent: string, cached: boolean = true) {
await this.#loadPromise;
const fileContent = await this.readFile(path, cached);
expect(fileContent.split("\n")[lineNumber]).toEqual(this.normalizeEOL(expectedContent));
}
public async assertLinesEqual(filePath: string, start: number, end: number, expected: string, cached: boolean = true) {
await this.#loadPromise;
const fileContent = await this.readFile(filePath, cached);
const lines = fileContent.split("\n").slice(start, end);
const expectedLines = this.normalizeEOL(expected).split("\n");
expect(lines.every((l, i) => l == expectedLines[i])).toEqual(true);
}
public getFile(file: string): TFile {
let f = this.app.vault.getFileByPath(file);
if(!f) {
throw new Error("File does not exist in vault.");
}
return f;
}
normalizeEOL(str: string): string {
return str.split(/\r\n|\r|\n/).join("\n");
}
async readFile(path: string, cached: boolean = true): Promise<string> {
await this.#loadPromise;
const file = this.getFile(path);
return this.normalizeEOL(await (cached ? this.app.vault.cachedRead(file) : this.app.vault.read(file)));
}
private async postInit(electronApp: ElectronApplication) {
this.electronApp = electronApp;
this.page = await this.electronApp.firstWindow();
this.app = await this.page.evaluate<App>("window.app");
}
public static getExe(): string {
if (process.platform == "win32") {
return path.join(process.env.LOCALAPPDATA!, "Obsidian", "Obsidian.exe");
}
if (process.platform == "darwin") {
throw new Error("use a non-toy operating system, dumbass");
}
return execSync("command -v obsidian").toString();
}
}

@ -1,8 +1,5 @@
import { App, TFile } from "obsidian";
import { PlaywrightTestArgs } from "playwright/test";
import { expect } from "vitest";
import { ObsidianTestFixtures } from "./fixtures";
import { test } from "src";
// type TestArgs = Parameters<Parameters<typeof test>[2]>[0];

@ -20,3 +20,7 @@ data.json
# Exclude macOS Finder (System Explorer) View States
.DS_Store
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/

@ -1,4 +1,5 @@
import { defineConfig, devices } from '@playwright/test';
import { ObsidianTestingConfig } from 'obsidian-testing-framework';
/**
* Read environment variables from file.
@ -11,7 +12,7 @@ import { defineConfig, devices } from '@playwright/test';
/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
export default defineConfig<ObsidianTestFixtures>({
testDir: './tests',
/* Run tests in files in parallel */
@ -31,6 +32,7 @@ export default defineConfig({
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},
/* Configure projects for major browsers */

@ -5,6 +5,8 @@
"moduleResolution": "node",
"lib": [
"es2015",
"DOM",
"ES2018"
],
"outDir": "lib",
"baseUrl": ".",

@ -5,6 +5,38 @@ __metadata:
version: 8
cacheKey: 10c0
"@codemirror/language@https://github.com/lishid/cm-language":
version: 6.10.1
resolution: "@codemirror/language@https://github.com/lishid/cm-language.git#commit=2644bfc27afda707a7e1f3aedaf3ca7120f63cd9"
dependencies:
"@codemirror/state": "npm:^6.0.0"
"@codemirror/view": "npm:^6.23.0"
"@lezer/common": "npm:^1.1.0"
"@lezer/highlight": "npm:^1.0.0"
"@lezer/lr": "npm:^1.0.0"
style-mod: "npm:^4.0.0"
checksum: 10c0/03f95ccf8b575f71b71ba7acd9061b3dffdef915936051323a7e7a19d65922ede673f7fdfd2fef51b2667d42df3d01ca0193cb5837694b0b8e19712ac1218c95
languageName: node
linkType: hard
"@codemirror/state@npm:^6.0.0, @codemirror/state@npm:^6.0.1, @codemirror/state@npm:^6.4.0":
version: 6.4.1
resolution: "@codemirror/state@npm:6.4.1"
checksum: 10c0/cdab74d0ca4e262531a257ac419c9c44124f3ace8b0ca1262598a9218fbb6fd8f0afeb4b5ed2f64552a9573a0fc5d55481d4b9b05e9505ef729f9bd0f9469423
languageName: node
linkType: hard
"@codemirror/view@npm:^6.0.1, @codemirror/view@npm:^6.23.0":
version: 6.34.1
resolution: "@codemirror/view@npm:6.34.1"
dependencies:
"@codemirror/state": "npm:^6.4.0"
style-mod: "npm:^4.1.0"
w3c-keyname: "npm:^2.2.4"
checksum: 10c0/cbb562ee7d6a443214e7f7c9822e207b506ef41ce31f331374aa9065931da8b40d6284081e21076579c2e6ea7c2d7b4e7ddf4b12058a667841428d29c5ae9a3e
languageName: node
linkType: hard
"@esbuild/aix-ppc64@npm:0.21.5":
version: 0.21.5
resolution: "@esbuild/aix-ppc64@npm:0.21.5"
@ -341,6 +373,31 @@ __metadata:
languageName: node
linkType: hard
"@lezer/common@npm:^1.0.0, @lezer/common@npm:^1.1.0":
version: 1.2.3
resolution: "@lezer/common@npm:1.2.3"
checksum: 10c0/fe9f8e111080ef94037a34ca2af1221c8d01c1763ba5ecf708a286185c76119509a5d19d924c8842172716716ddce22d7834394670c4a9432f0ba9f3b7c0f50d
languageName: node
linkType: hard
"@lezer/highlight@npm:^1.0.0":
version: 1.2.1
resolution: "@lezer/highlight@npm:1.2.1"
dependencies:
"@lezer/common": "npm:^1.0.0"
checksum: 10c0/51b4c08596a0dfeec6a7b7ed90a7f2743ab42e7e8ff8b89707fd042860e4e133dbd8243639fcaf077305ae6c303aa74e69794015eb16cb34741f5ac6721f283c
languageName: node
linkType: hard
"@lezer/lr@npm:^1.0.0":
version: 1.4.2
resolution: "@lezer/lr@npm:1.4.2"
dependencies:
"@lezer/common": "npm:^1.0.0"
checksum: 10c0/22bb5d0d4b33d0de5eb0706b7e5b5f2d20f570e112d9110009bd35b62ff10f2eb4eff8da4cf373dd4ddf5e06a304120b8f039add7ed9997c981c13945d5329cd
languageName: node
linkType: hard
"@nodelib/fs.scandir@npm:2.1.5":
version: 2.1.5
resolution: "@nodelib/fs.scandir@npm:2.1.5"
@ -1792,6 +1849,9 @@ __metadata:
version: 0.0.0-use.local
resolution: "obsidian-testing-framework@workspace:."
dependencies:
"@codemirror/language": "https://github.com/lishid/cm-language"
"@codemirror/state": "npm:^6.0.1"
"@codemirror/view": "npm:^6.0.1"
"@playwright/test": "npm:^1.48.1"
"@types/node": "npm:^22.7.8"
obsidian: "npm:latest"
@ -2192,6 +2252,13 @@ __metadata:
languageName: node
linkType: hard
"style-mod@npm:^4.0.0, style-mod@npm:^4.1.0":
version: 4.1.2
resolution: "style-mod@npm:4.1.2"
checksum: 10c0/ad4d870b3642b0e42ecc7be0e106dd14b7af11985e34fee8de34e5e38c3214bfc96fa7055acea86d75a3a59ddea3f6a8c6641001a66494d7df72d09685e3fadb
languageName: node
linkType: hard
"tar@npm:^6.1.11, tar@npm:^6.2.1":
version: 6.2.1
resolution: "tar@npm:6.2.1"
@ -2446,6 +2513,13 @@ __metadata:
languageName: node
linkType: hard
"w3c-keyname@npm:^2.2.4":
version: 2.2.8
resolution: "w3c-keyname@npm:2.2.8"
checksum: 10c0/37cf335c90efff31672ebb345577d681e2177f7ff9006a9ad47c68c5a9d265ba4a7b39d6c2599ceea639ca9315584ce4bd9c9fbf7a7217bfb7a599e71943c4c4
languageName: node
linkType: hard
"which@npm:^2.0.1":
version: 2.0.2
resolution: "which@npm:2.0.2"