Skip to content

Commit 234100b

Browse files
committed
update
1 parent e3e0032 commit 234100b

File tree

7 files changed

+75
-99
lines changed

7 files changed

+75
-99
lines changed

script/tool/lib/src/branch_for_batch_release_command.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ class BranchForBatchReleaseCommand extends PackageCommand {
6565
final GitDir repository = await gitDir;
6666

6767
print('Parsing package "${package.displayName}"...');
68-
final PendingChangelogs pendingChangelogs = package.getPendingChangelogs();
69-
if (pendingChangelogs.errors.isNotEmpty) {
70-
printError(
71-
'Failed to read pending changelogs for ${package.displayName}:');
72-
pendingChangelogs.errors.forEach(printError);
68+
List<PendingChangelogEntry> pendingChangelogs = <PendingChangelogEntry>[];
69+
try {
70+
pendingChangelogs = package.getPendingChangelogs();
71+
} on FormatException catch (e) {
72+
printError('Failed to parse pending changelogs: ${e.message}');
7373
throw ToolExit(_kExitPackageMalformed);
7474
}
75-
if (pendingChangelogs.entries.isEmpty) {
75+
76+
if (pendingChangelogs.isEmpty) {
7677
print('No pending changelogs found for ${package.displayName}.');
7778
return;
7879
}
@@ -85,7 +86,7 @@ class BranchForBatchReleaseCommand extends PackageCommand {
8586
throw ToolExit(_kExitPackageMalformed);
8687
}
8788
final _ReleaseInfo releaseInfo =
88-
_getReleaseInfo(pendingChangelogs.entries, pubspec.version!);
89+
_getReleaseInfo(pendingChangelogs, pubspec.version!);
8990

9091
if (releaseInfo.newVersion == null) {
9192
print('No version change specified in pending changelogs for '
@@ -97,7 +98,7 @@ class BranchForBatchReleaseCommand extends PackageCommand {
9798
git: repository,
9899
package: package,
99100
branchName: branchName,
100-
pendingChangelogFiles: pendingChangelogs.entries
101+
pendingChangelogFiles: pendingChangelogs
101102
.map<File>((PendingChangelogEntry e) => e.file)
102103
.toList(),
103104
releaseInfo: releaseInfo,

script/tool/lib/src/common/repository_package.dart

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,13 @@ class RepositoryPackage {
240240
///
241241
/// Returns the parsed changelog entries for the package, and any errors
242242
/// that occurred trying to read them.
243-
PendingChangelogs getPendingChangelogs() {
243+
List<PendingChangelogEntry> getPendingChangelogs() {
244244
final List<PendingChangelogEntry> entries = <PendingChangelogEntry>[];
245-
final List<String> errors = <String>[];
246245

247246
final Directory pendingChangelogsDir = pendingChangelogsDirectory;
248247
if (!pendingChangelogsDir.existsSync()) {
249-
errors.add('No pending_changelogs folder found for $displayName.');
250-
return PendingChangelogs(entries, errors);
248+
throw FormatException(
249+
'No pending_changelogs folder found for $displayName.');
251250
}
252251

253252
final List<File> allFiles =
@@ -261,25 +260,27 @@ class RepositoryPackage {
261260
pendingChangelogFiles.add(file);
262261
}
263262
} else {
264-
errors.add('Found non-YAML file in pending_changelogs: ${file.path}');
263+
throw FormatException(
264+
'Found non-YAML file in pending_changelogs: ${file.path}');
265265
}
266266
}
267267

268268
for (final File file in pendingChangelogFiles) {
269269
try {
270270
entries.add(PendingChangelogEntry.parse(file.readAsStringSync(), file));
271271
} on FormatException catch (e) {
272-
errors.add('Malformed pending changelog file: ${file.path}\n$e');
272+
throw FormatException(
273+
'Malformed pending changelog file: ${file.path}\n$e');
273274
}
274275
}
275-
return PendingChangelogs(entries, errors);
276+
return entries;
276277
}
277278
}
278279

279280
/// A class representing the parsed content of a `ci_config.yaml` file.
280281
class CiConfig {
281282
/// Creates a [CiConfig] from a parsed YAML map.
282-
CiConfig._(this._yaml, this.errors);
283+
CiConfig._(this.isBatchRelease);
283284

284285
/// Parses a [CiConfig] from a YAML string.
285286
factory CiConfig.parse(String yaml) {
@@ -288,10 +289,15 @@ class CiConfig {
288289
throw const FormatException('Root of ci_config.yaml must be a map.');
289290
}
290291

291-
final List<String> errors =
292-
_checkCiConfigEntries(loaded, syntax: _validCiConfigSyntax);
292+
_checkCiConfigEntries(loaded, syntax: _validCiConfigSyntax);
293+
294+
bool isBatchRelease = false;
295+
final Object? release = loaded['release'];
296+
if (release is Map) {
297+
isBatchRelease = release['batch'] == true;
298+
}
293299

294-
return CiConfig._(loaded, errors);
300+
return CiConfig._(isBatchRelease);
295301
}
296302

297303
static const Map<String, Object?> _validCiConfigSyntax = <String, Object?>{
@@ -300,26 +306,16 @@ class CiConfig {
300306
},
301307
};
302308

303-
final YamlMap _yaml;
309+
/// Returns true if the package is configured for batch release.
310+
final bool isBatchRelease;
304311

305-
/// A list of validation errors found in the config file.
306-
final List<String> errors;
307312

308-
/// Returns true if the package is configured for batch release.
309-
bool get isBatchRelease {
310-
final Object? release = _yaml['release'];
311-
if (release is! Map) {
312-
return false;
313-
}
314-
return release['batch'] == true;
315-
}
316313

317-
static List<String> _checkCiConfigEntries(YamlMap config,
314+
static void _checkCiConfigEntries(YamlMap config,
318315
{required Map<String, Object?> syntax, String configPrefix = ''}) {
319-
final List<String> errors = <String>[];
320316
for (final MapEntry<Object?, Object?> entry in config.entries) {
321317
if (!syntax.containsKey(entry.key)) {
322-
errors.add(
318+
throw FormatException(
323319
'Unknown key `${entry.key}` in config${_formatConfigPrefix(configPrefix)}, the possible keys are ${syntax.keys.toList()}');
324320
} else {
325321
final Object syntaxValue = syntax[entry.key]!;
@@ -328,20 +324,19 @@ class CiConfig {
328324
: '$configPrefix.${entry.key}';
329325
if (syntaxValue is Set) {
330326
if (!syntaxValue.contains(entry.value)) {
331-
errors.add(
327+
throw FormatException(
332328
'Invalid value `${entry.value}` for key${_formatConfigPrefix(newConfigPrefix)}, the possible values are ${syntaxValue.toList()}');
333329
}
334330
} else if (entry.value is! YamlMap) {
335-
errors.add(
331+
throw FormatException(
336332
'Invalid value `${entry.value}` for key${_formatConfigPrefix(newConfigPrefix)}, the value must be a map');
337333
} else {
338-
errors.addAll(_checkCiConfigEntries(entry.value! as YamlMap,
334+
_checkCiConfigEntries(entry.value! as YamlMap,
339335
syntax: syntaxValue as Map<String, Object?>,
340-
configPrefix: newConfigPrefix));
336+
configPrefix: newConfigPrefix);
341337
}
342338
}
343339
}
344-
return errors;
345340
}
346341

347342
static String _formatConfigPrefix(String configPrefix) =>
@@ -412,14 +407,4 @@ class PendingChangelogEntry {
412407
final File file;
413408
}
414409

415-
/// A data class for pending changelogs and any errors found while parsing them.
416-
class PendingChangelogs {
417-
/// Creates a new instance.
418-
PendingChangelogs(this.entries, this.errors);
419410

420-
/// The parsed pending changelog entries.
421-
final List<PendingChangelogEntry> entries;
422-
423-
/// A list of errors found while parsing changelogs.
424-
final List<String> errors;
425-
}

script/tool/lib/src/repo_package_info_check_command.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,22 @@ class RepoPackageInfoCheckCommand extends PackageLoopingCommand {
138138
}
139139

140140
// The content of ci_config.yaml must be valid if there is one.
141-
final CiConfig? ciConfig = package.parseCiConfig();
142-
if (ciConfig != null) {
143-
for (final String error in ciConfig.errors) {
144-
printError('$indentation$error');
145-
}
146-
errors.addAll(ciConfig.errors);
141+
CiConfig? ciConfig;
142+
try {
143+
ciConfig = package.parseCiConfig();
144+
} on FormatException catch (e) {
145+
printError('$indentation${e.message}');
146+
errors.add(e.message);
147147
}
148148

149149
// All packages with batch release enabled should have valid pending changelogs.
150150
if (ciConfig?.isBatchRelease ?? false) {
151-
errors.addAll(package.getPendingChangelogs().errors);
151+
try {
152+
package.getPendingChangelogs();
153+
} on FormatException catch (e) {
154+
printError('$indentation${e.message}');
155+
errors.add(e.message);
156+
}
152157
}
153158

154159
// All published packages should have a README.md entry.

script/tool/lib/src/version_check_command.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,9 +625,15 @@ ${indentation}The first version listed in CHANGELOG.md is $fromChangeLog.
625625
Future<List<String>> _validatePendingChangelogs(
626626
RepositoryPackage package, List<String> changedPaths) async {
627627
final List<String> errors = <String>[];
628-
final PendingChangelogs allChangelogs = package.getPendingChangelogs();
628+
List<PendingChangelogEntry> allChangelogs = <PendingChangelogEntry>[];
629+
try {
630+
allChangelogs = package.getPendingChangelogs();
631+
} on FormatException catch (e) {
632+
errors.add(e.message);
633+
return errors;
634+
}
629635

630-
final List<PendingChangelogEntry> newEntries = allChangelogs.entries
636+
final List<PendingChangelogEntry> newEntries = allChangelogs
631637
.where((PendingChangelogEntry entry) =>
632638
changedPaths.contains(entry.file.path))
633639
.toList();

script/tool/test/common/repository_package_test.dart

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,7 @@ release:
287287
foo: bar
288288
''');
289289

290-
final CiConfig? config = plugin.parseCiConfig();
291-
292-
expect(config, isNotNull);
293-
expect(config!.errors, hasLength(1));
294-
expect(config.errors[0], contains('Unknown key `foo`'));
290+
expect(() => plugin.parseCiConfig(), throwsFormatException);
295291
});
296292

297293
test('reports invalid values', () {
@@ -302,11 +298,7 @@ release:
302298
batch: not-a-bool
303299
''');
304300

305-
final CiConfig? config = plugin.parseCiConfig();
306-
307-
expect(config, isNotNull);
308-
expect(config!.errors, hasLength(1));
309-
expect(config.errors[0], contains('Invalid value `not-a-bool`'));
301+
expect(() => plugin.parseCiConfig(), throwsFormatException);
310302
});
311303
});
312304

@@ -315,22 +307,18 @@ release:
315307
final RepositoryPackage package =
316308
createFakePackage('a_package', packagesDir);
317309

318-
final PendingChangelogs changelogs = package.getPendingChangelogs();
319-
320-
expect(changelogs.entries, isEmpty);
321-
expect(changelogs.errors, hasLength(1));
322-
expect(changelogs.errors[0], contains('No pending_changelogs folder'));
310+
expect(() => package.getPendingChangelogs(), throwsFormatException);
323311
});
324312

325313
test('returns empty lists if the directory is empty', () {
326314
final RepositoryPackage package =
327315
createFakePackage('a_package', packagesDir);
328316
package.pendingChangelogsDirectory.createSync();
329317

330-
final PendingChangelogs changelogs = package.getPendingChangelogs();
318+
final List<PendingChangelogEntry> changelogs =
319+
package.getPendingChangelogs();
331320

332-
expect(changelogs.entries, isEmpty);
333-
expect(changelogs.errors, isEmpty);
321+
expect(changelogs, isEmpty);
334322
});
335323

336324
test('returns entries for valid files', () {
@@ -350,14 +338,14 @@ changelog: B
350338
version: minor
351339
''');
352340

353-
final PendingChangelogs changelogs = package.getPendingChangelogs();
341+
final List<PendingChangelogEntry> changelogs =
342+
package.getPendingChangelogs();
354343

355-
expect(changelogs.errors, isEmpty);
356-
expect(changelogs.entries, hasLength(2));
357-
expect(changelogs.entries[0].changelog, 'A');
358-
expect(changelogs.entries[0].version, VersionChange.patch);
359-
expect(changelogs.entries[1].changelog, 'B');
360-
expect(changelogs.entries[1].version, VersionChange.minor);
344+
expect(changelogs, hasLength(2));
345+
expect(changelogs[0].changelog, 'A');
346+
expect(changelogs[0].version, VersionChange.patch);
347+
expect(changelogs[1].changelog, 'B');
348+
expect(changelogs[1].version, VersionChange.minor);
361349
});
362350

363351
test('returns an error for a malformed file', () {
@@ -368,12 +356,7 @@ version: minor
368356
package.pendingChangelogsDirectory.childFile('a.yaml');
369357
changelogFile.writeAsStringSync('not yaml');
370358

371-
final PendingChangelogs changelogs = package.getPendingChangelogs();
372-
373-
expect(changelogs.entries, isEmpty);
374-
expect(changelogs.errors, hasLength(1));
375-
expect(
376-
changelogs.errors[0], contains('Malformed pending changelog file'));
359+
expect(() => package.getPendingChangelogs(), throwsFormatException);
377360
});
378361

379362
test('ignores template.yaml', () {
@@ -393,11 +376,11 @@ changelog: TEMPLATE
393376
version: skip
394377
''');
395378

396-
final PendingChangelogs changelogs = package.getPendingChangelogs();
379+
final List<PendingChangelogEntry> changelogs =
380+
package.getPendingChangelogs();
397381

398-
expect(changelogs.errors, isEmpty);
399-
expect(changelogs.entries, hasLength(1));
400-
expect(changelogs.entries[0].changelog, 'A');
382+
expect(changelogs, hasLength(1));
383+
expect(changelogs[0].changelog, 'A');
401384
});
402385

403386
test('returns an error for a non-yaml file', () {
@@ -414,11 +397,7 @@ version: patch
414397
.childFile('a.txt')
415398
.writeAsStringSync('text');
416399

417-
final PendingChangelogs changelogs = package.getPendingChangelogs();
418-
419-
expect(changelogs.entries, hasLength(1));
420-
expect(changelogs.errors, hasLength(1));
421-
expect(changelogs.errors[0], contains('Found non-YAML file'));
400+
expect(() => package.getPendingChangelogs(), throwsFormatException);
422401
});
423402
});
424403
}

script/tool/test/repo_package_info_check_command_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ release:
604604
output,
605605
containsAllInOrder(<Matcher>[
606606
contains(
607-
'Packages with batch releases enabled must have a "pending_changelogs" directory.'),
607+
'No pending_changelogs folder found for a_package.'),
608608
]),
609609
);
610610
});

script/tool/test/version_check_command_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ release:
369369
expect(
370370
output,
371371
containsAllInOrder(<Matcher>[
372-
contains('Missing CHANGELOG file'),
372+
contains('No pending_changelogs folder found for package'),
373373
]));
374374
});
375375

0 commit comments

Comments
 (0)