mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-31 12:12:28 +08:00
chore(prettier): fix data_dir.spec.ts
This commit is contained in:
parent
dba0ef4945
commit
cbc10e1f15
@ -1,21 +1,12 @@
|
|||||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||||
|
|
||||||
import type {
|
import type { getTriliumDataDir as getTriliumDataDirType, getDataDirs as getDataDirsType, getPlatformAppDataDir as getPlatformAppDataDirType } from "../src/services/data_dir";
|
||||||
getTriliumDataDir as getTriliumDataDirType,
|
|
||||||
getDataDirs as getDataDirsType,
|
|
||||||
getPlatformAppDataDir as getPlatformAppDataDirType,
|
|
||||||
} from "../src/services/data_dir";
|
|
||||||
|
|
||||||
|
|
||||||
describe("data_dir.ts unit tests", async () => {
|
describe("data_dir.ts unit tests", async () => {
|
||||||
|
|
||||||
|
|
||||||
let getTriliumDataDir: typeof getTriliumDataDirType;
|
let getTriliumDataDir: typeof getTriliumDataDirType;
|
||||||
let getPlatformAppDataDir: typeof getPlatformAppDataDirType;
|
let getPlatformAppDataDir: typeof getPlatformAppDataDirType;
|
||||||
let getDataDirs: typeof getDataDirsType;
|
let getDataDirs: typeof getDataDirsType;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const mockFn = {
|
const mockFn = {
|
||||||
existsSyncMock: vi.fn(),
|
existsSyncMock: vi.fn(),
|
||||||
mkdirSyncMock: vi.fn(),
|
mkdirSyncMock: vi.fn(),
|
||||||
@ -58,7 +49,7 @@ describe("data_dir.ts unit tests", async () => {
|
|||||||
// helper to reset call counts
|
// helper to reset call counts
|
||||||
const resetAllMocks = () => {
|
const resetAllMocks = () => {
|
||||||
Object.values(mockFn).forEach((mockedFn) => {
|
Object.values(mockFn).forEach((mockedFn) => {
|
||||||
mockedFn.mockReset()
|
mockedFn.mockReset();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -69,69 +60,33 @@ describe("data_dir.ts unit tests", async () => {
|
|||||||
mockFn.pathJoinMock.mockImplementation(() => pathJoin);
|
mockFn.pathJoinMock.mockImplementation(() => pathJoin);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
describe("#getPlatformAppDataDir()", () => {
|
describe("#getPlatformAppDataDir()", () => {
|
||||||
|
type TestCaseGetPlatformAppDataDir = [description: string, fnValue: Parameters<typeof getPlatformAppDataDir>, expectedValueFn: (val: ReturnType<typeof getPlatformAppDataDir>) => boolean];
|
||||||
type TestCaseGetPlatformAppDataDir = [
|
|
||||||
description: string,
|
|
||||||
fnValue: Parameters<typeof getPlatformAppDataDir>,
|
|
||||||
expectedValueFn: (val: ReturnType<typeof getPlatformAppDataDir>) => boolean
|
|
||||||
]
|
|
||||||
const testCases: TestCaseGetPlatformAppDataDir[] = [
|
const testCases: TestCaseGetPlatformAppDataDir[] = [
|
||||||
|
["w/ unsupported OS it should return 'null'", ["aix", undefined], (val) => val === null],
|
||||||
|
|
||||||
[
|
["w/ win32 and no APPDATA set it should return 'null'", ["win32", undefined], (val) => val === null],
|
||||||
"w/ unsupported OS it should return 'null'",
|
|
||||||
["aix", undefined],
|
|
||||||
(val) => val === null
|
|
||||||
],
|
|
||||||
|
|
||||||
[
|
["w/ win32 and set APPDATA it should return set 'APPDATA'", ["win32", "AppData"], (val) => val === "AppData"],
|
||||||
"w/ win32 and no APPDATA set it should return 'null'",
|
|
||||||
["win32", undefined],
|
|
||||||
(val) => val === null
|
|
||||||
],
|
|
||||||
|
|
||||||
[
|
["w/ linux it should return '/.local/share'", ["linux", undefined], (val) => val !== null && val.endsWith("/.local/share")],
|
||||||
"w/ win32 and set APPDATA it should return set 'APPDATA'",
|
|
||||||
["win32", "AppData"],
|
|
||||||
(val) => val === "AppData"
|
|
||||||
],
|
|
||||||
|
|
||||||
[
|
["w/ linux and wrongly set APPDATA it should ignore APPDATA and return /.local/share", ["linux", "FakeAppData"], (val) => val !== null && val.endsWith("/.local/share")],
|
||||||
"w/ linux it should return '/.local/share'",
|
|
||||||
["linux", undefined],
|
|
||||||
(val) => val !== null && val.endsWith("/.local/share")
|
|
||||||
],
|
|
||||||
|
|
||||||
[
|
["w/ darwin it should return /Library/Application Support", ["darwin", undefined], (val) => val !== null && val.endsWith("/Library/Application Support")]
|
||||||
"w/ linux and wrongly set APPDATA it should ignore APPDATA and return /.local/share",
|
|
||||||
["linux", "FakeAppData"],
|
|
||||||
(val) => val !== null && val.endsWith("/.local/share")
|
|
||||||
],
|
|
||||||
|
|
||||||
[
|
|
||||||
"w/ darwin it should return /Library/Application Support",
|
|
||||||
["darwin", undefined],
|
|
||||||
(val) => val !== null && val.endsWith("/Library/Application Support")
|
|
||||||
],
|
|
||||||
];
|
];
|
||||||
|
|
||||||
testCases.forEach(testCase => {
|
testCases.forEach((testCase) => {
|
||||||
const [testDescription, value, isExpected] = testCase;
|
const [testDescription, value, isExpected] = testCase;
|
||||||
return it(testDescription, () => {
|
return it(testDescription, () => {
|
||||||
const actual = getPlatformAppDataDir(...value);
|
const actual = getPlatformAppDataDir(...value);
|
||||||
const result = isExpected(actual);
|
const result = isExpected(actual);
|
||||||
expect(result).toBeTruthy()
|
expect(result).toBeTruthy();
|
||||||
|
});
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("#getTriliumDataDir", async () => {
|
describe("#getTriliumDataDir", async () => {
|
||||||
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// make sure these are not set
|
// make sure these are not set
|
||||||
delete process.env.TRILIUM_DATA_DIR;
|
delete process.env.TRILIUM_DATA_DIR;
|
||||||
@ -147,7 +102,6 @@ describe("data_dir.ts unit tests", async () => {
|
|||||||
* case D – fallback to creating Trilium folder in home dir
|
* case D – fallback to creating Trilium folder in home dir
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
describe("case A", () => {
|
describe("case A", () => {
|
||||||
it("when folder exists – it should return the path, without attempting to create the folder", async () => {
|
it("when folder exists – it should return the path, without attempting to create the folder", async () => {
|
||||||
const mockTriliumDataPath = "/home/mock/trilium-data-ENV-A1";
|
const mockTriliumDataPath = "/home/mock/trilium-data-ENV-A1";
|
||||||
@ -309,30 +263,22 @@ describe("data_dir.ts unit tests", async () => {
|
|||||||
expect(mockFn.mkdirSyncMock).toHaveBeenCalledTimes(1);
|
expect(mockFn.mkdirSyncMock).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
describe("#getDataDirs()", () => {
|
describe("#getDataDirs()", () => {
|
||||||
|
const envKeys: Omit<keyof ReturnType<typeof getDataDirs>, "TRILIUM_DATA_DIR">[] = ["DOCUMENT_PATH", "BACKUP_DIR", "LOG_DIR", "ANONYMIZED_DB_DIR", "CONFIG_INI_PATH"];
|
||||||
const envKeys: Omit<keyof ReturnType<typeof getDataDirs>, "TRILIUM_DATA_DIR">[] = [
|
|
||||||
"DOCUMENT_PATH",
|
|
||||||
"BACKUP_DIR",
|
|
||||||
"LOG_DIR",
|
|
||||||
"ANONYMIZED_DB_DIR",
|
|
||||||
"CONFIG_INI_PATH",
|
|
||||||
];
|
|
||||||
|
|
||||||
const setMockedEnv = (prefix: string | null) => {
|
const setMockedEnv = (prefix: string | null) => {
|
||||||
envKeys.forEach(key => {
|
envKeys.forEach((key) => {
|
||||||
if (prefix) {
|
if (prefix) {
|
||||||
process.env[`TRILIUM_${key}`] = `${prefix}_${key}`
|
process.env[`TRILIUM_${key}`] = `${prefix}_${key}`;
|
||||||
} else {
|
} else {
|
||||||
delete process.env[`TRILIUM_${key}`]
|
delete process.env[`TRILIUM_${key}`];
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
it("w/ process.env values present, it should return an object using values from process.env", () => {
|
it("w/ process.env values present, it should return an object using values from process.env", () => {
|
||||||
|
|
||||||
// set mocked values
|
// set mocked values
|
||||||
const mockValuePrefix = "MOCK";
|
const mockValuePrefix = "MOCK";
|
||||||
setMockedEnv(mockValuePrefix);
|
setMockedEnv(mockValuePrefix);
|
||||||
@ -341,12 +287,11 @@ describe("data_dir.ts unit tests", async () => {
|
|||||||
const result = getDataDirs(`${mockValuePrefix}_TRILIUM_DATA_DIR`);
|
const result = getDataDirs(`${mockValuePrefix}_TRILIUM_DATA_DIR`);
|
||||||
|
|
||||||
for (const key in result) {
|
for (const key in result) {
|
||||||
expect(result[key]).toEqual(`${mockValuePrefix}_${key}`)
|
expect(result[key]).toEqual(`${mockValuePrefix}_${key}`);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
it("w/ NO process.env values present, it should return an object using supplied TRILIUM_DATA_DIR as base", () => {
|
it("w/ NO process.env values present, it should return an object using supplied TRILIUM_DATA_DIR as base", () => {
|
||||||
|
|
||||||
// make sure values are undefined
|
// make sure values are undefined
|
||||||
setMockedEnv(null);
|
setMockedEnv(null);
|
||||||
|
|
||||||
@ -357,19 +302,17 @@ describe("data_dir.ts unit tests", async () => {
|
|||||||
const result = getDataDirs(mockDataDir);
|
const result = getDataDirs(mockDataDir);
|
||||||
|
|
||||||
for (const key in result) {
|
for (const key in result) {
|
||||||
expect(result[key].startsWith(mockDataDir)).toBeTruthy()
|
expect(result[key].startsWith(mockDataDir)).toBeTruthy();
|
||||||
}
|
}
|
||||||
|
|
||||||
mockFn.pathJoinMock.mockReset()
|
mockFn.pathJoinMock.mockReset();
|
||||||
|
});
|
||||||
})
|
|
||||||
|
|
||||||
it("should ignore attempts to change a property on the returned object", () => {
|
it("should ignore attempts to change a property on the returned object", () => {
|
||||||
|
|
||||||
// make sure values are undefined
|
// make sure values are undefined
|
||||||
setMockedEnv(null);
|
setMockedEnv(null);
|
||||||
|
|
||||||
const mockDataDirBase = "/home/test/MOCK_TRILIUM_DATA_DIR"
|
const mockDataDirBase = "/home/test/MOCK_TRILIUM_DATA_DIR";
|
||||||
const result = getDataDirs(mockDataDirBase);
|
const result = getDataDirs(mockDataDirBase);
|
||||||
|
|
||||||
// as per MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze#description
|
// as per MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze#description
|
||||||
@ -382,24 +325,21 @@ describe("data_dir.ts unit tests", async () => {
|
|||||||
//@ts-expect-error - attempt to change value of readonly property
|
//@ts-expect-error - attempt to change value of readonly property
|
||||||
result.BACKUP_DIR = "attempt to change";
|
result.BACKUP_DIR = "attempt to change";
|
||||||
return result.BACKUP_DIR;
|
return result.BACKUP_DIR;
|
||||||
|
} catch (error) {
|
||||||
|
return error;
|
||||||
}
|
}
|
||||||
catch(error) {
|
};
|
||||||
return error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const changeAttemptResult = getChangeAttemptResult();
|
const changeAttemptResult = getChangeAttemptResult();
|
||||||
|
|
||||||
if (typeof changeAttemptResult === "string") {
|
if (typeof changeAttemptResult === "string") {
|
||||||
// if it didn't throw above: assert that it did not change the value of it or any other keys of the object
|
// if it didn't throw above: assert that it did not change the value of it or any other keys of the object
|
||||||
for (const key in result) {
|
for (const key in result) {
|
||||||
expect(result[key].startsWith(mockDataDirBase)).toBeTruthy()
|
expect(result[key].startsWith(mockDataDirBase)).toBeTruthy();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
expect(changeAttemptResult).toBeInstanceOf(TypeError)
|
expect(changeAttemptResult).toBeInstanceOf(TypeError);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
})
|
});
|
||||||
})
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user