Skip to content

Commit 5f26a5b

Browse files
authored
Merge pull request #5790 from NomicFoundation/galargh/get-tmp-dir
feat: expose getTmpDir helper from the test utils
2 parents 2697d7a + 075bfae commit 5f26a5b

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

v-next/hardhat-test-utils/src/fs.ts

+17-6
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,24 @@ import {
99
remove,
1010
} from "@ignored/hardhat-vnext-utils/fs";
1111

12+
/**
13+
* Create a tmp directory.
14+
* @param nameHint - A hint to use as part of the name of the tmp directory.
15+
*/
16+
export async function getTmpDir(nameHint: string = "tmp-dir"): Promise<string> {
17+
const tmpDirContainer = await getRealPath(tmpdir());
18+
19+
const tmpDir = path.join(tmpDirContainer, `hardhat-tests-${nameHint}`);
20+
// TODO(#5601): Consider adding mkdtemp to hardhat-utils and using it here
21+
await ensureDir(tmpDir);
22+
await emptyDir(tmpDir);
23+
24+
return tmpDir;
25+
}
26+
1227
/**
1328
* Creates a tmp directory before each test, and removes it after each test.
14-
* @param nameHint - A hint to use as part of name of the tmp directory.
29+
* @param nameHint - A hint to use as part of the name of the tmp directory.
1530
*/
1631
export function useTmpDir(nameHint: string = "tmp-dir"): void {
1732
let previousWorkingDir: string;
@@ -20,11 +35,7 @@ export function useTmpDir(nameHint: string = "tmp-dir"): void {
2035
beforeEach(async function () {
2136
previousWorkingDir = process.cwd();
2237

23-
const tmpDirContainer = await getRealPath(tmpdir());
24-
25-
tmpDir = path.join(tmpDirContainer, `hardhat-tests-${nameHint}`);
26-
await ensureDir(tmpDir);
27-
await emptyDir(tmpDir);
38+
tmpDir = await getTmpDir(nameHint);
2839

2940
process.chdir(tmpDir);
3041
});

v-next/hardhat/test/internal/builtin-plugins/console/task-action.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ import type { HardhatRuntimeEnvironment } from "../../../../src/types/hre.js";
22
import type repl from "node:repl";
33

44
import assert from "node:assert/strict";
5-
import fsPromises from "node:fs/promises";
6-
import os from "node:os";
75
import path from "node:path";
86
import { PassThrough } from "node:stream";
97
import { afterEach, before, beforeEach, describe, it } from "node:test";
108

119
import { ensureError } from "@ignored/hardhat-vnext-utils/error";
1210
import { exists, remove } from "@ignored/hardhat-vnext-utils/fs";
13-
import { useFixtureProject } from "@nomicfoundation/hardhat-test-utils";
11+
import {
12+
getTmpDir,
13+
useFixtureProject,
14+
} from "@nomicfoundation/hardhat-test-utils";
1415
import debug from "debug";
1516

1617
import consoleAction from "../../../../src/internal/builtin-plugins/console/task-action.js";
@@ -145,12 +146,9 @@ describe("console/task-action", function () {
145146
let history: string;
146147

147148
beforeEach(async function () {
148-
// TODO(#5601): Use the mkdtemp from hardhat-utils once it's available
149149
// We use a temporary cache dir to avoid conflicts with other tests
150150
// and global user settings.
151-
cacheDir = await fsPromises.mkdtemp(
152-
path.resolve(os.tmpdir(), "console-action-test-"),
153-
);
151+
cacheDir = await getTmpDir("console-action-test");
154152
history = path.resolve(cacheDir, "console-history.txt");
155153
});
156154

0 commit comments

Comments
 (0)