Skip to content

Commit

Permalink
Include blank new lines in leading header preprocessing #1462
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Dec 8, 2022
1 parent 8df8d89 commit 83c5e1b
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 7 deletions.
58 changes: 58 additions & 0 deletions acceptance_tests/leading-seperator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,64 @@ EOM
assertEquals "$expected" "$X"
}


testLeadingSeperatorWithNewlinesNewDoc() {
cat >test.yml <<EOL
# hi peeps
# cool
---
a: test
---
b: cool
EOL

read -r -d '' expected << EOM
# hi peeps
# cool
---
a: thing
---
b: cool
EOM

X=$(./yq e '(select(di == 0) | .a) = "thing"' - < test.yml)
assertEquals "$expected" "$X"
}

testLeadingSeperatorWithNewlinesMoreComments() {
cat >test.yml <<EOL
# hi peeps
# cool
---
# great
a: test
---
b: cool
EOL

read -r -d '' expected << EOM
# hi peeps
# cool
---
# great
a: thing
---
b: cool
EOM

X=$(./yq e '(select(di == 0) | .a) = "thing"' - < test.yml)
assertEquals "$expected" "$X"
}


testLeadingSeperatorWithDirective() {
cat >test.yml <<EOL
%YAML 1.1
Expand Down
8 changes: 8 additions & 0 deletions pkg/yqlib/decoder_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func (dec *yamlDecoder) processReadStream(reader *bufio.Reader) (io.Reader, stri
return reader, sb.String(), nil
} else if err != nil {
return reader, sb.String(), err
} else if string(peekBytes[0]) == "\n" {
_, err := reader.ReadString('\n')
sb.WriteString("\n")
if errors.Is(err, io.EOF) {
return reader, sb.String(), nil
} else if err != nil {
return reader, sb.String(), err
}
} else if string(peekBytes) == "---" {
_, err := reader.ReadString('\n')
sb.WriteString("$yqDocSeperator$\n")
Expand Down
3 changes: 3 additions & 0 deletions pkg/yqlib/doc/operators/comment-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ Note the use of `...` to ensure key nodes are included.
Given a sample.yml file of:
```yaml
# hi
a: cat # comment
# great
b: # key comment
Expand All @@ -265,6 +266,7 @@ b:
Given a sample.yml file of:
```yaml
# welcome!
a: cat # meow
# have a great day
```
Expand Down Expand Up @@ -293,6 +295,7 @@ yq '. | head_comment' sample.yml
will output
```yaml
welcome!
```

## Head comment with document split
Expand Down
1 change: 1 addition & 0 deletions pkg/yqlib/doc/operators/path.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Like pick but recursive. This uses `ireduce` to deeply set the selected paths in

Given a sample.yml file of:
```yaml
parentA: bob
parentB:
child1: i am child1
Expand Down
3 changes: 1 addition & 2 deletions pkg/yqlib/operator_anchors_aliases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ var specDocument = `- &CENTER { x: 1, y: 2 }

var expectedSpecResult = "D0, P[4], (!!map)::x: 1\ny: 2\nr: 10\n"

var simpleArrayRef = `
item_value: &item_value
var simpleArrayRef = `item_value: &item_value
value: true
thingOne:
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/operator_comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ var commentOperatorScenarios = []expressionScenario{
document: "# welcome!\n\na: cat # meow\n\n# have a great day",
expression: `. | head_comment`,
expected: []string{
"D0, P[], (!!str)::welcome!\n",
"D0, P[], (!!str)::welcome!\n\n",
},
},
{
Expand Down
4 changes: 2 additions & 2 deletions pkg/yqlib/operator_multiply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ var multiplyOperatorScenarios = []expressionScenario{
document2: docNoComments,
expression: `select(fi == 0) * select(fi == 1)`,
expected: []string{
"D0, P[], (!!map)::# here\na: apple\nb: banana\n",
"D0, P[], (!!map)::# here\n\na: apple\nb: banana\n",
},
},
{
Expand All @@ -126,7 +126,7 @@ var multiplyOperatorScenarios = []expressionScenario{
document2: docWithHeader,
expression: `select(fi == 0) * select(fi == 1)`,
expected: []string{
"D0, P[], (!!map)::# here\nb: banana\na: apple\n",
"D0, P[], (!!map)::# here\n\nb: banana\na: apple\n",
},
},
{
Expand Down
3 changes: 1 addition & 2 deletions pkg/yqlib/operator_traverse_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"testing"
)

var mergeDocSample = `
foo: &foo
var mergeDocSample = `foo: &foo
a: foo_a
thing: foo_thing
c: foo_c
Expand Down

0 comments on commit 83c5e1b

Please sign in to comment.