Skip to content

Commit 0c06c32

Browse files
authored
Better logging in random_test.dart, and less overall noise from warnings (dart-archive/yaml_edit#81)
1 parent df0986d commit 0c06c32

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

pkgs/yaml_edit/lib/src/editor.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ class YamlEditor {
574574
_yaml);
575575
}
576576

577-
final actualTree = loadYamlNode(_yaml);
577+
final actualTree = withYamlWarningCallback(() => loadYamlNode(_yaml));
578578
if (!deepEquals(actualTree, expectedTree)) {
579579
throw createAssertionError(
580580
'Modification did not result in expected result.',

pkgs/yaml_edit/lib/src/utils.dart

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,34 @@ import 'package:yaml/yaml.dart';
88
import 'editor.dart';
99
import 'wrap.dart';
1010

11+
/// Invoke [fn] while setting [yamlWarningCallback] to [warn], and restore
12+
/// [YamlWarningCallback] after [fn] returns.
13+
///
14+
/// Defaults to a [warn] function that ignores all warnings.
15+
T withYamlWarningCallback<T>(
16+
T Function() fn, {
17+
YamlWarningCallback warn = _ignoreWarning,
18+
}) {
19+
final original = yamlWarningCallback;
20+
try {
21+
yamlWarningCallback = warn;
22+
return fn();
23+
} finally {
24+
yamlWarningCallback = original;
25+
}
26+
}
27+
28+
void _ignoreWarning(String warning, [SourceSpan? span]) {/* ignore warning */}
29+
1130
/// Determines if [string] is dangerous by checking if parsing the plain string
1231
/// can return a result different from [string].
1332
///
1433
/// This function is also capable of detecting if non-printable characters are
1534
/// in [string].
1635
bool isDangerousString(String string) {
1736
try {
18-
if (loadYamlNode(string).value != string) {
37+
final node = withYamlWarningCallback(() => loadYamlNode(string));
38+
if (node.value != string) {
1939
return true;
2040
}
2141

pkgs/yaml_edit/test/random_test.dart

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'dart:async';
6-
import 'dart:math';
5+
import 'dart:math' show Random;
76

87
import 'package:test/test.dart';
98
import 'package:yaml/yaml.dart';
@@ -44,15 +43,10 @@ dev_dependencies:
4443
''');
4544

4645
for (var j = 0; j < modificationsPerRound; j++) {
47-
/// Using [runZoned] to hide `package:yaml`'s warnings.
48-
/// Test failures and errors will still be shown.
49-
runZoned(() {
50-
expect(
51-
() => generator.performNextModification(editor), returnsNormally);
52-
},
53-
zoneSpecification: ZoneSpecification(
54-
print: (Zone self, ZoneDelegate parent, Zone zone,
55-
String message) {}));
46+
expect(
47+
() => generator.performNextModification(editor),
48+
returnsNormally,
49+
);
5650
}
5751
});
5852
}

0 commit comments

Comments
 (0)