Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
Add go-copyright pc hook and updated existing custom hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
l50 committed Apr 12, 2024
1 parent 0230d3c commit 7dc1cfe
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 62 deletions.
1 change: 0 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ binary_next_line = true # like -bn
switch_case_indent = true # like -ci
space_redirects = true # like -sr
keep_padding = true # like -kp
function_next_line = true # like -fn
4 changes: 3 additions & 1 deletion .hooks/generate-docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ source "${bashutils_path}"
rr || exit 1

# Check if mage is installed
if ! command -v mage > /dev/null 2>&1; then
if command -v mage > /dev/null 2>&1; then
echo "mage is installed"
else
echo -e "mage is not installed\n"
echo -e "Please install mage by running the following command:\n"
echo -e "go install github.com/magefile/mage@latest\n"
Expand Down
33 changes: 33 additions & 0 deletions .hooks/go-copyright.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash
set -ex

copyright_header='/*
Copyright © 2024-present, Meta Platforms, Inc. and affiliates
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.
*/'

while IFS= read -r -d '' file; do
if ! grep -qF "$copyright_header" "$file"; then
echo "Adding copyright header to ${file}"
temp_file=$(mktemp)
echo "${copyright_header}" > "${temp_file}"
# Add an empty line after the copyright header
echo "" >> "${temp_file}"
cat "${file}" >> "${temp_file}"
mv "${temp_file}" "${file}"
fi
done < <(find . -type f -name "*.go" -print0)
6 changes: 2 additions & 4 deletions .hooks/go-licenses.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#!/bin/bash

# Function to check if go mod vendor should run or not
run_vendor()
{
run_vendor() {
echo "Running go mod vendor..."
go mod vendor
}

# Function to check licenses
check_licenses()
{
check_licenses() {
action=$1

go install github.com/google/go-licenses@latest
Expand Down
8 changes: 4 additions & 4 deletions .hooks/go-unit-tests.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

set -ex
set -x

TESTS_TO_RUN=$1
RETURN_CODE=0
Expand All @@ -17,15 +17,15 @@ if [[ "${TESTS_TO_RUN}" == 'coverage' ]]; then
-tags=integration -coverprofile=coverage-all.out ./...
RETURN_CODE=$?
elif [[ "${TESTS_TO_RUN}" == 'all' ]]; then
go test -v -count=1 -race ./...
go test -v -race -failfast ./...
RETURN_CODE=$?
elif [[ "${TESTS_TO_RUN}" == 'short' ]] \
&& [[ "${GITHUB_ACTIONS}" != "true" ]]; then
go test -v -count=1 -short -race ./...
go test -v -short -failfast -race ./...
RETURN_CODE=$?
else
if [[ "${GITHUB_ACTIONS}" != 'true' ]]; then
go test -v -count=1 -race "./.../${TESTS_TO_RUN}"
go test -v -race -failfast "./.../${TESTS_TO_RUN}"
RETURN_CODE=$?
fi
fi
Expand Down
11 changes: 9 additions & 2 deletions .hooks/go-vet.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#!/bin/bash
set -e
set -ex

pkgs=$(go list ./...)

for pkg in $pkgs; do
dir="$(basename "$pkg")/"
if [[ "${dir}" != .*/ ]] && [[ "${dir}" != "magefiles/" ]]; then
if [[ "${dir}" != ".hooks/" ]] \
&& [[ "${dir}" != ".github/" ]] \
&& [[ "${dir}" != "bin/" ]] \
&& [[ "${dir}" != "docs/" ]] \
&& [[ "${dir}" != "logging/" ]] \
&& [[ "${dir}" != "modules/" ]] \
&& [[ "${dir}" != "resources/" ]] \
&& [[ "${dir}" != "templates/" ]]; then
go vet "${pkg}"
fi
done
26 changes: 1 addition & 25 deletions .hooks/run-go-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@ RETURN_CODE=0

TIMESTAMP=$(date +"%Y%m%d%H%M%S")
LOGFILE="/tmp/goutils-unit-test-results-$TIMESTAMP.log"
MODULE_ROOT=$(go list -m -f "{{.Dir}}")

if [[ -z "${TESTS_TO_RUN}" ]]; then
echo "No tests input" | tee -a "$LOGFILE"
echo "Example - Run all shorter collection of tests: bash run-go-tests.sh short" | tee -a "$LOGFILE"
echo "Example - Run all tests: bash run-go-tests.sh all" | tee -a "$LOGFILE"
echo "Example - Run coverage for a specific version: bash run-go-tests.sh coverage" | tee -a "$LOGFILE"
echo "Example - Run tests for modified files: bash run-go-tests.sh modified" | tee -a "$LOGFILE"
exit 1
fi

run_tests()
{
run_tests() {
local coverage_file=$1
repo_root=$(git rev-parse --show-toplevel 2> /dev/null) || exit
pushd "${repo_root}" || exit
Expand All @@ -34,27 +31,6 @@ run_tests()
go test -v -race -failfast ./... 2>&1 | tee -a "$LOGFILE"
elif [[ "${TESTS_TO_RUN}" == 'short' ]] && [[ "${GITHUB_ACTIONS}" != "true" ]]; then
go test -v -short -failfast -race ./... 2>&1 | tee -a "$LOGFILE"
elif [[ "${TESTS_TO_RUN}" == 'modified' ]]; then
# Run tests for modified files
local modified_files
IFS=$'\n' read -r -a modified_files <<< "$(git diff --name-only --cached | grep '\.go$')"

local pkg_dirs=()

for file in "${modified_files[@]}"; do
local pkg_dir
pkg_dir=$(dirname "$file")
pkg_dir=${pkg_dir#"$MODULE_ROOT/"}
pkg_dirs+=("$pkg_dir")
done

# Remove duplicate package directories
IFS=$'\n' read -r -a pkg_dirs <<< "$(sort -u <<< "${pkg_dirs[*]}")"
unset IFS

for dir in "${pkg_dirs[@]}"; do
go test -v -race -failfast "./$dir/..." 2>&1 | tee -a "$LOGFILE"
done
else
if [[ "${GITHUB_ACTIONS}" != 'true' ]]; then
go test -v -failfast -race "./.../${TESTS_TO_RUN}" 2>&1 | tee -a "$LOGFILE"
Expand Down
59 changes: 34 additions & 25 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: check-case-conflict
- id: check-merge-conflict
Expand All @@ -18,11 +18,30 @@ repos:
- id: yamllint
entry: yamllint --strict -c .hooks/linters/yamllint.yaml

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.1.0
hooks:
- id: prettier
files: \.(json|yaml|yml)$

- repo: https://github.com/jumanjihouse/pre-commit-hooks
rev: 3.0.0
hooks:
- id: script-must-have-extension
name: Ensure shell scripts end with .sh
types: [shell]
- id: shellcheck
- id: shfmt
# Configuration in .mdlrc and .hooks/linters/mdstyle.rb
- id: markdownlint

- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
hooks:
- id: codespell
entry: codespell -q 3 -f --skip=".git,.github,go.*,magefiles/go.*" README.md
entry: |
codespell -q 3 -f
-S ".git,.github,README.md,docs/*,go.sum"
- repo: https://github.com/dnephin/pre-commit-golang
rev: v0.5.1
Expand All @@ -37,19 +56,6 @@ repos:
- id: go-build
- id: go-mod-tidy

- repo: https://github.com/jumanjihouse/pre-commit-hooks
rev: 3.0.0
hooks:
- id: script-must-have-extension
name: Ensure shell scripts end with .sh
types: [shell]
exclude: .bats
- id: shellcheck
- id: shfmt
# Configuration in .mdlrc and .hooks/linters/mdstyle.rb
- id: markdownlint
exclude: README.md

- repo: local
hooks:
- id: go-no-replacement
Expand All @@ -61,27 +67,30 @@ repos:
- id: go-unit-tests
name: Go unit tests
language: script
entry: .hooks/run-go-tests.sh modified
entry: .hooks/run-go-tests.sh coverage
files: '\.go$'
pass_filenames: true

- id: go-vet
name: Run go vet
language: script
entry: .hooks/go-vet.sh
files: '\.go$'
always_run: true
pass_filenames: true
require_serial: true
log_file: /tmp/go-vet.log

- id: go-licenses
name: Run go-licenses
language: script
entry: .hooks/go-licenses.sh check_forbidden
files: '\.go$'

- id: prettier
name: Run prettier
entry: .hooks/prettier-hook.sh
- id: generate-docs
name: Update package docs
language: script
entry: .hooks/generate-docs.sh
require_serial: true
files: '\.go$'

- id: go-copyright
name: Ensure all go files have the copyright header
language: script
types: [json, yaml]
entry: .hooks/go-copyright.sh
files: '\.go$'

0 comments on commit 7dc1cfe

Please sign in to comment.