Skip to content

Commit 6aec157

Browse files
authored
Improve test coverage, drop unused code (#403)
1 parent 821d725 commit 6aec157

File tree

7 files changed

+199
-95
lines changed

7 files changed

+199
-95
lines changed

mono_repo/lib/src/mono_config.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,10 @@ String? _selfValidateFromValue(Object? value) {
233233
throw ArgumentError.value(value, 'value', 'Must be a `String` or `bool`.');
234234
}
235235

236-
@JsonSerializable(disallowUnrecognizedKeys: true)
236+
@JsonSerializable(
237+
createToJson: false,
238+
disallowUnrecognizedKeys: true,
239+
)
237240
class ConditionalStage {
238241
@JsonKey(required: true, disallowNullValue: true)
239242
final String name;
@@ -249,13 +252,6 @@ class ConditionalStage {
249252
}
250253
return _$ConditionalStageFromJson(json as Map);
251254
}
252-
253-
Object toJson() {
254-
if (ifCondition == null) {
255-
return name;
256-
}
257-
return _$ConditionalStageToJson(this);
258-
}
259255
}
260256

261257
// The available CI providers that we generate config for.

mono_repo/lib/src/mono_config.g.dart

Lines changed: 0 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mono_repo/lib/src/package_config.dart

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:checked_yaml/checked_yaml.dart';
6-
import 'package:collection/collection.dart';
76
import 'package:io/ansi.dart';
87
import 'package:json_annotation/json_annotation.dart';
98
import 'package:pub_semver/pub_semver.dart';
@@ -191,24 +190,34 @@ abstract class HasStageName {
191190
String get stageName;
192191
}
193192

194-
@JsonSerializable(explicitToJson: true)
193+
@JsonSerializable(
194+
explicitToJson: true,
195+
createFactory: false,
196+
ignoreUnannotated: true,
197+
)
195198
class CIJob implements HasStageName {
196199
@JsonKey(includeIfNull: false)
197200
final String? description;
198201

202+
@JsonKey()
199203
final String os;
200204

201205
/// Relative path to the directory containing the source package from the root
202206
/// of the repository.
207+
@JsonKey()
203208
final String package;
204209

210+
@JsonKey()
205211
final String sdk;
206212

207213
@override
214+
@JsonKey()
208215
final String stageName;
209216

217+
@JsonKey()
210218
final List<Task> tasks;
211219

220+
@JsonKey()
212221
final PackageFlavor flavor;
213222

214223
Iterable<String> get _taskCommandsTickQuoted =>
@@ -240,8 +249,6 @@ class CIJob implements HasStageName {
240249
'Should have caught bad sdk value `$sdk` before here!',
241250
);
242251

243-
factory CIJob.fromJson(Map<String, dynamic> json) => _$CIJobFromJson(json);
244-
245252
factory CIJob.parse(
246253
String os,
247254
String package,
@@ -271,6 +278,7 @@ class CIJob implements HasStageName {
271278
}
272279

273280
/// If [sdk] is a valid [Version], return it. Otherwise, `null`.
281+
@JsonKey(ignore: true)
274282
Version? get explicitSdkVersion {
275283
try {
276284
return Version.parse(sdk);
@@ -280,26 +288,21 @@ class CIJob implements HasStageName {
280288
}
281289

282290
Map<String, dynamic> toJson() => _$CIJobToJson(this);
283-
284-
@override
285-
String toString() => 'CIJob: ${toJson()}';
286-
287-
@override
288-
bool operator ==(Object other) =>
289-
other is CIJob && _equality.equals(_items, other._items);
290-
291-
@override
292-
int get hashCode => _equality.hash(_items);
293-
294-
List get _items => [description, package, sdk, stageName, tasks];
295291
}
296292

297-
@JsonSerializable(includeIfNull: false)
293+
@JsonSerializable(
294+
createFactory: false,
295+
ignoreUnannotated: true,
296+
includeIfNull: false,
297+
)
298298
class Task {
299+
@JsonKey()
299300
final PackageFlavor flavor;
300301

302+
@JsonKey()
301303
final TaskType type;
302304

305+
@JsonKey()
303306
final String? args;
304307

305308
final String command;
@@ -405,7 +408,16 @@ class Task {
405408
badKey: true,
406409
);
407410
}
408-
return Task(flavor, taskType, args: args);
411+
try {
412+
return Task(flavor, taskType, args: args);
413+
} on InvalidTaskConfigException catch (e) {
414+
throw CheckedFromJsonException(
415+
yamlValue,
416+
taskName,
417+
'Task',
418+
e.message,
419+
);
420+
}
409421
}
410422

411423
if (yamlValue is YamlNode) {
@@ -415,8 +427,6 @@ class Task {
415427
throw ArgumentError('huh? $yamlValue ${yamlValue.runtimeType}');
416428
}
417429

418-
factory Task.fromJson(Map<String, dynamic> json) => _$TaskFromJson(json);
419-
420430
Map<String, dynamic> toJson() => _$TaskToJson(this);
421431

422432
/// Stores the job names we've already warned about. Only warn once!
@@ -434,15 +444,4 @@ class Task {
434444
}
435445
return taskName;
436446
}
437-
438-
@override
439-
bool operator ==(Object other) =>
440-
other is Task && _equality.equals(_items, other._items);
441-
442-
@override
443-
int get hashCode => _equality.hash(_items);
444-
445-
List get _items => [type, args];
446447
}
447-
448-
const _equality = DeepCollectionEquality();

mono_repo/lib/src/package_config.g.dart

Lines changed: 0 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mono_repo/lib/src/task_type.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ abstract class TaskType implements Comparable<TaskType> {
2323

2424
const TaskType._(this.name);
2525

26-
factory TaskType.fromJson(String name) =>
27-
_values.singleWhere((element) => element.name == name);
28-
2926
List<String> commandValue(PackageFlavor flavor, String? args);
3027

3128
String toJson() => name;
@@ -57,6 +54,14 @@ abstract class TaskType implements Comparable<TaskType> {
5754
);
5855
}
5956

57+
/// Special [Exception] type used to convey error state that can be caught and
58+
/// re-thrown with more context.
59+
class InvalidTaskConfigException implements Exception {
60+
final String message;
61+
62+
const InvalidTaskConfigException(this.message);
63+
}
64+
6065
class _FormatTask extends TaskType {
6166
const _FormatTask() : super._('format');
6267

@@ -110,7 +115,7 @@ class _TestWithCoverageTask extends TaskType {
110115
@override
111116
List<String> commandValue(PackageFlavor flavor, String? args) {
112117
if (flavor == PackageFlavor.flutter) {
113-
throw ArgumentError(
118+
throw const InvalidTaskConfigException(
114119
'Code coverage tests are not supported with Flutter.',
115120
);
116121
}

mono_repo/test/generate_test.dart

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,66 @@ $_writeScriptOutput''',
626626
await d.file(ciScriptPath, ciShellOutput).validate();
627627
});
628628

629+
test('test_with_coverage', () async {
630+
await d.dir('pkg_a', [
631+
d.file(monoPkgFileName, r'''
632+
stages:
633+
- test:
634+
- description: "chrome tests"
635+
test: --platform chrome
636+
sdk: dev
637+
os: macos
638+
- test_with_coverage: --preset travis
639+
sdk: stable
640+
'''),
641+
d.file('pubspec.yaml', '''
642+
name: pkg_a
643+
''')
644+
]).create();
645+
646+
testGenerateBothConfig(
647+
printMatcher: '''
648+
package:pkg_a
649+
$_writeScriptOutput''',
650+
);
651+
652+
validateSandbox(
653+
'github_output_test_with_coverage.txt',
654+
defaultGitHubWorkflowFilePath,
655+
);
656+
});
657+
658+
test('test_with_coverage not supported with flutter', () async {
659+
await d.dir('pkg_a', [
660+
d.file(monoPkgFileName, r'''
661+
stages:
662+
- test:
663+
- test_with_coverage:
664+
sdk: stable
665+
'''),
666+
d.file('pubspec.yaml', '''
667+
name: pkg_a
668+
669+
environment:
670+
sdk: ">=2.17.0 <3.0.0"
671+
672+
dependencies:
673+
flutter:
674+
sdk: flutter
675+
''')
676+
]).create();
677+
678+
expect(
679+
testGenerateGitHubConfig,
680+
throwsAParsedYamlException('''
681+
line 3, column 25 of ${p.join('pkg_a', 'mono_pkg.yaml')}: Unsupported value for "test_with_coverage". Code coverage tests are not supported with Flutter.
682+
683+
3 │ - test_with_coverage:
684+
│ ^
685+
╵'''),
686+
);
687+
});
688+
629689
test(
630690
'command values must be either a String or a List containing strings',
631691
() async {

0 commit comments

Comments
 (0)