From e1c8a718fa2721bf1a778818da2cddb78b5dc941 Mon Sep 17 00:00:00 2001 From: Alysson Ribeiro <15274059+sonalys@users.noreply.github.com.> Date: Wed, 28 Feb 2024 20:46:04 +0100 Subject: [PATCH] add example to readme --- .github/{ => workflows}/build.yml | 0 Makefile | 10 +++--- README.md | 59 +++++++++++++++++++++++++++++++ dockerfile | 5 +-- entrypoint/cli/main.go | 42 ---------------------- testdata/out/testdata/stub.gen.go | 52 +++++++++++++-------------- testdata/stub.go | 6 ++-- 7 files changed, 94 insertions(+), 80 deletions(-) rename .github/{ => workflows}/build.yml (100%) delete mode 100644 entrypoint/cli/main.go diff --git a/.github/build.yml b/.github/workflows/build.yml similarity index 100% rename from .github/build.yml rename to .github/workflows/build.yml diff --git a/Makefile b/Makefile index f4e11ce..a5dab0a 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ IMG=ghcr.io/sonalys/fake - +ENTRYPOINT=./entrypoint/fake/main.go .PHONYE: all build: - @CGO_ENABLED=0 go build -o ./bin/fake ./entrypoint/cli/main.go + @CGO_ENABLED=0 go build -o ./bin/fake ${ENTRYPOINT} image: build @docker build -t ${IMG}:latest -f dockerfile . @@ -12,9 +12,9 @@ push: @docker push ${IMG} build_all: - @GOOS=windows GOARCH=amd64 go build -o ./bin/windows/amd64/fake.exe ./entrypoint/cli/main.go - @GOOS=linux GOARCH=amd64 go build -o ./bin/linux/amd64/fake ./entrypoint/cli/main.go - @GOOS=linux GOARCH=arm64 go build -o ./bin/linux/arm64/fake ./entrypoint/cli/main.go + @GOOS=windows GOARCH=amd64 go build -o ./bin/windows/amd64/fake.exe ${ENTRYPOINT} + @GOOS=linux GOARCH=amd64 go build -o ./bin/linux/amd64/fake ${ENTRYPOINT} + @GOOS=linux GOARCH=arm64 go build -o ./bin/linux/arm64/fake ${ENTRYPOINT} @mkdir releases @zip releases/fake_Windows_amd64.zip bin/windows/amd64/fake.exe @zip releases/fake_Linux_amd64.zip bin/linux/amd64/fake diff --git a/README.md b/README.md index 2da7a7a..c94c295 100644 --- a/README.md +++ b/README.md @@ -35,4 +35,63 @@ The flags are: -input STRING Folder to scan for interfaces, can be invoked multiple times -output STRING Output folder, it will follow a tree structure repeating the package path -ignore STRING Folder to ignore, can be invoked multiple times +``` + + +## Example + +Running against our weird interface + +```go +type StubInterface[T comparable] interface { + WeirdFunc1(a any, b interface { + A() int + }) + WeirdFunc2(in *<-chan time.Time, outs ...chan int) error + Empty() + WeirdFunc3(map[T]func(in ...*chan<- time.Time)) T +} +``` + +Will generate the following mock: + +```go +type StubInterface[T comparable] struct { + setupWeirdFunc1 mock[func(a any, b interface { + A() int + })] + setupWeirdFunc2 mock[func(in *<-chan time.Time, outs ...chan int) error] + setupEmpty mock[func()] + setupWeirdFunc3 mock[func(a0 map[stub.T]func(in ...*chan<- time.Time)) stub.T] +} + +func (s *StubInterface) OnWeirdFunc1(funcs ...func(a any, b interface { A() int })) Config +func (s *StubInterface) WeirdFunc1(a any, b interface { A() int }) +... +``` + +So you can use it like this + +```go + +func Test_Stub(t *testing.T) { + mock := mocks.NewStubInterface(t) + mock.OnWeirdFunc1(func(a any, b interface { A() int }) { + require.NotNil(t, a) + ... + }) + + var Stub StubInterface[any] = mock + + Stub.WeirdFunc1(1, nil) // Will call the previous function. +} +``` + +You can pass more than one function or set repetition groups with + +```go +mock.OnWeirdFunc1(func(a any, b interface { A() int }) { + require.NotNil(t, a) + ... +}).Repeat(2) ``` \ No newline at end of file diff --git a/dockerfile b/dockerfile index 47f4186..0572122 100644 --- a/dockerfile +++ b/dockerfile @@ -4,14 +4,11 @@ WORKDIR /build COPY go.mod go.sum ./ RUN --mount=type=cache,target=/root/.cache/go-build go mod download COPY . . -RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -o ./bin/fake ./entrypoint/cli/main.go +RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -o ./bin/fake ./entrypoint/fake/main.go FROM scratch -COPY ./builders/passwd /etc/passwd COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ -USER nobody - COPY --from=builder ./build/bin/fake /fake COPY --from=builder /var/run /var/run diff --git a/entrypoint/cli/main.go b/entrypoint/cli/main.go deleted file mode 100644 index c1ec234..0000000 --- a/entrypoint/cli/main.go +++ /dev/null @@ -1,42 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "os" - - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" - mockgen "github.com/sonalys/fake" -) - -func init() { - zerolog.SetGlobalLevel(zerolog.InfoLevel) - log.Logger = log.Output(zerolog.ConsoleWriter{ - Out: os.Stdout, - }) -} - -type StrSlice []string - -func (s *StrSlice) String() string { - return fmt.Sprintf("%v", *s) -} - -func (s *StrSlice) Set(value string) error { - *s = append(*s, value) - return nil -} - -func main() { - var input, ignore StrSlice - flag.Var(&input, "input", "Folder to scan for .go files recursively") - output := flag.String("output", "mocks", "Folder to output the generated mocks") - flag.Var(&ignore, "ignore", "Specify which folders should be ignored") - flag.Parse() - if len(input) == 0 { - // Defaults to $CWD - input = []string{"."} - } - mockgen.Run(input, *output, ignore) -} diff --git a/testdata/out/testdata/stub.gen.go b/testdata/out/testdata/stub.gen.go index 4ed36be..3b4414d 100644 --- a/testdata/out/testdata/stub.gen.go +++ b/testdata/out/testdata/stub.gen.go @@ -11,57 +11,57 @@ import ( ) type StubInterface[T comparable] struct { - setupInterf mock[func(a any, b interface { + setupWeirdFunc1 mock[func(a any, b interface { A() int })] - setupKillYourself mock[func(in *<-chan time.Time, outs ...chan int) error] - setupEmpty mock[func()] - setupWeird mock[func(a0 map[stub.T]func(in ...*chan<- time.Time)) stub.T] + setupWeirdFunc2 mock[func(in *<-chan time.Time, outs ...chan int) error] + setupEmpty mock[func()] + setupWeirdFunc3 mock[func(a0 map[stub.T]func(in ...*chan<- time.Time)) stub.T] } func NewStubInterface[T comparable](t *testing.T) *StubInterface[T] { return &StubInterface{ - setupInterf: newMock[func(a any, b interface { + setupWeirdFunc1: newMock[func(a any, b interface { A() int })](t), - setupKillYourself: newMock[func(in *<-chan time.Time, outs ...chan int) error](t), - setupEmpty: newMock[func()](t), - setupWeird: newMock[func(a0 map[stub.T]func(in ...*chan<- time.Time)) stub.T](t), + setupWeirdFunc2: newMock[func(in *<-chan time.Time, outs ...chan int) error](t), + setupEmpty: newMock[func()](t), + setupWeirdFunc3: newMock[func(a0 map[stub.T]func(in ...*chan<- time.Time)) stub.T](t), } } func (s *StubInterface) AssertExpectations(t *testing.T) bool { - return s.setupInterf.AssertExpectations(t) && - s.setupKillYourself.AssertExpectations(t) && + return s.setupWeirdFunc1.AssertExpectations(t) && + s.setupWeirdFunc2.AssertExpectations(t) && s.setupEmpty.AssertExpectations(t) && - s.setupWeird.AssertExpectations(t) && + s.setupWeirdFunc3.AssertExpectations(t) && true } -func (s *StubInterface) OnInterf(funcs ...func(a any, b interface { +func (s *StubInterface) OnWeirdFunc1(funcs ...func(a any, b interface { A() int })) Config { - return s.setupInterf.append(funcs...) + return s.setupWeirdFunc1.append(funcs...) } -func (s *StubInterface[T]) Interf(a any, b interface { +func (s *StubInterface[T]) WeirdFunc1(a any, b interface { A() int }) { - f, ok := s.setupInterf.call() + f, ok := s.setupWeirdFunc1.call() if !ok { - panic(fmt.Sprintf("unexpected call Interf(%v,%v)", a, b)) + panic(fmt.Sprintf("unexpected call WeirdFunc1(%v,%v)", a, b)) } (*f)(a, b) } -func (s *StubInterface) OnKillYourself(funcs ...func(in *<-chan time.Time, outs ...chan int) error) Config { - return s.setupKillYourself.append(funcs...) +func (s *StubInterface) OnWeirdFunc2(funcs ...func(in *<-chan time.Time, outs ...chan int) error) Config { + return s.setupWeirdFunc2.append(funcs...) } -func (s *StubInterface[T]) KillYourself(in *<-chan time.Time, outs ...chan int) error { - f, ok := s.setupKillYourself.call() +func (s *StubInterface[T]) WeirdFunc2(in *<-chan time.Time, outs ...chan int) error { + f, ok := s.setupWeirdFunc2.call() if !ok { - panic(fmt.Sprintf("unexpected call KillYourself(%v,%v)", in, outs)) + panic(fmt.Sprintf("unexpected call WeirdFunc2(%v,%v)", in, outs)) } return (*f)(in, outs...) } @@ -78,14 +78,14 @@ func (s *StubInterface[T]) Empty() { (*f)() } -func (s *StubInterface) OnWeird(funcs ...func(a0 map[stub.T]func(in ...*chan<- time.Time)) stub.T) Config { - return s.setupWeird.append(funcs...) +func (s *StubInterface) OnWeirdFunc3(funcs ...func(a0 map[stub.T]func(in ...*chan<- time.Time)) stub.T) Config { + return s.setupWeirdFunc3.append(funcs...) } -func (s *StubInterface[T]) Weird(a0 map[stub.T]func(in ...*chan<- time.Time)) stub.T { - f, ok := s.setupWeird.call() +func (s *StubInterface[T]) WeirdFunc3(a0 map[stub.T]func(in ...*chan<- time.Time)) stub.T { + f, ok := s.setupWeirdFunc3.call() if !ok { - panic(fmt.Sprintf("unexpected call Weird(%v)", a0)) + panic(fmt.Sprintf("unexpected call WeirdFunc3(%v)", a0)) } return (*f)(a0) } diff --git a/testdata/stub.go b/testdata/stub.go index d896f02..71bd4de 100644 --- a/testdata/stub.go +++ b/testdata/stub.go @@ -3,10 +3,10 @@ package stub import "time" type StubInterface[T comparable] interface { - Interf(a any, b interface { + WeirdFunc1(a any, b interface { A() int }) - KillYourself(in *<-chan time.Time, outs ...chan int) error + WeirdFunc2(in *<-chan time.Time, outs ...chan int) error Empty() - Weird(map[T]func(in ...*chan<- time.Time)) T + WeirdFunc3(map[T]func(in ...*chan<- time.Time)) T }