pre-commitでgoのtestを走らせる
gitのhook機能を使ってcommit時にgoのテストを走らせる設定を加えました。
goの設定ファイルがリポジトリのルートにある場合
.git/hooks/pre-commitファイルを作成して、以下をコピーします。
#!/bin/sh
echo "Running Go tests..."
if ! go test -v ./...; then
    echo "Go tests failed. Commit aborted."
        exit 1
        fi
echo "All tests passed. Proceeding with commit."goがリポジトリのルートにない場合
go.modやgo.workがリポジトリのルートにない場合は、cd等でフォルダの調整を行います。
pre-commitのコマンドが実行されるディレクトリはリポジトリのルートのようなので、cdは相対パスで素朴に書けます。
.
├── golang
│   ├── go.mod
│   └── main.go
...#!/bin/sh
echo "Running Go tests..."
cd golang
if ! go test -v ./...; then
    echo "Go tests failed. Commit aborted."
        exit 1
        fi
echo "All tests passed. Proceeding with commit." 
  
  
  
  
コメント