Skip to content

Commit 7009fb8

Browse files
authored
Migrate most end2end tests; null safety tweaks (#2882)
1 parent 6af09a2 commit 7009fb8

12 files changed

+130
-153
lines changed

bin/dartdoc.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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-
// @dart=2.9
6-
75
library dartdoc.bin;
86

97
import 'dart:async';

lib/src/dartdoc_options.dart

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,34 +1071,35 @@ abstract class _DartdocArgOption<T> implements DartdocOption<T> {
10711071
_OptionValueWithContext<T>? _valueAtFromArgsWithContext() {
10721072
if (!_argResults.wasParsed(argName)) return null;
10731073

1074-
T retval;
10751074
// Unlike in _DartdocFileOption, we throw here on inputs being invalid
10761075
// rather than silently proceeding. This is because the user presumably
10771076
// typed something wrong on the command line and can therefore fix it.
10781077
// dartdoc_option.yaml files from other packages may not be fully in the
10791078
// user's control.
10801079
if (_isBool || _isListString || _isString) {
1081-
retval = _argResults[argName];
1080+
return _OptionValueWithContext(
1081+
_argResults[argName], _directoryCurrentPath);
10821082
} else if (_isInt) {
1083-
retval = int.tryParse(_argResults[argName]) as T;
1084-
if (retval == null) _throwErrorForTypes(_argResults[argName]);
1083+
var value = int.tryParse(_argResults[argName]);
1084+
if (value == null) _throwErrorForTypes(_argResults[argName]);
1085+
return _OptionValueWithContext(value as T, _directoryCurrentPath);
10851086
} else if (_isDouble) {
1086-
retval = double.tryParse(_argResults[argName]) as T;
1087-
if (retval == null) _throwErrorForTypes(_argResults[argName]);
1087+
var value = double.tryParse(_argResults[argName]);
1088+
if (value == null) _throwErrorForTypes(_argResults[argName]);
1089+
return _OptionValueWithContext(value as T, _directoryCurrentPath);
10881090
} else if (_isMapString) {
1089-
retval = <String, String>{} as T;
1091+
var value = <String, String>{};
10901092
for (String pair in _argResults[argName]) {
10911093
var pairList = pair.split('::');
10921094
if (pairList.length != 2) {
10931095
_throwErrorForTypes(pair);
10941096
}
1095-
assert(pairList.length == 2);
1096-
(retval as Map<String, String>)[pairList.first] = pairList.last;
1097+
value[pairList.first] = pairList.last;
10971098
}
1099+
return _OptionValueWithContext(value as T, _directoryCurrentPath);
10981100
} else {
10991101
throw UnsupportedError('Type $T is not supported');
11001102
}
1101-
return _OptionValueWithContext(retval, _directoryCurrentPath);
11021103
}
11031104

11041105
/// The name of this option as a command line argument.
@@ -1130,7 +1131,7 @@ abstract class _DartdocArgOption<T> implements DartdocOption<T> {
11301131
if (_isBool) {
11311132
argParser.addFlag(argName,
11321133
abbr: abbr,
1133-
defaultsTo: defaultsTo as bool,
1134+
defaultsTo: defaultsTo as bool?,
11341135
help: help,
11351136
hide: hide,
11361137
negatable: negatable);

lib/src/model/documentation_comment.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,13 @@ mixin DocumentationComment
272272
return await config.tools.runner.run(args, content: basicMatch[2]!,
273273
toolErrorCallback: (String message) async {
274274
warn(PackageWarning.toolError, message: message);
275-
},
276-
environment: _toolsEnvironment(invocationIndex: invocationIndex)
277-
as Map<String, String>);
275+
}, environment: _toolsEnvironment(invocationIndex: invocationIndex));
278276
});
279277
}
280278

281279
/// The environment variables to use when running a tool.
282-
Map<String, String?> _toolsEnvironment({required int invocationIndex}) {
283-
return {
280+
Map<String, String> _toolsEnvironment({required int invocationIndex}) {
281+
var env = {
284282
'SOURCE_LINE': characterLocation?.lineNumber.toString(),
285283
'SOURCE_COLUMN': characterLocation?.columnNumber.toString(),
286284
if (sourceFileName != null && package?.packagePath != null)
@@ -292,7 +290,9 @@ mixin DocumentationComment
292290
'ELEMENT_NAME': fullyQualifiedNameWithoutLibrary,
293291
'INVOCATION_INDEX': invocationIndex.toString(),
294292
'PACKAGE_INVOCATION_INDEX': (package?.toolInvocationIndex++).toString(),
295-
}..removeWhere((key, value) => value == null);
293+
};
294+
return (env..removeWhere((key, value) => value == null))
295+
.cast<String, String>();
296296
}
297297

298298
/// Replace &#123;@example ...&#125; in API comments with the content of named file.

lib/src/model/package_builder.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import 'package:dartdoc/src/package_meta.dart'
3030
import 'package:dartdoc/src/quiver.dart' as quiver;
3131
import 'package:dartdoc/src/render/renderer_factory.dart';
3232
import 'package:dartdoc/src/special_elements.dart';
33-
import 'package:package_config/package_config.dart';
3433
import 'package:path/path.dart' as path show Context;
3534

3635
/// Everything you need to instantiate a PackageGraph object for documenting.
@@ -251,9 +250,8 @@ class PubPackageBuilder implements PackageBuilder {
251250
var packageDirs = {basePackageDir};
252251

253252
if (autoIncludeDependencies) {
254-
var info = await (packageConfigProvider
255-
.findPackageConfig(resourceProvider.getFolder(basePackageDir))
256-
as FutureOr<PackageConfig>);
253+
var info = (await packageConfigProvider
254+
.findPackageConfig(resourceProvider.getFolder(basePackageDir)))!;
257255
for (var package in info.packages) {
258256
if (!filterExcludes || !config.exclude.contains(package.name)) {
259257
packageDirs.add(_pathContext.dirname(

test/dartdoc_options_test.dart

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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-
// @dart=2.9
6-
75
library dartdoc.options_test;
86

97
import 'package:analyzer/file_system/file_system.dart';
@@ -15,16 +13,16 @@ import 'package:test/test.dart';
1513
import 'package:yaml/yaml.dart';
1614

1715
class ConvertedOption {
18-
final String param1;
19-
final String param2;
16+
final String? param1;
17+
final String? param2;
2018
final String myContextPath;
2119

2220
ConvertedOption._(this.param1, this.param2, this.myContextPath);
2321

2422
static ConvertedOption fromYamlMap(YamlMap yamlMap, String canonicalYamlPath,
2523
ResourceProvider resourceProvider) {
26-
String p1;
27-
String p2;
24+
String? p1;
25+
String? p2;
2826

2927
for (var entry in yamlMap.entries) {
3028
switch (entry.key.toString()) {
@@ -43,20 +41,19 @@ class ConvertedOption {
4341
void main() {
4442
var resourceProvider = pubPackageMetaProvider.resourceProvider;
4543

46-
DartdocOptionRoot dartdocOptionSetFiles;
47-
DartdocOptionRoot dartdocOptionSetArgs;
48-
DartdocOptionRoot dartdocOptionSetAll;
49-
DartdocOptionRoot dartdocOptionSetSynthetic;
50-
Folder tempDir;
51-
Folder firstDir;
52-
Folder secondDir;
53-
Folder secondDirFirstSub;
54-
Folder secondDirSecondSub;
55-
56-
File dartdocOptionsOne;
57-
File dartdocOptionsTwo;
58-
File dartdocOptionsTwoFirstSub;
59-
File firstExisting;
44+
late final DartdocOptionRoot dartdocOptionSetFiles;
45+
late final DartdocOptionRoot dartdocOptionSetArgs;
46+
late final DartdocOptionRoot dartdocOptionSetAll;
47+
late final DartdocOptionRoot dartdocOptionSetSynthetic;
48+
late final Folder tempDir;
49+
late final Folder firstDir;
50+
late final Folder secondDir;
51+
late final Folder secondDirFirstSub;
52+
late final Folder secondDirSecondSub;
53+
54+
late final File dartdocOptionsOne;
55+
late final File dartdocOptionsTwo;
56+
late final File firstExisting;
6057

6158
setUpAll(() {
6259
dartdocOptionSetSynthetic = DartdocOptionRoot('dartdoc', resourceProvider);
@@ -98,24 +95,24 @@ void main() {
9895
dartdocOptionSetFiles.add(DartdocOptionFileOnly<List<String>>(
9996
'fileOptionList', [], resourceProvider,
10097
optionIs: OptionKind.file, mustExist: true));
101-
dartdocOptionSetFiles.add(DartdocOptionFileOnly<String>(
98+
dartdocOptionSetFiles.add(DartdocOptionFileOnly<String?>(
10299
'fileOption', null, resourceProvider,
103100
optionIs: OptionKind.file, mustExist: true));
104101
dartdocOptionSetFiles.add(DartdocOptionFileOnly<String>(
105102
'parentOverride', 'oops', resourceProvider,
106103
parentDirOverridesChild: true));
107-
dartdocOptionSetFiles.add(DartdocOptionFileOnly<String>(
104+
dartdocOptionSetFiles.add(DartdocOptionFileOnly<String?>(
108105
'nonCriticalFileOption', null, resourceProvider,
109106
optionIs: OptionKind.file));
110107
dartdocOptionSetFiles.add(DartdocOptionSet('nestedOption', resourceProvider)
111108
..addAll([DartdocOptionFileOnly<bool>('flag', false, resourceProvider)]));
112-
dartdocOptionSetFiles.add(DartdocOptionFileOnly<String>(
109+
dartdocOptionSetFiles.add(DartdocOptionFileOnly<String?>(
113110
'dirOption', null, resourceProvider,
114111
optionIs: OptionKind.dir, mustExist: true));
115-
dartdocOptionSetFiles.add(DartdocOptionFileOnly<String>(
112+
dartdocOptionSetFiles.add(DartdocOptionFileOnly<String?>(
116113
'nonCriticalDirOption', null, resourceProvider,
117114
optionIs: OptionKind.dir));
118-
dartdocOptionSetFiles.add(DartdocOptionFileOnly<ConvertedOption>(
115+
dartdocOptionSetFiles.add(DartdocOptionFileOnly<ConvertedOption?>(
119116
'convertThisMap',
120117
null,
121118
resourceProvider,
@@ -166,7 +163,7 @@ void main() {
166163
'mapOption', {'hi': 'there'}, resourceProvider));
167164
dartdocOptionSetAll.add(DartdocOptionArgFile<String>(
168165
'notInAnyFile', 'so there', resourceProvider));
169-
dartdocOptionSetAll.add(DartdocOptionArgFile<String>(
166+
dartdocOptionSetAll.add(DartdocOptionArgFile<String?>(
170167
'fileOption', null, resourceProvider,
171168
optionIs: OptionKind.file, mustExist: true));
172169
dartdocOptionSetAll.add(DartdocOptionArgFile<List<String>>(
@@ -202,7 +199,7 @@ void main() {
202199
.join(firstDir.path, 'dartdoc_options.yaml'));
203200
dartdocOptionsTwo = resourceProvider.getFile(resourceProvider.pathContext
204201
.join(secondDir.path, 'dartdoc_options.yaml'));
205-
dartdocOptionsTwoFirstSub = resourceProvider.getFile(resourceProvider
202+
var dartdocOptionsTwoFirstSub = resourceProvider.getFile(resourceProvider
206203
.pathContext
207204
.join(secondDirFirstSub.path, 'dartdoc_options.yaml'));
208205

@@ -265,7 +262,7 @@ dartdoc:
265262
dartdocOptionSetSynthetic['vegetableLoaderChecked'].valueAt(firstDir),
266263
orderedEquals([path.canonicalize(firstExisting.path)]));
267264

268-
String errorMessage;
265+
String? errorMessage;
269266
try {
270267
dartdocOptionSetSynthetic['vegetableLoaderChecked'].valueAt(tempDir);
271268
} on DartdocFileMissing catch (e) {
@@ -313,7 +310,7 @@ dartdoc:
313310
() {
314311
dartdocOptionSetAll
315312
.parseArguments(['--file-option', 'override-not-existing.dart']);
316-
String errorMessage;
313+
String? errorMessage;
317314
try {
318315
dartdocOptionSetAll['fileOption'].valueAt(firstDir);
319316
} on DartdocFileMissing catch (e) {
@@ -338,7 +335,7 @@ dartdoc:
338335

339336
test('File errors still get passed through', () {
340337
dartdocOptionSetAll.parseArguments([]);
341-
String errorMessage;
338+
String? errorMessage;
342339
try {
343340
dartdocOptionSetAll['fileOption'].valueAt(secondDir);
344341
} on DartdocFileMissing catch (e) {
@@ -456,7 +453,7 @@ dartdoc:
456453
});
457454

458455
test('DartdocOptionArgOnly checks file existence', () {
459-
String errorMessage;
456+
String? errorMessage;
460457
dartdocOptionSetArgs.parseArguments(['--single-file', 'not_found.txt']);
461458
try {
462459
dartdocOptionSetArgs['singleFile'].valueAt(tempDir);
@@ -474,7 +471,7 @@ dartdoc:
474471
});
475472

476473
test('DartdocOptionArgOnly checks file existence on multi-options', () {
477-
String errorMessage;
474+
String? errorMessage;
478475
dartdocOptionSetArgs.parseArguments([
479476
'--files-flag',
480477
resourceProvider.pathContext.absolute(firstExisting.path),
@@ -571,7 +568,7 @@ dartdoc:
571568

572569
test('DartdocOptionArgOnly throws on double type mismatch', () {
573570
dartdocOptionSetArgs.parseArguments(['--respawn-probability', 'unknown']);
574-
String errorMessage;
571+
String? errorMessage;
575572
try {
576573
dartdocOptionSetArgs['respawnProbability'].valueAt(tempDir);
577574
} on DartdocOptionError catch (e) {
@@ -587,7 +584,7 @@ dartdoc:
587584
dartdocOptionSetArgs.parseArguments(['--number-of-heads', '3.6']);
588585
expect(() => dartdocOptionSetArgs['number_of_heads'].valueAt(tempDir),
589586
throwsA(const TypeMatcher<DartdocOptionError>()));
590-
String errorMessage;
587+
String? errorMessage;
591588
try {
592589
dartdocOptionSetArgs['number_of_heads'].valueAt(tempDir);
593590
} on DartdocOptionError catch (e) {
@@ -602,7 +599,7 @@ dartdoc:
602599
test('DartdocOptionArgOnly throws on a map type mismatch', () {
603600
dartdocOptionSetArgs
604601
.parseArguments(['--a-fancy-map-variable', 'not a map']);
605-
String errorMessage;
602+
String? errorMessage;
606603
try {
607604
dartdocOptionSetArgs['aFancyMapVariable'].valueAt(tempDir);
608605
} on DartdocOptionError catch (e) {
@@ -628,7 +625,7 @@ dartdoc:
628625
});
629626

630627
test('DartdocOptionSetFile checks file existence when appropriate', () {
631-
String errorMessage;
628+
String? errorMessage;
632629
try {
633630
dartdocOptionSetFiles['fileOptionList'].valueAt(secondDir);
634631
} on DartdocFileMissing catch (e) {
@@ -654,7 +651,7 @@ dartdoc:
654651
test(
655652
'DartdocOptionSetFile resolves paths for files relative to where they are declared',
656653
() {
657-
String errorMessage;
654+
String? errorMessage;
658655
try {
659656
dartdocOptionSetFiles['fileOption'].valueAt(secondDirFirstSub);
660657
} on DartdocFileMissing catch (e) {
@@ -681,7 +678,7 @@ dartdoc:
681678
test('DartdocOptionSetFile checks errors for directory options', () {
682679
expect(dartdocOptionSetFiles['dirOption'].valueAt(secondDir),
683680
equals(path.canonicalize(path.join(secondDir.path, 'firstSub'))));
684-
String errorMessage;
681+
String? errorMessage;
685682
try {
686683
dartdocOptionSetFiles['dirOption'].valueAt(firstDir);
687684
} on DartdocFileMissing catch (e) {

test/end2end/dartdoc_integration_test.dart

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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-
// @dart=2.9
6-
75
library dartdoc.dartdoc_integration_test;
86

97
import 'dart:async';
@@ -18,7 +16,7 @@ import '../../tool/subprocess_launcher.dart';
1816
import '../src/utils.dart';
1917

2018
Uri get _currentFileUri =>
21-
(reflect(main) as ClosureMirror).function.location.sourceUri;
19+
(reflect(main) as ClosureMirror).function.location!.sourceUri;
2220
String get _testPackagePath =>
2321
path.fromUri(_currentFileUri.resolve('../../testing/test_package'));
2422
String get _testPackageFlutterPluginPath => path.fromUri(_currentFileUri
@@ -29,8 +27,8 @@ String get _testPackageMinimumPath =>
2927
void main() {
3028
group('Invoking command-line dartdoc', () {
3129
var dartdocPath = path.canonicalize(path.join('bin', 'dartdoc.dart'));
32-
CoverageSubprocessLauncher subprocessLauncher;
33-
Directory tempDir;
30+
late final CoverageSubprocessLauncher subprocessLauncher;
31+
late final Directory tempDir;
3432

3533
setUpAll(() async {
3634
tempDir =
@@ -181,7 +179,7 @@ void main() {
181179
await subprocessLauncher.runStreamed(Platform.resolvedExecutable, args,
182180
workingDirectory: _testPackagePath,
183181
perLine: (s) => output.writeln(s));
184-
var dartdocMeta = pubPackageMetaProvider.fromFilename(dartdocPath);
182+
var dartdocMeta = pubPackageMetaProvider.fromFilename(dartdocPath)!;
185183
expect(output.toString(),
186184
endsWith('dartdoc version: ${dartdocMeta.version}\n'));
187185
});
@@ -238,9 +236,9 @@ void main() {
238236
var footerRegex =
239237
RegExp(r'<footer>(.*\s*?\n?)+?</footer>', multiLine: true);
240238
// get footer, check for version number
241-
var m = footerRegex.firstMatch(outFile.readAsStringSync());
239+
var m = footerRegex.firstMatch(outFile.readAsStringSync())!;
242240
var version = RegExp(r'(\d+\.)?(\d+\.)?(\*|\d+)');
243-
expect(version.hasMatch(m.group(0)), false);
241+
expect(version.hasMatch(m.group(0)!), false);
244242
});
245243
}, timeout: Timeout.factor(8));
246244
}

0 commit comments

Comments
 (0)