diff --git a/packages/config/src/ConfigManager.test.ts b/packages/config/src/ConfigManager.test.ts index cadb240d39a4..01305cca3bff 100644 --- a/packages/config/src/ConfigManager.test.ts +++ b/packages/config/src/ConfigManager.test.ts @@ -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"; @@ -25,7 +26,7 @@ const test = baseTest.extend({ 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); @@ -34,15 +35,15 @@ const test = baseTest.extend({ 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(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( @@ -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, @@ -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, @@ -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, @@ -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); @@ -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", @@ -190,7 +191,7 @@ async function testDeviceConfigPriorityDir( }, ], }; - await fs.writeFile( + await fsp.writeFile( path.join(priorityDir, "aeotec.json"), JSON.stringify(json, null, 4), ); @@ -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), ); @@ -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 diff --git a/packages/config/src/JsonTemplate.test.ts b/packages/config/src/JsonTemplate.test.ts index 0534aebe806e..05fa57765fda 100644 --- a/packages/config/src/JsonTemplate.test.ts +++ b/packages/config/src/JsonTemplate.test.ts @@ -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"; @@ -8,17 +9,17 @@ import { readJsonWithTemplate } from "./JsonTemplate.js"; const mockDir = path.join(tmpdir(), `zwave-js-template-test`); async function mockFs(files: Record): Promise { - 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); } } mockFs.restore = async (): Promise => { - await fs.rm(mockDir, { recursive: true, force: true }); + await fsp.rm(mockDir, { recursive: true, force: true }); }; beforeAll(() => mockFs.restore()); @@ -36,6 +37,7 @@ test.sequential( }); const content = await readJsonWithTemplate( + fs, path.join(mockDir, "test.json"), ); t.expect(content).toStrictEqual(file); @@ -58,6 +60,7 @@ test.sequential( }); const content = await readJsonWithTemplate( + fs, path.join(mockDir, "test.json"), ); t.expect(content).toStrictEqual(template); @@ -81,6 +84,7 @@ test.sequential( }); const content = await readJsonWithTemplate( + fs, path.join(mockDir, "test.json"), ); t.expect(content).toStrictEqual(template); @@ -106,6 +110,7 @@ test.sequential( }); const content = await readJsonWithTemplate( + fs, path.join(mockDir, "test.json"), ); t.expect(content).toStrictEqual(expected); @@ -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, }, @@ -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", @@ -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, }, @@ -219,6 +224,7 @@ test.sequential( }); const content = await readJsonWithTemplate( + fs, path.join(mockDir, "test.json"), ); t.expect(content).toStrictEqual(expected); @@ -266,6 +272,7 @@ test.sequential( }); const content = await readJsonWithTemplate( + fs, path.join(mockDir, "test.json"), ); t.expect(content).toStrictEqual(expected); @@ -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, }, @@ -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, }, @@ -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, }, @@ -384,6 +391,7 @@ test.sequential( }); const content = await readJsonWithTemplate( + fs, path.join(mockDir, "foo/bar/test.json"), ); t.expect(content).toStrictEqual(template); @@ -406,6 +414,7 @@ test.sequential( }); const content = await readJsonWithTemplate( + fs, path.join(mockDir, "foo/bar/test.json"), path.join(mockDir, "foo"), ); @@ -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, @@ -454,6 +467,7 @@ test.sequential( }); const content = await readJsonWithTemplate( + fs, path.join(mockDir, "test.json"), ); t.expect(content).toStrictEqual(expected); @@ -487,6 +501,7 @@ test.sequential( }); const content = await readJsonWithTemplate( + fs, path.join(mockDir, "test.json"), ); t.expect(content).toStrictEqual(expected); @@ -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, }, @@ -545,7 +560,7 @@ test.sequential( await assertZWaveError( t.expect, - () => readJsonWithTemplate(path.join(mockDir, "test.json")), + () => readJsonWithTemplate(fs, path.join(mockDir, "test.json")), {}, ); }, @@ -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, }, @@ -616,6 +631,7 @@ test.sequential( }; const content = await readJsonWithTemplate( + fs, path.join(mockDir, "test.json"), ); t.expect(content).toStrictEqual(expected); @@ -661,6 +677,7 @@ test.sequential( }; const content = await readJsonWithTemplate( + fs, path.join(mockDir, "test.json"), ); t.expect(content).toStrictEqual(expected); @@ -699,6 +716,7 @@ test.sequential( }; const content = await readJsonWithTemplate( + fs, path.join(mockDir, "test.json"), ); t.expect(content).toStrictEqual(expected); @@ -721,6 +739,7 @@ test.sequential( t.expect, () => readJsonWithTemplate( + fs, path.join(mockDir, rootDir, "test.json"), path.join(mockDir, rootDir), ), diff --git a/packages/config/src/Manufacturers.test.ts b/packages/config/src/Manufacturers.test.ts index 8aa75123cd31..1ea08bb036cd 100644 --- a/packages/config/src/Manufacturers.test.ts +++ b/packages/config/src/Manufacturers.test.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-restricted-globals */ import { pathExists } from "@zwave-js/shared"; import { readFile } from "node:fs/promises"; import { test, vi } from "vitest"; @@ -54,7 +55,9 @@ const pathExistsStub = vi.mocked(pathExists); pathExistsStub.mockClear(); readFileStub.mockClear(); pathExistsStub.mockResolvedValue(true); - readFileStub.mockResolvedValue(`{"0x000e": ` as any); + readFileStub.mockResolvedValue( + Buffer.from(`{"0x000e": `, "utf8"), + ); const configManager = new ConfigManager(); await configManager.loadManufacturers(); @@ -89,9 +92,12 @@ const pathExistsStub = vi.mocked(pathExists); pathExistsStub.mockClear(); pathExistsStub.mockResolvedValue(true); readFileStub.mockResolvedValue( - JSON.stringify({ - "0x000e": "Test", - }) as any, + Buffer.from( + JSON.stringify({ + "0x000e": "Test", + }), + "utf8", + ), ); const configManager = new ConfigManager(); diff --git a/packages/nvmedit/src/convert.test.ts b/packages/nvmedit/src/convert.test.ts index b13fd1daae87..49a80973455d 100644 --- a/packages/nvmedit/src/convert.test.ts +++ b/packages/nvmedit/src/convert.test.ts @@ -1,6 +1,6 @@ +import { fs } from "@zwave-js/core/bindings/fs/node"; import { readJSON } from "@zwave-js/shared"; import { cloneDeep } from "@zwave-js/shared/safe"; -import fs from "node:fs"; import fsp from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; @@ -31,7 +31,7 @@ function bufferEquals( const suite = "700-series, binary to JSON"; const fixturesDir = path.join(__dirname, "../test/fixtures/nvm_700_binary"); - const files = fs.readdirSync(fixturesDir); + const files = await fsp.readdir(fixturesDir); for (const file of files) { test(`${suite} -> ${file}`, async (t) => { @@ -46,11 +46,12 @@ function bufferEquals( const suite = "700 series, JSON to NVM to JSON round-trip"; const fixturesDir = path.join(__dirname, "../test/fixtures/nvm_700_json"); - const files = fs.readdirSync(fixturesDir); + const files = await fsp.readdir(fixturesDir); for (const file of files) { test(`${suite} -> ${file}`, async (t) => { const jsonInput: NVMJSON = await readJSON( + fs, path.join(fixturesDir, file), ); const nvm = await jsonToNVM( @@ -73,7 +74,7 @@ function bufferEquals( __dirname, "../test/fixtures/nvm_700_invariants", ); - const files = fs.readdirSync(fixturesDir); + const files = await fsp.readdir(fixturesDir); for (const file of files) { test(`${suite} -> ${file}`, async (t) => { @@ -92,7 +93,7 @@ function bufferEquals( const suite = "500-series, binary to JSON"; const fixturesDir = path.join(__dirname, "../test/fixtures/nvm_500_binary"); - const files = fs.readdirSync(fixturesDir); + const files = await fsp.readdir(fixturesDir); for (const file of files) { test(`${suite} -> ${file}`, async (t) => { @@ -110,7 +111,7 @@ function bufferEquals( __dirname, "../test/fixtures/nvm_500_invariants", ); - const files = fs.readdirSync(fixturesDir); + const files = await fsp.readdir(fixturesDir); // For debugging purposes // function toHex(buffer: Buffer): string { @@ -148,11 +149,12 @@ function bufferEquals( const suite = "500 to 700 series JSON conversion"; const fixturesDir = path.join(__dirname, "../test/fixtures/nvm_500_json"); - const files = fs.readdirSync(fixturesDir); + const files = await fsp.readdir(fixturesDir); for (const file of files) { test(`${suite} -> ${file}`, async (t) => { const json500: NVM500JSON = await readJSON( + fs, path.join(fixturesDir, file), ); const json700 = json500To700(json500, true); @@ -165,11 +167,12 @@ function bufferEquals( const suite = "500 to 700 to 500 series JSON round-trip"; const fixturesDir = path.join(__dirname, "../test/fixtures/nvm_500_json"); - const files = fs.readdirSync(fixturesDir); + const files = await fsp.readdir(fixturesDir); for (const file of files) { test(`${suite} -> ${file}`, async (t) => { const json500: NVM500JSON = await readJSON( + fs, path.join(fixturesDir, file), ); const json700 = json500To700(json500, true); diff --git a/packages/zwave-js/src/lib/test/integrationTestSuite.ts b/packages/zwave-js/src/lib/test/integrationTestSuite.ts index 91e818bd40ba..55bb3c7d648f 100644 --- a/packages/zwave-js/src/lib/test/integrationTestSuite.ts +++ b/packages/zwave-js/src/lib/test/integrationTestSuite.ts @@ -1,3 +1,4 @@ +import { fs } from "@zwave-js/core/bindings/fs/node"; import { type ZWaveSerialStream } from "@zwave-js/serial"; import type { MockPort } from "@zwave-js/serial/mock"; import { copyFilesRecursive, noop } from "@zwave-js/shared"; @@ -9,7 +10,7 @@ import { } from "@zwave-js/testing"; import { wait } from "alcalzone-shared/async"; import crypto from "node:crypto"; -import fs from "node:fs/promises"; +import fsp from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { type TestContext, test } from "vitest"; @@ -87,12 +88,12 @@ function suite( } // Make sure every test is starting fresh - await fs.rm(cacheDir, { recursive: true, force: true }).catch(noop); - await fs.mkdir(cacheDir, { recursive: true }); + await fsp.rm(cacheDir, { recursive: true, force: true }).catch(noop); + await fsp.mkdir(cacheDir, { recursive: true }); // And potentially provision the cache if (provisioningDirectory) { - await copyFilesRecursive(provisioningDirectory, cacheDir); + await copyFilesRecursive(fs, provisioningDirectory, cacheDir); } ({ driver, continueStartup, mockPort, serial } = await prepareDriver( @@ -171,7 +172,7 @@ function suite( await driver.destroy(); if (!debug) { - await fs.rm(cacheDir, { recursive: true, force: true }) + await fsp.rm(cacheDir, { recursive: true, force: true }) .catch(noop); } }); diff --git a/packages/zwave-js/src/lib/test/integrationTestSuiteMulti.ts b/packages/zwave-js/src/lib/test/integrationTestSuiteMulti.ts index 52f387a41382..6c0136250e38 100644 --- a/packages/zwave-js/src/lib/test/integrationTestSuiteMulti.ts +++ b/packages/zwave-js/src/lib/test/integrationTestSuiteMulti.ts @@ -1,3 +1,4 @@ +import { fs } from "@zwave-js/core/bindings/fs/node"; import { type ZWaveSerialStream } from "@zwave-js/serial"; import type { MockPort } from "@zwave-js/serial/mock"; import { copyFilesRecursive, noop } from "@zwave-js/shared"; @@ -9,7 +10,7 @@ import { } from "@zwave-js/testing"; import { wait } from "alcalzone-shared/async"; import crypto from "node:crypto"; -import fs from "node:fs/promises"; +import fsp from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { type TestContext, test } from "vitest"; @@ -87,12 +88,12 @@ function suite( } // Make sure every test is starting fresh - await fs.rm(cacheDir, { recursive: true, force: true }).catch(noop); - await fs.mkdir(cacheDir, { recursive: true }); + await fsp.rm(cacheDir, { recursive: true, force: true }).catch(noop); + await fsp.mkdir(cacheDir, { recursive: true }); // And potentially provision the cache if (provisioningDirectory) { - await copyFilesRecursive(provisioningDirectory, cacheDir); + await copyFilesRecursive(fs, provisioningDirectory, cacheDir); } ({ driver, continueStartup, mockPort, serial } = await prepareDriver( @@ -173,7 +174,7 @@ function suite( await driver.destroy(); if (!debug) { - await fs.rm(cacheDir, { recursive: true, force: true }) + await fsp.rm(cacheDir, { recursive: true, force: true }) .catch(noop); } });