Fixed bug in adding to empty map values, when it is followed by other content. (#63)
diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ea9c51..e5c2d3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md
@@ -1,3 +1,8 @@ +## v1.0.2 + +- Throws an error if the final YAML after edit is not parsable. +- Fixed bug in adding to empty map values, when it is followed by other content. + ## v1.0.1 - Updated behavior surrounding list and map removal.
diff --git a/lib/src/editor.dart b/lib/src/editor.dart index 9e16598..730261e 100644 --- a/lib/src/editor.dart +++ b/lib/src/editor.dart
@@ -576,19 +576,24 @@ ArgumentError.checkNotNull(path, 'path'); final expectedTree = _deepModify(_contents, path, [], expectedNode); + final initialYaml = _yaml; _yaml = edit.apply(_yaml); - _initialize(); + + try { + _initialize(); + } on YamlException { + throw createAssertionError( + 'Failed to produce valid YAML after modification.', + initialYaml, + _yaml); + } final actualTree = loadYamlNode(_yaml); if (!deepEquals(actualTree, expectedTree)) { - throw AssertionError(''' -Modification did not result in expected result! - -Obtained: -$actualTree - -Expected: -$expectedTree'''); + throw createAssertionError( + 'Modification did not result in expected result.', + initialYaml, + _yaml); } _contents = actualTree;
diff --git a/lib/src/errors.dart b/lib/src/errors.dart index 83241cc..871d40a 100644 --- a/lib/src/errors.dart +++ b/lib/src/errors.dart
@@ -76,3 +76,36 @@ 'by this library.\n\n' '${anchor.span.message('The alias was first defined here.')}'); } + +/// Error thrown when an assertion about the YAML fails. Extends +/// [AssertionError] to override the [toString] method for pretty printing. +class _YamlAssertionError extends AssertionError { + _YamlAssertionError(message) : super(message); + + @override + String toString() { + if (message != null) { + return 'Assertion failed: ${message}'; + } + return 'Assertion failed'; + } +} + +/// Throws an [AssertionError] with the given [message], and format +/// [oldYaml] and [newYaml] for information. +@alwaysThrows +Error createAssertionError(String message, String oldYaml, String newYaml) { + return _YamlAssertionError(''' +(package:yaml_edit) $message + +# YAML before edit: +> ${oldYaml.replaceAll('\n', '\n> ')} + +# YAML after edit: +> ${newYaml.replaceAll('\n', '\n> ')} + +Please file an issue at: +''' + 'https://github.com/google/dart-neats/issues/new?labels=pkg%3Ayaml_edit' + '%2C+pending-triage&template=yaml_edit.md\n'); +}
diff --git a/lib/src/map_mutations.dart b/lib/src/map_mutations.dart index b3c3cf8..1a3d1f3 100644 --- a/lib/src/map_mutations.dart +++ b/lib/src/map_mutations.dart
@@ -172,7 +172,7 @@ /// `package:yaml` parses empty nodes in a way where the start/end of the /// empty value node is the end of the key node, so we have to adjust for /// this. - if (end < start) end = start + 1; + if (end < start) end = start; return SourceEdit(start, end - start, ' ' + valueAsString); }
diff --git a/pubspec.yaml b/pubspec.yaml index f143892..11923e3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml
@@ -1,5 +1,5 @@ name: yaml_edit -version: 1.0.1 +version: 1.0.2 description: A library for YAML manipulation with comment and whitespace preservation. homepage: https://github.com/google/dart-neats/tree/master/yaml_edit repository: https://github.com/google/dart-neats.git
diff --git a/test/testdata/output/pubspec.golden b/test/testdata/output/pubspec.golden index 7cd5b25..b5e5ea0 100644 --- a/test/testdata/output/pubspec.golden +++ b/test/testdata/output/pubspec.golden
@@ -113,3 +113,4 @@ dev_dependencies: test: ^1.14.4 +
diff --git a/test/update_test.dart b/test/update_test.dart index bee48b6..23b0023 100644 --- a/test/update_test.dart +++ b/test/update_test.dart
@@ -104,7 +104,7 @@ }); test('empty value', () { - final doc = YamlEditor('YAML: '); + final doc = YamlEditor('YAML:'); doc.update(['YAML'], 'test'); expect(doc.toString(), equals('YAML: test')); @@ -112,7 +112,7 @@ }); test('empty value (2)', () { - final doc = YamlEditor('YAML : '); + final doc = YamlEditor('YAML :'); doc.update(['YAML'], 'test'); expect(doc.toString(), equals('YAML : test')); @@ -257,6 +257,36 @@ ''')); }); + test('nested (8)', () { + final doc = YamlEditor(''' +a: +b: false +'''); + doc.update(['a'], {'retry': '3.0.1'}); + + expect(doc.toString(), equals(''' +a: + retry: 3.0.1 +b: false +''')); + }); + + test('nested (9)', () { + final doc = YamlEditor(''' +# comment +a: # comment +# comment +'''); + doc.update(['a'], {'retry': '3.0.1'}); + + expect(doc.toString(), equals(''' +# comment +a: + retry: 3.0.1 # comment +# comment +''')); + }); + test('nested scalar -> flow list', () { final doc = YamlEditor(''' a: 1