Skip to content
Closed
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
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- master
- main
pull_request:

jobs:
Expand All @@ -13,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
env:
GO_VERSION: stable
GOLANGCI_LINT_VERSION: v1.62.0
GOLANGCI_LINT_VERSION: v2.3.0
CGO_ENABLED: 0

steps:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/go-cross.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- master
- main
pull_request:

jobs:
Expand Down
124 changes: 66 additions & 58 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
version: "2"

run:
timeout: 10m

linters-settings:
govet:
enable-all: true
disable:
- fieldalignment
gocyclo:
min-complexity: 15
goconst:
min-len: 5
min-occurrences: 3
misspell:
locale: US
funlen:
lines: -1
statements: 50
godox:
keywords:
- FIXME
gofumpt:
extra-rules: true
depguard:
rules:
main:
deny:
- pkg: "github.com/instana/testify"
desc: not allowed
- pkg: "github.com/pkg/errors"
desc: Should be replaced by standard lib errors package
gocritic:
enabled-tags:
- diagnostic
- style
- performance
disabled-checks:
- sloppyReassign
- rangeValCopy
- octalLiteral
- paramTypeCombine # already handle by gofumpt.extra-rules
- unnamedResult
- hugeParam
tagliatelle:
case:
linters:
settings:
govet:
enable-all: true
disable:
- fieldalignment
gocyclo:
min-complexity: 15
goconst:
min-len: 5
min-occurrences: 3
misspell:
locale: US
funlen:
lines: -1
statements: 50
godox:
keywords:
- FIXME
depguard:
rules:
json: pascal
gosec:
excludes:
- G304
- G306
main:
deny:
- pkg: "github.com/instana/testify"
desc: not allowed
- pkg: "github.com/pkg/errors"
desc: Should be replaced by standard lib errors package
gocritic:
enabled-tags:
- diagnostic
- style
- performance
disabled-checks:
- sloppyReassign
- rangeValCopy
- octalLiteral
- paramTypeCombine # already handle by gofumpt.extra-rules
- unnamedResult
- hugeParam
tagliatelle:
case:
rules:
json: pascal
gosec:
excludes:
- G304
- G306

linters:
enable-all: true
default: all
disable:
- exportloopref # deprecated
- sqlclosecheck # not relevant (SQL)
- rowserrcheck # not relevant (SQL)
- cyclop # duplicate of gocyclo
- lll
- dupl
- wsl
- wsl_v5
- nlreturn
- mnd
- err113
Expand All @@ -78,17 +78,25 @@ linters:
- errchkjson
- contextcheck

exclusions:
warn-unused: true
rules:
- text: fmt.Sprintf can be replaced with string
linters:
- perfsprint

issues:
exclude-use-default: false
max-issues-per-linter: 0
max-same-issues: 0
exclude:
- 'fmt.Sprintf can be replaced with string'
exclude-rules:
- path: .*_test.go
linters:
- funlen
- noctx

formatters:
enable:
- gci
- gofumpt

settings:
gofumpt:
extra-rules: true

output:
show-stats: true
13 changes: 0 additions & 13 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,4 @@ archives:
files:
- LICENSE

brews:
- repository:
owner: traefik
name: homebrew-tap
commit_author:
name: traefiker
email: [email protected]
directory: Formula
homepage: https://github.com/traefik/mocktail
description: |
Naive code generator that creates mock implementation using testify.mock.
test: |
system "echo 0"

1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [2025] [PaperBalls Authors]
Copyright [2022] [Traefik Labs]

Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.PHONY: clean lint test build
.PHONY: default clean lint test build

default: clean lint test build

lint:
golangci-lint run
go run github.com/golangci/golangci-lint/v2/cmd/[email protected] run

clean:
rm -rf cover.out
Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module github.com/traefik/mocktail
module github.com/paperballs/mocktail

go 1.23.0
go 1.24.0

require (
github.com/ettle/strcase v0.2.0
github.com/stretchr/testify v1.9.0
golang.org/x/tools v0.32.0
github.com/stretchr/testify v1.10.0
golang.org/x/tools v0.35.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.24.0 // indirect
golang.org/x/sync v0.13.0 // indirect
golang.org/x/mod v0.26.0 // indirect
golang.org/x/sync v0.16.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610=
golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/tools v0.32.0 h1:Q7N1vhpkQv7ybVzLFtTjvQya2ewbwNDZzUgfXGqtMWU=
golang.org/x/tools v0.32.0/go.mod h1:ZxrU41P/wAbZD8EDa6dDCa6XfpkhJ7HFMjHJXfBDu8s=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/mod v0.26.0 h1:EGMPT//Ezu+ylkCijjPc+f4Aih7sZvaAr+O3EHBxvZg=
golang.org/x/mod v0.26.0/go.mod h1:/j6NAhSk8iQ723BGAUyoAcn7SlD7s15Dp9Nd/SfeaFQ=
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/tools v0.35.0 h1:mBffYraMEf7aa0sB+NuKnuCy8qI/9Bughn8dC2Gu5r0=
golang.org/x/tools v0.35.0/go.mod h1:NKdj5HkL/73byiZSJjqJgKn3ep7KjFkBOkR/Hps3VPw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
21 changes: 17 additions & 4 deletions mocktail.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main
import (
"bufio"
"bytes"
"context"
"flag"
"fmt"
"go/format"
Expand Down Expand Up @@ -37,12 +38,15 @@ type PackageDesc struct {

// InterfaceDesc represent an interface.
type InterfaceDesc struct {
Name string
Methods []*types.Func
Name string
Methods []*types.Func
TypeParams *types.TypeParamList // Generic type parameters
}

func main() {
info, err := getModuleInfo(os.Getenv("MOCKTAIL_TEST_PATH"))
ctx := context.Background()

info, err := getModuleInfo(ctx, os.Getenv("MOCKTAIL_TEST_PATH"))
if err != nil {
log.Fatal("get module path", err)
}
Expand Down Expand Up @@ -153,6 +157,11 @@ func walk(root, moduleName string) (map[string]PackageDesc, error) {

interfaceDesc := InterfaceDesc{Name: interfaceName}

// Check if this is a generic interface
if namedType, ok := lookup.Type().(*types.Named); ok {
interfaceDesc.TypeParams = namedType.TypeParams()
}

interfaceType, ok := lookup.Type().Underlying().(*types.Interface)
if !ok {
return fmt.Errorf("type %q in %q is not an interface", lookup.Type(), fp)
Expand Down Expand Up @@ -252,6 +261,9 @@ func getTypeImports(t types.Type) []string {
case *types.Chan:
return []string{""}

case *types.TypeParam:
return []string{""}

default:
panic(fmt.Sprintf("OOPS %[1]T %[1]s", t))
}
Expand All @@ -267,7 +279,7 @@ func generate(model map[string]PackageDesc, exported bool) error {
}

for _, interfaceDesc := range pkgDesc.Interfaces {
err = writeMockBase(buffer, interfaceDesc.Name, exported)
err = writeMockBase(buffer, interfaceDesc, exported)
if err != nil {
return err
}
Expand All @@ -282,6 +294,7 @@ func generate(model map[string]PackageDesc, exported bool) error {
InterfaceName: interfaceDesc.Name,
Method: method,
Signature: signature,
TypeParams: interfaceDesc.TypeParams,
}

err = syrup.MockMethod(buffer)
Expand Down
8 changes: 4 additions & 4 deletions mocktail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestMocktail(t *testing.T) {

t.Setenv("MOCKTAIL_TEST_PATH", filepath.Join(testRoot, entry.Name()))

output, err := exec.Command("go", "run", ".").CombinedOutput()
output, err := exec.CommandContext(t.Context(), "go", "run", ".").CombinedOutput()
t.Log(string(output))

require.NoError(t, err)
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestMocktail(t *testing.T) {
continue
}

cmd := exec.Command("go", "test", "-v", "./...")
cmd := exec.CommandContext(t.Context(), "go", "test", "-v", "./...")
cmd.Dir = filepath.Join(testRoot, entry.Name())

output, err := cmd.CombinedOutput()
Expand All @@ -88,7 +88,7 @@ func TestMocktail_exported(t *testing.T) {

t.Setenv("MOCKTAIL_TEST_PATH", filepath.Join(testRoot, entry.Name()))

output, err := exec.Command("go", "run", ".", "-e").CombinedOutput()
output, err := exec.CommandContext(t.Context(), "go", "run", ".", "-e").CombinedOutput()
t.Log(string(output))

require.NoError(t, err)
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestMocktail_exported(t *testing.T) {
continue
}

cmd := exec.Command("go", "test", "-v", "./...")
cmd := exec.CommandContext(t.Context(), "go", "test", "-v", "./...")
cmd.Dir = filepath.Join(testRoot, entry.Name())

output, err := cmd.CombinedOutput()
Expand Down
5 changes: 3 additions & 2 deletions mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -16,9 +17,9 @@ type modInfo struct {
Main bool `json:"Main"`
}

func getModuleInfo(dir string) (modInfo, error) {
func getModuleInfo(ctx context.Context, dir string) (modInfo, error) {
// https://github.com/golang/go/issues/44753#issuecomment-790089020
cmd := exec.Command("go", "list", "-m", "-json")
cmd := exec.CommandContext(ctx, "go", "list", "-m", "-json")
if dir != "" {
cmd.Dir = dir
}
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ go install github.com/traefik/mocktail@latest

You can use pre-compiled binaries:

* To get the binary just download the latest release for your OS/Arch from [the releases page](https://github.com/traefik/mocktail/releases)
* To get the binary just download the latest release for your OS/Arch from [the releases page](https://github.com/paperballs/mocktail/releases)
* Unzip the archive.
* Add `mocktail` in your `PATH`.

Expand Down
Loading
Loading