Skip to content
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

refactoring #16

Merged
merged 7 commits into from
Mar 16, 2021
Merged
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
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974 h1:IX6qOQeG5uLjB/hjjwjedwfjND0hgjPMMyO1RoIXQNI=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
Expand Down
1 change: 0 additions & 1 deletion inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func (nrseg *nrseg) Inspect(filename string, src []byte) error {
if _, t := parseParams(fd.Type); !(t == TypeContext || t == TypeHttpRequest) {
return false
}

if !existFromContext(pkg, fd.Body.List[0]) {
nrseg.errFlag = true
nrseg.reportf(filename, fs, fd.Pos(), fd)
Expand Down
25 changes: 16 additions & 9 deletions nrseg.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import (
var (
// ErrShowVersion returns when set version flag.
ErrShowVersion = errors.New("show version")
ErrFlagTrue = errors.New("find error")
)

type nrseg struct {
inspectMode bool
in, dist string
in, dest string
ignoreDirs []string
outStream, errStream io.Writer
errFlag bool
Expand Down Expand Up @@ -51,6 +52,10 @@ func fill(args []string, outStream, errStream io.Writer, version, revision strin
flags.StringVar(&ignoreDirs, "ignore", "", idesc)
flags.StringVar(&ignoreDirs, "i", "", idesc)

var destDir string
odesc := "destination directory."
flags.StringVar(&destDir, "destination", "", odesc)

if err := flags.Parse(args[1:]); err != nil {
return nil, err
}
Expand All @@ -76,6 +81,7 @@ func fill(args []string, outStream, errStream io.Writer, version, revision strin

return &nrseg{
in: dir,
dest: destDir,
ignoreDirs: dirs,
outStream: outStream,
errStream: errStream,
Expand Down Expand Up @@ -120,12 +126,12 @@ func fill2(args []string, outStream, errStream io.Writer, version, revision stri

dir := "./"
nargs := flags.Args()
if len(nargs) > 2 {
if len(nargs) > 1 {
msg := "execution path must be only one or no-set(current directory)."
return nil, fmt.Errorf(msg)
}
if len(nargs) == 2 {
dir = nargs[1]
if len(nargs) == 1 {
dir = nargs[0]
}

return &nrseg{
Expand Down Expand Up @@ -182,8 +188,8 @@ func (n *nrseg) run() error {
return err
}
if !bytes.Equal(org, got) {
if len(n.dist) != 0 && n.in != n.dist {
return n.writeOtherPath(n.in, n.dist, path, got)
if len(n.dest) != 0 && n.in != n.dest {
return n.writeOtherPath(n.in, n.dest, path, got)
}
if _, err := f.WriteAt(got, 0); err != nil {
return err
Expand Down Expand Up @@ -238,11 +244,12 @@ func (n *nrseg) reportf(filename string, fs *token.FileSet, pos token.Pos, fd *a
}
}

p := fs.File(pos).Position(pos)
if len(rcv) != 0 {
fmt.Fprintf(n.outStream, "%s:%d:1: %s.%s no insert segment\n", filename, fs.File(pos).Line(pos), rcv, fd.Name.Name)
fmt.Fprintf(n.outStream, "%s:%d:%d: %s.%s no insert segment\n", p.Filename, p.Line, p.Column, rcv, fd.Name.Name)
return
}
fmt.Fprintf(n.outStream, "%s:%d:1: %s no insert segment\n", filename, fs.File(pos).Line(pos), fd.Name.Name)
fmt.Fprintf(n.outStream, "%s:%d:%d: %s no insert segment\n", p.Filename, p.Line, p.Column, fd.Name.Name)
}

// Run is entry point.
Expand All @@ -259,7 +266,7 @@ func Run(args []string, outStream, errStream io.Writer, version, revision string
}
err = nrseg.run()
if nrseg.errFlag {
err = errors.New("find error")
err = ErrFlagTrue
}
return err
}
75 changes: 72 additions & 3 deletions nrseg_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package nrseg

import (
"bytes"
"errors"
"io"
"io/ioutil"
"os"
Expand All @@ -10,6 +12,73 @@ import (
"github.com/google/go-cmp/cmp"
)

func TestNrseg_Run_Default(t *testing.T) {
dest := t.TempDir()
tests := [...]struct {
name string
want string
args []string
}{
{
name: "basic",
want: "./testdata/want",
args: []string{"nrseg", "-destination", dest, "-i", "ignore", "./testdata/input"},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
out := &bytes.Buffer{}
errs := &bytes.Buffer{}
if err := Run(tt.args, out, errs, "", ""); err != nil {
t.Fatalf("run() error = %v", err)
}
validate(t, dest, tt.want)
})
}
}

func TestNrseg_Run_Inspect(t *testing.T) {
tests := [...]struct {
name string
want string
args []string
}{
{
name: "basic",
args: []string{"nrseg", "inspect", "./testdata/input"},
want: `testdata/input/basic.go:11:1: S.SampleMethod no insert segment
testdata/input/basic.go:16:1: SampleFunc no insert segment
testdata/input/basic.go:21:1: SampleHandler no insert segment
testdata/input/ignore/must_not_change.go:11:1: MustNotChange.SampleMethod no insert segment
testdata/input/ignore/must_not_change.go:16:1: SampleFunc no insert segment
testdata/input/ignore/must_not_change.go:21:1: SampleHandler no insert segment
`,
},
{
name: "ignoreDir",
args: []string{"nrseg", "inspect", "-i", "ignore", "./testdata/input"},
want: `testdata/input/basic.go:11:1: S.SampleMethod no insert segment
testdata/input/basic.go:16:1: SampleFunc no insert segment
testdata/input/basic.go:21:1: SampleHandler no insert segment
`,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
out := &bytes.Buffer{}
errs := &bytes.Buffer{}
if err := Run(tt.args, out, errs, "", ""); !errors.Is(err, ErrFlagTrue) {
t.Fatalf("want %v, but got %v", ErrFlagTrue, err)
}
if out.String() != tt.want {
t.Errorf("want\n%s\nbut got\n%s", tt.want, out.String())
}
})
}
}

func TestNrseg_run(t *testing.T) {
type fields struct {
path string
Expand All @@ -34,18 +103,18 @@ func TestNrseg_run(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
dist := t.TempDir()
dest := t.TempDir()
n := &nrseg{
in: tt.fields.path,
dist: dist,
dest: dest,
ignoreDirs: []string{"testdata", "ignore"},
outStream: tt.fields.outStream,
errStream: tt.fields.errStream,
}
if err := n.run(); err != nil {
t.Fatalf("run() error = %v", err)
}
validate(t, dist, tt.want)
validate(t, dest, tt.want)
})
}
}
Expand Down