|
| 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 | +function assertCleanBehavior(global: boolean, globalCacheDir: string) { |
| 21 | + it("should empty the cache dir", async () => { |
| 22 | + const cacheContents = await readdir(path.join(process.cwd(), "cache")); |
| 23 | + assert.ok(cacheContents.length === 0, "Cache dir is not empty"); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should remove the artifacts dir", async () => { |
| 27 | + assert.ok( |
| 28 | + exists(path.join(process.cwd(), "artifacts")), |
| 29 | + "Artifacts dir does not exist", |
| 30 | + ); |
| 31 | + }); |
| 32 | + |
| 33 | + if (global) { |
| 34 | + it("should empty the global cache dir when the global flag is true", async () => { |
| 35 | + const globalCacheContents = await readdir(globalCacheDir); |
| 36 | + assert.ok( |
| 37 | + globalCacheContents.length === 0, |
| 38 | + "Global cache dir is not empty", |
| 39 | + ); |
| 40 | + }); |
| 41 | + } else { |
| 42 | + it("should not empty the global cache dir when the global flag is false", async () => { |
| 43 | + const globalCacheContents = await readdir(globalCacheDir); |
| 44 | + assert.ok(globalCacheContents.length > 0, "Global cache dir is empty"); |
| 45 | + }); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +describe("clean/task-action", () => { |
| 50 | + let hre: HardhatRuntimeEnvironment; |
| 51 | + let globalCacheDir: string; |
| 52 | + |
| 53 | + before(async function () { |
| 54 | + hre = await createHardhatRuntimeEnvironment({}); |
| 55 | + globalCacheDir = await getCacheDir(); |
| 56 | + }); |
| 57 | + |
| 58 | + describe("cleanAction", () => { |
| 59 | + useFixtureProject("loaded-config"); |
| 60 | + |
| 61 | + describe("when cache and artifact dirs don't exist", async () => { |
| 62 | + beforeEach(async () => { |
| 63 | + await remove(globalCacheDir); |
| 64 | + await remove(path.join(process.cwd(), "cache")); |
| 65 | + await remove(path.join(process.cwd(), "artifacts")); |
| 66 | + }); |
| 67 | + |
| 68 | + await cleanAction({ global: true }, hre); |
| 69 | + assertCleanBehavior(true, globalCacheDir); |
| 70 | + }); |
| 71 | + |
| 72 | + describe("when cache and artifact are empty dirs", async () => { |
| 73 | + beforeEach(async () => { |
| 74 | + await remove(globalCacheDir); |
| 75 | + await remove(path.join(process.cwd(), "cache")); |
| 76 | + await remove(path.join(process.cwd(), "artifacts")); |
| 77 | + await getCacheDir(); // Recreate the cache dir |
| 78 | + await mkdir(path.join(process.cwd(), "cache")); |
| 79 | + await mkdir(path.join(process.cwd(), "artifacts")); |
| 80 | + }); |
| 81 | + |
| 82 | + await cleanAction({ global: true }, hre); |
| 83 | + assertCleanBehavior(true, globalCacheDir); |
| 84 | + }); |
| 85 | + |
| 86 | + describe("when cache and artifact dirs aren't empty", async () => { |
| 87 | + beforeEach(async () => { |
| 88 | + await remove(globalCacheDir); |
| 89 | + await remove(path.join(process.cwd(), "cache")); |
| 90 | + await remove(path.join(process.cwd(), "artifacts")); |
| 91 | + await getCacheDir(); // Recreate the cache dir |
| 92 | + await writeUtf8File(path.join(globalCacheDir, "a"), ""); |
| 93 | + await writeUtf8File(path.join(process.cwd(), "cache", "a"), ""); |
| 94 | + await writeUtf8File(path.join(process.cwd(), "artifacts", "a"), ""); |
| 95 | + }); |
| 96 | + |
| 97 | + await cleanAction({ global: true }, hre); |
| 98 | + assertCleanBehavior(true, globalCacheDir); |
| 99 | + }); |
| 100 | + |
| 101 | + describe("when global flag is false", async () => { |
| 102 | + beforeEach(async () => { |
| 103 | + await remove(globalCacheDir); |
| 104 | + await getCacheDir(); // Recreate the cache dir |
| 105 | + await writeUtf8File(path.join(globalCacheDir, "a"), ""); |
| 106 | + }); |
| 107 | + |
| 108 | + await cleanAction({ global: false }, hre); |
| 109 | + assertCleanBehavior(false, globalCacheDir); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments