remyoudompheng / go-misc

Miscellaneous Go toys
Other
133 stars 24 forks source link

deadcode: TestXXX is unused ? #13

Open Dieterbe opened 6 years ago

Dieterbe commented 6 years ago

Hello Rémy, first of all, deadcode is a neat tool. thank you. i've used it to find some deadcode that i was able to remove.

I found a problem though. It doesn't recognize Test methods. see this example: (or am i using the tool wrong?)

$ pwd
/home/dieter/go/src/github.com/Dieterbe/deadtest
$ ls *.go
main.go  main_test.go
$ cat main.go
package main

import "fmt"

func main() {
    fmt.Println(foo(10))
}

func foo(in int) int {
    return in / 10
}
$ cat main_test.go
package main

import "testing"

func TestFoo(t *testing.T) {
    if foo(10) != 1 {
        t.Errorf("foo(10) should be 1")
    }
}
$ go test -v
=== RUN   TestFoo
--- PASS: TestFoo (0.00s)
PASS
ok      github.com/Dieterbe/deadtest    0.001s
$ deadcode -test .
main_test.go:5:6: TestFoo is unused
$                                                                      

Btw happy holidays! hope you are doing well.

Dieterbe commented 6 years ago

When I reorganize the code a bit, it works fine:

$ pwd
/home/dieter/go/src/github.com/Dieterbe/deadtest
$ find .
.
./main.go
./foo
./foo/f.go
./foo/f_test.go
$ cat main.go
package main

import (
    "fmt"

    "github.com/Dieterbe/deadtest/foo"
)

func main() {
    fmt.Println(foo.F(10))
}
$ cat ./foo/f.go
package foo

func F(in int) int {
    return in / 10
}
$ cat ./foo/f_test.go
package foo

import "testing"

func TestF(t *testing.T) {
    if F(10) != 1 {
        t.Errorf("F(10) should be 1")
    }
}
$ go test -v github.com/Dieterbe/deadtest/foo
=== RUN   TestF
--- PASS: TestF (0.00s)
PASS
ok      github.com/Dieterbe/deadtest/foo    0.001s
$ deadcode -test .
$ deadcode -test github.com/Dieterbe/deadtest/foo
$
caarlos0 commented 6 years ago

I have the same issue.

ryboe commented 6 years ago

I'm experiencing the same issue. Tests in the main package are falsely reported as unused.

ryboe commented 6 years ago

This issue has been fixed in golangci-lint. I recommend using the forked-and-fixed deadcode in that tool.

golangci-lint run --disable-all --enable deadcode