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

Put comments behind an option in formatter #271

Merged
merged 2 commits into from
Jul 17, 2023
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
20 changes: 15 additions & 5 deletions formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ type Formatter interface {
//nolint:revive // Ignore "stuttering" name format.FormatterOption
type FormatterOption func(*formatter)

// WithIndent uses the given string for indenting block bodies in the output,
// instead of the default, `"\t"`.
func WithIndent(indent string) FormatterOption {
return func(f *formatter) {
f.indent = indent
}
}

// WithComments includes comments from the source/AST in the formatted output.
func WithComments() FormatterOption {
return func(f *formatter) {
f.emitComments = true
}
}

func NewFormatter(w io.Writer, options ...FormatterOption) Formatter {
f := &formatter{
indent: "\t",
Expand All @@ -38,9 +47,10 @@ func NewFormatter(w io.Writer, options ...FormatterOption) Formatter {
type formatter struct {
writer io.Writer

indent string
indentSize int
emitBuiltin bool
indent string
indentSize int
emitBuiltin bool
emitComments bool

padNext bool
lineHead bool
Expand Down Expand Up @@ -714,7 +724,7 @@ func (f *formatter) FormatValue(value *ast.Value) {
}

func (f *formatter) FormatCommentGroup(group *ast.CommentGroup) {
if group == nil {
if !f.emitComments || group == nil {
return
}
for _, comment := range group.List {
Expand All @@ -723,7 +733,7 @@ func (f *formatter) FormatCommentGroup(group *ast.CommentGroup) {
}

func (f *formatter) FormatComment(comment *ast.Comment) {
if comment == nil {
if !f.emitComments || comment == nil {
return
}
f.WriteString("#").WriteString(comment.Text()).WriteNewline()
Expand Down
218 changes: 123 additions & 95 deletions formatter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"os"
"path"
"path/filepath"
"testing"
"unicode/utf8"

Expand All @@ -17,118 +18,145 @@ import (

var update = flag.Bool("u", false, "update golden files")

var optionSets = []struct {
name string
opts []formatter.FormatterOption
}{
{"default", nil},
{"spaceIndent", []formatter.FormatterOption{formatter.WithIndent(" ")}},
{"comments", []formatter.FormatterOption{formatter.WithComments()}},
}

func TestFormatter_FormatSchema(t *testing.T) {
const testSourceDir = "./testdata/source/schema"
const testBaselineDir = "./testdata/baseline/FormatSchema"

executeGoldenTesting(t, &goldenConfig{
SourceDir: testSourceDir,
BaselineFileName: func(cfg *goldenConfig, f os.DirEntry) string {
return path.Join(testBaselineDir, f.Name())
},
Run: func(t *testing.T, cfg *goldenConfig, f os.DirEntry) []byte {
// load stuff
schema, gqlErr := gqlparser.LoadSchema(&ast.Source{
Name: f.Name(),
Input: mustReadFile(path.Join(testSourceDir, f.Name())),
for _, optionSet := range optionSets {
testBaselineDir := filepath.Join(testBaselineDir, optionSet.name)
opts := optionSet.opts
t.Run(optionSet.name, func(t *testing.T) {
executeGoldenTesting(t, &goldenConfig{
SourceDir: testSourceDir,
BaselineFileName: func(cfg *goldenConfig, f os.DirEntry) string {
return path.Join(testBaselineDir, f.Name())
},
Run: func(t *testing.T, cfg *goldenConfig, f os.DirEntry) []byte {
// load stuff
schema, gqlErr := gqlparser.LoadSchema(&ast.Source{
Name: f.Name(),
Input: mustReadFile(path.Join(testSourceDir, f.Name())),
})
if gqlErr != nil {
t.Fatal(gqlErr)
}

// exec format
var buf bytes.Buffer
formatter.NewFormatter(&buf, opts...).FormatSchema(schema)

// validity check
_, gqlErr = gqlparser.LoadSchema(&ast.Source{
Name: f.Name(),
Input: buf.String(),
})
if gqlErr != nil {
t.Log(buf.String())
t.Fatal(gqlErr)
}

return buf.Bytes()
},
})
if gqlErr != nil {
t.Fatal(gqlErr)
}

// exec format
var buf bytes.Buffer
formatter.NewFormatter(&buf).FormatSchema(schema)

// validity check
_, gqlErr = gqlparser.LoadSchema(&ast.Source{
Name: f.Name(),
Input: buf.String(),
})
if gqlErr != nil {
t.Log(buf.String())
t.Fatal(gqlErr)
}

return buf.Bytes()
},
})
})
}
}

func TestFormatter_FormatSchemaDocument(t *testing.T) {
const testSourceDir = "./testdata/source/schema"
const testBaselineDir = "./testdata/baseline/FormatSchemaDocument"

executeGoldenTesting(t, &goldenConfig{
SourceDir: testSourceDir,
BaselineFileName: func(cfg *goldenConfig, f os.DirEntry) string {
return path.Join(testBaselineDir, f.Name())
},
Run: func(t *testing.T, cfg *goldenConfig, f os.DirEntry) []byte {
// load stuff
doc, gqlErr := parser.ParseSchema(&ast.Source{
Name: f.Name(),
Input: mustReadFile(path.Join(testSourceDir, f.Name())),
})
if gqlErr != nil {
t.Fatal(gqlErr)
}

// exec format
var buf bytes.Buffer
formatter.NewFormatter(&buf).FormatSchemaDocument(doc)

// validity check
_, gqlErr = parser.ParseSchema(&ast.Source{
Name: f.Name(),
Input: buf.String(),
for _, optionSet := range optionSets {
testBaselineDir := filepath.Join(testBaselineDir, optionSet.name)
opts := optionSet.opts
t.Run(optionSet.name, func(t *testing.T) {
executeGoldenTesting(t, &goldenConfig{
SourceDir: testSourceDir,
BaselineFileName: func(cfg *goldenConfig, f os.DirEntry) string {
return path.Join(testBaselineDir, f.Name())
},
Run: func(t *testing.T, cfg *goldenConfig, f os.DirEntry) []byte {
// load stuff
doc, gqlErr := parser.ParseSchema(&ast.Source{
Name: f.Name(),
Input: mustReadFile(path.Join(testSourceDir, f.Name())),
})
if gqlErr != nil {
t.Fatal(gqlErr)
}

// exec format
var buf bytes.Buffer
formatter.NewFormatter(&buf, opts...).FormatSchemaDocument(doc)

// validity check
_, gqlErr = parser.ParseSchema(&ast.Source{
Name: f.Name(),
Input: buf.String(),
})
if gqlErr != nil {
t.Log(buf.String())
t.Fatal(gqlErr)
}

return buf.Bytes()
},
})
if gqlErr != nil {
t.Log(buf.String())
t.Fatal(gqlErr)
}

return buf.Bytes()
},
})
})
}
}

func TestFormatter_FormatQueryDocument(t *testing.T) {
const testSourceDir = "./testdata/source/query"
const testBaselineDir = "./testdata/baseline/FormatQueryDocument"

executeGoldenTesting(t, &goldenConfig{
SourceDir: testSourceDir,
BaselineFileName: func(cfg *goldenConfig, f os.DirEntry) string {
return path.Join(testBaselineDir, f.Name())
},
Run: func(t *testing.T, cfg *goldenConfig, f os.DirEntry) []byte {
// load stuff
doc, gqlErr := parser.ParseQuery(&ast.Source{
Name: f.Name(),
Input: mustReadFile(path.Join(testSourceDir, f.Name())),
for _, optionSet := range optionSets {
testBaselineDir := filepath.Join(testBaselineDir, optionSet.name)
opts := optionSet.opts
t.Run(optionSet.name, func(t *testing.T) {
executeGoldenTesting(t, &goldenConfig{
SourceDir: testSourceDir,
BaselineFileName: func(cfg *goldenConfig, f os.DirEntry) string {
return path.Join(testBaselineDir, f.Name())
},
Run: func(t *testing.T, cfg *goldenConfig, f os.DirEntry) []byte {
// load stuff
doc, gqlErr := parser.ParseQuery(&ast.Source{
Name: f.Name(),
Input: mustReadFile(path.Join(testSourceDir, f.Name())),
})
if gqlErr != nil {
t.Fatal(gqlErr)
}

// exec format
var buf bytes.Buffer
formatter.NewFormatter(&buf, opts...).FormatQueryDocument(doc)

// validity check
_, gqlErr = parser.ParseQuery(&ast.Source{
Name: f.Name(),
Input: buf.String(),
})
if gqlErr != nil {
t.Log(buf.String())
t.Fatal(gqlErr)
}

return buf.Bytes()
},
})
if gqlErr != nil {
t.Fatal(gqlErr)
}

// exec format
var buf bytes.Buffer
formatter.NewFormatter(&buf).FormatQueryDocument(doc)

// validity check
_, gqlErr = parser.ParseQuery(&ast.Source{
Name: f.Name(),
Input: buf.String(),
})
if gqlErr != nil {
t.Log(buf.String())
t.Fatal(gqlErr)
}

return buf.Bytes()
},
})
})
}
}

type goldenConfig struct {
Expand Down Expand Up @@ -178,11 +206,11 @@ func executeGoldenTesting(t *testing.T, cfg *goldenConfig) {

expected, err := os.ReadFile(expectedFilePath)
if os.IsNotExist(err) {
err = os.MkdirAll(path.Dir(expectedFilePath), 0755)
err = os.MkdirAll(path.Dir(expectedFilePath), 0o755)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(expectedFilePath, result, 0444)
err = os.WriteFile(expectedFilePath, result, 0o444)
if err != nil {
t.Fatal(err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query FooBarQuery ($after: String!) {
fizzList(first: 100, after: $after) {
nodes {
id
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query {
bar: foo
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
query FooBarQuery ($after: String!) {
fizzList(first: 100, after: $after) {
nodes {
id
... FooFragment
... on Foo {
id
}
... {
id
}
name
}
}
}
fragment FooFragment on Foo {
id
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
query ($first: Int = 30, $after: String!) {
searchCats(first: $first, after: $after) {
nodes {
id
name
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query FooBarQuery ($after: String!) {
fizzList(first: 100, after: $after) {
nodes {
id
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query {
bar: foo
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
query FooBarQuery ($after: String!) {
fizzList(first: 100, after: $after) {
nodes {
id
... FooFragment
... on Foo {
id
}
... {
id
}
name
}
}
}
fragment FooFragment on Foo {
id
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
query ($first: Int = 30, $after: String!) {
searchCats(first: $first, after: $after) {
nodes {
id
name
}
}
}
Empty file.
Loading
Loading