Skip to content

Commit 399324c

Browse files
authored
command: correctly propogate line_ending setting (#216)
* command: correctly propogate line_ending setting The line_ending setting from the formatter was accidentally being ignored in favour of only checking the top level. This PR refactors the code while taking this edge case into account. * undo unwanted changes that I left in * add license header to new test file
1 parent 3e85e19 commit 399324c

File tree

3 files changed

+76
-29
lines changed

3 files changed

+76
-29
lines changed

command/command.go

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -60,35 +60,9 @@ type Command struct {
6060
}
6161

6262
func (c *Command) Run() error {
63-
var formatter yamlfmt.Formatter
64-
if c.Config.FormatterConfig == nil {
65-
factory, err := c.Registry.GetDefaultFactory()
66-
if err != nil {
67-
return err
68-
}
69-
formatter, err = factory.NewFormatter(nil)
70-
if err != nil {
71-
return err
72-
}
73-
} else {
74-
var (
75-
factory yamlfmt.Factory
76-
err error
77-
)
78-
if c.Config.FormatterConfig.Type == "" {
79-
factory, err = c.Registry.GetDefaultFactory()
80-
} else {
81-
factory, err = c.Registry.GetFactory(c.Config.FormatterConfig.Type)
82-
}
83-
if err != nil {
84-
return err
85-
}
86-
87-
c.Config.FormatterConfig.FormatterSettings["line_ending"] = c.Config.LineEnding
88-
formatter, err = factory.NewFormatter(c.Config.FormatterConfig.FormatterSettings)
89-
if err != nil {
90-
return err
91-
}
63+
formatter, err := c.getFormatter()
64+
if err != nil {
65+
return err
9266
}
9367

9468
lineSepChar, err := c.Config.LineEnding.Separator()
@@ -191,6 +165,30 @@ func (c *Command) Run() error {
191165
return nil
192166
}
193167

168+
func (c *Command) getFormatter() (yamlfmt.Formatter, error) {
169+
var factoryType string
170+
171+
// In the existing codepaths, this value is always set. But
172+
// it's a habit of mine to check anything that can possibly be nil
173+
// if I remember that to be the case. :)
174+
if c.Config.FormatterConfig != nil {
175+
factoryType = c.Config.FormatterConfig.Type
176+
177+
// The line ending set within the formatter settings takes precedence over setting
178+
// it from the top level config. If it's not set in formatter settings, then
179+
// we use the value from the top level.
180+
if _, ok := c.Config.FormatterConfig.FormatterSettings["line_ending"]; !ok {
181+
c.Config.FormatterConfig.FormatterSettings["line_ending"] = c.Config.LineEnding
182+
}
183+
}
184+
185+
factory, err := c.Registry.GetFactory(factoryType)
186+
if err != nil {
187+
return nil, err
188+
}
189+
return factory.NewFormatter(c.Config.FormatterConfig.FormatterSettings)
190+
}
191+
194192
func (c *Command) collectPaths() ([]string, error) {
195193
collector := c.makePathCollector()
196194
return collector.CollectPaths()

command/command_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package command
16+
17+
import (
18+
"testing"
19+
20+
"github.com/google/yamlfmt"
21+
"github.com/google/yamlfmt/formatters/basic"
22+
"github.com/google/yamlfmt/internal/assert"
23+
)
24+
25+
// This test asserts the proper behaviour for `line_ending` settings specified
26+
// in formatter settings overriding the global configuration.
27+
func TestLineEndingFormatterVsGloabl(t *testing.T) {
28+
c := &Command{
29+
Config: &Config{
30+
LineEnding: "lf",
31+
FormatterConfig: &FormatterConfig{
32+
FormatterSettings: map[string]any{
33+
"line_ending": yamlfmt.LineBreakStyleLF,
34+
},
35+
},
36+
},
37+
Registry: yamlfmt.NewFormatterRegistry(&basic.BasicFormatterFactory{}),
38+
}
39+
40+
f, err := c.getFormatter()
41+
assert.NilErr(t, err)
42+
configMap, err := f.ConfigMap()
43+
assert.NilErr(t, err)
44+
formatterLineEnding := configMap["line_ending"].(yamlfmt.LineBreakStyle)
45+
assert.Assert(t, formatterLineEnding == yamlfmt.LineBreakStyleLF, "expected formatter's line ending to be lf")
46+
}

formatter.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ func (r *Registry) Add(f Factory) {
4646
}
4747

4848
func (r *Registry) GetFactory(fType string) (Factory, error) {
49+
if fType == "" {
50+
return r.GetDefaultFactory()
51+
}
4952
factory, ok := r.registry[fType]
5053
if !ok {
5154
return nil, fmt.Errorf("no formatter registered with type \"%s\"", fType)

0 commit comments

Comments
 (0)