-
Notifications
You must be signed in to change notification settings - Fork 94
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
import cycle if i write a test for the faked interface in the package of the interface #121
Comments
var _ abc.Foo = new(FakeFoo)
// Code generated by counterfeiter. DO NOT EDIT.
- package abcfakes
+ package abc
import (
"sync"
-
- "github.com/where/is/abc"
)
// ...
- func (fake *FakeFoo) Bar() (abc.Stuff) {
+ func (fake *FakeFoo) Bar() (Stuff) {
// and for any other method using a type from the package
// ...
}
// ...
- var _ abc.Foo = new(FakeFoo)
+ // nothing
+ // or
+ var _ Foo = new(FakeFoo) |
I would say we definitely should keep the last line, as it guarantees that |
I see four possible solutions (not mutually exclusive) to this problem
|
Among possible solutions listed above only |
I overall agree with all the points of @LasTshaMAN. Just my 2 cents:
I only see 3. as the best option. Edit, addition to |
Been hitting this issue when trying to test private methods that need a fake. I'm just some random guy, but option 3 seems the most reasonable. I don't believe a fake needs to confirm that it satisfies the interface, using the fake in a test will do that. |
Note, this issue is not presented if you do not export your interface. For instance, type foo interface {
Bar() string
} |
This project is absolute great. It allows to get rid of soooo much boilerplate, that I needed to update too frequently. And opens easy access to more detailed testing - thanks a lot for creating it! To use a mocked interface within the same package (I need access to internals), I use the admittedly very hacky script below and go:generate like this: // +build ignore
package main
import (
"bytes"
"flag"
"log"
"os"
"os/exec"
"path/filepath"
)
func main() {
var path string
flag.StringVar(&path, "t", "", "Name of file to prune")
flag.Parse()
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Fatal(err)
}
if !info.Mode().IsRegular() {
return nil
}
err = pruneInterfaceCheck(path, info.Size())
if err != nil {
log.Fatal(err)
}
err = exec.Command("goimports", "-w", path).Run()
if err != nil {
log.Fatal(err)
}
return nil
})
}
func pruneInterfaceCheck(path string, size int64) error {
fd, err := os.OpenFile(path, os.O_RDWR, 0666)
if err != nil {
return err
}
defer fd.Close()
var chunk int64 = 100
buf := make([]byte, chunk)
searched := []byte("var _ ")
pos := size - chunk
for {
_, err = fd.ReadAt(buf, pos)
if err != nil {
return err
}
if i := bytes.LastIndex(buf, searched); i != -1 {
pos += int64(i)
break
}
pos -= chunk
}
return fd.Truncate(pos)
} I understand that you are opposed to doing it, however I'd rather not do one package for every interface - the project would explode. |
Related to #103, but with the difference, that i'm ok with having the fake in a sub-package (omitting
-o
flag)I write a test file in the same package
abc
once generated,
abcfakes/fake_foo.go
has these lines:So, first problem is the same as in Interface fakes generated in the same package as the interface cause an import cycle #103.
var _ abc.Foo = new(FakeFoo)
forces theFakeFoo
file to importgithub.com/where/is/abc
which causes an import cycle, because coming fromabc
i'm importinggithub.com/where/is/abc/abcfakes
.Independent of the first problem, we have still another one:
func (fake *FakeFoo) Bar() (abc.Stuff) { ... }
forces theFakeFoo
file to importgithub.com/where/is/abc
too, which causes an import cycle for the same reason.The text was updated successfully, but these errors were encountered: