Skip to content

Commit

Permalink
util: makeTmpDir test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed Feb 9, 2024
1 parent 2a9e3be commit cb42cd3
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 116 deletions.
8 changes: 5 additions & 3 deletions integ/browser-tests/tests/segmented-object/test.t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "./api";

import { makeObjectBody } from "@ndn/segmented-object/test-fixture/object-body";
import { delay, sha256, toHex } from "@ndn/util";
import { deleteTmpFiles, writeTmpFile } from "@ndn/util/test-fixture/tmpfile";
import { makeTmpDir } from "@ndn/util/test-fixture/tmp";
import { beforeAll, beforeEach, expect, test } from "vitest";

import { navigateToPage, page, pageInvoke } from "../../test-fixture/pptr";
Expand All @@ -13,8 +13,10 @@ let filename: string;
beforeAll(async () => {
objectBody = makeObjectBody(128 * 1024);
objectBodyDigest = toHex(await sha256(objectBody));
filename = writeTmpFile(objectBody);
return deleteTmpFiles;

const tmpDir = makeTmpDir();
filename = tmpDir.createFile(objectBody);
return tmpDir[Symbol.dispose];
});

beforeEach(() => navigateToPage(import.meta.url));
Expand Down
11 changes: 5 additions & 6 deletions integ/cxx-tests/tests/keychain/verify/test.t.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Certificate, EcCurve, ECDSA, generateSigningKey, RSA, RsaModulusLength, type SigningAlgorithm } from "@ndn/keychain";
import { Data } from "@ndn/packet";
import { Encoder } from "@ndn/tlv";
import { deleteTmpFiles, writeTmpFile } from "@ndn/util/test-fixture/tmpfile";
import { afterEach, expect, test } from "vitest";
import { makeTmpDir } from "@ndn/util/test-fixture/tmp";
import { expect, test } from "vitest";

import { execute } from "../../../test-fixture/cxxprogram";

afterEach(deleteTmpFiles);

type Row<G> = [
desc: string,
algo: SigningAlgorithm<any, true, G>,
Expand All @@ -27,8 +25,9 @@ test.each(TABLE)("%s", async (desc, algo, genParam) => {
const packet = new Data("/D", Uint8Array.of(0xC0, 0xC1));
await privateKey.sign(packet);

const certFile = writeTmpFile(Encoder.encode(cert.data));
const packetFile = writeTmpFile(Encoder.encode(packet));
using tmpDir = makeTmpDir();
const certFile = tmpDir.createFile(Encoder.encode(cert.data));
const packetFile = tmpDir.createFile(Encoder.encode(packet));
const { stdout } = await execute(import.meta.url, [certFile, packetFile]);

const [certOk, packetOk] = stdout.split("\n");
Expand Down
4 changes: 0 additions & 4 deletions pkg/cli-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,5 @@
"env-var": "^7.4.1",
"tslib": "^2.6.2",
"wtfnode": "^0.9.1"
},
"devDependencies": {
"@types/tmp": "^0.2.6",
"tmp": "^0.2.1"
}
}
11 changes: 6 additions & 5 deletions pkg/cli-common/tests/basic.t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ import { generateSigningKey, KeyChain } from "@ndn/keychain";
import { FakeNfd } from "@ndn/nfdmgmt/test-fixture/prefix-reg";
import { Data, type Name } from "@ndn/packet";
import { Closers } from "@ndn/util";
import { dirSync as tmpDir } from "tmp";
import { makeTmpDir } from "@ndn/util/test-fixture/tmp";
import { afterAll, expect, test } from "vitest";

const closers = new Closers();
afterAll(closers.close);

const tmpKeyChain = tmpDir({ unsafeCleanup: true });
afterAll(tmpKeyChain.removeCallback);
const tmpDir = makeTmpDir();
closers.push(tmpDir); // `using tmpDir` seems to cause premature cleanup
const keyChainDir = tmpDir.join("keychain");
let signerName: Name;
{
const keyChain = KeyChain.open(tmpKeyChain.name);
const keyChain = KeyChain.open(keyChainDir);
const [signerPvt] = await generateSigningKey(keyChain, "/key-signer");
signerName = signerPvt.name;
}
process.env.NDNTS_KEYCHAIN = tmpKeyChain.name;
process.env.NDNTS_KEYCHAIN = keyChainDir;
process.env.NDNTS_KEY = "/key-signer";

const nfd = await new FakeNfd().open();
Expand Down
4 changes: 1 addition & 3 deletions pkg/keychain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
},
"devDependencies": {
"@types/b64-lite": "^1.4.2",
"@types/tmp": "^0.2.6",
"b64-lite": "^1.4.0",
"tmp": "^0.2.1"
"b64-lite": "^1.4.0"
}
}
33 changes: 13 additions & 20 deletions pkg/keychain/tests/store/keychain.t.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import "@ndn/packet/test-fixture/expect";

import { Component, Data, digestSigning, KeyLocator, Name } from "@ndn/packet";
import { dirSync as tmpDir } from "tmp";
import { beforeAll, beforeEach, describe, expect, test, vi } from "vitest";
import { makeTmpDir } from "@ndn/util/test-fixture/tmp";
import { beforeAll, describe, expect, test, vi } from "vitest";

import { Certificate, CertNaming, generateSigningKey, KeyChain, type NamedSigner, type NamedVerifier, SigningAlgorithmListFull, ValidityPeriod } from "../..";
import * as TestCertStore from "../../test-fixture/cert-store";
Expand All @@ -20,25 +20,18 @@ test("temp CertStore", async () => {
TestCertStore.check(record);
});

describe("persistent", () => {
let locator: string;
beforeEach(async () => {
const d = tmpDir({ unsafeCleanup: true });
locator = d.name;
return d.removeCallback;
});

test("persistent KeyStore", async () => {
const keyChain = KeyChain.open(locator, SigningAlgorithmListFull);
const record = await TestKeyStore.execute(keyChain);
TestKeyStore.check(record);
});
test("persistent KeyStore", async () => {
using tmpDir = makeTmpDir();
const keyChain = KeyChain.open(tmpDir.name, SigningAlgorithmListFull);
const record = await TestKeyStore.execute(keyChain);
TestKeyStore.check(record);
});

test("persistent CertStore", async () => {
const keyChain = KeyChain.open(locator, SigningAlgorithmListFull);
const record = await TestCertStore.execute(keyChain);
TestCertStore.check(record);
});
test("persistent CertStore", async () => {
using tmpDir = makeTmpDir();
const keyChain = KeyChain.open(tmpDir.name, SigningAlgorithmListFull);
const record = await TestCertStore.execute(keyChain);
TestCertStore.check(record);
});

describe("getSigner", () => {
Expand Down
4 changes: 0 additions & 4 deletions pkg/ndnsec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,5 @@
"@yoursunny/asn1": "0.0.20200718",
"execa": "^8.0.1",
"tslib": "^2.6.2"
},
"devDependencies": {
"@types/tmp": "^0.2.6",
"tmp": "^0.2.1"
}
}
17 changes: 6 additions & 11 deletions pkg/ndnsec/tests/keychain.t.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import * as TestCertStore from "@ndn/keychain/test-fixture/cert-store";
import * as TestKeyStore from "@ndn/keychain/test-fixture/key-store";
import { dirSync as tmpDir } from "tmp";
import { beforeEach, test } from "vitest";
import { makeTmpDir } from "@ndn/util/test-fixture/tmp";
import { test } from "vitest";

import { NdnsecKeyChain } from "..";

let home: string;
beforeEach(() => {
const { name, removeCallback } = tmpDir({ unsafeCleanup: true });
home = name;
return removeCallback;
});

test.runIf(NdnsecKeyChain.supported)("KeyStore", async () => {
using tmpDir = makeTmpDir();
const enabled: TestKeyStore.Enable = { HMAC: false, Ed25519: false };
const keyChain = new NdnsecKeyChain({ home });
const keyChain = new NdnsecKeyChain({ home: tmpDir.name });
const record = await TestKeyStore.execute(keyChain, enabled);
TestKeyStore.check(record, enabled);
}, 20000);

test.runIf(NdnsecKeyChain.supported)("CertStore", async () => {
const keyChain = new NdnsecKeyChain({ home });
using tmpDir = makeTmpDir();
const keyChain = new NdnsecKeyChain({ home: tmpDir.name });
const record = await TestCertStore.execute(keyChain);
TestCertStore.check(record);
}, 20000);
4 changes: 1 addition & 3 deletions pkg/node-transport/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@
"@ndn/fw": "workspace:*",
"@ndn/packet": "workspace:*",
"@ndn/util": "workspace:*",
"@types/tmp": "^0.2.6",
"@types/url-format-lax": "^2.0.3",
"@types/url-parse-lax": "^5.0.2",
"streaming-iterables": "^8.0.1",
"tmp": "^0.2.1"
"streaming-iterables": "^8.0.1"
}
}
11 changes: 9 additions & 2 deletions pkg/node-transport/test-fixture/net-server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type EventEmitter, once } from "node:events";
import net from "node:net";

import { tmpNameSync } from "tmp";
import { makeTmpDir, type TmpDir } from "@ndn/util/test-fixture/tmp";

/**
* Transport test server.
Expand Down Expand Up @@ -127,7 +127,14 @@ export class IpcServer extends NetServer {
/** Unix/IPC server path. */
public readonly path = process.platform === "win32" ?
`//./pipe/2a8370be-8abc-448f-bb09-54d8b243cf7a/${Math.trunc(Math.random() * 0x100000000)}` :
tmpNameSync();
(this.tmpDir = makeTmpDir()).filename();

private tmpDir?: TmpDir;

public override [Symbol.asyncDispose](): Promise<void> {
this.tmpDir?.[Symbol.dispose]();
return super[Symbol.asyncDispose]();
}

protected override listenBegin(): void {
this.server.listen(this.path);
Expand Down
2 changes: 0 additions & 2 deletions pkg/repo-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@
},
"devDependencies": {
"@ndn/node-transport": "workspace:*",
"@types/tmp": "^0.2.6",
"stream-mock": "^2.0.5",
"tmp": "^0.2.1",
"type-fest": "^4.10.2"
}
}
25 changes: 9 additions & 16 deletions pkg/repo-api/tests/data-tape.t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { BufferBreaker } from "@ndn/node-transport/test-fixture/buffer-breaker";
import { Data, Interest, Name } from "@ndn/packet";
import { Encoder } from "@ndn/tlv";
import { Closers, delay, randomJitter } from "@ndn/util";
import { makeTmpDir } from "@ndn/util/test-fixture/tmp";
import { BufferReadableMock, BufferWritableMock } from "stream-mock";
import { collect } from "streaming-iterables";
import { dirSync as tmpDir } from "tmp";
import { beforeEach, describe, expect, test, vi } from "vitest";

import { BulkInsertInitiator, BulkInsertTarget, copy, DataTape } from "..";
Expand Down Expand Up @@ -65,23 +65,16 @@ describe("DataTape reader", () => {
});
});

describe("DataTape file", () => {
let dir: string;
beforeEach(() => {
const d = tmpDir({ unsafeCleanup: true });
dir = d.name;
return d.removeCallback;
});
test("DataTape file copy", async () => {
using tmpDir = makeTmpDir();

test("copy", async () => {
const tapeA = new DataTape(`${dir}/A.dtar`);
await copy(new DataTape(makeDataTapeReadStream), tapeA);
await expect(collect(tapeA.listNames())).resolves.toHaveLength(500);
const tapeA = new DataTape(tmpDir.join("A.dtar"));
await copy(new DataTape(makeDataTapeReadStream), tapeA);
await expect(collect(tapeA.listNames())).resolves.toHaveLength(500);

const tapeB = new DataTape(`${dir}/B.dtar`);
await copy(tapeA, new Name("/A/2"), tapeB);
await expect(collect(tapeB.listNames())).resolves.toHaveLength(100);
});
const tapeB = new DataTape(tmpDir.join("B.dtar"));
await copy(tapeA, new Name("/A/2"), tapeB);
await expect(collect(tapeB.listNames())).resolves.toHaveLength(100);
});

async function testBulkInsertTarget(
Expand Down
2 changes: 1 addition & 1 deletion pkg/repo-external/tests/pyrepo.t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Endpoint } from "@ndn/endpoint";
import { Forwarder } from "@ndn/fw";
import { Segment } from "@ndn/naming-convention2";
import { Data, digestSigning, Name } from "@ndn/packet";
import { makeTmpDir } from "@ndn/util/test-fixture/tmpfile";
import { makeTmpDir } from "@ndn/util/test-fixture/tmp";
import { expect, test } from "vitest";

import { PyRepoStore } from "..";
Expand Down
7 changes: 4 additions & 3 deletions pkg/segmented-object/tests/serve-fetch.t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Bridge } from "@ndn/l3face";
import { Segment2, Segment3 } from "@ndn/naming-convention2";
import { Data, FwHint, Name, type Verifier } from "@ndn/packet";
import { Closers, delay } from "@ndn/util";
import { deleteTmpFiles, writeTmpFile } from "@ndn/util/test-fixture/tmpfile";
import { makeTmpDir } from "@ndn/util/test-fixture/tmp";
import { BufferReadableMock, BufferWritableMock } from "stream-mock";
import { collect, consume } from "streaming-iterables";
import { afterEach, beforeAll, beforeEach, describe, expect, test, vi } from "vitest";
Expand Down Expand Up @@ -75,8 +75,9 @@ test("stream to stream", async () => {
describe("file source", () => {
let filename: string;
beforeAll(() => {
filename = writeTmpFile(objectBody);
return deleteTmpFiles;
const tmpDir = makeTmpDir();
filename = tmpDir.createFile(objectBody);
return tmpDir[Symbol.dispose];
});

test("file to buffer", async () => {
Expand Down
47 changes: 47 additions & 0 deletions pkg/util/test-fixture/tmp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import path from "node:path";

import { dirSync as tmpDir, tmpNameSync as tmpName } from "tmp";
import { sync as write } from "write";

/** Create a temporary directory. */
export function makeTmpDir(): TmpDir {
const { name, removeCallback } = tmpDir({ prefix: "tmp-NDNts-", unsafeCleanup: true });
const filename = () => tmpName({ dir: name });
return {
name,
join: (...segments) => path.join(name, ...segments),
filename,
createFile: (content) => {
const fn = filename();
write(fn, content);
return fn;
},
[Symbol.dispose]: removeCallback,
};
}

/**
* Temporary directory.
*
* @remarks
* Disposing this object deletes the directory and its content.
*/
export interface TmpDir extends Disposable {
/** Directory path. */
readonly name: string;

/** Join with additional path segment(s). */
join: (...segments: string[]) => string;

/**
* Generate random filename within the directory.
* @returns Full filename.
*/
filename: () => string;

/**
* Write content to a file within the directory.
* @returns Full filename.
*/
createFile: (content: string | Uint8Array) => string;
}
33 changes: 0 additions & 33 deletions pkg/util/test-fixture/tmpfile.ts

This file was deleted.

0 comments on commit cb42cd3

Please sign in to comment.