Skip to content

Commit

Permalink
test: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone committed Dec 11, 2024
1 parent f6d447c commit d31ada5
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 60 deletions.
47 changes: 24 additions & 23 deletions packages/config/src/ConfigManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ZWaveLogContainer } from "@zwave-js/core";
import { fs } from "@zwave-js/core/bindings/fs/node";
import { pathExists } from "@zwave-js/shared";
import fs from "node:fs/promises";
import fsp from "node:fs/promises";
import { tmpdir } from "node:os";
import * as path from "node:path";
import semverInc from "semver/functions/inc.js";
Expand All @@ -25,7 +26,7 @@ const test = baseTest.extend<LocalTestContext>({
async ({}, use) => {
// Setup
const tempDir = path.join(tmpdir(), "zwavejs_test");
await fs.mkdir(tempDir, { recursive: true });
await fsp.mkdir(tempDir, { recursive: true });

const logContainer = new ZWaveLogContainer({ enabled: false });
const logger = new ConfigLogger(logContainer);
Expand All @@ -34,15 +35,15 @@ const test = baseTest.extend<LocalTestContext>({
await use({ tempDir, logContainer, logger });

// Teardown
await fs.rm(tempDir, { recursive: true, force: true });
await fsp.rm(tempDir, { recursive: true, force: true });
},
{ auto: true },
],
});

beforeEach<LocalTestContext>(async ({ context, expect }) => {
await fs.rm(context.tempDir, { recursive: true, force: true });
await fs.mkdir(context.tempDir, { recursive: true });
await fsp.rm(context.tempDir, { recursive: true, force: true });
await fsp.mkdir(context.tempDir, { recursive: true });
});

test.sequential(
Expand All @@ -51,11 +52,11 @@ test.sequential(
const { tempDir, logger } = context;

const configDir = path.join(tempDir, "extconfig");
await syncExternalConfigDir(configDir, logger);
await syncExternalConfigDir(fs, configDir, logger);

expect(await pathExists(configDir)).toBe(true);
expect(await pathExists(fs, configDir)).toBe(true);
expect(
await fs.readFile(path.join(configDir, "version"), "utf8"),
await fsp.readFile(path.join(configDir, "version"), "utf8"),
).toBe(ownVersion);
},
60000,
Expand All @@ -69,19 +70,19 @@ test.sequential(
const configDir = path.join(tempDir, "extconfig");
const otherVersion = semverInc(ownVersion, "major");

await fs.mkdir(configDir, { recursive: true });
await fs.writeFile(
await fsp.mkdir(configDir, { recursive: true });
await fsp.writeFile(
path.join(configDir, "version"),
otherVersion!,
"utf8",
);

await syncExternalConfigDir(configDir, logger);
await syncExternalConfigDir(fs, configDir, logger);

expect(await pathExists(configDir)).toBe(true);
expect(await pathExists(fs, configDir)).toBe(true);

expect(
await fs.readFile(path.join(configDir, "version"), "utf8"),
await fsp.readFile(path.join(configDir, "version"), "utf8"),
).toBe(ownVersion);
},
60000,
Expand All @@ -95,19 +96,19 @@ test.sequential(
const configDir = path.join(tempDir, "extconfig");
const otherVersion = semverInc(ownVersion, "prerelease")!;

await fs.mkdir(configDir, { recursive: true });
await fs.writeFile(
await fsp.mkdir(configDir, { recursive: true });
await fsp.writeFile(
path.join(configDir, "version"),
otherVersion,
"utf8",
);

await syncExternalConfigDir(configDir, logger);
await syncExternalConfigDir(fs, configDir, logger);

expect(await pathExists(configDir)).toBe(true);
expect(await pathExists(fs, configDir)).toBe(true);

expect(
await fs.readFile(path.join(configDir, "version"), "utf8"),
await fsp.readFile(path.join(configDir, "version"), "utf8"),
).toBe(otherVersion);
},
60000,
Expand Down Expand Up @@ -140,7 +141,7 @@ test.sequential(

const cm = new ConfigManager({ logContainer });
await cm.loadAll();
expect(await pathExists(configDir)).toBe(true);
expect(await pathExists(fs, configDir)).toBe(true);

// Load the Aeotec ZW100 Multisensor 6 - we know that it uses multiple imports that could fail validation
const device = await cm.lookupDevice(0x0086, 0x0002, 0x0064);
Expand All @@ -167,7 +168,7 @@ async function testDeviceConfigPriorityDir(

// Set up a dummy structure in the priority dir
const priorityDir = path.join(tempDir, "priority");
await fs.mkdir(path.join(priorityDir, "templates"), { recursive: true });
await fsp.mkdir(path.join(priorityDir, "templates"), { recursive: true });
let json: any = {
manufacturer: "AEON Labs",
manufacturerId: "0x0086",
Expand All @@ -190,7 +191,7 @@ async function testDeviceConfigPriorityDir(
},
],
};
await fs.writeFile(
await fsp.writeFile(
path.join(priorityDir, "aeotec.json"),
JSON.stringify(json, null, 4),
);
Expand All @@ -204,7 +205,7 @@ async function testDeviceConfigPriorityDir(
unsigned: true,
},
};
await fs.writeFile(
await fsp.writeFile(
path.join(priorityDir, "templates/template.json"),
JSON.stringify(json, null, 4),
);
Expand All @@ -217,7 +218,7 @@ async function testDeviceConfigPriorityDir(
await cm.loadAll();

if (useExternalConfig) {
expect(await pathExists(externalConfigDir!)).toBe(true);
expect(await pathExists(fs, externalConfigDir!)).toBe(true);
}

// Load the dummy device
Expand Down
49 changes: 34 additions & 15 deletions packages/config/src/JsonTemplate.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ZWaveErrorCodes, assertZWaveError } from "@zwave-js/core";
import fs from "node:fs/promises";
import { fs } from "@zwave-js/core/bindings/fs/node";
import fsp from "node:fs/promises";
import { tmpdir } from "node:os";
import * as path from "node:path";
import { afterEach, beforeAll, test } from "vitest";
Expand All @@ -8,17 +9,17 @@ import { readJsonWithTemplate } from "./JsonTemplate.js";
const mockDir = path.join(tmpdir(), `zwave-js-template-test`);

async function mockFs(files: Record<string, string>): Promise<void> {
await fs.mkdir(mockDir, { recursive: true });
await fsp.mkdir(mockDir, { recursive: true });
for (const [name, content] of Object.entries(files)) {
const relative = name.replace(/^\//, "./");
const filename = path.join(mockDir, relative);
const dirname = path.join(mockDir, path.dirname(relative));
await fs.mkdir(dirname, { recursive: true });
await fs.writeFile(filename, content);
await fsp.mkdir(dirname, { recursive: true });
await fsp.writeFile(filename, content);

Check failure

Code scanning / CodeQL

Insecure temporary file High

Insecure creation of file in
the os temp dir
.
}
}
mockFs.restore = async (): Promise<void> => {
await fs.rm(mockDir, { recursive: true, force: true });
await fsp.rm(mockDir, { recursive: true, force: true });
};

beforeAll(() => mockFs.restore());
Expand All @@ -36,6 +37,7 @@ test.sequential(
});

const content = await readJsonWithTemplate(
fs,
path.join(mockDir, "test.json"),
);
t.expect(content).toStrictEqual(file);
Expand All @@ -58,6 +60,7 @@ test.sequential(
});

const content = await readJsonWithTemplate(
fs,
path.join(mockDir, "test.json"),
);
t.expect(content).toStrictEqual(template);
Expand All @@ -81,6 +84,7 @@ test.sequential(
});

const content = await readJsonWithTemplate(
fs,
path.join(mockDir, "test.json"),
);
t.expect(content).toStrictEqual(template);
Expand All @@ -106,6 +110,7 @@ test.sequential(
});

const content = await readJsonWithTemplate(
fs,
path.join(mockDir, "test.json"),
);
t.expect(content).toStrictEqual(expected);
Expand All @@ -127,7 +132,7 @@ test.sequential(

await assertZWaveError(
t.expect,
() => readJsonWithTemplate(path.join(mockDir, "test.json")),
() => readJsonWithTemplate(fs, path.join(mockDir, "test.json")),
{
errorCode: ZWaveErrorCodes.Config_Invalid,
},
Expand Down Expand Up @@ -157,7 +162,7 @@ test.sequential(

await assertZWaveError(
t.expect,
() => readJsonWithTemplate(path.join(mockDir, "test.json")),
() => readJsonWithTemplate(fs, path.join(mockDir, "test.json")),
{
errorCode: ZWaveErrorCodes.Config_Invalid,
messageMatches: "Import specifier",
Expand All @@ -184,7 +189,7 @@ test.sequential(

await assertZWaveError(
t.expect,
() => readJsonWithTemplate(path.join(mockDir, "test.json")),
() => readJsonWithTemplate(fs, path.join(mockDir, "test.json")),
{
errorCode: ZWaveErrorCodes.Config_Invalid,
},
Expand Down Expand Up @@ -219,6 +224,7 @@ test.sequential(
});

const content = await readJsonWithTemplate(
fs,
path.join(mockDir, "test.json"),
);
t.expect(content).toStrictEqual(expected);
Expand Down Expand Up @@ -266,6 +272,7 @@ test.sequential(
});

const content = await readJsonWithTemplate(
fs,
path.join(mockDir, "test.json"),
);
t.expect(content).toStrictEqual(expected);
Expand All @@ -289,7 +296,7 @@ test.sequential(

await assertZWaveError(
t.expect,
() => readJsonWithTemplate(path.join(mockDir, "test.json")),
() => readJsonWithTemplate(fs, path.join(mockDir, "test.json")),
{
errorCode: ZWaveErrorCodes.Config_CircularImport,
},
Expand Down Expand Up @@ -318,7 +325,7 @@ test.sequential(

await assertZWaveError(
t.expect,
() => readJsonWithTemplate(path.join(mockDir, "test.json")),
() => readJsonWithTemplate(fs, path.join(mockDir, "test.json")),
{
errorCode: ZWaveErrorCodes.Config_CircularImport,
},
Expand Down Expand Up @@ -360,7 +367,7 @@ test.sequential(

await assertZWaveError(
t.expect,
() => readJsonWithTemplate(path.join(mockDir, "test.json")),
() => readJsonWithTemplate(fs, path.join(mockDir, "test.json")),
{
errorCode: ZWaveErrorCodes.Config_CircularImport,
},
Expand All @@ -384,6 +391,7 @@ test.sequential(
});

const content = await readJsonWithTemplate(
fs,
path.join(mockDir, "foo/bar/test.json"),
);
t.expect(content).toStrictEqual(template);
Expand All @@ -406,6 +414,7 @@ test.sequential(
});

const content = await readJsonWithTemplate(
fs,
path.join(mockDir, "foo/bar/test.json"),
path.join(mockDir, "foo"),
);
Expand All @@ -425,7 +434,11 @@ test.sequential(
});
await assertZWaveError(
t.expect,
() => readJsonWithTemplate(path.join(mockDir, "foo/bar/test.json")),
() =>
readJsonWithTemplate(
fs,
path.join(mockDir, "foo/bar/test.json"),
),
{
messageMatches: "import specifier cannot start with ~/",
errorCode: ZWaveErrorCodes.Config_Invalid,
Expand Down Expand Up @@ -454,6 +467,7 @@ test.sequential(
});

const content = await readJsonWithTemplate(
fs,
path.join(mockDir, "test.json"),
);
t.expect(content).toStrictEqual(expected);
Expand Down Expand Up @@ -487,6 +501,7 @@ test.sequential(
});

const content = await readJsonWithTemplate(
fs,
path.join(mockDir, "test.json"),
);
t.expect(content).toStrictEqual(expected);
Expand Down Expand Up @@ -524,7 +539,7 @@ test.sequential(

await assertZWaveError(
t.expect,
() => readJsonWithTemplate(path.join(mockDir, "test.json")),
() => readJsonWithTemplate(fs, path.join(mockDir, "test.json")),
{
errorCode: ZWaveErrorCodes.Config_CircularImport,
},
Expand All @@ -545,7 +560,7 @@ test.sequential(

await assertZWaveError(
t.expect,
() => readJsonWithTemplate(path.join(mockDir, "test.json")),
() => readJsonWithTemplate(fs, path.join(mockDir, "test.json")),
{},
);
},
Expand All @@ -569,7 +584,7 @@ test.sequential(

await assertZWaveError(
t.expect,
() => readJsonWithTemplate(path.join(mockDir, "test.json")),
() => readJsonWithTemplate(fs, path.join(mockDir, "test.json")),
{
errorCode: ZWaveErrorCodes.Config_CircularImport,
},
Expand Down Expand Up @@ -616,6 +631,7 @@ test.sequential(
};

const content = await readJsonWithTemplate(
fs,
path.join(mockDir, "test.json"),
);
t.expect(content).toStrictEqual(expected);
Expand Down Expand Up @@ -661,6 +677,7 @@ test.sequential(
};

const content = await readJsonWithTemplate(
fs,
path.join(mockDir, "test.json"),
);
t.expect(content).toStrictEqual(expected);
Expand Down Expand Up @@ -699,6 +716,7 @@ test.sequential(
};

const content = await readJsonWithTemplate(
fs,
path.join(mockDir, "test.json"),
);
t.expect(content).toStrictEqual(expected);
Expand All @@ -721,6 +739,7 @@ test.sequential(
t.expect,
() =>
readJsonWithTemplate(
fs,
path.join(mockDir, rootDir, "test.json"),
path.join(mockDir, rootDir),
),
Expand Down
Loading

0 comments on commit d31ada5

Please sign in to comment.