From 12ba9046d7dd824a23645122504d48ee148d0dd8 Mon Sep 17 00:00:00 2001 From: Jenn Magder Date: Wed, 7 Apr 2021 16:49:23 -0700 Subject: [PATCH] Migrate tool version to null safety (#79911) --- .../flutter_tools/lib/src/base/process.dart | 32 ++-- .../lib/src/commands/channel.dart | 2 +- .../lib/src/commands/create.dart | 2 +- .../lib/src/commands/create_base.dart | 2 +- packages/flutter_tools/lib/src/globals.dart | 2 - .../lib/src/globals_null_migrated.dart | 2 + .../flutter_tools/lib/src/sksl_writer.dart | 2 +- packages/flutter_tools/lib/src/version.dart | 172 ++++++++---------- .../runner/flutter_command_runner_test.dart | 2 +- .../test/general.shard/version_test.dart | 117 +++++++----- packages/flutter_tools/test/src/fakes.dart | 5 - 11 files changed, 171 insertions(+), 169 deletions(-) diff --git a/packages/flutter_tools/lib/src/base/process.dart b/packages/flutter_tools/lib/src/base/process.dart index fe053b14bd..c5924b1d7f 100644 --- a/packages/flutter_tools/lib/src/base/process.dart +++ b/packages/flutter_tools/lib/src/base/process.dart @@ -167,11 +167,11 @@ abstract class ProcessUtils { Future run( List cmd, { bool throwOnError = false, - RunResultChecker allowedFailures, - String workingDirectory, + RunResultChecker? allowedFailures, + String? workingDirectory, bool allowReentrantFlutter = false, - Map environment, - Duration timeout, + Map? environment, + Duration? timeout, int timeoutRetries = 0, }); @@ -180,10 +180,10 @@ abstract class ProcessUtils { List cmd, { bool throwOnError = false, bool verboseExceptions = false, - RunResultChecker allowedFailures, + RunResultChecker? allowedFailures, bool hideStdout = false, - String workingDirectory, - Map environment, + String? workingDirectory, + Map? environment, bool allowReentrantFlutter = false, Encoding encoding = systemEncoding, }); @@ -192,9 +192,9 @@ abstract class ProcessUtils { /// directory. Completes when the process has been started. Future start( List cmd, { - String workingDirectory, + String? workingDirectory, bool allowReentrantFlutter = false, - Map environment, + Map? environment, }); /// This runs the command and streams stdout/stderr from the child process to @@ -210,24 +210,24 @@ abstract class ProcessUtils { /// treated as errors, just as if they had been logged to stderr instead. Future stream( List cmd, { - String workingDirectory, + String? workingDirectory, bool allowReentrantFlutter = false, String prefix = '', bool trace = false, - RegExp filter, - RegExp stdoutErrorMatcher, - StringConverter mapFunction, - Map environment, + RegExp? filter, + RegExp? stdoutErrorMatcher, + StringConverter? mapFunction, + Map? environment, }); bool exitsHappySync( List cli, { - Map environment, + Map? environment, }); Future exitsHappy( List cli, { - Map environment, + Map? environment, }); } diff --git a/packages/flutter_tools/lib/src/commands/channel.dart b/packages/flutter_tools/lib/src/commands/channel.dart index 8f2d030f19..8e1d27f96f 100644 --- a/packages/flutter_tools/lib/src/commands/channel.dart +++ b/packages/flutter_tools/lib/src/commands/channel.dart @@ -6,7 +6,7 @@ import '../base/common.dart'; import '../cache.dart'; -import '../globals.dart' as globals; +import '../globals_null_migrated.dart' as globals; import '../runner/flutter_command.dart'; import '../version.dart'; diff --git a/packages/flutter_tools/lib/src/commands/create.dart b/packages/flutter_tools/lib/src/commands/create.dart index 83b6ac6f44..d0d7290bee 100644 --- a/packages/flutter_tools/lib/src/commands/create.dart +++ b/packages/flutter_tools/lib/src/commands/create.dart @@ -15,7 +15,7 @@ import '../dart/pub.dart'; import '../features.dart'; import '../flutter_manifest.dart'; import '../flutter_project_metadata.dart'; -import '../globals.dart' as globals; +import '../globals_null_migrated.dart' as globals; import '../project.dart'; import '../reporting/reporting.dart'; import '../runner/flutter_command.dart'; diff --git a/packages/flutter_tools/lib/src/commands/create_base.dart b/packages/flutter_tools/lib/src/commands/create_base.dart index de14934175..1d820bdebd 100644 --- a/packages/flutter_tools/lib/src/commands/create_base.dart +++ b/packages/flutter_tools/lib/src/commands/create_base.dart @@ -18,7 +18,7 @@ import '../convert.dart'; import '../dart/pub.dart'; import '../features.dart'; import '../flutter_project_metadata.dart'; -import '../globals.dart' as globals; +import '../globals_null_migrated.dart' as globals; import '../project.dart'; import '../runner/flutter_command.dart'; import '../template.dart'; diff --git a/packages/flutter_tools/lib/src/globals.dart b/packages/flutter_tools/lib/src/globals.dart index b628ae55d5..81e10df866 100644 --- a/packages/flutter_tools/lib/src/globals.dart +++ b/packages/flutter_tools/lib/src/globals.dart @@ -25,7 +25,6 @@ import 'persistent_tool_state.dart'; import 'project.dart'; import 'reporting/reporting.dart'; import 'runner/local_engine.dart'; -import 'version.dart'; export 'globals_null_migrated.dart'; @@ -49,7 +48,6 @@ CocoaPodsValidator get cocoapodsValidator => context.get(); LocalEngineLocator get localEngineLocator => context.get(); CocoaPods get cocoaPods => context.get(); -FlutterVersion get flutterVersion => context.get(); FuchsiaArtifacts get fuchsiaArtifacts => context.get(); IOSSimulatorUtils get iosSimulatorUtils => context.get(); IOSWorkflow get iosWorkflow => context.get(); diff --git a/packages/flutter_tools/lib/src/globals_null_migrated.dart b/packages/flutter_tools/lib/src/globals_null_migrated.dart index 78dcc075d5..b3cf7bcfc2 100644 --- a/packages/flutter_tools/lib/src/globals_null_migrated.dart +++ b/packages/flutter_tools/lib/src/globals_null_migrated.dart @@ -23,6 +23,7 @@ import 'base/time.dart'; import 'base/user_messages.dart'; import 'cache.dart'; import 'ios/plist_parser.dart'; +import 'version.dart'; Cache get cache => context.get()!; Config get config => context.get()!; @@ -32,6 +33,7 @@ OperatingSystemUtils get os => context.get()!; Signals get signals => context.get() ?? LocalSignals.instance; AndroidStudio? get androidStudio => context.get(); AndroidSdk? get androidSdk => context.get(); +FlutterVersion get flutterVersion => context.get()!; /// Currently active implementation of the file system. /// diff --git a/packages/flutter_tools/lib/src/sksl_writer.dart b/packages/flutter_tools/lib/src/sksl_writer.dart index b733fcd012..470281cc47 100644 --- a/packages/flutter_tools/lib/src/sksl_writer.dart +++ b/packages/flutter_tools/lib/src/sksl_writer.dart @@ -13,7 +13,7 @@ import 'base/logger.dart'; import 'build_info.dart'; import 'convert.dart'; import 'device.dart'; -import 'globals.dart' as globals; +import 'globals_null_migrated.dart' as globals; Future sharedSkSlWriter(Device device, Map data, { File outputFile, diff --git a/packages/flutter_tools/lib/src/version.dart b/packages/flutter_tools/lib/src/version.dart index 0759794fcd..6593e2e703 100644 --- a/packages/flutter_tools/lib/src/version.dart +++ b/packages/flutter_tools/lib/src/version.dart @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// @dart = 2.8 - import 'package:meta/meta.dart'; import 'base/common.dart'; @@ -18,6 +16,8 @@ import 'globals_null_migrated.dart' as globals; /// The flutter GitHub repository. String get _flutterGit => globals.platform.environment['FLUTTER_GIT_URL'] ?? 'https://github.com/flutter/flutter.git'; +const String _unknownFrameworkVersion = '0.0.0-unknown'; + /// The names of each channel/branch in order of increasing stability. enum Channel { master, @@ -45,7 +45,7 @@ String getNameForChannel(Channel channel) { /// /// Returns `null` if [name] is not in the list of official channels, according /// to [kOfficialChannels]. -Channel getChannelForName(String name) { +Channel? getChannelForName(String name) { if (kOfficialChannels.contains(name)) { return Channel.values[kOfficialChannels.toList().indexOf(name)]; } @@ -60,8 +60,8 @@ class FlutterVersion { /// available upstream. FlutterVersion({ SystemClock clock = const SystemClock(), - String workingDirectory, - String frameworkRevision, + String? workingDirectory, + String? frameworkRevision, }) : _clock = clock, _workingDirectory = workingDirectory { _frameworkRevision = frameworkRevision ?? _runGit( @@ -74,7 +74,7 @@ class FlutterVersion { } final SystemClock _clock; - final String _workingDirectory; + final String? _workingDirectory; /// Fetches tags from the upstream Flutter repository and re-calculates the /// version. @@ -87,52 +87,54 @@ class FlutterVersion { _frameworkVersion = gitTagVersion.frameworkVersionFor(_frameworkRevision); } - String _repositoryUrl; - String get repositoryUrl { + String? _repositoryUrl; + String? get repositoryUrl { final String _ = channel; return _repositoryUrl; } - String _channel; + String? _channel; /// The channel is the upstream branch. /// `master`, `dev`, `beta`, `stable`; or old ones, like `alpha`, `hackathon`, ... String get channel { - if (_channel == null) { - final String channel = _runGit( + String? channel = _channel; + if (channel == null) { + final String gitChannel = _runGit( 'git rev-parse --abbrev-ref --symbolic @{u}', globals.processUtils, _workingDirectory, ); - final int slash = channel.indexOf('/'); + final int slash = gitChannel.indexOf('/'); if (slash != -1) { - final String remote = channel.substring(0, slash); + final String remote = gitChannel.substring(0, slash); _repositoryUrl = _runGit( 'git ls-remote --get-url $remote', globals.processUtils, _workingDirectory, ); - _channel = channel.substring(slash + 1); - } else if (channel.isEmpty) { - _channel = 'unknown'; + channel = gitChannel.substring(slash + 1); + } else if (gitChannel.isEmpty) { + channel = 'unknown'; } else { - _channel = channel; + channel = gitChannel; } + _channel = channel; } - return _channel; + return channel; } - GitTagVersion _gitTagVersion; + late GitTagVersion _gitTagVersion; GitTagVersion get gitTagVersion => _gitTagVersion; /// The name of the local branch. /// Use getBranchName() to read this. - String _branch; + String? _branch; - String _frameworkRevision; + late String _frameworkRevision; String get frameworkRevision => _frameworkRevision; String get frameworkRevisionShort => _shortGitRevision(frameworkRevision); - String _frameworkAge; + String? _frameworkAge; String get frameworkAge { return _frameworkAge ??= _runGit( gitLog(['-n', '1', '--pretty=format:%ar']).join(' '), @@ -141,7 +143,7 @@ class FlutterVersion { ); } - String _frameworkVersion; + late String _frameworkVersion; String get frameworkVersion => _frameworkVersion; String get dartSdkVersion => globals.cache.dartSdkVersion; @@ -150,12 +152,12 @@ class FlutterVersion { String get engineRevisionShort => _shortGitRevision(engineRevision); void ensureVersionFile() { - globals.fs.file(globals.fs.path.join(Cache.flutterRoot, 'version')).writeAsStringSync(_frameworkVersion); + globals.fs.file(globals.fs.path.join(Cache.flutterRoot!, 'version')).writeAsStringSync(_frameworkVersion); } @override String toString() { - final String versionText = frameworkVersion == 'unknown' ? '' : ' $frameworkVersion'; + final String versionText = frameworkVersion == _unknownFrameworkVersion ? '' : ' $frameworkVersion'; final String flutterText = 'Flutter$versionText • channel $channel • ${repositoryUrl ?? 'unknown source'}'; final String frameworkText = 'Framework • revision $frameworkRevisionShort ($frameworkAge) • $frameworkCommitDate'; final String engineText = 'Engine • revision $engineRevisionShort'; @@ -170,7 +172,7 @@ class FlutterVersion { } Map toJson() => { - 'frameworkVersion': frameworkVersion ?? 'unknown', + 'frameworkVersion': frameworkVersion, 'channel': channel, 'repositoryUrl': repositoryUrl ?? 'unknown source', 'frameworkRevision': frameworkRevision, @@ -192,7 +194,7 @@ class FlutterVersion { // If lenient is true, and the git command fails, a placeholder date is // returned. Otherwise, the VersionCheckError exception is propagated. static String _latestGitCommitDate({ - String branch, + String? branch, bool lenient = false, }) { final List args = gitLog([ @@ -270,7 +272,7 @@ class FlutterVersion { /// Return a short string for the version (e.g. `master/0.0.59-pre.92`, `scroll_refactor/a76bc8e22b`). String getVersionString({ bool redactUnknownBranches = false }) { - if (frameworkVersion != 'unknown') { + if (frameworkVersion != _unknownFrameworkVersion) { return '${getBranchName(redactUnknownBranches: redactUnknownBranches)}/$frameworkVersion'; } return '${getBranchName(redactUnknownBranches: redactUnknownBranches)}/$frameworkRevisionShort'; @@ -285,33 +287,13 @@ class FlutterVersion { final String branch = _runGit('git rev-parse --abbrev-ref HEAD', globals.processUtils); return branch == 'HEAD' ? channel : branch; }(); - if (redactUnknownBranches || _branch.isEmpty) { + if (redactUnknownBranches || _branch!.isEmpty) { // Only return the branch names we know about; arbitrary branch names might contain PII. if (!kOfficialChannels.contains(_branch)) { return '[user-branch]'; } } - return _branch; - } - - /// Returns true if `tentativeDescendantRevision` is a direct descendant to - /// the `tentativeAncestorRevision` revision on the Flutter framework repo - /// tree. - bool checkRevisionAncestry({ - String tentativeDescendantRevision, - String tentativeAncestorRevision, - }) { - final ProcessResult result = globals.processManager.runSync( - [ - 'git', - 'merge-base', - '--is-ancestor', - tentativeAncestorRevision, - tentativeDescendantRevision - ], - workingDirectory: Cache.flutterRoot, - ); - return result.exitCode == 0; + return _branch!; } /// The amount of time we wait before pinging the server to check for the @@ -396,7 +378,7 @@ class FlutterVersion { // Get whether there's a newer version on the remote. This only goes // to the server if we haven't checked recently so won't happen on every // command. - final DateTime latestFlutterCommitDate = await _getLatestAvailableFlutterDate(); + final DateTime? latestFlutterCommitDate = await _getLatestAvailableFlutterDate(); final VersionCheckResult remoteVersionStatus = latestFlutterCommitDate == null ? VersionCheckResult.unknown : latestFlutterCommitDate.isAfter(localFrameworkCommitDate) @@ -471,13 +453,13 @@ class FlutterVersion { /// /// Returns null if the cached version is out-of-date or missing, and we are /// unable to reach the server to get the latest version. - Future _getLatestAvailableFlutterDate() async { + Future _getLatestAvailableFlutterDate() async { globals.cache.checkLockAcquired(); final VersionCheckStamp versionCheckStamp = await VersionCheckStamp.load(); if (versionCheckStamp.lastTimeVersionWasChecked != null) { final Duration timeSinceLastCheck = _clock.now().difference( - versionCheckStamp.lastTimeVersionWasChecked, + versionCheckStamp.lastTimeVersionWasChecked!, ); // Don't ping the server too often. Return cached value if it's fresh. @@ -520,16 +502,16 @@ class VersionCheckStamp { this.lastTimeWarningWasPrinted, }); - final DateTime lastTimeVersionWasChecked; - final DateTime lastKnownRemoteVersion; - final DateTime lastTimeWarningWasPrinted; + final DateTime? lastTimeVersionWasChecked; + final DateTime? lastKnownRemoteVersion; + final DateTime? lastTimeWarningWasPrinted; /// The prefix of the stamp file where we cache Flutter version check data. @visibleForTesting static const String flutterVersionCheckStampFile = 'flutter_version_check'; static Future load() async { - final String versionCheckStamp = globals.cache.getStampFor(flutterVersionCheckStampFile); + final String? versionCheckStamp = globals.cache.getStampFor(flutterVersionCheckStampFile); if (versionCheckStamp != null) { // Attempt to parse stamp JSON. @@ -551,7 +533,7 @@ class VersionCheckStamp { } static VersionCheckStamp fromJson(Map jsonObject) { - DateTime readDateTime(String property) { + DateTime? readDateTime(String property) { return jsonObject.containsKey(property) ? DateTime.parse(jsonObject[property] as String) : null; @@ -565,9 +547,9 @@ class VersionCheckStamp { } Future store({ - DateTime newTimeVersionWasChecked, - DateTime newKnownRemoteVersion, - DateTime newTimeWarningWasPrinted, + DateTime? newTimeVersionWasChecked, + DateTime? newKnownRemoteVersion, + DateTime? newTimeWarningWasPrinted, }) async { final Map jsonData = toJson(); @@ -588,9 +570,9 @@ class VersionCheckStamp { } Map toJson({ - DateTime updateTimeVersionWasChecked, - DateTime updateKnownRemoteVersion, - DateTime updateTimeWarningWasPrinted, + DateTime? updateTimeVersionWasChecked, + DateTime? updateKnownRemoteVersion, + DateTime? updateTimeWarningWasPrinted, }) { updateTimeVersionWasChecked = updateTimeVersionWasChecked ?? lastTimeVersionWasChecked; updateKnownRemoteVersion = updateKnownRemoteVersion ?? lastKnownRemoteVersion; @@ -654,7 +636,7 @@ String _runSync(List command, { bool lenient = true }) { return ''; } -String _runGit(String command, ProcessUtils processUtils, [String workingDirectory]) { +String _runGit(String command, ProcessUtils processUtils, [String? workingDirectory]) { return processUtils.runSync( command.split(' '), workingDirectory: workingDirectory ?? Cache.flutterRoot, @@ -678,7 +660,7 @@ Future _run(List command) async { ); } -String _shortGitRevision(String revision) { +String _shortGitRevision(String? revision) { if (revision == null) { return ''; } @@ -710,33 +692,33 @@ class GitTagVersion { gitTag = ''; /// The X in vX.Y.Z. - final int x; + final int? x; /// The Y in vX.Y.Z. - final int y; + final int? y; /// The Z in vX.Y.Z. - final int z; + final int? z; /// the F in vX.Y.Z+hotfix.F. - final int hotfix; + final int? hotfix; /// Number of commits since the vX.Y.Z tag. - final int commits; + final int? commits; /// The git hash (or an abbreviation thereof) for this commit. - final String hash; + final String? hash; /// The N in X.Y.Z-dev.N.M. - final int devVersion; + final int? devVersion; /// The M in X.Y.Z-dev.N.M. - final int devPatch; + final int? devPatch; /// The git tag that is this version's closest ancestor. - final String gitTag; + final String? gitTag; - static GitTagVersion determine(ProcessUtils processUtils, {String workingDirectory, bool fetchTags = false, String gitRef = 'HEAD'}) { + static GitTagVersion determine(ProcessUtils processUtils, {String? workingDirectory, bool fetchTags = false, String gitRef = 'HEAD'}) { if (fetchTags) { final String channel = _runGit('git rev-parse --abbrev-ref HEAD', processUtils, workingDirectory); if (channel == 'dev' || channel == 'beta' || channel == 'stable') { @@ -783,26 +765,26 @@ class GitTagVersion { static GitTagVersion parseVersion(String version) { final RegExp versionPattern = RegExp( r'^(\d+)\.(\d+)\.(\d+)(-\d+\.\d+\.pre)?(?:-(\d+)-g([a-f0-9]+))?$'); - final Match match = versionPattern.firstMatch(version.trim()); + final Match? match = versionPattern.firstMatch(version.trim()); if (match == null) { return const GitTagVersion.unknown(); } - final List matchGroups = match.groups([1, 2, 3, 4, 5, 6]); - final int x = matchGroups[0] == null ? null : int.tryParse(matchGroups[0]); - final int y = matchGroups[1] == null ? null : int.tryParse(matchGroups[1]); - final int z = matchGroups[2] == null ? null : int.tryParse(matchGroups[2]); - final String devString = matchGroups[3]; - int devVersion, devPatch; + final List matchGroups = match.groups([1, 2, 3, 4, 5, 6]); + final int? x = matchGroups[0] == null ? null : int.tryParse(matchGroups[0]!); + final int? y = matchGroups[1] == null ? null : int.tryParse(matchGroups[1]!); + final int? z = matchGroups[2] == null ? null : int.tryParse(matchGroups[2]!); + final String? devString = matchGroups[3]; + int? devVersion, devPatch; if (devString != null) { - final Match devMatch = RegExp(r'^-(\d+)\.(\d+)\.pre$') + final Match? devMatch = RegExp(r'^-(\d+)\.(\d+)\.pre$') .firstMatch(devString); - final List devGroups = devMatch.groups([1, 2]); - devVersion = devGroups[0] == null ? null : int.tryParse(devGroups[0]); - devPatch = devGroups[1] == null ? null : int.tryParse(devGroups[1]); + final List? devGroups = devMatch?.groups([1, 2]); + devVersion = devGroups?[0] == null ? null : int.tryParse(devGroups![0]!); + devPatch = devGroups?[1] == null ? null : int.tryParse(devGroups![1]!); } // count of commits past last tagged version - final int commits = matchGroups[4] == null ? 0 : int.tryParse(matchGroups[4]); + final int? commits = matchGroups[4] == null ? 0 : int.tryParse(matchGroups[4]!); final String hash = matchGroups[5] ?? ''; return GitTagVersion( @@ -829,20 +811,20 @@ class GitTagVersion { } String frameworkVersionFor(String revision) { - if (x == null || y == null || z == null || !revision.startsWith(hash)) { - return '0.0.0-unknown'; + if (x == null || y == null || z == null || (hash != null && !revision.startsWith(hash!))) { + return _unknownFrameworkVersion; } - if (commits == 0) { - return gitTag; + if (commits == 0 && gitTag != null) { + return gitTag!; } if (hotfix != null) { // This is an unexpected state where untagged commits exist past a hotfix - return '$x.$y.$z+hotfix.${hotfix + 1}.pre.$commits'; + return '$x.$y.$z+hotfix.${hotfix! + 1}.pre.$commits'; } if (devPatch != null && devVersion != null) { - return '$x.$y.$z-${devVersion + 1}.0.pre.$commits'; + return '$x.$y.$z-${devVersion! + 1}.0.pre.$commits'; } - return '$x.$y.${z + 1}-0.0.pre.$commits'; + return '$x.$y.${z! + 1}-0.0.pre.$commits'; } } diff --git a/packages/flutter_tools/test/general.shard/runner/flutter_command_runner_test.dart b/packages/flutter_tools/test/general.shard/runner/flutter_command_runner_test.dart index 7835cd0d37..e3b0efa208 100644 --- a/packages/flutter_tools/test/general.shard/runner/flutter_command_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/runner/flutter_command_runner_test.dart @@ -12,7 +12,7 @@ import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/base/platform.dart'; import 'package:flutter_tools/src/base/terminal.dart'; import 'package:flutter_tools/src/cache.dart'; -import 'package:flutter_tools/src/globals.dart' as globals; +import 'package:flutter_tools/src/globals_null_migrated.dart' as globals; import 'package:flutter_tools/src/runner/flutter_command.dart'; import 'package:flutter_tools/src/runner/flutter_command_runner.dart'; import 'package:flutter_tools/src/version.dart'; diff --git a/packages/flutter_tools/test/general.shard/version_test.dart b/packages/flutter_tools/test/general.shard/version_test.dart index 3046627aeb..40e7ffbbd6 100644 --- a/packages/flutter_tools/test/general.shard/version_test.dart +++ b/packages/flutter_tools/test/general.shard/version_test.dart @@ -14,7 +14,7 @@ import 'package:flutter_tools/src/base/process.dart'; import 'package:flutter_tools/src/base/time.dart'; import 'package:flutter_tools/src/base/utils.dart'; import 'package:flutter_tools/src/cache.dart'; -import 'package:flutter_tools/src/globals.dart' as globals; +import 'package:flutter_tools/src/globals_null_migrated.dart' as globals; import 'package:flutter_tools/src/version.dart'; import 'package:mockito/mockito.dart'; @@ -61,17 +61,6 @@ void main() { }); testUsingContext('prints nothing when Flutter installation looks fresh', () async { - fakeData( - mockProcessManager, - mockCache, - localCommitDate: getChannelUpToDateVersion(), - // Server will be pinged because we haven't pinged within last x days - expectServerPing: true, - remoteCommitDate: getChannelOutOfDateVersion(), - expectSetStamp: true, - channel: channel, - ); - processManager.addCommand(const FakeCommand( command: ['git', '-c', 'log.showSignature=false', 'log', '-n', '1', '--pretty=format:%H'], stdout: '1234abcd', @@ -116,7 +105,40 @@ void main() { command: ['git', 'remote'], )); - await globals.flutterVersion.checkFlutterVersionFreshness(); + processManager.addCommands([ + const FakeCommand( + command: ['git', '-c', 'log.showSignature=false', 'log', '-n', '1', '--pretty=format:%ar'], + stdout: '1 second ago', + ), + FakeCommand( + command: const ['git', '-c', 'log.showSignature=false', 'log', '-n', '1', '--pretty=format:%ad', '--date=iso'], + stdout: getChannelUpToDateVersion().toString(), + ), + FakeCommand( + command: const ['git', 'rev-parse', '--abbrev-ref', 'HEAD'], + stdout: channel, + ), + ]); + + final FlutterVersion flutterVersion = globals.flutterVersion; + await flutterVersion.checkFlutterVersionFreshness(); + expect(flutterVersion.channel, channel); + expect(flutterVersion.frameworkRevision, '1234abcd'); + expect(flutterVersion.frameworkRevisionShort, '1234abcd'); + expect(flutterVersion.frameworkVersion, '0.0.0-unknown'); + expect( + flutterVersion.toString(), + 'Flutter • channel $channel • unknown source\n' + 'Framework • revision 1234abcd (1 second ago) • ${getChannelUpToDateVersion()}\n' + 'Engine • revision \n' + 'Tools • Dart null', + ); + expect(flutterVersion.frameworkAge, '1 second ago'); + expect(flutterVersion.getVersionString(), '$channel/1234abcd'); + expect(flutterVersion.getBranchName(), channel); + expect(flutterVersion.getVersionString(redactUnknownBranches: true), '$channel/1234abcd'); + expect(flutterVersion.getBranchName(redactUnknownBranches: true), channel); + _expectVersionMessage(''); expect(processManager.hasRemainingExpectations, isFalse); }, overrides: { @@ -291,39 +313,6 @@ void main() { ProcessManager: () => mockProcessManager, Cache: () => mockCache, }); - - testUsingContext('versions comparison', () async { - fakeData( - mockProcessManager, - mockCache, - localCommitDate: getChannelOutOfDateVersion(), - errorOnFetch: true, - expectServerPing: true, - expectSetStamp: true, - channel: channel, - ); - final FlutterVersion version = globals.flutterVersion; - - when(mockProcessManager.runSync( - ['git', 'merge-base', '--is-ancestor', 'abcdef', '123456'], - workingDirectory: anyNamed('workingDirectory'), - )).thenReturn(ProcessResult(1, 0, '', '')); - - expect( - version.checkRevisionAncestry( - tentativeDescendantRevision: '123456', - tentativeAncestorRevision: 'abcdef', - ), - true); - - verify(mockProcessManager.runSync( - ['git', 'merge-base', '--is-ancestor', 'abcdef', '123456'], - workingDirectory: anyNamed('workingDirectory'), - )); - }, overrides: { - FlutterVersion: () => FlutterVersion(clock: _testClock), - ProcessManager: () => mockProcessManager, - }); }); group('$VersionCheckStamp for $channel', () { @@ -449,6 +438,42 @@ void main() { }); } + testUsingContext('version handles unknown branch', () async { + processManager.addCommands([ + const FakeCommand( + command: ['git', '-c', 'log.showSignature=false', 'log', '-n', '1', '--pretty=format:%H'], + stdout: '1234abcd', + ), + const FakeCommand( + command: ['git', 'tag', '--points-at', '1234abcd'], + ), + const FakeCommand( + command: ['git', 'describe', '--match', '*.*.*', '--long', '--tags', '1234abcd'], + stdout: '0.1.2-3-1234abcd', + ), + const FakeCommand( + command: ['git', 'rev-parse', '--abbrev-ref', '--symbolic', '@{u}'], + stdout: 'feature-branch', + ), + const FakeCommand( + command: ['git', 'rev-parse', '--abbrev-ref', 'HEAD'], + stdout: 'feature-branch', + ), + ]); + + final FlutterVersion flutterVersion = globals.flutterVersion; + expect(flutterVersion.channel, 'feature-branch'); + expect(flutterVersion.getVersionString(), 'feature-branch/1234abcd'); + expect(flutterVersion.getBranchName(), 'feature-branch'); + expect(flutterVersion.getVersionString(redactUnknownBranches: true), '[user-branch]/1234abcd'); + expect(flutterVersion.getBranchName(redactUnknownBranches: true), '[user-branch]'); + expect(processManager.hasRemainingExpectations, isFalse); + }, overrides: { + FlutterVersion: () => FlutterVersion(clock: _testClock), + ProcessManager: () => processManager, + Cache: () => mockCache, + }); + testUsingContext('GitTagVersion', () { const String hash = 'abcdef'; GitTagVersion gitTagVersion; diff --git a/packages/flutter_tools/test/src/fakes.dart b/packages/flutter_tools/test/src/fakes.dart index 94725d68d0..2ca5395231 100644 --- a/packages/flutter_tools/test/src/fakes.dart +++ b/packages/flutter_tools/test/src/fakes.dart @@ -526,11 +526,6 @@ class FakeFlutterVersion implements FlutterVersion { _didCheckFlutterVersionFreshness = true; } - @override - bool checkRevisionAncestry({String tentativeDescendantRevision, String tentativeAncestorRevision}) { - throw UnimplementedError(); - } - @override Future ensureVersionFile() async { }