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

refactor: enhance test readability #8

Open
wants to merge 4 commits into
base: main
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
20 changes: 17 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,34 @@ jobs:
- name: Tidy
run: go mod tidy && git diff --exit-code

- name: Install gotestfmt
uses: gotesttools/gotestfmt-action@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}

- name: Test
run: |
set -euo pipefail
mkdir -p "${{ runner.temp }}/covdata"
go test -v -cover ./... -test.gocoverdir="${{ runner.temp }}/covdata"
go tool covdata percent -i="${{ runner.temp }}/covdata"
go test -v -cover ./... -json -test.gocoverdir="${{ runner.temp }}/covdata" 2>&1 | tee "${{ runner.temp }}/test.log" | gotestfmt
go tool covdata textfmt -o "${{ runner.temp }}/coverage.out" -i="${{ runner.temp }}/covdata"
go tool covdata percent -i="${{ runner.temp }}/covdata"

- name: Coverage report
uses: ncruces/go-coverage-report@v0
with:
coverage-file: "${{ runner.temp }}/coverage.out"
report: true
chart: true
amend: true
if: |
github.event_name == 'push'
continue-on-error: true

- name: Upload test artifacts
uses: actions/upload-artifact@v4
if: always()
with:
name: test-logs
path: |
${{ runner.temp }}/test.log
${{ runner.temp }}/coverage.out
3 changes: 2 additions & 1 deletion cmd/vermockgen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

var env []string
var covdir *string

func TestMockgen(t *testing.T) {
ctx, eng := context.Background(), script.NewEngine()
Expand Down Expand Up @@ -43,7 +44,7 @@ func TestMain(m *testing.M) {
// test script.
fset := flag.NewFlagSet("vermockgen", flag.ContinueOnError)
fset.SetOutput(&bytes.Buffer{}) // ignore errors
covdir := fset.String("test.gocoverdir", "", "write coverage intermediate files to this directory")
covdir = fset.String("test.gocoverdir", "", "write coverage intermediate files to this directory")
err = fset.Parse(os.Args[1:])
for err != nil {
err = fset.Parse(fset.Args())
Expand Down
6 changes: 0 additions & 6 deletions cmd/vermockgen/testdata/commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,3 @@ module test
go 1.20

require github.com/Versent/go-vermock v0.0.0-00010101000000-000000000000
-- tools.go --
package main

import (
_ "github.com/Versent/go-vermock/cmd/vermockgen"
)
6 changes: 0 additions & 6 deletions cmd/vermockgen/testdata/fallback.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ module test
go 1.20

require github.com/Versent/go-vermock v0.0.0-00010101000000-000000000000
-- tools.go --
package main

import (
_ "github.com/Versent/go-vermock/cmd/vermockgen"
)
-- mock.go --
//go:build vermockstub

Expand Down
6 changes: 0 additions & 6 deletions cmd/vermockgen/testdata/fallback_all_args.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ module test
go 1.20

require github.com/Versent/go-vermock v0.0.0-00010101000000-000000000000
-- tools.go --
package main

import (
_ "github.com/Versent/go-vermock/cmd/vermockgen"
)
-- HEADER --
// HEADER
-- mock.go --
Expand Down
6 changes: 0 additions & 6 deletions cmd/vermockgen/testdata/fallback_flag_undefined.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,3 @@ module test
go 1.20

require github.com/Versent/go-vermock v0.0.0-00010101000000-000000000000
-- tools.go --
package main

import (
_ "github.com/Versent/go-vermock/cmd/vermockgen"
)
6 changes: 0 additions & 6 deletions cmd/vermockgen/testdata/fallback_header.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ module test
go 1.20

require github.com/Versent/go-vermock v0.0.0-00010101000000-000000000000
-- tools.go --
package main

import (
_ "github.com/Versent/go-vermock/cmd/vermockgen"
)
-- HEADER --
// HEADER
-- mock.go --
Expand Down
6 changes: 0 additions & 6 deletions cmd/vermockgen/testdata/fallback_packages_missing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,3 @@ module test
go 1.20

require github.com/Versent/go-vermock v0.0.0-00010101000000-000000000000
-- tools.go --
package main

import (
_ "github.com/Versent/go-vermock/cmd/vermockgen"
)
6 changes: 0 additions & 6 deletions cmd/vermockgen/testdata/fallback_tags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ module test
go 1.20

require github.com/Versent/go-vermock v0.0.0-00010101000000-000000000000
-- tools.go --
package main

import (
_ "github.com/Versent/go-vermock/cmd/vermockgen"
)
-- mock.go --
//go:build vermockstub

Expand Down
6 changes: 0 additions & 6 deletions cmd/vermockgen/testdata/flags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,3 @@ module test
go 1.20

require github.com/Versent/go-vermock v0.0.0-00010101000000-000000000000
-- tools.go --
package main

import (
_ "github.com/Versent/go-vermock/cmd/vermockgen"
)
6 changes: 0 additions & 6 deletions cmd/vermockgen/testdata/flags_gen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,3 @@ module test
go 1.20

require github.com/Versent/go-vermock v0.0.0-00010101000000-000000000000
-- tools.go --
package main

import (
_ "github.com/Versent/go-vermock/cmd/vermockgen"
)
6 changes: 0 additions & 6 deletions cmd/vermockgen/testdata/gen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ module test
go 1.20

require github.com/Versent/go-vermock v0.0.0-00010101000000-000000000000
-- tools.go --
package main

import (
_ "github.com/Versent/go-vermock/cmd/vermockgen"
)
-- mock.go --
//go:build vermockstub

Expand Down
6 changes: 0 additions & 6 deletions cmd/vermockgen/testdata/gen_all_args.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ module test
go 1.20

require github.com/Versent/go-vermock v0.0.0-00010101000000-000000000000
-- tools.go --
package main

import (
_ "github.com/Versent/go-vermock/cmd/vermockgen"
)
-- HEADER --
// HEADER
-- mock.go --
Expand Down
6 changes: 0 additions & 6 deletions cmd/vermockgen/testdata/gen_flag_undefined.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,3 @@ module test
go 1.20

require github.com/Versent/go-vermock v0.0.0-00010101000000-000000000000
-- tools.go --
package main

import (
_ "github.com/Versent/go-vermock/cmd/vermockgen"
)
6 changes: 0 additions & 6 deletions cmd/vermockgen/testdata/gen_header.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ module test
go 1.20

require github.com/Versent/go-vermock v0.0.0-00010101000000-000000000000
-- tools.go --
package main

import (
_ "github.com/Versent/go-vermock/cmd/vermockgen"
)
-- HEADER --
// HEADER
-- mock.go --
Expand Down
6 changes: 0 additions & 6 deletions cmd/vermockgen/testdata/gen_package.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ module test
go 1.20

require github.com/Versent/go-vermock v0.0.0-00010101000000-000000000000
-- tools.go --
package main

import (
_ "github.com/Versent/go-vermock/cmd/vermockgen"
)
-- mock.go --
//go:build vermockstub

Expand Down
6 changes: 0 additions & 6 deletions cmd/vermockgen/testdata/gen_tags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ module test
go 1.20

require github.com/Versent/go-vermock v0.0.0-00010101000000-000000000000
-- tools.go --
package main

import (
_ "github.com/Versent/go-vermock/cmd/vermockgen"
)
-- mock.go --
//go:build vermockstub

Expand Down
7 changes: 7 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (m *mockCache) Load(keys ...string) {
var UnusedCache func(*mockCache) = nil

func ExampleUnusedCache() {
// See ./testdata/example.txt for a full example.
t := &exampleT{} // or any testing.TB, your test does not create this
// 1. Create a mock object.
var cache Cache = vermock.New(t, UnusedCache)
Expand All @@ -66,6 +67,7 @@ func ExpectDelete(delegate func(t testing.TB, key string)) func(*mockCache) {
}

func Example_pass() {
// See ./testdata/example.txt for a full example.
t := &exampleT{} // or any testing.TB, your test does not create this
// 1. Create a mock object with expected calls.
var cache Cache = vermock.New(t,
Expand Down Expand Up @@ -99,6 +101,7 @@ func Example_pass() {
}

func Example_unmetExpectation() {
// See ./testdata/example.txt for a full example.
t := &testing.T{} // or any testing.TB, your test does not create this
// 1. Create a mock object with expected calls.
var cache Cache = vermock.New(t,
Expand Down Expand Up @@ -137,6 +140,7 @@ func Example_unmetExpectation() {
}

func Example_unexpectedCall() {
// See ./testdata/example.txt for a full example.
t := &testing.T{} // or any testing.TB, your test does not create this
// 1. Create a mock object with expected calls.
var cache Cache = vermock.New(t,
Expand Down Expand Up @@ -165,6 +169,7 @@ func Example_unexpectedCall() {
}

func Example_allowRepeatedCalls() {
// See ./testdata/example.txt for a full example.
t := &testing.T{} // or any testing.TB, your test does not create this
// 1. Create a mock object with ExpectMany.
var cache Cache = vermock.New(t,
Expand Down Expand Up @@ -211,6 +216,7 @@ func Example_allowRepeatedCalls() {
}

func Example_orderedCalls() {
// See ./testdata/example.txt for a full example.
t := &testing.T{} // or any testing.TB, your test does not create this
// 1. Create a mock object with ExpectInOrder.
var cache Cache = vermock.New(t,
Expand Down Expand Up @@ -240,6 +246,7 @@ func Example_orderedCalls() {
}

func Example_mixedOrderedCalls() {
// See ./testdata/example.txt for a full example.
t := &exampleT{} // or any testing.TB, your test does not create this
// 1. Create a mock object with ExpectInOrder.
get := vermock.Expect[mockCache]("Get", func(key string) (any, bool) {
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/Versent/go-vermock

go 1.20
go 1.21

toolchain go1.21.6

require (
github.com/google/subcommands v1.2.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.16.0 h1:GO788SKMRunPIBCXiQyo2AaexLstOrVhuAL5YwsckQM=
Expand Down
36 changes: 36 additions & 0 deletions internal/cmd/vermockgen/gen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package vermockgen_test

import (
"bytes"
"flag"
"log"
"testing"

vermockgen "github.com/Versent/go-vermock/internal/cmd/vermockgen"
)

func TestSetFlags(t *testing.T) {
f := flag.NewFlagSet("vermockgen", flag.ContinueOnError)
vermockgen.NewGenCmd(nil, f)
expected := []string{"header", "tags"}
f.VisitAll(func(f *flag.Flag) {
if len(expected) == 0 {
t.Errorf("unexpected flag %q", f.Name)
return
}

var got, want string
got, want, expected = f.Name, expected[0], expected[1:]
if got != want {
t.Errorf("unexpected name, got %q, want %q", got, want)
}
})
}

func TestExecute(t *testing.T) {
var buf bytes.Buffer
l := log.New(&buf, "", 0)
f := flag.NewFlagSet("vermockgen", flag.ContinueOnError)
vermockgen.NewGenCmd(l, f)

}
6 changes: 5 additions & 1 deletion internal/mock/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func TestGenerate(t *testing.T) {
t,
context.Background(),
engine,
[]string{"MUT=" + mutdir},
[]string{
"PATH=" + os.Getenv("PATH"),
"HOME=" + os.Getenv("HOME"),
"MUT=" + mutdir,
},
"testdata/*.txt",
)
}
Expand Down
2 changes: 2 additions & 0 deletions internal/mock/testdata/custom_func.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

replace ../../../.. $MUT go.mod

exec go mod tidy

vermockgen

cmpenv stdout testdata/stdout
Expand Down
2 changes: 2 additions & 0 deletions internal/mock/testdata/custom_func_multi_expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

replace ../../../.. $MUT go.mod

exec go mod tidy

vermockgen

cmpenv stdout testdata/stdout
Expand Down
2 changes: 2 additions & 0 deletions internal/mock/testdata/custom_func_multi_file.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

replace ../../../.. $MUT go.mod

exec go mod tidy

vermockgen

cmpenv stdout testdata/stdout
Expand Down
Loading