Skip to content

Commit

Permalink
Merge pull request #5790 from NomicFoundation/galargh/get-tmp-dir
Browse files Browse the repository at this point in the history
feat: expose getTmpDir helper from the test utils
  • Loading branch information
galargh authored Oct 1, 2024
2 parents 2697d7a + 075bfae commit 5f26a5b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
23 changes: 17 additions & 6 deletions v-next/hardhat-test-utils/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,24 @@ import {
remove,
} from "@ignored/hardhat-vnext-utils/fs";

/**
* Create a tmp directory.
* @param nameHint - A hint to use as part of the name of the tmp directory.
*/
export async function getTmpDir(nameHint: string = "tmp-dir"): Promise<string> {
const tmpDirContainer = await getRealPath(tmpdir());

const tmpDir = path.join(tmpDirContainer, `hardhat-tests-${nameHint}`);
// TODO(#5601): Consider adding mkdtemp to hardhat-utils and using it here
await ensureDir(tmpDir);
await emptyDir(tmpDir);

return tmpDir;
}

/**
* Creates a tmp directory before each test, and removes it after each test.
* @param nameHint - A hint to use as part of name of the tmp directory.
* @param nameHint - A hint to use as part of the name of the tmp directory.
*/
export function useTmpDir(nameHint: string = "tmp-dir"): void {
let previousWorkingDir: string;
Expand All @@ -20,11 +35,7 @@ export function useTmpDir(nameHint: string = "tmp-dir"): void {
beforeEach(async function () {
previousWorkingDir = process.cwd();

const tmpDirContainer = await getRealPath(tmpdir());

tmpDir = path.join(tmpDirContainer, `hardhat-tests-${nameHint}`);
await ensureDir(tmpDir);
await emptyDir(tmpDir);
tmpDir = await getTmpDir(nameHint);

process.chdir(tmpDir);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import type { HardhatRuntimeEnvironment } from "../../../../src/types/hre.js";
import type repl from "node:repl";

import assert from "node:assert/strict";
import fsPromises from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { PassThrough } from "node:stream";
import { afterEach, before, beforeEach, describe, it } from "node:test";

import { ensureError } from "@ignored/hardhat-vnext-utils/error";
import { exists, remove } from "@ignored/hardhat-vnext-utils/fs";
import { useFixtureProject } from "@nomicfoundation/hardhat-test-utils";
import {
getTmpDir,
useFixtureProject,
} from "@nomicfoundation/hardhat-test-utils";
import debug from "debug";

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

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

Expand Down

0 comments on commit 5f26a5b

Please sign in to comment.