Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KyrylR committed Dec 6, 2024
1 parent 8b12e87 commit ba85175
Show file tree
Hide file tree
Showing 18 changed files with 76 additions and 49 deletions.
6 changes: 4 additions & 2 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { join } from "path";
import { join, sep } from "path";

import { HardhatRuntimeEnvironment } from "hardhat/types";

import { UNKNOWN_CONTRACT_NAME } from "../constants";

export function resolvePathToFile(hre: HardhatRuntimeEnvironment, path: string, file: string = ""): string {
return join(join(hre.config.paths.root, path), file);
const pathToMigration = join(hre.config.paths.root, path, file);

return pathToMigration.endsWith(sep) ? pathToMigration.slice(0, -1) : pathToMigration;
}

export function getInstanceNameFromClass(instance: any): string {
Expand Down
2 changes: 2 additions & 0 deletions test/fixture-projects/hardhat-project-mock-files/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/cache
/artifacts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Deployer } from "../../../../src/deployer/Deployer";

export = async (deployer: Deployer) => {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Deployer } from "../../../../src/deployer/Deployer";

export = async (deployer: Deployer) => {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Deployer } from "../../../../src/deployer/Deployer";

export = async (deployer: Deployer) => {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Deployer } from "../../../../src/deployer/Deployer";

export = async (deployer: Deployer) => {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Deployer } from "../../../../src/deployer/Deployer";

export = async (deployer: Deployer) => {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Deployer } from "../../../../src/deployer/Deployer";

export = async (deployer: Deployer) => {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import config from "../hardhat.config";

const defaultConfig = {
...config,
};

export default defaultConfig;
8 changes: 8 additions & 0 deletions test/fixture-projects/hardhat-project-mock-files/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "hardhat-project-mock-files",
"version": "1.0.0",
"scripts": {
"compile": "",
"clean": ""
}
}
5 changes: 4 additions & 1 deletion test/unit/files.ts → test/integration/tools/files.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { assert } from "chai";

import { resolvePathToFile } from "../../src/utils";
import { useEnvironment } from "../../helpers";
import { resolvePathToFile } from "../../../src/utils";

describe("ResolvePathToFile()", () => {
useEnvironment("typechain-ethers");

it("should correctly resolve paths with and without slash", () => {
const firstResolvedPath = resolvePathToFile(require("hardhat"), __dirname + "/deploy-files/");

Expand Down
61 changes: 33 additions & 28 deletions test/unit/migrator.ts → test/integration/tools/migrator.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
import { assert } from "chai";

import { Migrator } from "../../src/migrator/Migrator";

function getMigratorInstance(from: number = -1, to: number = -1, only: number = -1, skip: number = -1): any {
import { useEnvironment } from "../../helpers";
import { Migrator } from "../../../src/migrator/Migrator";
import { HardhatRuntimeEnvironment } from "hardhat/types";

function getMigratorInstance(
hre: HardhatRuntimeEnvironment,
from: number = -1,
to: number = -1,
only: number = -1,
skip: number = -1,
): any {
return new Migrator({
...hre,
config: {
...hre.config,
migrate: {
from: from,
to: to,
only: only,
skip: skip,
verify: false,
confirmations: 0,
skipVerificationErrors: [],
attempts: 0,
pathToMigrations: "./deploy-files",
force: false,
continuePreviousDeployment: false,
wait: 0,
verificationDelay: 0,
verifyParallel: 0,
verifyAttempts: 0,
continue: false,
transactionStatusCheckInterval: 0,
},
},
network: {
provider: {},
},
} as any);
});
}

describe("Migrator", function () {
describe("getMigrationFiles()", function () {
before("Loading chdir", function () {
process.chdir(__dirname);
});

describe("from/to", function () {
useEnvironment("mock-files");

it("should correctly return specified migrations", function () {
const instance = getMigratorInstance(3, 5, -1, -1);
const instance = getMigratorInstance(require("hardhat"), 3, 5, -1, -1);
const migrationFiles = instance._migrationFiles;

const expectedFiles = ["3_mock.migration.ts", "4_mock.migration.ts", "5_mock.migration.ts"];
Expand All @@ -43,21 +50,25 @@ describe("Migrator", function () {
});

describe("from/to/only", () => {
useEnvironment("mock-files");

it("should return only one migration", () => {
const instance = getMigratorInstance(3, 5, 4, -1);
const instance = getMigratorInstance(require("hardhat"), 3, 5, 4, -1);
const migrationFiles = instance._migrationFiles;

assert.deepStrictEqual(migrationFiles, ["4_mock.migration.ts"]);
});

it("should return no migration if only specified out of from/to range", () => {
assert.throw(() => getMigratorInstance(3, 5, 1, -1), "No migration files were found.");
assert.throw(() => getMigratorInstance(require("hardhat"), 3, 5, 1, -1), "No migration files were found.");
});
});

describe("from/to/only/skip", () => {
useEnvironment("mock-files");

it("should skip migrations", () => {
const instance = getMigratorInstance(-1, 5, -1, 2);
const instance = getMigratorInstance(require("hardhat"), -1, 5, -1, 2);
const migrationFiles = instance._migrationFiles;

const expectedFiles = [
Expand All @@ -71,25 +82,19 @@ describe("Migrator", function () {
});

it("should return only migration if there is a no collision between them", () => {
const instance = getMigratorInstance(3, 4, 4, 3);
const instance = getMigratorInstance(require("hardhat"), 3, 4, 4, 3);
const migrationFiles = instance._migrationFiles;

assert.deepStrictEqual(migrationFiles, ["4_mock.migration.ts"]);
});

it("should skip all migrations with only parameter specified", () => {
assert.throw(() => getMigratorInstance(1, 5, 2, 2), "No migration files were found.");
assert.throw(() => getMigratorInstance(require("hardhat"), 1, 5, 2, 2), "No migration files were found.");
});

it("should skip all migrations without only parameter specified", () => {
assert.throw(() => getMigratorInstance(3, 3, -1, 3), "No migration files were found.");
assert.throw(() => getMigratorInstance(require("hardhat"), 3, 3, -1, 3), "No migration files were found.");
});
});
});

describe("migrate()", () => {
before("Loading chdir", function () {
process.chdir(__dirname);
});
});
});
3 changes: 0 additions & 3 deletions test/unit/deploy-files/-1_mock.migration.ts

This file was deleted.

3 changes: 0 additions & 3 deletions test/unit/deploy-files/1_mock.migration.ts

This file was deleted.

3 changes: 0 additions & 3 deletions test/unit/deploy-files/2_mock.migration.ts

This file was deleted.

3 changes: 0 additions & 3 deletions test/unit/deploy-files/3_mock.migration.ts

This file was deleted.

3 changes: 0 additions & 3 deletions test/unit/deploy-files/4_mock.migration.ts

This file was deleted.

3 changes: 0 additions & 3 deletions test/unit/deploy-files/5_mock.migration.ts

This file was deleted.

0 comments on commit ba85175

Please sign in to comment.