Skip to content

Commit

Permalink
first working prototype test version
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherDedominici committed Nov 22, 2023
1 parent a792e82 commit 8e27b46
Show file tree
Hide file tree
Showing 19 changed files with 2,736 additions and 0 deletions.
112 changes: 112 additions & 0 deletions e2e-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Node modules
/node_modules

# Compilation output
/build-test/
/dist

# Code coverage artifacts
/coverage
/.nyc_output

/*.js
/*.js.map
/*.d.ts
/*.d.ts.map
/builtin-tasks
/common
/internal
/types
/utils

# Below is Github's node gitignore template,
# ignoring the node_modules part, as it'd ignore every node_modules, and we have some for testing

# Logs
logs
*.log

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
#node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'pnpm pack'
*.tgz

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

test/fixture-projects/*/artifacts
test/fixture-projects/*/cache

node_modules
.env

# Hardhat files
/cache
/artifacts

# TypeChain files
/typechain
/typechain-types

# solidity-coverage files
/coverage
/coverage.json
5 changes: 5 additions & 0 deletions e2e-tests/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": "ts-node/register/files",
"ignore": ["test/fixture-projects/**/*"],
"timeout": 60000
}
7 changes: 7 additions & 0 deletions e2e-tests/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/node_modules
/dist
/test/fixture-projects/**/artifacts
/test/fixture-projects/**/artifacts-dir
/test/fixture-projects/**/cache
CHANGELOG.md
.nyc_output
Empty file added e2e-tests/CHANGELOG.md
Empty file.
21 changes: 21 additions & 0 deletions e2e-tests/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Nomic Foundation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Empty file added e2e-tests/README.md
Empty file.
117 changes: 117 additions & 0 deletions e2e-tests/helpers/project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { execSync } from "child_process";
import * as fs from "fs";
import path from "path";

/**
* This helper adds mocha hooks to run the tests inside one of the projects
* from test/fixture-projects.
*
* @param projectName The base name of the folder with the project to use.
* @param artifacts The artifacts required to run the tests. The artifacts should be named according
* to the 'name' property mentioned in the package.json of the artifact.
*/
export function useFixtureProject(projectName: string, artifacts: string[]) {
let projectPath: string;
let prevWorkingDir: string;

before(async () => {
projectPath = await getFixtureProjectPath(projectName);
});

before(() => {
prevWorkingDir = process.cwd();
process.chdir(projectPath);

// Copy the artifacts to the project test folder
copyArtifactsInTestProject(projectPath, artifacts);

// Install the dependencies and the artifacts in the project test folder
installDependenciesAndArtifacts(projectPath, artifacts);
});

after(() => {
process.chdir(prevWorkingDir);
});
}

export async function getFixtureProjectPath(
projectName: string
): Promise<string> {
const normalizedProjectName = projectName.replaceAll("/", path.sep);

const projectPath = path.join(
__dirname,
"..",
"test",
"fixture-projects",
normalizedProjectName
);

if (!fs.existsSync(projectPath)) {
throw new Error(`Fixture project ${projectName} doesn't exist`);
}

return getRealPath(projectPath);
}

/**
* Returns the real path of absolutePath, resolving symlinks.
*
* @throws FileNotFoundError if absolutePath doesn't exist.
*/
export async function getRealPath(absolutePath: string): Promise<string> {
const fsPromises = fs.promises;

try {
// This method returns the actual casing.
// Please read Node.js' docs to learn more.
return await fsPromises.realpath(path.normalize(absolutePath));
} catch (e: any) {
if (e.code === "ENOENT") {
// eslint-disable-next-line @nomicfoundation/hardhat-internal-rules/only-hardhat-error
console.error("Path doesn't exist:", absolutePath);
throw e;
}

// eslint-disable-next-line @nomicfoundation/hardhat-internal-rules/only-hardhat-error
console.error("Cannot access to project path:", absolutePath);
throw e;
}
}

/**
* Copy the necessary artifacts from the artifacts folder to the test folder to run the tests
*/
function copyArtifactsInTestProject(projectPath: string, artifacts: string[]) {
const artifactsPath = path.join(__dirname, "..", "artifacts");

fs.readdirSync(artifactsPath).forEach((file) => {
for (const artifact of artifacts) {
// Adjust the name to match the prefix generated by the 'pnpm pack' command
const artifactPrefixName = artifact
.replaceAll("@", "")
.replaceAll("/", "-");

if (file.startsWith(artifactPrefixName)) {
// Overwrite the file if already exists
fs.copyFileSync(`${artifactsPath}/${file}`, `${projectPath}/${file}`);
break;
}
}
});
}

function installDependenciesAndArtifacts(
projectPath: string,
artifacts: string[]
) {
// Install the artifacts
fs.readdirSync(projectPath).forEach((file) => {
if (file.endsWith(".tgz")) {
execSync(`npm install ${file}`, { encoding: "utf-8" });
}
});

// Install dependencies mentioned in the package.json of the project
execSync("npm install", { encoding: "utf-8" });
}
51 changes: 51 additions & 0 deletions e2e-tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "e2e-tests",
"version": "1.0.0",
"description": "__________",
"homepage": "__________",
"repository": "github:nomicfoundation/hardhat",
"author": "Nomic Foundation",
"license": "MIT",
"main": "__________",
"types": "__________",
"keywords": [
"__________"
],
"scripts": {
"lint": "pnpm prettier --check && pnpm eslint",
"lint:fix": "pnpm prettier --write && pnpm eslint --fix",
"eslint": "eslint 'src/**/*.ts' 'test/**/*.ts'",
"prettier": "prettier \"**/*.{js,md,json}\"",
"coverage": "nyc pnpm test -- --reporter min",
"build": "tsc --build .",
"prepublishOnly": "pnpm build",
"clean": "rimraf dist",
"pretest": "",
"test-pkg": "mocha --recursive \"test/packages/$PKG_NAME/**/*.ts\" -c --exit",
"add-hardhat-core-pkg": "pnpm add ./hardhat-2.19.1.tgz --no-save",
"test-hardhat-core": "PKG_NAME=hardhat-core pnpm test-pkg"
},
"files": [
"dist/src/",
"src/",
"LICENSE",
"README.md"
],
"devDependencies": {
"@types/chai": "^4.2.0",
"@types/mocha": ">=9.1.0",
"@types/node": "^16.0.0",
"@typescript-eslint/eslint-plugin": "5.61.0",
"@typescript-eslint/parser": "5.61.0",
"chai": "^4.2.0",
"eslint": "^8.44.0",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-import": "2.27.5",
"eslint-plugin-no-only-tests": "3.0.0",
"eslint-plugin-prettier": "3.4.0",
"mocha": "^10.0.0",
"prettier": "2.4.1",
"ts-node": "^10.8.0",
"typescript": "~5.0.0"
}
}
Loading

0 comments on commit 8e27b46

Please sign in to comment.