From 92ed4efff050fb915fe46278195d677cd944afce Mon Sep 17 00:00:00 2001 From: Hatton Date: Thu, 18 Jul 2024 14:30:32 -0600 Subject: [PATCH] Patch up unit tests after change to casing rules --- src/export/consentImdi-edolo.spec.ts | 54 +++++++++++++++++---------- src/export/sessionImdi-custom.spec.ts | 4 +- src/export/sessionImdi-edolo.spec.ts | 4 +- src/other/UserSettings.ts | 38 +++++++++++-------- src/vitest.mock.ts | 7 +++- 5 files changed, 67 insertions(+), 40 deletions(-) diff --git a/src/export/consentImdi-edolo.spec.ts b/src/export/consentImdi-edolo.spec.ts index 178d73d6..cb92d804 100644 --- a/src/export/consentImdi-edolo.spec.ts +++ b/src/export/consentImdi-edolo.spec.ts @@ -11,6 +11,7 @@ import temp from "temp"; import * as fs from "fs-extra"; import * as Path from "path"; import { IMDIMode } from "./ImdiGenerator"; +import { c } from "vitest/dist/reporters-5f784f42.js"; temp.track(); // cleanup on exit: doesn't work @@ -18,6 +19,7 @@ temp.track(); // cleanup on exit: doesn't work wouldn't normally have that file since imdi's don't come with the sample, they need to be generated.*/ let rootDirectory: string; +let lock = false; describe("Consent Form Inclusion", () => { afterAll(() => { try { @@ -26,29 +28,41 @@ describe("Consent Form Inclusion", () => { } catch (e) { // was having trouble cleaning up when running all tests console.warn(`consentImdi-edolo.spec.ts: afterAll: ${e}`); + } finally { + lock = false; } }); beforeAll(async () => { - const project = Project.fromDirectory("sample data/Edolo sample"); - // including "fssync" in the path tells our file copy thing to just do the copy synchronously - rootDirectory = temp.mkdirSync("ImdiBundlerTest-fssync"); - await ImdiBundler.addConsentBundle( - project, - rootDirectory, - "", - [], - IMDIMode.RAW_IMDI, - true, //<-- copy in files - // eslint-disable-next-line @typescript-eslint/no-unused-vars - (f) => true, - true - ); - const xml = fs.readFileSync( - Path.join(rootDirectory, "ConsentDocuments.imdi"), - "utf8" - ); - expect(xml).toBeTruthy(); - setResultXml(xml); + // I was getting errors that appeared to be related to parallel tests + while (lock) { + await new Promise((resolve) => setTimeout(resolve, 100)); + } + + lock = true; + try { + const project = Project.fromDirectory("sample data/Edolo sample"); + // including "fssync" in the path tells our file copy thing to just do the copy synchronously + rootDirectory = temp.mkdirSync("ImdiBundlerTest-fssync"); + await ImdiBundler.addConsentBundle( + project, + rootDirectory, + "", + [], + IMDIMode.RAW_IMDI, + true, //<-- copy in files + // eslint-disable-next-line @typescript-eslint/no-unused-vars + (f) => true, + true + ); + const xml = fs.readFileSync( + Path.join(rootDirectory, "ConsentDocuments.imdi"), + "utf8" + ); + expect(xml).toBeTruthy(); + setResultXml(xml); + } finally { + lock = false; + } }); it("The consent form dummy session to look reasonable", () => { expect(count("METATRANSCRIPT")).toBe(1); diff --git a/src/export/sessionImdi-custom.spec.ts b/src/export/sessionImdi-custom.spec.ts index cfb8ed2a..c5cee7ae 100644 --- a/src/export/sessionImdi-custom.spec.ts +++ b/src/export/sessionImdi-custom.spec.ts @@ -71,9 +71,9 @@ describe("session imdi export", () => { true /*omit namespace*/ ) ); - expect(`//Keys/Key[@Name="Keyword"]`).toHaveSomeMatch("One two UN"); + expect(`//Keys/Key[@Name="Keyword"]`).toHaveSomeMatch("one two UN"); expect(`//Keys/Key[@Name="Keyword"]`).toHaveSomeMatch("FLEx"); - expect(`//Keys/Key[@Name="Keyword"]`).toHaveSomeMatch("Foo"); + expect(`//Keys/Key[@Name="Keyword"]`).toHaveSomeMatch("foo"); expect(`//Keys/Key[@Name="Keyword"]`).toHaveSomeMatch("XYZ"); }); diff --git a/src/export/sessionImdi-edolo.spec.ts b/src/export/sessionImdi-edolo.spec.ts index 5c5d8514..06974cb4 100644 --- a/src/export/sessionImdi-edolo.spec.ts +++ b/src/export/sessionImdi-edolo.spec.ts @@ -134,10 +134,10 @@ it("should make separate keys for each topic separated by a comma", () => { "Incoming" ); expect("METATRANSCRIPT/Session/MDGroup/Content/Keys/Key[2]").toMatch( - "Fishing" + "fishing" ); expect("METATRANSCRIPT/Session/MDGroup/Content/Keys/Key[3]").toMatch( - "Poison" + "poison" ); }); diff --git a/src/other/UserSettings.ts b/src/other/UserSettings.ts index 9c8d144d..6140d9df 100644 --- a/src/other/UserSettings.ts +++ b/src/other/UserSettings.ts @@ -58,30 +58,38 @@ export class UserSettings { if (E2E_USER_SETTINGS_STORE_NAME?.length > 0) { name = E2E_USER_SETTINGS_STORE_NAME; } else { - // at some point during 2.3 beta, we broke the file name here, - // leaving it named ".json" - // So here we fix it up if we find that. - const brokenPath = Path.join(app.getPath("userData"), ".json"); - if (fs.existsSync(brokenPath)) { - const correctPath = Path.join(app.getPath("userData"), name + ".json"); - try { - if (!fs.existsSync(correctPath)) { - fs.renameSync(brokenPath, correctPath); - } else { - fs.rmSync(brokenPath); // we have two files, bad and good names. Remove incorrectly named one (note, it could be the more recent, ah well) + if (app.getPath) { + // false in unit tests + // at some point during 2.3 beta, we broke the file name here, + // leaving it named ".json" + // So here we fix it up if we find that. + const brokenPath = Path.join(app.getPath("userData"), ".json"); + if (fs.existsSync(brokenPath)) { + const correctPath = Path.join( + app.getPath("userData"), + name + ".json" + ); + try { + if (!fs.existsSync(correctPath)) { + fs.renameSync(brokenPath, correctPath); + } else { + fs.rmSync(brokenPath); // we have two files, bad and good names. Remove incorrectly named one (note, it could be the more recent, ah well) + } + } catch (e) { + // ah well } - } catch (e) { - // ah well } } } this.store = - process.env.NODE_ENV === "test" || E2E_USER_SETTINGS_STORE_NAME === "none" // like we're running for the first time + process.env == undefined || // unit tests + process.env.NODE_ENV === "test" || + E2E_USER_SETTINGS_STORE_NAME === "none" // like we're running for the first time ? new FakeStore() : new Store({ name: name }); - this.sendErrors = process.env.NODE_ENV === "production"; // developer has a menu that can toggle this + this.sendErrors = process.env?.NODE_ENV === "production"; // developer has a menu that can toggle this this.imdiMode = this.store.get("imdiMode") || false; this.paradisecMode = this.store.get("paradisecMode") || false; diff --git a/src/vitest.mock.ts b/src/vitest.mock.ts index 776cf2be..8d5dc390 100644 --- a/src/vitest.mock.ts +++ b/src/vitest.mock.ts @@ -1,6 +1,11 @@ +import { app } from "electron"; import { beforeAll, vi } from "vitest"; -vi.mock("@electron/remote", () => ({ exec: vi.fn(), process: vi.fn() })); //See commit msg for info +vi.mock("@electron/remote", () => ({ + exec: vi.fn(), + process: vi.fn(), + app: vi.fn() +})); //See commit msg for info // // getTestEnvironment: () => ({ // E2E: false,