2021-07-18 06:55:08 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2022-09-29 23:36:38 +00:00
|
|
|
|
|
|
|
# examples where header-preprocess is required
|
|
|
|
|
2021-07-18 06:55:08 +00:00
|
|
|
setUp() {
|
2021-10-29 03:14:39 +00:00
|
|
|
rm test*.yml || true
|
2021-07-18 06:55:08 +00:00
|
|
|
cat >test.yml <<EOL
|
|
|
|
---
|
|
|
|
a: test
|
|
|
|
EOL
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorWithDoc() {
|
2021-07-19 09:52:51 +00:00
|
|
|
cat >test.yml <<EOL
|
|
|
|
# hi peeps
|
|
|
|
# cool
|
|
|
|
---
|
|
|
|
a: test
|
2021-07-19 10:18:42 +00:00
|
|
|
---
|
|
|
|
b: cool
|
2021-07-19 09:52:51 +00:00
|
|
|
EOL
|
|
|
|
|
|
|
|
read -r -d '' expected << EOM
|
|
|
|
# hi peeps
|
|
|
|
# cool
|
|
|
|
---
|
|
|
|
a: thing
|
2021-07-19 10:18:42 +00:00
|
|
|
---
|
|
|
|
b: cool
|
2021-07-19 09:52:51 +00:00
|
|
|
EOM
|
|
|
|
|
2021-07-19 10:18:42 +00:00
|
|
|
X=$(./yq e '(select(di == 0) | .a) = "thing"' - < test.yml)
|
2021-07-19 09:52:51 +00:00
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
2022-12-08 02:33:06 +00:00
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorWithNewlinesNewDoc() {
|
2022-12-08 02:33:06 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorWithNewlinesMoreComments() {
|
2022-12-08 02:33:06 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorWithDirective() {
|
2022-11-14 06:22:13 +00:00
|
|
|
cat >test.yml <<EOL
|
|
|
|
%YAML 1.1
|
|
|
|
---
|
|
|
|
this: should really work
|
|
|
|
EOL
|
|
|
|
|
|
|
|
read -r -d '' expected << EOM
|
|
|
|
%YAML 1.1
|
|
|
|
---
|
|
|
|
this: should really work
|
|
|
|
EOM
|
|
|
|
|
|
|
|
X=$(./yq < test.yml)
|
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorPipeIntoEvalSeq() {
|
2021-07-18 07:05:12 +00:00
|
|
|
X=$(./yq e - < test.yml)
|
2021-07-18 06:55:08 +00:00
|
|
|
expected=$(cat test.yml)
|
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorExtractField() {
|
2021-07-20 01:12:41 +00:00
|
|
|
X=$(./yq e '.a' - < test.yml)
|
|
|
|
assertEquals "test" "$X"
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorExtractFieldWithCommentsAfterSep() {
|
2021-07-20 01:12:41 +00:00
|
|
|
cat >test.yml <<EOL
|
|
|
|
---
|
|
|
|
# hi peeps
|
|
|
|
# cool
|
|
|
|
a: test
|
|
|
|
EOL
|
|
|
|
X=$(./yq e '.a' test.yml)
|
|
|
|
assertEquals "test" "$X"
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorExtractFieldWithCommentsBeforeSep() {
|
2021-07-20 01:12:41 +00:00
|
|
|
cat >test.yml <<EOL
|
|
|
|
# hi peeps
|
|
|
|
# cool
|
|
|
|
---
|
|
|
|
a: test
|
|
|
|
EOL
|
|
|
|
X=$(./yq e '.a' test.yml)
|
|
|
|
assertEquals "test" "$X"
|
|
|
|
}
|
|
|
|
|
2021-07-18 06:55:08 +00:00
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorExtractFieldMultiDoc() {
|
2021-07-20 01:14:54 +00:00
|
|
|
cat >test.yml <<EOL
|
|
|
|
---
|
|
|
|
a: test
|
|
|
|
---
|
|
|
|
a: test2
|
|
|
|
EOL
|
|
|
|
|
|
|
|
read -r -d '' expected << EOM
|
|
|
|
test
|
|
|
|
---
|
|
|
|
test2
|
|
|
|
EOM
|
|
|
|
X=$(./yq e '.a' test.yml)
|
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorExtractFieldMultiDocWithComments() {
|
2021-07-20 01:14:54 +00:00
|
|
|
cat >test.yml <<EOL
|
|
|
|
# here
|
|
|
|
---
|
|
|
|
# there
|
|
|
|
a: test
|
2023-09-26 04:18:18 +00:00
|
|
|
# wherever
|
2021-07-20 01:14:54 +00:00
|
|
|
---
|
|
|
|
# you are
|
|
|
|
a: test2
|
|
|
|
# woop
|
|
|
|
EOL
|
|
|
|
|
|
|
|
read -r -d '' expected << EOM
|
|
|
|
test
|
|
|
|
---
|
|
|
|
test2
|
|
|
|
EOM
|
|
|
|
X=$(./yq e '.a' test.yml)
|
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorEvalSeq() {
|
2021-07-18 06:55:08 +00:00
|
|
|
X=$(./yq e test.yml)
|
|
|
|
expected=$(cat test.yml)
|
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorPipeIntoEvalAll() {
|
2021-07-18 07:05:12 +00:00
|
|
|
X=$(./yq ea - < test.yml)
|
2021-07-18 06:55:08 +00:00
|
|
|
expected=$(cat test.yml)
|
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorEvalAll() {
|
2021-07-18 06:55:08 +00:00
|
|
|
X=$(./yq ea test.yml)
|
|
|
|
expected=$(cat test.yml)
|
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorMultiDocEvalSimple() {
|
2021-07-18 06:55:08 +00:00
|
|
|
read -r -d '' expected << EOM
|
|
|
|
---
|
|
|
|
a: test
|
|
|
|
---
|
|
|
|
version: 3
|
|
|
|
application: MyApp
|
|
|
|
EOM
|
|
|
|
|
|
|
|
|
|
|
|
X=$(./yq e '.' test.yml examples/order.yaml)
|
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorMultiDocInOneFile() {
|
2021-07-19 09:52:51 +00:00
|
|
|
cat >test.yml <<EOL
|
|
|
|
---
|
|
|
|
# hi peeps
|
|
|
|
# cool
|
|
|
|
a: test
|
|
|
|
---
|
|
|
|
b: things
|
|
|
|
EOL
|
|
|
|
expected=$(cat test.yml)
|
|
|
|
X=$(./yq e '.' test.yml)
|
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorMultiDocInOneFileEvalAll() {
|
2021-07-19 10:18:42 +00:00
|
|
|
cat >test.yml <<EOL
|
|
|
|
---
|
|
|
|
# hi peeps
|
|
|
|
# cool
|
|
|
|
a: test
|
|
|
|
---
|
|
|
|
b: things
|
|
|
|
EOL
|
|
|
|
expected=$(cat test.yml)
|
|
|
|
X=$(./yq ea '.' test.yml)
|
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorMultiDocEvalComments() {
|
2021-07-19 09:52:51 +00:00
|
|
|
cat >test.yml <<EOL
|
|
|
|
# hi peeps
|
|
|
|
# cool
|
|
|
|
a: test
|
|
|
|
EOL
|
|
|
|
|
|
|
|
cat >test2.yml <<EOL
|
|
|
|
# this is another doc
|
|
|
|
# great
|
|
|
|
b: sane
|
|
|
|
EOL
|
|
|
|
|
|
|
|
read -r -d '' expected << EOM
|
|
|
|
# hi peeps
|
|
|
|
# cool
|
|
|
|
a: test
|
|
|
|
---
|
|
|
|
# this is another doc
|
|
|
|
# great
|
|
|
|
b: sane
|
|
|
|
EOM
|
|
|
|
|
|
|
|
|
|
|
|
X=$(./yq e '.' test.yml test2.yml)
|
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorMultiDocEvalCommentsTrailingSep() {
|
2021-07-19 09:52:51 +00:00
|
|
|
cat >test.yml <<EOL
|
|
|
|
# hi peeps
|
|
|
|
# cool
|
|
|
|
---
|
|
|
|
a: test
|
|
|
|
EOL
|
|
|
|
|
|
|
|
cat >test2.yml <<EOL
|
|
|
|
# this is another doc
|
|
|
|
# great
|
|
|
|
---
|
|
|
|
b: sane
|
|
|
|
EOL
|
|
|
|
|
|
|
|
read -r -d '' expected << EOM
|
|
|
|
# hi peeps
|
|
|
|
# cool
|
|
|
|
---
|
|
|
|
a: test
|
|
|
|
---
|
|
|
|
# this is another doc
|
|
|
|
# great
|
|
|
|
---
|
|
|
|
b: sane
|
|
|
|
EOM
|
|
|
|
|
|
|
|
|
|
|
|
X=$(./yq e '.' test.yml test2.yml)
|
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorMultiMultiDocEvalCommentsTrailingSep() {
|
2021-07-19 10:18:42 +00:00
|
|
|
cat >test.yml <<EOL
|
|
|
|
# hi peeps
|
|
|
|
# cool
|
|
|
|
---
|
|
|
|
a: test
|
|
|
|
---
|
|
|
|
a1: test2
|
|
|
|
EOL
|
|
|
|
|
|
|
|
cat >test2.yml <<EOL
|
|
|
|
# this is another doc
|
|
|
|
# great
|
|
|
|
---
|
|
|
|
b: sane
|
|
|
|
---
|
|
|
|
b2: cool
|
|
|
|
EOL
|
|
|
|
|
|
|
|
read -r -d '' expected << EOM
|
|
|
|
# hi peeps
|
|
|
|
# cool
|
|
|
|
---
|
|
|
|
a: test
|
|
|
|
---
|
|
|
|
a1: test2
|
|
|
|
---
|
|
|
|
# this is another doc
|
|
|
|
# great
|
|
|
|
---
|
|
|
|
b: sane
|
|
|
|
---
|
|
|
|
b2: cool
|
|
|
|
EOM
|
|
|
|
|
|
|
|
|
|
|
|
X=$(./yq e '.' test.yml test2.yml)
|
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorMultiDocEvalCommentsLeadingSep() {
|
2021-07-19 09:52:51 +00:00
|
|
|
cat >test.yml <<EOL
|
|
|
|
---
|
|
|
|
# hi peeps
|
|
|
|
# cool
|
|
|
|
a: test
|
|
|
|
EOL
|
|
|
|
|
|
|
|
cat >test2.yml <<EOL
|
|
|
|
---
|
|
|
|
# this is another doc
|
|
|
|
# great
|
|
|
|
b: sane
|
|
|
|
EOL
|
|
|
|
|
|
|
|
read -r -d '' expected << EOM
|
|
|
|
---
|
|
|
|
# hi peeps
|
|
|
|
# cool
|
|
|
|
a: test
|
|
|
|
---
|
|
|
|
# this is another doc
|
|
|
|
# great
|
|
|
|
b: sane
|
|
|
|
EOM
|
|
|
|
|
|
|
|
|
|
|
|
X=$(./yq e '.' test.yml test2.yml)
|
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
2021-08-26 06:31:26 +00:00
|
|
|
# https://github.com/mikefarah/yq/issues/919
|
|
|
|
testLeadingSeparatorDoesNotBreakCommentsOnOtherFiles() {
|
|
|
|
cat >test.yml <<EOL
|
|
|
|
# a1
|
|
|
|
a: 1
|
|
|
|
# a2
|
|
|
|
EOL
|
|
|
|
|
|
|
|
cat >test2.yml <<EOL
|
|
|
|
# b1
|
|
|
|
b: 2
|
|
|
|
# b2
|
|
|
|
EOL
|
|
|
|
|
|
|
|
read -r -d '' expected << EOM
|
|
|
|
# a1
|
|
|
|
a: 1
|
|
|
|
# a2
|
|
|
|
|
|
|
|
# b1
|
|
|
|
b: 2
|
|
|
|
# b2
|
|
|
|
EOM
|
|
|
|
|
|
|
|
|
|
|
|
X=$(./yq ea 'select(fi == 0) * select(fi == 1)' test.yml test2.yml)
|
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorMultiDocEvalCommentsStripComments() {
|
2021-07-19 23:18:40 +00:00
|
|
|
cat >test.yml <<EOL
|
|
|
|
---
|
|
|
|
# hi peeps
|
|
|
|
# cool
|
|
|
|
a: test
|
|
|
|
---
|
|
|
|
# this is another doc
|
|
|
|
# great
|
|
|
|
b: sane
|
|
|
|
EOL
|
|
|
|
|
|
|
|
# it will be hard to remove that top level separator
|
|
|
|
read -r -d '' expected << EOM
|
|
|
|
a: test
|
|
|
|
---
|
|
|
|
b: sane
|
|
|
|
EOM
|
|
|
|
|
2021-07-20 00:19:55 +00:00
|
|
|
X=$(./yq e '... comments=""' test.yml)
|
2021-07-19 23:18:40 +00:00
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorMultiDocEvalCommentsLeadingSepNoDocFlag() {
|
2021-07-19 23:18:40 +00:00
|
|
|
cat >test.yml <<EOL
|
|
|
|
---
|
|
|
|
# hi peeps
|
|
|
|
# cool
|
|
|
|
a: test
|
|
|
|
---
|
|
|
|
# this is another doc
|
|
|
|
# great
|
|
|
|
b: sane
|
|
|
|
EOL
|
|
|
|
|
|
|
|
read -r -d '' expected << EOM
|
|
|
|
# hi peeps
|
|
|
|
# cool
|
|
|
|
a: test
|
|
|
|
# this is another doc
|
|
|
|
# great
|
|
|
|
b: sane
|
|
|
|
EOM
|
|
|
|
|
|
|
|
|
|
|
|
X=$(./yq e '.' --no-doc test.yml)
|
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorMultiDocEvalJsonFlag() {
|
2021-07-19 23:18:40 +00:00
|
|
|
cat >test.yml <<EOL
|
|
|
|
---
|
|
|
|
# hi peeps
|
|
|
|
# cool
|
|
|
|
a: test
|
|
|
|
EOL
|
|
|
|
|
|
|
|
cat >test2.yml <<EOL
|
|
|
|
---
|
|
|
|
# this is another doc
|
|
|
|
# great
|
|
|
|
b: sane
|
|
|
|
EOL
|
|
|
|
|
|
|
|
read -r -d '' expected << EOM
|
|
|
|
{
|
|
|
|
"a": "test"
|
|
|
|
}
|
|
|
|
{
|
|
|
|
"b": "sane"
|
|
|
|
}
|
|
|
|
EOM
|
|
|
|
|
|
|
|
|
|
|
|
X=$(./yq e '.' -j test.yml test2.yml)
|
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorMultiDocEvalAllJsonFlag() {
|
2021-07-19 23:18:40 +00:00
|
|
|
cat >test.yml <<EOL
|
|
|
|
---
|
|
|
|
# hi peeps
|
|
|
|
# cool
|
|
|
|
a: test
|
|
|
|
EOL
|
|
|
|
|
|
|
|
cat >test2.yml <<EOL
|
|
|
|
---
|
|
|
|
# this is another doc
|
|
|
|
# great
|
|
|
|
b: sane
|
|
|
|
EOL
|
|
|
|
|
|
|
|
read -r -d '' expected << EOM
|
|
|
|
{
|
|
|
|
"a": "test"
|
|
|
|
}
|
|
|
|
{
|
|
|
|
"b": "sane"
|
|
|
|
}
|
|
|
|
EOM
|
|
|
|
|
|
|
|
|
|
|
|
X=$(./yq ea '.' -j test.yml test2.yml)
|
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
testLeadingSeparatorMultiDocEvalAll() {
|
2021-07-18 06:55:08 +00:00
|
|
|
read -r -d '' expected << EOM
|
|
|
|
---
|
|
|
|
a: test
|
|
|
|
---
|
|
|
|
version: 3
|
|
|
|
application: MyApp
|
|
|
|
EOM
|
|
|
|
|
|
|
|
|
|
|
|
X=$(./yq ea '.' test.yml examples/order.yaml)
|
|
|
|
assertEquals "$expected" "$X"
|
|
|
|
}
|
|
|
|
|
|
|
|
source ./scripts/shunit2
|