Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre-commit hook definition with test #596

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/PRE-COMMIT-HOOK-TEST.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: PRE-COMMIT

on:
push:
branches:
- master
- develop
tags:
- '*'
pull_request:
paths:
- .github/workflows/PRE-COMMIT-HOOK-TEST.yml
- .pre-commit-hooks.yaml
- e2e/pre-commit-hook/*
- package.json

jobs:
hook-test:
runs-on: ubuntu-latest
name: Test Hook
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install pre-commit
run: |
python -m pip install pre-commit
- name: Test hooks
run: |
./e2e/pre-commit-hook/test.sh
6 changes: 6 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- id: solhint
name: solhint
description: Lint Solidity files with Solhint
entry: solhint
types: ["solidity"]
language: node
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@ Or disable all validations for a group of lines:
### Solhint has an official Docker Image
Go to docker folder and follow [this](docker/docker.md) instructions.

## pre-commit
### Solhint can also be used as [pre-commit](https://pre-commit.com/) hook

Replace `$GIT_TAG` with real tag:

```YAML
- repo: https://github.com/protofire/solhint
rev: $GIT_TAG
hooks:
- id: solhint
```

## Documentation
Related documentation you may find [here](https://protofire.github.io/solhint/).

Expand Down
3 changes: 3 additions & 0 deletions e2e/pre-commit-hook/.solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "solhint:recommended"
}
14 changes: 14 additions & 0 deletions e2e/pre-commit-hook/Counter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.26;

contract Counter {
uint256 public number;

function setNumber(uint256 newNumber) public {
number = newNumber;
}

function increment() public {
number++;
}
}
31 changes: 31 additions & 0 deletions e2e/pre-commit-hook/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

set -o errtrace -o nounset -o pipefail -o errexit

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"

# Create temp working directory for mock repo
MOCK_REPO=$(mktemp -d)
if [[ ! "$MOCK_REPO" || ! -d "$MOCK_REPO" ]]; then
echo "Could not create temp dir"
exit 1
fi
function cleanup {
echo "Deleting temp working directory $MOCK_REPO"
rm -rf "$MOCK_REPO"
}

trap cleanup EXIT

# Filling the mock repo
pushd "$MOCK_REPO" >/dev/null || exit 1
git init --initial-branch=master
git config user.email "[email protected]"
git config user.name "pre-commit test"
cp "$SCRIPT_DIR/.solhint.json" "$SCRIPT_DIR/Counter.sol" .
git add .
git commit -m "Initial commit"

# Run pre-commit inside the mock repo while referencing the solhint directory,
# where the .pre-commit-hooks.yaml is located.
pre-commit try-repo "$SCRIPT_DIR/../.." solhint --verbose --color=always --all-files
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"lint": "eslint .",
"generate-rulesets": "node scripts/generate-rulesets.js && prettier --write conf/rulesets",
"docs": "node scripts/generate-rule-docs.js",
"prepare": "husky install",
"prepublishOnly": "npm run lint && npm run test && npm run generate-rulesets"
},
"bin": {
Expand Down