diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..d4f086a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,36 @@ +node_modules +.DS_Store +.idea +.vscode +.npm +.eslintcache +Dockerfile +.git +.gitignore +.env + +# Compilation output +/build-test/ +/dist + +# Coverage +.nyc_output +coverage.json +coverage + +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# node-waf configuration +.lock-wscript diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 1b35bd1..6b66ae6 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -18,21 +18,14 @@ jobs: - 18 - 20 steps: - - uses: actions/setup-node@v3 - with: - node-version: '${{ matrix.node }}' + - name: Checkout the repository + uses: actions/checkout@v3 - - uses: actions/checkout@v3 - - name: 'Cache node_modules' - uses: actions/cache@v3 - with: - path: ~/.npm - key: ${{ runner.os }}-node-v${{ matrix.node }}-${{ hashFiles('**/package.json') }} - restore-keys: | - ${{ runner.os }}-node-v${{ matrix.node }} + - name: Setup Docker Buildx + uses: docker/setup-buildx-action@v2 - - name: Install Dependencies - run: npm install + - name: Build Docker image + run: docker build --build-arg NODE_VERSION=${{ matrix.node }} -t hardhat-zkit-node${{ matrix.node }} --progress=plain . - - name: Run All Tests - run: npm run test + - name: Run tests + run: docker run --rm hardhat-zkit-node${{ matrix.node }} test-local diff --git a/.gitignore b/.gitignore index 6775c8b..d4408ba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,24 +1,22 @@ node_modules .DS_Store .idea +.vscode .eslintcache +.env +.npm # Hardhat files artifacts -coverage.json -coverage - -publish -!publish/package.json # Compilation output /build-test/ /dist +# Coverage .nyc_output - -# 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 +coverage.json +coverage # Logs logs @@ -41,7 +39,5 @@ pids typings/ **/.storage.json -# Optional npm cache directory -.npm - -!src/**/artifacts \ No newline at end of file +# Exclude +!src/**/artifacts diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..89b03eb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +ARG NODE_VERSION=20 + +FROM node:${NODE_VERSION} AS base +WORKDIR /hardhat-zkit + +FROM base AS install +RUN mkdir -p /temp/dev +COPY package.json package-lock.json /temp/dev/ +RUN cd /temp/dev && npm ci + +FROM base AS dev +COPY --from=install /temp/dev/node_modules node_modules +COPY . . + +ENTRYPOINT ["npm", "run"] diff --git a/package.json b/package.json index 4c9762e..4be6422 100644 --- a/package.json +++ b/package.json @@ -37,8 +37,12 @@ "compile": "npm run prepare-tests && npm run build", "build": "tsc --build .", "prepare-tests": "npm run compile --workspaces", - "test": "mocha --recursive 'test/**/*.ts' --exit", - "coverage": "nyc mocha --recursive 'test/**/*.ts' --exit", + "test": "npm run test-docker", + "test-docker": "bash scripts/test-docker.sh", + "test-local": "mocha --recursive 'test/**/*.ts' --exit", + "coverage": "npm run coverage-docker", + "coverage-docker": "bash scripts/coverage-docker.sh", + "coverage-local": "nyc mocha --recursive 'test/**/*.ts' --exit", "clean-tests": "npm run clean --workspaces", "lint-fix": "prettier --write \"./**/*.ts\" && eslint \"{src,test}/**/*.{js,ts}\" --cache --fix", "publish-to-npm": "npm run build && npm run lint-fix && npm publish ./ --access public" diff --git a/scripts/coverage-docker.sh b/scripts/coverage-docker.sh new file mode 100644 index 0000000..530f261 --- /dev/null +++ b/scripts/coverage-docker.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +docker build -t hardhat-zkit --progress=plain . +docker run --rm -v $(pwd)/coverage:/hardhat-zkit/coverage hardhat-zkit coverage-local diff --git a/scripts/test-docker.sh b/scripts/test-docker.sh new file mode 100644 index 0000000..c37bc28 --- /dev/null +++ b/scripts/test-docker.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +docker build -t hardhat-zkit --progress=plain . +docker run --rm hardhat-zkit test-local diff --git a/test/helpers.ts b/test/helpers.ts index bbc5a91..a8f17e6 100644 --- a/test/helpers.ts +++ b/test/helpers.ts @@ -1,4 +1,4 @@ -import { join, dirname } from "path"; +import { join } from "path"; import fsExtra from "fs-extra"; import { resetHardhatContext } from "hardhat/plugins-testing"; @@ -32,10 +32,6 @@ export function useEnvironment(fixtureProjectName: string, withCleanUp: boolean }); } -export function getProjectRootPath(): string { - return dirname(__dirname); -} - export function cleanUp(rootPath: string) { resetCircuitsCompileCache(); resetCircuitsSetupCache(); diff --git a/test/unit/core/compile/circom-compiler.test.ts b/test/unit/core/compile/circom-compiler.test.ts index 2002a0d..0212037 100644 --- a/test/unit/core/compile/circom-compiler.test.ts +++ b/test/unit/core/compile/circom-compiler.test.ts @@ -1,8 +1,9 @@ import fsExtra from "fs-extra"; +import { execSync } from "child_process"; import { expect } from "chai"; -import { getProjectRootPath, useEnvironment } from "@test-helpers"; +import { useEnvironment } from "@test-helpers"; import { getNormalizedFullPath } from "@src/utils/path-utils"; import { CircomCompilerFactory, createCircomCompilerFactory, WASMCircomCompiler } from "@src/core"; @@ -159,14 +160,15 @@ describe("WASMCircomCompiler", () => { }); it("should correctly compile circuit with library include", async function () { - const circuitFullPath: string = getNormalizedFullPath(this.hre.config.paths.root, "circuits/hash2.circom"); - const artifactsFullPath: string = getNormalizedFullPath( - this.hre.config.paths.root, - "zkit/artifacts/test/hash2.circom", - ); + const root = this.hre.config.paths.root; + + execSync("npm install --no-workspaces", { cwd: root }); + + const circuitFullPath: string = getNormalizedFullPath(root, "circuits/hash2.circom"); + const artifactsFullPath: string = getNormalizedFullPath(root, "zkit/artifacts/test/hash2.circom"); const errorFileFullPath: string = getNormalizedFullPath(artifactsFullPath, "errors.log"); - const typesDir: string = getNormalizedFullPath(this.hre.config.paths.root, "generated-types/zkit"); - const nodeModulesPath: string = getNormalizedFullPath(getProjectRootPath(), NODE_MODULES); + const typesDir: string = getNormalizedFullPath(root, "generated-types/zkit"); + const nodeModulesPath: string = getNormalizedFullPath(root, NODE_MODULES); fsExtra.rmSync(artifactsFullPath, { recursive: true, force: true }); fsExtra.mkdirSync(artifactsFullPath, { recursive: true }); @@ -185,6 +187,9 @@ describe("WASMCircomCompiler", () => { expect(fsExtra.readdirSync(artifactsFullPath)).to.be.deep.eq(["hash2.r1cs", "hash2.sym", "hash2_js"]); fsExtra.rmSync(typesDir, { recursive: true, force: true }); + + fsExtra.rmSync(`${root}/node_modules`, { recursive: true, force: true }); + fsExtra.rmSync(`${root}/package-lock.json`, { recursive: true, force: true }); }); });