|
| 1 | +import type { HardhatRuntimeEnvironment } from "@ignored/hardhat-vnext-core/types/hre"; |
| 2 | + |
| 3 | +import assert from "node:assert/strict"; |
| 4 | +import path from "node:path"; |
| 5 | +import { before, beforeEach, describe, it } from "node:test"; |
| 6 | + |
| 7 | +import { getCacheDir } from "@ignored/hardhat-vnext-core/global-dir"; |
| 8 | +import { |
| 9 | + exists, |
| 10 | + mkdir, |
| 11 | + readdir, |
| 12 | + remove, |
| 13 | + writeUtf8File, |
| 14 | +} from "@ignored/hardhat-vnext-utils/fs"; |
| 15 | + |
| 16 | +import { createHardhatRuntimeEnvironment } from "../../../../src/hre.js"; |
| 17 | +import cleanAction from "../../../../src/internal/builtin-plugins/clean/task-action.js"; |
| 18 | +import { useFixtureProject } from "../../../helpers/project.js"; |
| 19 | + |
| 20 | +let hre: HardhatRuntimeEnvironment; |
| 21 | +let globalCacheDir: string; |
| 22 | +let cacheDir: string; |
| 23 | +let artifactsDir: string; |
| 24 | + |
| 25 | +function assertCleanBehavior(global: boolean) { |
| 26 | + it("should clean the cache and artifacts directories", async () => { |
| 27 | + await cleanAction({ global }, hre); |
| 28 | + |
| 29 | + // If the cache dir exists, it should be empty |
| 30 | + if (await exists(cacheDir)) { |
| 31 | + const cacheContents = await readdir(cacheDir); |
| 32 | + assert.ok(cacheContents.length === 0, "Cache dir is not empty"); |
| 33 | + } |
| 34 | + |
| 35 | + // The artifacts dir should not exist |
| 36 | + assert.ok(!(await exists(artifactsDir)), "Artifacts dir exists"); |
| 37 | + |
| 38 | + // If the global cache dir exists, it should be empty if the global flag is |
| 39 | + // true, and not empty otherwise |
| 40 | + if (await exists(globalCacheDir)) { |
| 41 | + const globalCacheContents = await readdir(globalCacheDir); |
| 42 | + if (global) { |
| 43 | + assert.ok( |
| 44 | + globalCacheContents.length === 0, |
| 45 | + "Global cache dir is not empty", |
| 46 | + ); |
| 47 | + } else { |
| 48 | + assert.ok( |
| 49 | + globalCacheContents.length > 0, |
| 50 | + "Global cache dir is empty when it shouldn't be", |
| 51 | + ); |
| 52 | + } |
| 53 | + } |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +describe("clean/task-action", () => { |
| 58 | + describe("cleanAction", () => { |
| 59 | + useFixtureProject("loaded-config"); |
| 60 | + |
| 61 | + before(async function () { |
| 62 | + globalCacheDir = await getCacheDir(); |
| 63 | + cacheDir = path.join(process.cwd(), "cache"); |
| 64 | + artifactsDir = path.join(process.cwd(), "artifacts"); |
| 65 | + hre = await createHardhatRuntimeEnvironment({ |
| 66 | + // TODO remove this once cache and artifacts are resolved in the config |
| 67 | + paths: { cache: cacheDir, artifacts: artifactsDir }, |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe("when cache and artifact dirs don't exist", async () => { |
| 72 | + beforeEach(async () => { |
| 73 | + await remove(globalCacheDir); |
| 74 | + await remove(cacheDir); |
| 75 | + await remove(artifactsDir); |
| 76 | + }); |
| 77 | + |
| 78 | + assertCleanBehavior(true); |
| 79 | + }); |
| 80 | + |
| 81 | + describe("when cache and artifact are empty dirs", async () => { |
| 82 | + beforeEach(async () => { |
| 83 | + await remove(globalCacheDir); |
| 84 | + await remove(cacheDir); |
| 85 | + await remove(artifactsDir); |
| 86 | + await getCacheDir(); // Calling this recreates the cache dir |
| 87 | + await mkdir(cacheDir); |
| 88 | + await mkdir(artifactsDir); |
| 89 | + }); |
| 90 | + |
| 91 | + assertCleanBehavior(true); |
| 92 | + }); |
| 93 | + |
| 94 | + describe("when cache and artifact dirs aren't empty", async () => { |
| 95 | + beforeEach(async () => { |
| 96 | + await remove(globalCacheDir); |
| 97 | + await remove(cacheDir); |
| 98 | + await remove(artifactsDir); |
| 99 | + await getCacheDir(); // Calling this recreates the cache dir |
| 100 | + await writeUtf8File(path.join(globalCacheDir, "a"), ""); |
| 101 | + await writeUtf8File(path.join(cacheDir, "a"), ""); |
| 102 | + await writeUtf8File(path.join(artifactsDir, "a"), ""); |
| 103 | + }); |
| 104 | + |
| 105 | + assertCleanBehavior(true); |
| 106 | + }); |
| 107 | + |
| 108 | + describe("when global flag is false", async () => { |
| 109 | + beforeEach(async () => { |
| 110 | + await remove(globalCacheDir); |
| 111 | + await getCacheDir(); // Calling this recreates the cache dir |
| 112 | + await writeUtf8File(path.join(globalCacheDir, "a"), ""); |
| 113 | + }); |
| 114 | + |
| 115 | + assertCleanBehavior(false); |
| 116 | + }); |
| 117 | + }); |
| 118 | +}); |
0 commit comments