[tool] Update to Dart 3 (#6030)

Updates the repo tooling to Dart 3, now that the N-2 version has Dart 3, which allows us to use Dart 3 features (e.g., records) going forward.

To allow the update:
- Removes `break` commands from `switch`es (all done automatically with `dart fix --apply`)
- Replaces mocking of `ProcessResult` with just creating an actual `ProcessResult` since it's a `final` data class and thus can't (but also doesn't need to be) mocked.
This commit is contained in:
stuartmorgan
2024-02-06 11:31:48 -08:00
committed by GitHub
parent 9382257ce7
commit 7403992f15
15 changed files with 70 additions and 99 deletions

View File

@ -123,7 +123,6 @@ abstract class PackageLoopingCommand extends PackageCommand {
switch (packageLoopingType) {
case PackageLoopingType.topLevelOnly:
yield* getTargetPackages(filterExcluded: false);
break;
case PackageLoopingType.includeExamples:
await for (final PackageEnumerationEntry packageEntry
in getTargetPackages(filterExcluded: false)) {
@ -135,10 +134,8 @@ abstract class PackageLoopingCommand extends PackageCommand {
package,
excluded: packageEntry.excluded)));
}
break;
case PackageLoopingType.includeAllSubpackages:
yield* getTargetPackagesAndSubpackages(filterExcluded: false);
break;
}
}

View File

@ -72,22 +72,16 @@ class RepositoryPackage {
switch (platform) {
case FlutterPlatform.android:
directoryName = 'android';
break;
case FlutterPlatform.ios:
directoryName = 'ios';
break;
case FlutterPlatform.linux:
directoryName = 'linux';
break;
case FlutterPlatform.macos:
directoryName = 'macos';
break;
case FlutterPlatform.web:
directoryName = 'web';
break;
case FlutterPlatform.windows:
directoryName = 'windows';
break;
}
return directory.childDirectory(directoryName);
}

View File

@ -125,30 +125,23 @@ class FetchDepsCommand extends PackageLoopingCommand {
switch (platform) {
case FlutterPlatform.android:
result = await _fetchAndroidDeps(package);
break;
case FlutterPlatform.ios:
result = await _fetchDarwinDeps(package, platformIOS);
break;
case FlutterPlatform.macos:
result = await _fetchDarwinDeps(package, platformMacOS);
break;
case FlutterPlatform.linux:
case FlutterPlatform.web:
case FlutterPlatform.windows:
// No native dependency handling yet.
result = PackageResult.skip('Nothing to do for $platform.');
break;
}
switch (result.state) {
case RunState.succeeded:
fetchedDeps = true;
break;
case RunState.skipped:
skips.add(result.details.first);
break;
case RunState.failed:
errors.addAll(result.details);
break;
case RunState.excluded:
throw StateError('Unreachable');
}

View File

@ -42,7 +42,6 @@ class ListCommand extends PackageCommand {
await for (final PackageEnumerationEntry entry in getTargetPackages()) {
print(entry.package.path);
}
break;
case _example:
final Stream<RepositoryPackage> examples = getTargetPackages()
.expand<RepositoryPackage>(
@ -50,18 +49,15 @@ class ListCommand extends PackageCommand {
await for (final RepositoryPackage package in examples) {
print(package.path);
}
break;
case _allPackage:
await for (final PackageEnumerationEntry entry
in getTargetPackagesAndSubpackages()) {
print(entry.package.path);
}
break;
case _file:
await for (final File file in getFiles()) {
print(file.path);
}
break;
}
}
}

View File

@ -481,7 +481,6 @@ this command.
switch (exitCode) {
case xcodebuildNoTestExitCode:
_printNoExampleTestsMessage(example, platform);
break;
case 0:
printSuccess('Successfully ran $platform xctest for $exampleName');
// If this is the first test, assume success until something fails.
@ -491,7 +490,6 @@ this command.
if (exampleHasUnitTests) {
ranUnitTests = true;
}
break;
default:
// Any failure means a failure overall.
overallResult = RunState.failed;

View File

@ -113,7 +113,6 @@ class UpdateDependencyCommand extends PackageLoopingCommand {
switch (response.result) {
case PubVersionFinderResult.success:
_targetVersion = response.versions.first.toString();
break;
case PubVersionFinderResult.fail:
printError('''
Error fetching $_targetPubPackage version from pub: ${response.httpResponse.statusCode}:

View File

@ -138,20 +138,15 @@ class UpdateExcerptsCommand extends PackageLoopingCommand {
switch (extension) {
case '':
language = 'txt';
break;
case '.kt':
language = 'kotlin';
break;
case '.cc':
case '.cpp':
language = 'c++';
break;
case '.m':
language = 'objectivec';
break;
case '.gradle':
language = 'groovy';
break;
default:
language = extension.substring(1);
break;
@ -173,7 +168,6 @@ class UpdateExcerptsCommand extends PackageLoopingCommand {
}
}
output.writeln(line);
break;
case _ExcerptParseMode.pragma:
if (!line.startsWith('```')) {
errors.add(
@ -195,7 +189,6 @@ class UpdateExcerptsCommand extends PackageLoopingCommand {
mode = _ExcerptParseMode.injecting;
}
output.writeln(line);
break;
case _ExcerptParseMode.injecting:
if (line == '```') {
if (existingBlock.toString() != excerpt) {
@ -210,7 +203,6 @@ class UpdateExcerptsCommand extends PackageLoopingCommand {
} else {
existingBlock.writeln(line);
}
break;
}
}
if (detectedChange) {
@ -250,23 +242,18 @@ class UpdateExcerptsCommand extends PackageLoopingCommand {
case 'objectivec':
case 'swift':
prefix = '// ';
break;
case 'css':
prefix = '/* ';
suffix = ' */';
break;
case 'html':
case 'xml':
prefix = '<!--';
suffix = '-->';
padding = ' ';
break;
case 'yaml':
prefix = '# ';
break;
case 'sh':
prefix = '# ';
break;
}
final String startRegionMarker = '$prefix#docregion $section$suffix';
final String endRegionMarker = '$prefix#enddocregion $section$suffix';

View File

@ -100,10 +100,8 @@ class UpdateReleaseInfoCommand extends PackageLoopingCommand {
switch (getStringArg(_versionTypeFlag)) {
case _versionMinor:
_versionChange = _VersionIncrementType.minor;
break;
case _versionBugfix:
_versionChange = _VersionIncrementType.bugfix;
break;
case _versionMinimal:
final GitVersionFinder gitVersionFinder = await retrieveVersionFinder();
// If the line below fails with "Not a valid object name FETCH_HEAD"
@ -113,10 +111,8 @@ class UpdateReleaseInfoCommand extends PackageLoopingCommand {
_changedFiles = await gitVersionFinder.getChangedFiles();
// Anothing other than a fixed change is null.
_versionChange = null;
break;
case _versionNext:
_versionChange = null;
break;
default:
throw UnimplementedError('Unimplemented version change type');
}
@ -169,10 +165,8 @@ class UpdateReleaseInfoCommand extends PackageLoopingCommand {
switch (updateOutcome) {
case _ChangelogUpdateOutcome.addedSection:
print('${indentation}Added a $nextVersionString section.');
break;
case _ChangelogUpdateOutcome.updatedSection:
print('${indentation}Updated NEXT section.');
break;
case _ChangelogUpdateOutcome.failed:
return PackageResult.fail(<String>['Could not update CHANGELOG.md.']);
}
@ -217,7 +211,6 @@ class UpdateReleaseInfoCommand extends PackageLoopingCommand {
].forEach(newChangelog.writeln);
state = _ChangelogUpdateState.finishedUpdating;
}
break;
case _ChangelogUpdateState.findingFirstListItem:
final RegExpMatch? match = listItemPattern.firstMatch(line);
if (match != null) {
@ -239,11 +232,9 @@ class UpdateReleaseInfoCommand extends PackageLoopingCommand {
printError(' Existing NEXT section has unrecognized format.');
return _ChangelogUpdateOutcome.failed;
}
break;
case _ChangelogUpdateState.finishedUpdating:
// Once changes are done, add the rest of the lines as-is.
newChangelog.writeln(line);
break;
}
}

View File

@ -210,20 +210,16 @@ class VersionCheckCommand extends PackageLoopingCommand {
switch (versionState) {
case _CurrentVersionState.unchanged:
versionChanged = false;
break;
case _CurrentVersionState.validIncrease:
case _CurrentVersionState.validRevert:
case _CurrentVersionState.newPackage:
versionChanged = true;
break;
case _CurrentVersionState.invalidChange:
versionChanged = true;
errors.add('Disallowed version change.');
break;
case _CurrentVersionState.unknown:
versionChanged = false;
errors.add('Unable to determine previous version.');
break;
}
if (!(await _validateChangelogVersion(package,

View File

@ -28,7 +28,7 @@ dependencies:
dev_dependencies:
build_runner: ^2.0.3
matcher: ^0.12.10
mockito: '>=5.3.2 <=5.4.0'
mockito: '>=5.3.2 <=5.5.0'
environment:
sdk: '>=2.18.0 <4.0.0'
sdk: '>=3.0.0 <4.0.0'

View File

@ -25,15 +25,14 @@ void main() {
final List<String> arguments =
invocation.positionalArguments[0]! as List<String>;
gitDirCommands.add(arguments);
final MockProcessResult mockProcessResult = MockProcessResult();
String? gitStdOut;
if (arguments[0] == 'diff') {
when<String?>(mockProcessResult.stdout as String?)
.thenReturn(gitDiffResponse);
gitStdOut = gitDiffResponse;
} else if (arguments[0] == 'merge-base') {
when<String?>(mockProcessResult.stdout as String?)
.thenReturn(mergeBaseResponse);
gitStdOut = mergeBaseResponse;
}
return Future<ProcessResult>.value(mockProcessResult);
return Future<ProcessResult>.value(
ProcessResult(0, 0, gitStdOut ?? '', ''));
});
});
@ -128,5 +127,3 @@ file2/file2.cc
verify(gitDir.runCommand(<String>['diff', '--name-only', customBaseSha]));
});
}
class MockProcessResult extends Mock implements ProcessResult {}

View File

@ -1,23 +1,26 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.4 from annotations
// in flutter_plugin_tools/test/common/package_command_test.dart.
// Do not manually edit this file.
// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i6;
import 'dart:io' as _i4;
import 'dart:io' as _i10;
import 'package:git/src/branch_reference.dart' as _i3;
import 'package:git/src/commit.dart' as _i2;
import 'package:git/src/commit_reference.dart' as _i8;
import 'package:git/src/git_dir.dart' as _i5;
import 'package:git/src/git_dir.dart' as _i4;
import 'package:git/src/tag.dart' as _i7;
import 'package:git/src/tree_entry.dart' as _i9;
import 'package:mockito/mockito.dart' as _i1;
import 'package:mockito/src/dummies.dart' as _i5;
// ignore_for_file: type=lint
// ignore_for_file: avoid_redundant_argument_values
// ignore_for_file: avoid_setters_without_getters
// ignore_for_file: comment_references
// ignore_for_file: deprecated_member_use
// ignore_for_file: deprecated_member_use_from_same_package
// ignore_for_file: implementation_imports
// ignore_for_file: invalid_use_of_visible_for_testing_member
// ignore_for_file: prefer_const_constructors
@ -46,20 +49,10 @@ class _FakeBranchReference_1 extends _i1.SmartFake
);
}
class _FakeProcessResult_2 extends _i1.SmartFake implements _i4.ProcessResult {
_FakeProcessResult_2(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
}
/// A class which mocks [GitDir].
///
/// See the documentation for Mockito's code generation for more information.
class MockGitDir extends _i1.Mock implements _i5.GitDir {
class MockGitDir extends _i1.Mock implements _i4.GitDir {
MockGitDir() {
_i1.throwOnMissingStub(this);
}
@ -67,8 +60,12 @@ class MockGitDir extends _i1.Mock implements _i5.GitDir {
@override
String get path => (super.noSuchMethod(
Invocation.getter(#path),
returnValue: '',
returnValue: _i5.dummyValue<String>(
this,
Invocation.getter(#path),
),
) as String);
@override
_i6.Future<int> commitCount([String? branchName = r'HEAD']) =>
(super.noSuchMethod(
@ -78,6 +75,7 @@ class MockGitDir extends _i1.Mock implements _i5.GitDir {
),
returnValue: _i6.Future<int>.value(0),
) as _i6.Future<int>);
@override
_i6.Future<_i2.Commit> commitFromRevision(String? revision) =>
(super.noSuchMethod(
@ -93,6 +91,7 @@ class MockGitDir extends _i1.Mock implements _i5.GitDir {
),
)),
) as _i6.Future<_i2.Commit>);
@override
_i6.Future<Map<String, _i2.Commit>> commits([String? branchName = r'HEAD']) =>
(super.noSuchMethod(
@ -103,6 +102,7 @@ class MockGitDir extends _i1.Mock implements _i5.GitDir {
returnValue:
_i6.Future<Map<String, _i2.Commit>>.value(<String, _i2.Commit>{}),
) as _i6.Future<Map<String, _i2.Commit>>);
@override
_i6.Future<_i3.BranchReference?> branchReference(String? branchName) =>
(super.noSuchMethod(
@ -112,6 +112,7 @@ class MockGitDir extends _i1.Mock implements _i5.GitDir {
),
returnValue: _i6.Future<_i3.BranchReference?>.value(),
) as _i6.Future<_i3.BranchReference?>);
@override
_i6.Future<List<_i3.BranchReference>> branches() => (super.noSuchMethod(
Invocation.method(
@ -121,6 +122,7 @@ class MockGitDir extends _i1.Mock implements _i5.GitDir {
returnValue: _i6.Future<List<_i3.BranchReference>>.value(
<_i3.BranchReference>[]),
) as _i6.Future<List<_i3.BranchReference>>);
@override
_i6.Stream<_i7.Tag> tags() => (super.noSuchMethod(
Invocation.method(
@ -129,6 +131,7 @@ class MockGitDir extends _i1.Mock implements _i5.GitDir {
),
returnValue: _i6.Stream<_i7.Tag>.empty(),
) as _i6.Stream<_i7.Tag>);
@override
_i6.Future<List<_i8.CommitReference>> showRef({
bool? heads = false,
@ -146,6 +149,7 @@ class MockGitDir extends _i1.Mock implements _i5.GitDir {
returnValue: _i6.Future<List<_i8.CommitReference>>.value(
<_i8.CommitReference>[]),
) as _i6.Future<List<_i8.CommitReference>>);
@override
_i6.Future<_i3.BranchReference> currentBranch() => (super.noSuchMethod(
Invocation.method(
@ -161,6 +165,7 @@ class MockGitDir extends _i1.Mock implements _i5.GitDir {
),
)),
) as _i6.Future<_i3.BranchReference>);
@override
_i6.Future<List<_i9.TreeEntry>> lsTree(
String? treeish, {
@ -178,6 +183,7 @@ class MockGitDir extends _i1.Mock implements _i5.GitDir {
),
returnValue: _i6.Future<List<_i9.TreeEntry>>.value(<_i9.TreeEntry>[]),
) as _i6.Future<List<_i9.TreeEntry>>);
@override
_i6.Future<String?> createOrUpdateBranch(
String? branchName,
@ -195,6 +201,7 @@ class MockGitDir extends _i1.Mock implements _i5.GitDir {
),
returnValue: _i6.Future<String?>.value(),
) as _i6.Future<String?>);
@override
_i6.Future<String> commitTree(
String? treeSha,
@ -210,8 +217,19 @@ class MockGitDir extends _i1.Mock implements _i5.GitDir {
],
{#parentCommitShas: parentCommitShas},
),
returnValue: _i6.Future<String>.value(''),
returnValue: _i6.Future<String>.value(_i5.dummyValue<String>(
this,
Invocation.method(
#commitTree,
[
treeSha,
commitMessage,
],
{#parentCommitShas: parentCommitShas},
),
)),
) as _i6.Future<String>);
@override
_i6.Future<Map<String, String>> writeObjects(List<String>? paths) =>
(super.noSuchMethod(
@ -221,8 +239,9 @@ class MockGitDir extends _i1.Mock implements _i5.GitDir {
),
returnValue: _i6.Future<Map<String, String>>.value(<String, String>{}),
) as _i6.Future<Map<String, String>>);
@override
_i6.Future<_i4.ProcessResult> runCommand(
_i6.Future<_i10.ProcessResult> runCommand(
Iterable<String>? args, {
bool? throwOnError = true,
}) =>
@ -232,7 +251,8 @@ class MockGitDir extends _i1.Mock implements _i5.GitDir {
[args],
{#throwOnError: throwOnError},
),
returnValue: _i6.Future<_i4.ProcessResult>.value(_FakeProcessResult_2(
returnValue: _i6.Future<_i10.ProcessResult>.value(
_i5.dummyValue<_i10.ProcessResult>(
this,
Invocation.method(
#runCommand,
@ -240,7 +260,8 @@ class MockGitDir extends _i1.Mock implements _i5.GitDir {
{#throwOnError: throwOnError},
),
)),
) as _i6.Future<_i4.ProcessResult>);
) as _i6.Future<_i10.ProcessResult>);
@override
_i6.Future<bool> isWorkingTreeClean() => (super.noSuchMethod(
Invocation.method(
@ -249,10 +270,11 @@ class MockGitDir extends _i1.Mock implements _i5.GitDir {
),
returnValue: _i6.Future<bool>.value(false),
) as _i6.Future<bool>);
@override
_i6.Future<_i2.Commit?> updateBranch(
String? branchName,
_i6.Future<dynamic> Function(_i4.Directory)? populater,
_i6.Future<dynamic> Function(_i10.Directory)? populater,
String? commitMessage,
) =>
(super.noSuchMethod(
@ -266,6 +288,7 @@ class MockGitDir extends _i1.Mock implements _i5.GitDir {
),
returnValue: _i6.Future<_i2.Commit?>.value(),
) as _i6.Future<_i2.Commit?>);
@override
_i6.Future<_i2.Commit?> updateBranchWithDirectoryContents(
String? branchName,

View File

@ -116,12 +116,12 @@ void main() {
.thenAnswer((Invocation invocation) {
final List<String> arguments =
invocation.positionalArguments[0]! as List<String>;
final MockProcessResult mockProcessResult = MockProcessResult();
String? gitStdOut;
if (arguments[0] == 'diff') {
when<String?>(mockProcessResult.stdout as String?)
.thenReturn(gitDiffResponse);
gitStdOut = gitDiffResponse;
}
return Future<io.ProcessResult>.value(mockProcessResult);
return Future<io.ProcessResult>.value(
io.ProcessResult(0, 0, gitStdOut ?? '', ''));
});
return TestPackageLoopingCommand(
@ -945,5 +945,3 @@ class TestPackageLoopingCommand extends PackageLoopingCommand {
capturedOutput.addAll(output);
}
}
class MockProcessResult extends Mock implements io.ProcessResult {}

View File

@ -394,8 +394,9 @@ void main() {
platformIOS: const PlatformDetails(PlatformSupport.inline)
});
final Iterable<Directory> exampleDirs = plugin.getExamples().map(
(RepositoryPackage example) => example.directory);
final Iterable<Directory> exampleDirs = plugin
.getExamples()
.map((RepositoryPackage example) => example.directory);
final List<String> output = await runCapturingPrint(
runner, <String>['fetch-deps', '--no-dart', '--ios']);
@ -432,10 +433,11 @@ void main() {
});
processRunner
.mockProcessesForExecutable[getFlutterCommand(mockPlatform)] =
<FakeProcessInfo>[
.mockProcessesForExecutable[getFlutterCommand(mockPlatform)] =
<FakeProcessInfo>[
FakeProcessInfo(MockProcess(), <String>['precache']),
FakeProcessInfo(MockProcess(exitCode: 1), <String>['build', 'ios', '--config-only']),
FakeProcessInfo(MockProcess(exitCode: 1),
<String>['build', 'ios', '--config-only']),
];
Error? commandError;
@ -499,8 +501,9 @@ void main() {
platformMacOS: const PlatformDetails(PlatformSupport.inline)
});
final Iterable<Directory> exampleDirs = plugin.getExamples().map(
(RepositoryPackage example) => example.directory);
final Iterable<Directory> exampleDirs = plugin
.getExamples()
.map((RepositoryPackage example) => example.directory);
final List<String> output = await runCapturingPrint(
runner, <String>['fetch-deps', '--no-dart', '--macos']);
@ -537,18 +540,19 @@ void main() {
});
processRunner
.mockProcessesForExecutable[getFlutterCommand(mockPlatform)] =
<FakeProcessInfo>[
.mockProcessesForExecutable[getFlutterCommand(mockPlatform)] =
<FakeProcessInfo>[
FakeProcessInfo(MockProcess(), <String>['precache']),
FakeProcessInfo(MockProcess(exitCode: 1), <String>['build', 'macos', '--config-only']),
FakeProcessInfo(MockProcess(exitCode: 1),
<String>['build', 'macos', '--config-only']),
];
Error? commandError;
final List<String> output = await runCapturingPrint(
runner, <String>['fetch-deps', '--no-dart', '--macos'],
errorHandler: (Error e) {
commandError = e;
});
commandError = e;
});
expect(commandError, isA<ToolExit>());
expect(

View File

@ -306,13 +306,11 @@ String _pluginPlatformSection(
if (support.hasDartCode) {
lines.add(' dartPluginClass: FakeDartPlugin');
}
break;
case platformWeb:
lines.addAll(<String>[
' pluginClass: FakePlugin',
' fileName: ${packageName}_web.dart',
]);
break;
default:
assert(false, 'Unrecognized platform: $platform');
break;