Skip to content

Commit

Permalink
chore: add integrity tests (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox authored May 23, 2023
1 parent c861c1f commit 4787ac6
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 20 deletions.
15 changes: 5 additions & 10 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ jobs:
parallel: true
flag-name: ${{ join(matrix.*, ' ') }}

test-integration:
test-integrity:
strategy:
matrix:
go-version: [1.20.x]
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
env:
GOCOVERDIR: _icoverdir_
GOCOVERDIR: ${{ github.workspace }}/_icoverdir_
steps:
- name: Install Go
uses: actions/setup-go@v2
Expand All @@ -46,13 +46,8 @@ jobs:
- name: Integration tests
run: |
mkdir _icoverdir_
make build-with-coverage
cp examples/complete/lefthook.yml .
# TODO: prepare a framework to test the results
- run: |
./lefthook dump
./lefthook dump --json
./lefthook dump --toml
go install -cover
- run: go test integrity_test.go -tags=integrity
- name: Format coverage
run: |
go tool covdata textfmt -i _icoverdir_ -o coverage.out
Expand Down Expand Up @@ -91,7 +86,7 @@ jobs:
runs-on: ubuntu-latest
needs:
- test
- test-integration
- test-integrity
steps:
- uses: coverallsapp/github-action@v2
with:
Expand Down
12 changes: 5 additions & 7 deletions cmd/lefthook.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package cmd

import (
"os"
import "github.com/evilmartians/lefthook/internal/log"

"github.com/evilmartians/lefthook/internal/log"
)

func Lefthook() {
func Lefthook() int {
rootCmd := newRootCmd()

if err := rootCmd.Execute(); err != nil {
if err.Error() != "" {
log.Errorf("Error: %s", err)
}
os.Exit(1)
return 1
}

return 0
}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ require (
github.com/gobwas/glob v0.2.3
github.com/google/go-cmp v0.5.9
github.com/mitchellh/mapstructure v1.5.0
github.com/rogpeppe/go-internal v1.10.0
github.com/spf13/afero v1.9.5
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.15.0
gopkg.in/alessio/shellescape.v1 v1.0.0-20170105083845-52074bc9df61
)

require github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
gopkg.in/errgo.v2 v2.1.0 // indirect
)

require (
github.com/alessio/shellescape v1.4.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
Expand Down Expand Up @@ -513,6 +516,7 @@ gopkg.in/alessio/shellescape.v1 v1.0.0-20170105083845-52074bc9df61/go.mod h1:IfM
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0 h1:0vLT13EuvQ0hNvakwLuFZ/jYrLp5F3kcWHXdRggjCE8=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
Expand Down
22 changes: 22 additions & 0 deletions integrity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build integrity
// +build integrity

package main_test

import (
"fmt"
"os"
"testing"

"github.com/rogpeppe/go-internal/testscript"
)

func TestLefthookIntegrity(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata",
Setup: func(env *testscript.Env) error {
env.Vars = append(env.Vars, fmt.Sprintf("GOCOVERDIR=%s", os.Getenv("GOCOVERDIR")))
return nil
},
})
}
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package main

import "github.com/evilmartians/lefthook/cmd"
import (
"os"

"github.com/evilmartians/lefthook/cmd"
)

func main() {
cmd.Lefthook()
os.Exit(cmd.Lefthook())
}
4 changes: 4 additions & 0 deletions testdata/add.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exec git init
exec lefthook add pre-commit
exists .git/hooks/pre-commit
! stderr .
86 changes: 86 additions & 0 deletions testdata/dump.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
exec git init
exec lefthook dump
cmp stdout lefthook-dumped.yml
! stderr .

exec lefthook dump --json
cmp stdout lefthook-dumped.json
! stderr .

exec lefthook dump --toml
cmp stdout lefthook-dumped.toml
! stderr .

-- lefthook.yml --
colors: false
pre-commit:
follow: true
commands:
lint:
interactive: true
skip:
- merge
- rebase
- ref: main
run: yarn lint {staged_files}
test:
skip: merge
glob: "*.js"
run: yarn test
-- lefthook-dumped.yml --
colors: false
pre-commit:
commands:
lint:
run: yarn lint {staged_files}
skip:
- merge
- rebase
- ref: main
interactive: true
test:
run: yarn test
skip: merge
glob: '*.js'
follow: true
-- lefthook-dumped.json --
{
"colors": false,
"pre-commit": {
"commands": {
"lint": {
"run": "yarn lint {staged_files}",
"skip": [
"merge",
"rebase",
{
"ref": "main"
}
],
"interactive": true
},
"test": {
"run": "yarn test",
"skip": "merge",
"glob": "*.js"
}
},
"follow": true
}
}
-- lefthook-dumped.toml --
colors = false

[pre-commit]
follow = true

[pre-commit.commands]
[pre-commit.commands.lint]
run = 'yarn lint {staged_files}'
skip = ['merge', 'rebase', {ref = 'main'}]
interactive = true

[pre-commit.commands.test]
run = 'yarn test'
skip = 'merge'
glob = '*.js'
4 changes: 4 additions & 0 deletions testdata/install.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exec git init
exec lefthook install
exists lefthook.yml
! stderr .
3 changes: 3 additions & 0 deletions testdata/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exec lefthook version
stdout \d\.\d\.\d
! stderr .

0 comments on commit 4787ac6

Please sign in to comment.