Skip to content

Commit

Permalink
Merge pull request #28 from wwkimball/development
Browse files Browse the repository at this point in the history
Fix yaml-set forgot to keep blockiness of values
  • Loading branch information
wwkimball authored May 29, 2019
2 parents ba6ebd5 + 6fa703d commit a97b836
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2.0.1:
Bug Fixes:
* yaml-set v1.0.4 lost track of EYAML block formatting between read and write,
causing replacement values to use unexpected formatting. This is fixed in
yaml-set v.1.0.5.

2.0.0:
Enhancements:
* Added Collectors to YAML Path expressions. These take the form of "(YAML
Expand Down
18 changes: 13 additions & 5 deletions bin/yaml-set
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ from ruamel.yaml.parser import ParserError

from yamlpath import YAMLPath
from yamlpath.exceptions import YAMLPathException
from yamlpath.enums import YAMLValueFormats, PathSeperators
from yamlpath.eyaml.exceptions import EYAMLCommandException
from yamlpath.eyaml.enums import EYAMLOutputFormats
from yamlpath.eyaml import EYAMLProcessor
from yamlpath.enums import YAMLValueFormats, PathSeperators

# pylint: disable=locally-disabled,unused-import
import yamlpath.patches
from yamlpath.func import clone_node
from yamlpath.wrappers import ConsolePrinter

# Implied Constants
MY_VERSION = "1.0.4"
MY_VERSION = "1.0.5"

def processcli():
"""Process command-line arguments."""
Expand Down Expand Up @@ -237,6 +239,12 @@ def main():
if not change_nodes:
log.warning("Nothing to do!")
exit(0)
elif len(change_nodes) == 1:
# When there is exactly one result, its old format can be known. This
# is necessary to retain whether the replacement value should be
# represented later as a multi-line string when the new value is to be
# encrypted.
old_format = YAMLValueFormats.from_node(change_nodes[0])

log.debug("Collected nodes:")
log.debug(change_nodes)
Expand Down Expand Up @@ -292,7 +300,6 @@ def main():
# format, despite being properly typed. To restore the original
# written form, reverse the conversion, here.
old_value = change_nodes[0]
old_format = YAMLValueFormats.from_node(old_value)
if (
(old_format is YAMLValueFormats.FOLDED
or old_format is YAMLValueFormats.LITERAL
Expand All @@ -311,7 +318,6 @@ def main():
# Set the requested value
log.verbose("Setting the new value for {}.".format(change_path))
if args.eyamlcrypt:
output_type = "string"
try:
format_type = YAMLValueFormats.from_str(args.format)
except NameError:
Expand All @@ -324,8 +330,10 @@ def main():
if format_type is YAMLValueFormats.DEFAULT:
format_type = old_format

output_type = EYAMLOutputFormats.STRING
if format_type in [YAMLValueFormats.FOLDED, YAMLValueFormats.LITERAL]:
output_type = "block"
output_type = EYAMLOutputFormats.BLOCK

try:
processor.set_eyaml_value(
change_path, new_value
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="yamlpath",
version="2.0.0",
version="2.0.1",
description="Read and change YAML/Compatible data using powerful, intuitive, command-line friendly syntax",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
19 changes: 19 additions & 0 deletions tests/test_eyaml_eyamlprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ruamel.yaml import YAML

import yamlpath.patches
from yamlpath.enums import YAMLValueFormats
from yamlpath.eyaml.enums import EYAMLOutputFormats
from yamlpath.eyaml import EYAMLProcessor
from yamlpath.wrappers import ConsolePrinter
Expand Down Expand Up @@ -249,6 +250,24 @@ def test_happy_set_eyaml_value(self, logger_f, eyamldata_f, eyamlkeys, yaml_path

assert EYAMLProcessor.is_eyaml_value(encvalue)

@requireseyaml
@pytest.mark.parametrize("yaml_path,newval,eoformat,yvformat", [
("/aliased::secrets/novel_values/ident", "New, novel, encrypted identity in BLOCK format", EYAMLOutputFormats.BLOCK, YAMLValueFormats.FOLDED),
("/aliased::secrets/string_values/ident", "New, novel, encrypted identity in STRING format", EYAMLOutputFormats.STRING, YAMLValueFormats.BARE),
])
def test_preserve_old_blockiness(self, logger_f, eyamldata_f, eyamlkeys, yaml_path, newval, eoformat, yvformat):
processor = EYAMLProcessor(logger_f, eyamldata_f, privatekey=eyamlkeys[0], publickey=eyamlkeys[1])
processor.set_eyaml_value(yaml_path, newval, output=eoformat)

encvalue = None
encformat = YAMLValueFormats.DEFAULT
for encnode in processor.get_nodes(yaml_path):
encvalue = encnode
encformat = YAMLValueFormats.from_node(encvalue)
break

assert EYAMLProcessor.is_eyaml_value(encvalue) and yvformat == encformat

def test_none_eyaml_value(self):
assert False == EYAMLProcessor.is_eyaml_value(None)

Expand Down

0 comments on commit a97b836

Please sign in to comment.