Fixed bug in adding dependency to empty dependencies key (#2640)
* Fixed bug in adding dependency to empty dependencies key
* Fixed log messages and test
diff --git a/lib/src/command/add.dart b/lib/src/command/add.dart
index 1ba0410..fff9d11 100644
--- a/lib/src/command/add.dart
+++ b/lib/src/command/add.dart
@@ -378,6 +378,8 @@
final packagePath = [dependencyKey, package.name];
final yamlEditor = YamlEditor(readTextFile(entrypoint.pubspecPath));
+ log.io('Reading ${entrypoint.pubspecPath}.');
+ log.fine('Contents:\n$yamlEditor');
/// Handle situations where the user might not have the dependencies or
/// dev_dependencies map.
@@ -390,6 +392,8 @@
packagePath, pubspecInformation ?? '^${resultPackage.version}');
}
+ log.fine('Added ${package.name} to "$dependencyKey".');
+
/// Remove the package from dev_dependencies if we are adding it to
/// dependencies. Refer to [_addPackageToPubspec] for additional discussion.
if (!isDevelopment) {
@@ -403,6 +407,8 @@
} else {
yamlEditor.remove(['dev_dependencies', package.name]);
}
+
+ log.fine('Removed ${package.name} from "dev_dependencies".');
}
}
diff --git a/lib/src/yaml_edit/editor.dart b/lib/src/yaml_edit/editor.dart
index fa397ce..f65b996 100644
--- a/lib/src/yaml_edit/editor.dart
+++ b/lib/src/yaml_edit/editor.dart
@@ -566,21 +566,25 @@
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;
_edits.add(edit);
}
diff --git a/lib/src/yaml_edit/errors.dart b/lib/src/yaml_edit/errors.dart
index 5d1c9a0..e7fbc45 100644
--- a/lib/src/yaml_edit/errors.dart
+++ b/lib/src/yaml_edit/errors.dart
@@ -66,3 +66,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/yaml_edit/map_mutations.dart b/lib/src/yaml_edit/map_mutations.dart
index 3dd8682..c95b88b 100644
--- a/lib/src/yaml_edit/map_mutations.dart
+++ b/lib/src/yaml_edit/map_mutations.dart
@@ -162,7 +162,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/test/add/common/add_test.dart b/test/add/common/add_test.dart
index cca9fd9..cfea9d1 100644
--- a/test/add/common/add_test.dart
+++ b/test/add/common/add_test.dart
@@ -790,10 +790,7 @@
await pubGet();
await pubAdd(args: ['bar']);
-
- await d.dir(appPath, [
- d.appPubspec({'bar': '^1.0.0'})
- ]).validate();
+ await d.appDir({'bar': '^1.0.0'}).validate();
});
test('preserves comments', () async {