@@ -19,8 +19,10 @@ package main
1919// TODO: This does not support embedding package-local interfaces in a separate file.
2020
2121import (
22+ "bytes"
2223 "flag"
2324 "fmt"
25+ "go/format"
2426 "go/token"
2527 "io"
2628 "log"
@@ -86,9 +88,7 @@ func main() {
8688 packageName = "mock_" + sanitize (pkg .Name )
8789 }
8890
89- g := generator {
90- w : dst ,
91- }
91+ g := new (generator )
9292 if * source != "" {
9393 g .filename = * source
9494 } else {
@@ -98,6 +98,9 @@ func main() {
9898 if err := g .Generate (pkg , packageName ); err != nil {
9999 log .Fatalf ("Failed generating mock: %v" , err )
100100 }
101+ if _ , err := dst .Write (g .Output ()); err != nil {
102+ log .Fatalf ("Failed writing to destination: %v" , err )
103+ }
101104}
102105
103106func usage () {
@@ -123,7 +126,7 @@ Example:
123126`
124127
125128type generator struct {
126- w io. Writer
129+ buf bytes. Buffer
127130 indent string
128131
129132 filename string // may be empty
@@ -133,7 +136,7 @@ type generator struct {
133136}
134137
135138func (g * generator ) p (format string , args ... interface {}) {
136- fmt .Fprintf (g . w , g .indent + format + "\n " , args ... )
139+ fmt .Fprintf (& g . buf , g .indent + format + "\n " , args ... )
137140}
138141
139142func (g * generator ) in () {
@@ -411,3 +414,12 @@ func (g *generator) GenerateMockRecorderMethod(mockType string, m *model.Method)
411414 g .p ("}" )
412415 return nil
413416}
417+
418+ // Output returns the generator's output, formatted in the standard Go style.
419+ func (g * generator ) Output () []byte {
420+ src , err := format .Source (g .buf .Bytes ())
421+ if err != nil {
422+ log .Fatalf ("Failed to format generated source code: %s\n %s" , err , g .buf .String ())
423+ }
424+ return src
425+ }
0 commit comments