Skip to content

Commit

Permalink
feat(fixer.go): introduce ErrFuncNotFound for standardized error hand…
Browse files Browse the repository at this point in the history
…ling

refactor(fixer.go): use fmt.Errorf with %w for error wrapping
test(fixer_test.go): update tests to use errors.Is for error comparison
feat(fixer_test.go): add test case for ErrFuncNotFound
build(go.mod, go.sum): add dependencies for rsc.io/script and golang.org/x/tools
test(main_test.go): add golden file testing for main function
feat(testdata/golden.json): add golden file for testing expected output

feat(testdata): add lookup.go and main.go.golden for testing purposes
chore: remove output.json to clean up test data artifacts
  • Loading branch information
yyoshiki41 committed May 5, 2024
1 parent 6135a87 commit be073f1
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 40 deletions.
6 changes: 5 additions & 1 deletion fixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"strings"
)

var (
ErrFuncNotFound = fmt.Errorf("not found")
)

func fix(filename, functionName string) error {
start, end, err := lookup(filename, functionName)
if err != nil {
Expand Down Expand Up @@ -65,7 +69,7 @@ func lookup(filename, functionName string) (int, int, error) {
return true
})
if start == 0 || end == 0 {
return 0, 0, fmt.Errorf("func %s not found in %s", functionName, filename)
return 0, 0, fmt.Errorf("%w, func %s in %s", ErrFuncNotFound, functionName, filename)
}
return start, end, nil
}
Expand Down
10 changes: 6 additions & 4 deletions fixer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"testing"
)

Expand All @@ -12,16 +13,17 @@ func TestLookup(t *testing.T) {
start, end int
err error
}{
{"testdata/main.go", "Reachable", 14, 16, nil},
{"testdata/main.go", "myString.String", 33, 35, nil},
{"testdata/main.go", "myString.Unreachable", 41, 43, nil},
{"testdata/lookup.go", "Reachable", 14, 16, nil},
{"testdata/lookup.go", "myString.String", 33, 35, nil},
{"testdata/lookup.go", "myString.Unreachable", 41, 43, nil},
{"testdata/lookup.go", "NotFound", 0, 0, ErrFuncNotFound},
}

for _, tt := range tests {
t.Run(tt.filename+":"+tt.functionName, func(t *testing.T) {
t.Parallel()
start, end, err := lookup(tt.filename, tt.functionName)
if start != tt.start || end != tt.end || err != tt.err {
if start != tt.start || end != tt.end || !errors.Is(err, tt.err) {
t.Errorf("lookup(%q, %q) = (%d, %d, %v); want (%d, %d, %v)",
tt.filename, tt.functionName, start, end, err, tt.start, tt.end, tt.err)
}
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module github.com/yyoshiki41/rm-deadcode

go 1.22.2

require rsc.io/script v0.0.2

require golang.org/x/tools v0.14.0 // indirect
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc=
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
rsc.io/script v0.0.2 h1:eYoG7A3GFC3z1pRx3A2+s/vZ9LA8cxojHyCvslnj4RI=
rsc.io/script v0.0.2/go.mod h1:cKBjCtFBBeZ0cbYFRXkRoxP+xGqhArPa9t3VWhtXfzU=
42 changes: 42 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"context"
"os"
"testing"

"rsc.io/script"
)

func TestGolden(t *testing.T) {
ctx := context.Background()
state, err := script.NewState(ctx, "./", nil)
if err != nil {
t.Fatal(err)
}
_, err = script.Cp().Run(state, "testdata/main.go", "testdata/main.go.backup")
if err != nil {
t.Fatal(err)
}
defer func() {
_, err = script.Mv().Run(state, "testdata/main.go.backup", "testdata/main.go")
if err != nil {
t.Fatal(err)
}
}()

f, err := os.Open("testdata/golden.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()

os.Stdin = f
main()

got, err := os.Stat("testdata/main.go")
expected, err := os.Stat("testdata/main.go.golden")
if !os.SameFile(got, expected) {
t.Errorf("unexpected file content")
}
}
35 changes: 35 additions & 0 deletions testdata/golden.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"Name": "main",
"Path": "github.com/yyoshiki41/rm-deadcode/testdata",
"Funcs": [
{
"Name": "Unreachable",
"Position": {
"File": "testdata/main.go",
"Line": 18,
"Col": 6
},
"Generated": false
},
{
"Name": "myString.String",
"Position": {
"File": "testdata/main.go",
"Line": 33,
"Col": 19
},
"Generated": false
},
{
"Name": "myString.Unreachable",
"Position": {
"File": "testdata/main.go",
"Line": 41,
"Col": 20
},
"Generated": false
}
]
}
]
43 changes: 43 additions & 0 deletions testdata/lookup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import "fmt"

func main() {
Reachable()
}

func init() {
s := myString{Value: "hello"}
s.Reachable()
}

func Reachable() {
fmt.Println("reachable")
}

func Unreachable() {
fmt.Println("unreachable")
}

func ReachableByTest() {
fmt.Println("reachableByTest")
}

var _ fmt.Stringer = myString{}

type myString struct {
Value string
}

// NOTE: This function is unreachable but it is neccessary to implement the fmt.Stringer interface
func (s myString) String() string {
return s.Value
}

func (s myString) Reachable() {
return
}

func (s *myString) Unreachable() {
return
}
32 changes: 32 additions & 0 deletions testdata/main.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import "fmt"

func main() {
Reachable()
}

func init() {
s := myString{Value: "hello"}
s.Reachable()
}

func Reachable() {
fmt.Println("reachable")
}

func ReachableByTest() {
fmt.Println("reachableByTest")
}

var _ fmt.Stringer = myString{}

type myString struct {
Value string
}

// NOTE: This function is unreachable but it is neccessary to implement the fmt.Stringer interface

func (s myString) Reachable() {
return
}
35 changes: 0 additions & 35 deletions testdata/output.json

This file was deleted.

0 comments on commit be073f1

Please sign in to comment.