Skip to content

Commit

Permalink
bug: Fix STDIN/STDOUT buffer issue on code generation with code forma…
Browse files Browse the repository at this point in the history
…tters

There was a problem that it cannot generate code If the input size is too big, because of the buffer of the OS.
This commit fixes that problem by using `exec.Cmd#StdinPipe()` and removes some redundant/unsophisticated code.

As far as I checked, this problem appeared on only Linux.

Signed-off-by: moznion <[email protected]>
  • Loading branch information
moznion committed Feb 22, 2021
1 parent 78541c0 commit 07420ca
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
24 changes: 14 additions & 10 deletions generator/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,29 @@ func (g *Root) applyGoimports(generatedCode string) (string, error) {
}

func applyCodeFormatter(generatedCode string, formatterCmdName string, formatterOpts ...string) (string, error) {
echoCmd := exec.Command("echo", generatedCode)
formatterCmd := exec.Command(formatterCmdName, formatterOpts...)

r, w := io.Pipe()
echoCmd.Stdout = w
formatterCmd.Stdin = r
stdinPipe, _ := formatterCmd.StdinPipe()

var out, errout bytes.Buffer
formatterCmd.Stdout = &out
formatterCmd.Stderr = &errout

echoCmd.Start()
if err := formatterCmd.Start(); err != nil {
err := formatterCmd.Start()
if err != nil {
cmds := []string{formatterCmdName}
return "", errmsg.CodeFormatterError(strings.Join(append(cmds, formatterOpts...), " "), errout.String(), err)
}
echoCmd.Wait()
w.Close()
err := formatterCmd.Wait()

_, err = io.WriteString(stdinPipe, generatedCode)
if err != nil {
return "", err
}
err = stdinPipe.Close()
if err != nil {
return "", err
}

err = formatterCmd.Wait()
if err != nil {
cmds := []string{formatterCmdName}
return "", errmsg.CodeFormatterError(strings.Join(append(cmds, formatterOpts...), " "), errout.String(), err)
Expand Down
30 changes: 29 additions & 1 deletion generator/root_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package generator

import (
"fmt"
"regexp"
"strings"
"testing"

"github.com/moznion/gowrtr/internal/errmsg"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -517,3 +517,31 @@ func TestShouldGenerateCodeRaiseErrorWhenCodeFormatterIsExited(t *testing.T) {
assert.Regexp(t, regexp.MustCompile(`^\[GOWRTR-13\] code formatter raises error: command='not-existed-cmd'.+`), err.Error())
}
}

// to check STDIN buffer behavior with code formatter
func TestMassiveAmountCode(t *testing.T) {
generator := NewRoot(
NewComment(" THIS CODE WAS AUTO GENERATED"),
NewNewline(),
NewPackage("mypkg"),
NewNewline(),
NewImport("fmt"),
NewNewline(),
)

for i := 0; i <= 1000; i++ {
generator = generator.AddStatements(
NewFunc(
nil,
NewFuncSignature(fmt.Sprintf("f%d", i)),
NewRawStatement(
`fmt.Println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")`,
).WithNewline(true),
),
)
}

generated, err := generator.Gofmt().Generate(0)
assert.NoError(t, err)
assert.NotEmpty(t, generated)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/moznion/gowrtr
go 1.14

require (
github.com/iancoleman/strcase v0.1.3 // indirect
github.com/moznion/go-errgen v1.8.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.6.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/iancoleman/strcase v0.0.0-20180726023541-3605ed457bf7 h1:ux/56T2xqZO/3cP1I2F86qpeoYPCOzk+KF/UH/Ar+lk=
github.com/iancoleman/strcase v0.0.0-20180726023541-3605ed457bf7/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/iancoleman/strcase v0.1.3 h1:dJBk1m2/qjL1twPLf68JND55vvivMupZ4wIzE8CTdBw=
github.com/iancoleman/strcase v0.1.3/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/moznion/go-errgen v1.3.1/go.mod h1:+BkSRLNc98WWAJmdtxYcxxucQ7sl8m4sroepmHio33Y=
github.com/moznion/go-errgen v1.8.1 h1:jxaMIi1WiX/N9xnOecBN3ZQUhbBgnrvedSjNmU7EXFo=
github.com/moznion/go-errgen v1.8.1/go.mod h1:3mE7v+d7czyIqbfd3jOK/wLgRn5lTOtNRNEeCHXHaCk=
Expand Down

0 comments on commit 07420ca

Please sign in to comment.