obsidian-testing-framework 0.3.13 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/fixtures.d.ts CHANGED
@@ -7,5 +7,4 @@ export interface ObsidianTestFixtures {
7
7
  obsidian: ObsidianTestingConfig;
8
8
  ___vaultId: string;
9
9
  appHandle: JSHandle<App>;
10
- vaultPoolDir: string;
11
10
  }
package/lib/index.d.ts CHANGED
@@ -1,13 +1,13 @@
1
+ import { ObsidianTestFixtures } from "./fixtures.js";
2
+ import { Concurrency } from "./internal-util.js";
1
3
  export interface ObsidianTestingConfig {
4
+ vaultPoolDir?: string;
2
5
  vault?: string;
3
6
  obsidianPath?: string;
4
- concurrent?: false | "copy" | "symlink";
7
+ concurrent?: Concurrency;
5
8
  }
6
- export declare const test: import("vitest").TestAPI<{
7
- page: import("playwright-core").Page;
8
- obsidian: ObsidianTestingConfig;
9
- electronApp: import("playwright-core").ElectronApplication;
10
- ___vaultId: string;
11
- appHandle: import("playwright-core").JSHandle<import("obsidian").App>;
12
- vaultPoolDir: string;
9
+ export declare const test: import("vitest").TestAPI<Omit<object, "$__worker"> & Record<ObsidianTestFixtures, K extends never ? object[K] : unknown> & {
10
+ readonly $__worker?: Record<ObsidianTestFixtures, K extends never ? object[K] : unknown>;
11
+ readonly $__file?: unknown;
12
+ readonly $__test?: unknown;
13
13
  }>;
package/lib/index.js CHANGED
@@ -1,89 +1,27 @@
1
1
  import { test as base, inject } from "vitest";
2
2
  import { _electron as electron } from "playwright";
3
3
  import path from "path";
4
- import os from "os";
5
- import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmdirSync, writeFileSync, } from "fs";
4
+ import { readFileSync, } from "fs";
5
+ import { findConfig, generateVaultConfig } from "./internal-util.js";
6
6
  import { pageUtils, waitForIndexingComplete } from "./util.js";
7
7
  import { randomBytes } from "crypto";
8
- import { addConsoleListener, checkToy, getExe } from "./internal-util.js";
9
- import symlinkDir from "symlink-dir";
10
- function findConfig() {
11
- let configLocation;
12
- checkToy();
13
- if (process.platform == "win32") {
14
- configLocation = path.join(`${process.env.APPDATA}`, "Obsidian");
15
- }
16
- else {
17
- configLocation = path.join(`${process.env.XDG_CONFIG_HOME}`, "obsidian");
18
- try {
19
- mkdirSync(configLocation, { recursive: true });
20
- }
21
- catch (e) { }
22
- }
23
- return configLocation;
24
- }
25
- function generateVaultConfig(vault, vaultHash, concurrent, vaultPoolDir) {
26
- let configLocation = findConfig();
27
- console.log("vault is", vault, existsSync(vault));
28
- const obsidianConfigFile = path.join(configLocation, "obsidian.json");
29
- if (!existsSync(obsidianConfigFile)) {
30
- writeFileSync(obsidianConfigFile, JSON.stringify({ vaults: {} }));
31
- }
32
- const json = JSON.parse(readFileSync(obsidianConfigFile).toString());
33
- if (concurrent === "symlink") {
34
- symlinkDir.sync(vault, path.join(vaultPoolDir, vaultHash));
35
- }
36
- else if (concurrent === "copy") {
37
- cpSync(vault, path.join(vaultPoolDir, vaultHash), { recursive: true });
38
- }
39
- if (!Object.values(json.vaults).some((a) => a.path === vault)) {
40
- json.vaults[vaultHash] = {
41
- path: concurrent ? path.join(vaultPoolDir, vaultHash) : vault,
42
- ts: Date.now(),
43
- };
44
- writeFileSync(obsidianConfigFile, JSON.stringify(json, null, "\t"));
45
- writeFileSync(path.join(configLocation, `${vaultHash}.json`), "{}");
46
- return vaultHash;
47
- }
48
- else {
49
- return Object.entries(json.vaults).find((a) => a[1].path === vault)[0];
50
- }
51
- }
8
+ import { addConsoleListener, getExe } from "./internal-util.js";
52
9
  // @ts-ignore some error about a string type now having `undefined` as part of it's union
53
10
  export const test = base.extend({
54
- vaultPoolDir: [
55
- async ({}, run) => {
56
- const concurrent = inject("concurrent") ?? "copy";
57
- if (!concurrent) {
58
- await run("");
59
- return;
60
- }
61
- const prefix = path.join(os.tmpdir(), "obsidian-testing-framework-vault-pool-");
62
- const vaultPoolDir = mkdtempSync(prefix);
63
- console.log("BEFORE", vaultPoolDir);
64
- await run(vaultPoolDir);
65
- console.log("AFTER");
66
- rmdirSync(vaultPoolDir, { recursive: true });
67
- const configFile = path.join(findConfig(), "obsidian.json");
68
- const json = JSON.parse(readFileSync(configFile).toString());
69
- const filtered = Object.fromEntries(Object.entries(json.vaults).filter(([, v]) => !v.path.includes(prefix)));
70
- writeFileSync(configFile, JSON.stringify({ vaults: filtered }));
71
- },
72
- { scope: "file", auto: true },
73
- ],
74
11
  ___vaultId: [
75
12
  async ({}, run) => {
76
13
  process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = "true";
77
14
  const vaultHash = randomBytes(8).toString("hex").toLocaleLowerCase();
78
15
  await run(vaultHash);
79
16
  },
80
- { auto: true },
17
+ { auto: true, scope: "test" },
81
18
  ],
82
19
  electronApp: [
83
- async ({ ___vaultId: vaultId, vaultPoolDir }, run) => {
20
+ async ({ ___vaultId: vaultId }, run) => {
84
21
  const obsidianPath = inject("obsidianPath") ?? undefined;
85
22
  const concurrent = inject("concurrent") ?? "copy";
86
23
  const vault = inject("vault");
24
+ const vaultPoolDir = inject("vaultPoolDir");
87
25
  console.log("asar located at:", getExe(obsidianPath));
88
26
  generateVaultConfig(vault, vaultId, concurrent, vaultPoolDir);
89
27
  const tmp = JSON.parse(readFileSync(path.join(findConfig(), "obsidian.json")).toString());
@@ -1,4 +1,16 @@
1
1
  import { ElectronApplication } from "playwright";
2
+ export type ConfigJson = {
3
+ vaults: {
4
+ [key: string]: {
5
+ path: string;
6
+ ts: number;
7
+ open?: boolean;
8
+ };
9
+ };
10
+ };
11
+ export type Concurrency = false | "copy" | "symlink";
2
12
  export declare function checkToy(): void;
3
13
  export declare function getExe(obsidianPath?: string): string;
4
14
  export declare function addConsoleListener(app: ElectronApplication): void;
15
+ export declare function findConfig(): any;
16
+ export declare function generateVaultConfig(vault: string, vaultHash: string, concurrent: Concurrency, vaultPoolDir: string): string;
@@ -1,5 +1,6 @@
1
1
  import path from "path";
2
- import { existsSync } from "fs";
2
+ import { cpSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
3
+ import symlinkDir from "symlink-dir";
3
4
  export function checkToy() {
4
5
  if (process.platform == "darwin") {
5
6
  throw new Error("use a non-toy operating system, dumbass");
@@ -41,3 +42,45 @@ export function addConsoleListener(app) {
41
42
  console.log(...(await Promise.all(msg.args().map((a) => a.jsonValue()))));
42
43
  });
43
44
  }
45
+ export function findConfig() {
46
+ let configLocation;
47
+ checkToy();
48
+ if (process.platform == "win32") {
49
+ configLocation = path.join(`${process.env.APPDATA}`, "Obsidian");
50
+ }
51
+ else {
52
+ configLocation = path.join(`${process.env.XDG_CONFIG_HOME}`, "obsidian");
53
+ try {
54
+ mkdirSync(configLocation, { recursive: true });
55
+ }
56
+ catch (e) { }
57
+ }
58
+ return configLocation;
59
+ }
60
+ export function generateVaultConfig(vault, vaultHash, concurrent, vaultPoolDir) {
61
+ let configLocation = findConfig();
62
+ console.log("vault is", vault, existsSync(vault));
63
+ const obsidianConfigFile = path.join(configLocation, "obsidian.json");
64
+ if (!existsSync(obsidianConfigFile)) {
65
+ writeFileSync(obsidianConfigFile, JSON.stringify({ vaults: {} }));
66
+ }
67
+ const json = JSON.parse(readFileSync(obsidianConfigFile).toString());
68
+ if (concurrent === "symlink") {
69
+ symlinkDir.sync(vault, path.join(vaultPoolDir, vaultHash));
70
+ }
71
+ else if (concurrent === "copy") {
72
+ cpSync(vault, path.join(vaultPoolDir, vaultHash), { recursive: true });
73
+ }
74
+ if (!Object.values(json.vaults).some((a) => a.path === vault)) {
75
+ json.vaults[vaultHash] = {
76
+ path: concurrent ? path.join(vaultPoolDir, vaultHash) : vault,
77
+ ts: Date.now(),
78
+ };
79
+ writeFileSync(obsidianConfigFile, JSON.stringify(json, null, "\t"));
80
+ writeFileSync(path.join(configLocation, `${vaultHash}.json`), "{}");
81
+ return vaultHash;
82
+ }
83
+ else {
84
+ return Object.entries(json.vaults).find((a) => a[1].path === vault)[0];
85
+ }
86
+ }
package/lib/setup.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { TestProject } from "vitest/node";
2
+ export default function setup(project: TestProject): () => Promise<void>;
package/lib/setup.js ADDED
@@ -0,0 +1,22 @@
1
+ import path from "path";
2
+ import os from "os";
3
+ import { mkdtempSync, rmdirSync, readFileSync, writeFileSync } from "fs";
4
+ import { findConfig } from "./internal-util.js";
5
+ export default function setup(project) {
6
+ const concurrent = project.config.provide.concurrent ?? "copy";
7
+ if (!concurrent) {
8
+ return async () => { };
9
+ }
10
+ const prefix = path.join(os.tmpdir(), "obsidian-testing-framework-vault-pool-");
11
+ const vaultPoolDir = mkdtempSync(prefix);
12
+ project.provide("vaultPoolDir", vaultPoolDir);
13
+ console.log("BEFORE", vaultPoolDir);
14
+ return async () => {
15
+ console.log("AFTER");
16
+ rmdirSync(vaultPoolDir, { recursive: true });
17
+ const configFile = path.join(findConfig(), "obsidian.json");
18
+ const json = JSON.parse(readFileSync(configFile).toString());
19
+ const filtered = Object.fromEntries(Object.entries(json.vaults).filter(([, v]) => !v.path.includes(prefix)));
20
+ writeFileSync(configFile, JSON.stringify({ vaults: filtered }));
21
+ };
22
+ }
package/package.json CHANGED
@@ -24,10 +24,11 @@
24
24
  "exports": {
25
25
  "./fixture": "./lib/fixtures.js",
26
26
  "./util": "./lib/util.js",
27
+ "./setup": "./lib/setup.js",
27
28
  ".": "./lib/index.js"
28
29
  },
29
30
  "readme": "",
30
- "version": "0.3.13",
31
+ "version": "0.4.0",
31
32
  "main": "./lib/index.js",
32
33
  "typings": "./lib/index.d.ts",
33
34
  "repository": {
@@ -43,6 +44,6 @@
43
44
  "devDependencies": {
44
45
  "@types/tmp": "^0",
45
46
  "rimraf": "^6.0.1",
46
- "vitest": "^4.0.18"
47
+ "vitest": "^4.1.0"
47
48
  }
48
49
  }