Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import 'dart:convert'; |
| 6 | |
Ian Hickson | 8f56f6f | 2017-07-21 16:39:04 -0700 | [diff] [blame] | 7 | import 'package:collection/collection.dart' show ListEquality; |
Jonah Williams | a871d59 | 2018-11-10 17:02:32 -0800 | [diff] [blame] | 8 | import 'package:flutter_tools/src/base/time.dart'; |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 9 | import 'package:mockito/mockito.dart'; |
| 10 | import 'package:process/process.dart'; |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 11 | |
| 12 | import 'package:flutter_tools/src/base/context.dart'; |
| 13 | import 'package:flutter_tools/src/base/io.dart'; |
| 14 | import 'package:flutter_tools/src/base/logger.dart'; |
| 15 | import 'package:flutter_tools/src/cache.dart'; |
| 16 | import 'package:flutter_tools/src/version.dart'; |
| 17 | |
Ian Hickson | d919e69 | 2019-07-13 11:51:44 -0700 | [diff] [blame] | 18 | import '../src/common.dart'; |
| 19 | import '../src/context.dart'; |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 20 | |
Jonah Williams | a871d59 | 2018-11-10 17:02:32 -0800 | [diff] [blame] | 21 | final SystemClock _testClock = SystemClock.fixed(DateTime(2015, 1, 1)); |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 22 | final DateTime _stampUpToDate = _testClock.ago(FlutterVersion.checkAgeConsideredUpToDate ~/ 2); |
| 23 | final DateTime _stampOutOfDate = _testClock.ago(FlutterVersion.checkAgeConsideredUpToDate * 2); |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 24 | |
| 25 | void main() { |
Todd Volkert | 8d11f5c | 2018-03-28 10:58:28 -0700 | [diff] [blame] | 26 | MockProcessManager mockProcessManager; |
| 27 | MockCache mockCache; |
xster | 9a0d4cf | 2017-11-14 18:50:15 -0800 | [diff] [blame] | 28 | |
Todd Volkert | 8d11f5c | 2018-03-28 10:58:28 -0700 | [diff] [blame] | 29 | setUp(() { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 30 | mockProcessManager = MockProcessManager(); |
| 31 | mockCache = MockCache(); |
Todd Volkert | 8d11f5c | 2018-03-28 10:58:28 -0700 | [diff] [blame] | 32 | }); |
| 33 | |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 34 | for (String channel in FlutterVersion.officialChannels) { |
| 35 | DateTime getChannelUpToDateVersion() { |
| 36 | return _testClock.ago(FlutterVersion.versionAgeConsideredUpToDate(channel) ~/ 2); |
Greg Spencer | a2ed3ec | 2018-11-13 17:18:06 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 39 | DateTime getChannelOutOfDateVersion() { |
| 40 | return _testClock.ago(FlutterVersion.versionAgeConsideredUpToDate(channel) * 2); |
| 41 | } |
| 42 | |
| 43 | group('$FlutterVersion for $channel', () { |
| 44 | setUpAll(() { |
| 45 | Cache.disableLocking(); |
| 46 | FlutterVersion.timeToPauseToLetUserReadTheMessage = Duration.zero; |
| 47 | }); |
| 48 | |
| 49 | testUsingContext('prints nothing when Flutter installation looks fresh', () async { |
| 50 | fakeData( |
| 51 | mockProcessManager, |
| 52 | mockCache, |
| 53 | localCommitDate: getChannelUpToDateVersion(), |
| 54 | // Server will be pinged because we haven't pinged within last x days |
| 55 | expectServerPing: true, |
| 56 | remoteCommitDate: getChannelOutOfDateVersion(), |
| 57 | expectSetStamp: true, |
| 58 | channel: channel, |
| 59 | ); |
| 60 | await FlutterVersion.instance.checkFlutterVersionFreshness(); |
| 61 | _expectVersionMessage(''); |
| 62 | }, overrides: <Type, Generator>{ |
| 63 | FlutterVersion: () => FlutterVersion(_testClock), |
| 64 | ProcessManager: () => mockProcessManager, |
| 65 | Cache: () => mockCache, |
| 66 | }); |
| 67 | |
| 68 | testUsingContext('prints nothing when Flutter installation looks out-of-date but is actually up-to-date', () async { |
| 69 | fakeData( |
| 70 | mockProcessManager, |
| 71 | mockCache, |
| 72 | localCommitDate: getChannelOutOfDateVersion(), |
| 73 | stamp: VersionCheckStamp( |
| 74 | lastTimeVersionWasChecked: _stampOutOfDate, |
| 75 | lastKnownRemoteVersion: getChannelOutOfDateVersion(), |
| 76 | ), |
| 77 | remoteCommitDate: getChannelOutOfDateVersion(), |
| 78 | expectSetStamp: true, |
| 79 | expectServerPing: true, |
| 80 | channel: channel, |
| 81 | ); |
| 82 | final FlutterVersion version = FlutterVersion.instance; |
| 83 | |
| 84 | await version.checkFlutterVersionFreshness(); |
| 85 | _expectVersionMessage(''); |
| 86 | }, overrides: <Type, Generator>{ |
| 87 | FlutterVersion: () => FlutterVersion(_testClock), |
| 88 | ProcessManager: () => mockProcessManager, |
| 89 | Cache: () => mockCache, |
| 90 | }); |
| 91 | |
| 92 | testUsingContext('does not ping server when version stamp is up-to-date', () async { |
| 93 | fakeData( |
| 94 | mockProcessManager, |
| 95 | mockCache, |
| 96 | localCommitDate: getChannelOutOfDateVersion(), |
| 97 | stamp: VersionCheckStamp( |
| 98 | lastTimeVersionWasChecked: _stampUpToDate, |
| 99 | lastKnownRemoteVersion: getChannelUpToDateVersion(), |
| 100 | ), |
| 101 | expectSetStamp: true, |
| 102 | channel: channel, |
| 103 | ); |
| 104 | |
| 105 | final FlutterVersion version = FlutterVersion.instance; |
| 106 | await version.checkFlutterVersionFreshness(); |
| 107 | _expectVersionMessage(FlutterVersion.newVersionAvailableMessage()); |
| 108 | }, overrides: <Type, Generator>{ |
| 109 | FlutterVersion: () => FlutterVersion(_testClock), |
| 110 | ProcessManager: () => mockProcessManager, |
| 111 | Cache: () => mockCache, |
| 112 | }); |
| 113 | |
| 114 | testUsingContext('does not print warning if printed recently', () async { |
| 115 | fakeData( |
| 116 | mockProcessManager, |
| 117 | mockCache, |
| 118 | localCommitDate: getChannelOutOfDateVersion(), |
| 119 | stamp: VersionCheckStamp( |
| 120 | lastTimeVersionWasChecked: _stampUpToDate, |
| 121 | lastKnownRemoteVersion: getChannelUpToDateVersion(), |
| 122 | ), |
| 123 | expectSetStamp: true, |
| 124 | channel: channel, |
| 125 | ); |
| 126 | |
| 127 | final FlutterVersion version = FlutterVersion.instance; |
| 128 | await version.checkFlutterVersionFreshness(); |
| 129 | _expectVersionMessage(FlutterVersion.newVersionAvailableMessage()); |
| 130 | expect((await VersionCheckStamp.load()).lastTimeWarningWasPrinted, _testClock.now()); |
| 131 | |
| 132 | await version.checkFlutterVersionFreshness(); |
| 133 | _expectVersionMessage(''); |
| 134 | }, overrides: <Type, Generator>{ |
| 135 | FlutterVersion: () => FlutterVersion(_testClock), |
| 136 | ProcessManager: () => mockProcessManager, |
| 137 | Cache: () => mockCache, |
| 138 | }); |
| 139 | |
| 140 | testUsingContext('pings server when version stamp is missing then does not', () async { |
| 141 | fakeData( |
| 142 | mockProcessManager, |
| 143 | mockCache, |
| 144 | localCommitDate: getChannelOutOfDateVersion(), |
| 145 | remoteCommitDate: getChannelUpToDateVersion(), |
| 146 | expectSetStamp: true, |
| 147 | expectServerPing: true, |
| 148 | channel: channel, |
| 149 | ); |
| 150 | final FlutterVersion version = FlutterVersion.instance; |
| 151 | |
| 152 | await version.checkFlutterVersionFreshness(); |
| 153 | _expectVersionMessage(FlutterVersion.newVersionAvailableMessage()); |
| 154 | |
| 155 | // Immediate subsequent check is not expected to ping the server. |
| 156 | fakeData( |
| 157 | mockProcessManager, |
| 158 | mockCache, |
| 159 | localCommitDate: getChannelOutOfDateVersion(), |
| 160 | stamp: await VersionCheckStamp.load(), |
| 161 | channel: channel, |
| 162 | ); |
| 163 | await version.checkFlutterVersionFreshness(); |
| 164 | _expectVersionMessage(''); |
| 165 | }, overrides: <Type, Generator>{ |
| 166 | FlutterVersion: () => FlutterVersion(_testClock), |
| 167 | ProcessManager: () => mockProcessManager, |
| 168 | Cache: () => mockCache, |
| 169 | }); |
| 170 | |
| 171 | testUsingContext('pings server when version stamp is out-of-date', () async { |
| 172 | fakeData( |
| 173 | mockProcessManager, |
| 174 | mockCache, |
| 175 | localCommitDate: getChannelOutOfDateVersion(), |
| 176 | stamp: VersionCheckStamp( |
| 177 | lastTimeVersionWasChecked: _stampOutOfDate, |
| 178 | lastKnownRemoteVersion: _testClock.ago(const Duration(days: 2)), |
| 179 | ), |
| 180 | remoteCommitDate: getChannelUpToDateVersion(), |
| 181 | expectSetStamp: true, |
| 182 | expectServerPing: true, |
| 183 | channel: channel, |
| 184 | ); |
| 185 | final FlutterVersion version = FlutterVersion.instance; |
| 186 | |
| 187 | await version.checkFlutterVersionFreshness(); |
| 188 | _expectVersionMessage(FlutterVersion.newVersionAvailableMessage()); |
| 189 | }, overrides: <Type, Generator>{ |
| 190 | FlutterVersion: () => FlutterVersion(_testClock), |
| 191 | ProcessManager: () => mockProcessManager, |
| 192 | Cache: () => mockCache, |
| 193 | }); |
| 194 | |
| 195 | testUsingContext('does not print warning when unable to connect to server if not out of date', () async { |
| 196 | fakeData( |
| 197 | mockProcessManager, |
| 198 | mockCache, |
| 199 | localCommitDate: getChannelUpToDateVersion(), |
| 200 | errorOnFetch: true, |
| 201 | expectServerPing: true, |
| 202 | expectSetStamp: true, |
| 203 | channel: channel, |
| 204 | ); |
| 205 | final FlutterVersion version = FlutterVersion.instance; |
| 206 | |
| 207 | await version.checkFlutterVersionFreshness(); |
| 208 | _expectVersionMessage(''); |
| 209 | }, overrides: <Type, Generator>{ |
| 210 | FlutterVersion: () => FlutterVersion(_testClock), |
| 211 | ProcessManager: () => mockProcessManager, |
| 212 | Cache: () => mockCache, |
| 213 | }); |
| 214 | |
| 215 | testUsingContext('prints warning when unable to connect to server if really out of date', () async { |
| 216 | fakeData( |
| 217 | mockProcessManager, |
| 218 | mockCache, |
| 219 | localCommitDate: getChannelOutOfDateVersion(), |
| 220 | errorOnFetch: true, |
| 221 | expectServerPing: true, |
| 222 | expectSetStamp: true, |
| 223 | channel: channel, |
| 224 | ); |
| 225 | final FlutterVersion version = FlutterVersion.instance; |
| 226 | |
| 227 | await version.checkFlutterVersionFreshness(); |
| 228 | _expectVersionMessage(FlutterVersion.versionOutOfDateMessage(_testClock.now().difference(getChannelOutOfDateVersion()))); |
| 229 | }, overrides: <Type, Generator>{ |
| 230 | FlutterVersion: () => FlutterVersion(_testClock), |
| 231 | ProcessManager: () => mockProcessManager, |
| 232 | Cache: () => mockCache, |
| 233 | }); |
| 234 | |
| 235 | testUsingContext('versions comparison', () async { |
| 236 | fakeData( |
| 237 | mockProcessManager, |
| 238 | mockCache, |
| 239 | localCommitDate: getChannelOutOfDateVersion(), |
| 240 | errorOnFetch: true, |
| 241 | expectServerPing: true, |
| 242 | expectSetStamp: true, |
| 243 | channel: channel, |
| 244 | ); |
| 245 | final FlutterVersion version = FlutterVersion.instance; |
| 246 | |
| 247 | when(mockProcessManager.runSync( |
| 248 | <String>['git', 'merge-base', '--is-ancestor', 'abcdef', '123456'], |
| 249 | workingDirectory: anyNamed('workingDirectory'), |
| 250 | )).thenReturn(ProcessResult(1, 0, '', '')); |
| 251 | |
| 252 | expect( |
| 253 | version.checkRevisionAncestry( |
| 254 | tentativeDescendantRevision: '123456', |
| 255 | tentativeAncestorRevision: 'abcdef', |
| 256 | ), |
| 257 | true); |
| 258 | |
| 259 | verify(mockProcessManager.runSync( |
| 260 | <String>['git', 'merge-base', '--is-ancestor', 'abcdef', '123456'], |
| 261 | workingDirectory: anyNamed('workingDirectory'), |
| 262 | )); |
| 263 | }, overrides: <Type, Generator>{ |
| 264 | FlutterVersion: () => FlutterVersion(_testClock), |
| 265 | ProcessManager: () => mockProcessManager, |
| 266 | }); |
Greg Spencer | a2ed3ec | 2018-11-13 17:18:06 -0800 | [diff] [blame] | 267 | }); |
| 268 | |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 269 | group('$VersionCheckStamp for $channel', () { |
| 270 | void _expectDefault(VersionCheckStamp stamp) { |
| 271 | expect(stamp.lastKnownRemoteVersion, isNull); |
| 272 | expect(stamp.lastTimeVersionWasChecked, isNull); |
| 273 | expect(stamp.lastTimeWarningWasPrinted, isNull); |
| 274 | } |
Greg Spencer | a2ed3ec | 2018-11-13 17:18:06 -0800 | [diff] [blame] | 275 | |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 276 | testUsingContext('loads blank when stamp file missing', () async { |
| 277 | fakeData(mockProcessManager, mockCache, channel: channel); |
| 278 | _expectDefault(await VersionCheckStamp.load()); |
| 279 | }, overrides: <Type, Generator>{ |
| 280 | FlutterVersion: () => FlutterVersion(_testClock), |
| 281 | ProcessManager: () => mockProcessManager, |
| 282 | Cache: () => mockCache, |
| 283 | }); |
Greg Spencer | a2ed3ec | 2018-11-13 17:18:06 -0800 | [diff] [blame] | 284 | |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 285 | testUsingContext('loads blank when stamp file is malformed JSON', () async { |
| 286 | fakeData(mockProcessManager, mockCache, stampJson: '<', channel: channel); |
| 287 | _expectDefault(await VersionCheckStamp.load()); |
| 288 | }, overrides: <Type, Generator>{ |
| 289 | FlutterVersion: () => FlutterVersion(_testClock), |
| 290 | ProcessManager: () => mockProcessManager, |
| 291 | Cache: () => mockCache, |
| 292 | }); |
| 293 | |
| 294 | testUsingContext('loads blank when stamp file is well-formed but invalid JSON', () async { |
| 295 | fakeData( |
| 296 | mockProcessManager, |
| 297 | mockCache, |
| 298 | stampJson: '[]', |
| 299 | channel: channel, |
| 300 | ); |
| 301 | _expectDefault(await VersionCheckStamp.load()); |
| 302 | }, overrides: <Type, Generator>{ |
| 303 | FlutterVersion: () => FlutterVersion(_testClock), |
| 304 | ProcessManager: () => mockProcessManager, |
| 305 | Cache: () => mockCache, |
| 306 | }); |
| 307 | |
| 308 | testUsingContext('loads valid JSON', () async { |
| 309 | fakeData( |
| 310 | mockProcessManager, |
| 311 | mockCache, |
| 312 | stampJson: ''' |
Yegor | 5efbe05 | 2017-04-10 13:21:02 -0700 | [diff] [blame] | 313 | { |
Jonah Williams | a871d59 | 2018-11-10 17:02:32 -0800 | [diff] [blame] | 314 | "lastKnownRemoteVersion": "${_testClock.ago(const Duration(days: 1))}", |
| 315 | "lastTimeVersionWasChecked": "${_testClock.ago(const Duration(days: 2))}", |
Yegor | 5efbe05 | 2017-04-10 13:21:02 -0700 | [diff] [blame] | 316 | "lastTimeWarningWasPrinted": "${_testClock.now()}" |
| 317 | } |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 318 | ''', |
| 319 | channel: channel, |
| 320 | ); |
Yegor | 5efbe05 | 2017-04-10 13:21:02 -0700 | [diff] [blame] | 321 | |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 322 | final VersionCheckStamp stamp = await VersionCheckStamp.load(); |
| 323 | expect(stamp.lastKnownRemoteVersion, _testClock.ago(const Duration(days: 1))); |
| 324 | expect(stamp.lastTimeVersionWasChecked, _testClock.ago(const Duration(days: 2))); |
| 325 | expect(stamp.lastTimeWarningWasPrinted, _testClock.now()); |
| 326 | }, overrides: <Type, Generator>{ |
| 327 | FlutterVersion: () => FlutterVersion(_testClock), |
| 328 | ProcessManager: () => mockProcessManager, |
| 329 | Cache: () => mockCache, |
| 330 | }); |
| 331 | |
| 332 | testUsingContext('stores version stamp', () async { |
| 333 | fakeData( |
| 334 | mockProcessManager, |
| 335 | mockCache, |
| 336 | expectSetStamp: true, |
| 337 | channel: channel, |
| 338 | ); |
| 339 | |
| 340 | _expectDefault(await VersionCheckStamp.load()); |
| 341 | |
| 342 | final VersionCheckStamp stamp = VersionCheckStamp( |
| 343 | lastKnownRemoteVersion: _testClock.ago(const Duration(days: 1)), |
| 344 | lastTimeVersionWasChecked: _testClock.ago(const Duration(days: 2)), |
| 345 | lastTimeWarningWasPrinted: _testClock.now(), |
| 346 | ); |
| 347 | await stamp.store(); |
| 348 | |
| 349 | final VersionCheckStamp storedStamp = await VersionCheckStamp.load(); |
| 350 | expect(storedStamp.lastKnownRemoteVersion, _testClock.ago(const Duration(days: 1))); |
| 351 | expect(storedStamp.lastTimeVersionWasChecked, _testClock.ago(const Duration(days: 2))); |
| 352 | expect(storedStamp.lastTimeWarningWasPrinted, _testClock.now()); |
| 353 | }, overrides: <Type, Generator>{ |
| 354 | FlutterVersion: () => FlutterVersion(_testClock), |
| 355 | ProcessManager: () => mockProcessManager, |
| 356 | Cache: () => mockCache, |
| 357 | }); |
| 358 | |
| 359 | testUsingContext('overwrites individual fields', () async { |
| 360 | fakeData( |
| 361 | mockProcessManager, |
| 362 | mockCache, |
| 363 | expectSetStamp: true, |
| 364 | channel: channel, |
| 365 | ); |
| 366 | |
| 367 | _expectDefault(await VersionCheckStamp.load()); |
| 368 | |
| 369 | final VersionCheckStamp stamp = VersionCheckStamp( |
| 370 | lastKnownRemoteVersion: _testClock.ago(const Duration(days: 10)), |
| 371 | lastTimeVersionWasChecked: _testClock.ago(const Duration(days: 9)), |
| 372 | lastTimeWarningWasPrinted: _testClock.ago(const Duration(days: 8)), |
| 373 | ); |
| 374 | await stamp.store( |
| 375 | newKnownRemoteVersion: _testClock.ago(const Duration(days: 1)), |
| 376 | newTimeVersionWasChecked: _testClock.ago(const Duration(days: 2)), |
| 377 | newTimeWarningWasPrinted: _testClock.now(), |
| 378 | ); |
| 379 | |
| 380 | final VersionCheckStamp storedStamp = await VersionCheckStamp.load(); |
| 381 | expect(storedStamp.lastKnownRemoteVersion, _testClock.ago(const Duration(days: 1))); |
| 382 | expect(storedStamp.lastTimeVersionWasChecked, _testClock.ago(const Duration(days: 2))); |
| 383 | expect(storedStamp.lastTimeWarningWasPrinted, _testClock.now()); |
| 384 | }, overrides: <Type, Generator>{ |
| 385 | FlutterVersion: () => FlutterVersion(_testClock), |
| 386 | ProcessManager: () => mockProcessManager, |
| 387 | Cache: () => mockCache, |
| 388 | }); |
Yegor | 5efbe05 | 2017-04-10 13:21:02 -0700 | [diff] [blame] | 389 | }); |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 390 | } |
Ian Hickson | a07c9a1 | 2019-03-08 19:26:34 -0800 | [diff] [blame] | 391 | |
| 392 | testUsingContext('GitTagVersion', () { |
| 393 | const String hash = 'abcdef'; |
| 394 | expect(GitTagVersion.parse('v1.2.3-4-g$hash').frameworkVersionFor(hash), '1.2.4-pre.4'); |
| 395 | expect(GitTagVersion.parse('v98.76.54-32-g$hash').frameworkVersionFor(hash), '98.76.55-pre.32'); |
| 396 | expect(GitTagVersion.parse('v10.20.30-0-g$hash').frameworkVersionFor(hash), '10.20.30'); |
Jonah Williams | eb996af | 2019-05-07 10:11:00 -0700 | [diff] [blame] | 397 | expect(GitTagVersion.parse('v1.2.3+hotfix.1-4-g$hash').frameworkVersionFor(hash), '1.2.3+hotfix.2-pre.4'); |
| 398 | expect(GitTagVersion.parse('v7.2.4+hotfix.8-0-g$hash').frameworkVersionFor(hash), '7.2.4+hotfix.8'); |
Ian Hickson | a07c9a1 | 2019-03-08 19:26:34 -0800 | [diff] [blame] | 399 | expect(testLogger.traceText, ''); |
| 400 | expect(GitTagVersion.parse('x1.2.3-4-g$hash').frameworkVersionFor(hash), '0.0.0-unknown'); |
| 401 | expect(GitTagVersion.parse('v1.0.0-unknown-0-g$hash').frameworkVersionFor(hash), '0.0.0-unknown'); |
| 402 | expect(GitTagVersion.parse('beta-1-g$hash').frameworkVersionFor(hash), '0.0.0-unknown'); |
| 403 | expect(GitTagVersion.parse('v1.2.3-4-gx$hash').frameworkVersionFor(hash), '0.0.0-unknown'); |
| 404 | expect(testLogger.statusText, ''); |
| 405 | expect(testLogger.errorText, ''); |
Alexandre Ardhuin | bfa1d25 | 2019-03-23 00:02:21 +0100 | [diff] [blame] | 406 | expect( |
| 407 | testLogger.traceText, |
Ian Hickson | a07c9a1 | 2019-03-08 19:26:34 -0800 | [diff] [blame] | 408 | 'Could not interpret results of "git describe": x1.2.3-4-gabcdef\n' |
| 409 | 'Could not interpret results of "git describe": v1.0.0-unknown-0-gabcdef\n' |
| 410 | 'Could not interpret results of "git describe": beta-1-gabcdef\n' |
Alexandre Ardhuin | bfa1d25 | 2019-03-23 00:02:21 +0100 | [diff] [blame] | 411 | 'Could not interpret results of "git describe": v1.2.3-4-gxabcdef\n', |
Ian Hickson | a07c9a1 | 2019-03-08 19:26:34 -0800 | [diff] [blame] | 412 | ); |
| 413 | }); |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | void _expectVersionMessage(String message) { |
Jonah Williams | 0acd3e6 | 2019-04-25 15:51:08 -0700 | [diff] [blame] | 417 | final BufferLogger logger = context.get<Logger>(); |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 418 | expect(logger.statusText.trim(), message.trim()); |
Yegor | 5efbe05 | 2017-04-10 13:21:02 -0700 | [diff] [blame] | 419 | logger.clear(); |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Todd Volkert | 8d11f5c | 2018-03-28 10:58:28 -0700 | [diff] [blame] | 422 | void fakeData( |
| 423 | ProcessManager pm, |
| 424 | Cache cache, { |
Yegor | 5efbe05 | 2017-04-10 13:21:02 -0700 | [diff] [blame] | 425 | DateTime localCommitDate, |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 426 | DateTime remoteCommitDate, |
Yegor | 5efbe05 | 2017-04-10 13:21:02 -0700 | [diff] [blame] | 427 | VersionCheckStamp stamp, |
| 428 | String stampJson, |
Alexandre Ardhuin | 09276be | 2018-06-05 08:50:40 +0200 | [diff] [blame] | 429 | bool errorOnFetch = false, |
| 430 | bool expectSetStamp = false, |
| 431 | bool expectServerPing = false, |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 432 | String channel = 'master', |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 433 | }) { |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 434 | ProcessResult success(String standardOutput) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 435 | return ProcessResult(1, 0, standardOutput, ''); |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | ProcessResult failure(int exitCode) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 439 | return ProcessResult(1, exitCode, '', 'error'); |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | when(cache.getStampFor(any)).thenAnswer((Invocation invocation) { |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 443 | expect(invocation.positionalArguments.single, VersionCheckStamp.flutterVersionCheckStampFile); |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 444 | |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 445 | if (stampJson != null) { |
Yegor | 5efbe05 | 2017-04-10 13:21:02 -0700 | [diff] [blame] | 446 | return stampJson; |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 447 | } |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 448 | |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 449 | if (stamp != null) { |
Jason Simmons | 466d154 | 2018-03-12 11:06:32 -0700 | [diff] [blame] | 450 | return json.encode(stamp.toJson()); |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 451 | } |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 452 | |
Yegor | 5efbe05 | 2017-04-10 13:21:02 -0700 | [diff] [blame] | 453 | return null; |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 454 | }); |
| 455 | |
| 456 | when(cache.setStampFor(any, any)).thenAnswer((Invocation invocation) { |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 457 | expect(invocation.positionalArguments.first, VersionCheckStamp.flutterVersionCheckStampFile); |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 458 | |
| 459 | if (expectSetStamp) { |
Jason Simmons | 466d154 | 2018-03-12 11:06:32 -0700 | [diff] [blame] | 460 | stamp = VersionCheckStamp.fromJson(json.decode(invocation.positionalArguments[1])); |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 461 | return null; |
| 462 | } |
| 463 | |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 464 | throw StateError('Unexpected call to Cache.setStampFor(${invocation.positionalArguments}, ${invocation.namedArguments})'); |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 465 | }); |
| 466 | |
Sam Rawlins | 2329cb7 | 2018-04-09 12:43:31 -0700 | [diff] [blame] | 467 | final Answering<ProcessResult> syncAnswer = (Invocation invocation) { |
∂ω∂ | 4277f36 | 2019-08-21 23:55:57 +0300 | [diff] [blame] | 468 | bool argsAre(String a1, [ String a2, String a3, String a4, String a5, String a6, String a7, String a8, String a9 ]) { |
Alexandre Ardhuin | eda03e2 | 2018-08-02 12:02:32 +0200 | [diff] [blame] | 469 | const ListEquality<String> equality = ListEquality<String>(); |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 470 | final List<String> args = invocation.positionalArguments.single; |
∂ω∂ | 4277f36 | 2019-08-21 23:55:57 +0300 | [diff] [blame] | 471 | final List<String> expectedArgs = <String>[a1, a2, a3, a4, a5, a6, a7, a8, a9].where((String arg) => arg != null).toList(); |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 472 | return equality.equals(args, expectedArgs); |
| 473 | } |
| 474 | |
∂ω∂ | 4277f36 | 2019-08-21 23:55:57 +0300 | [diff] [blame] | 475 | bool listArgsAre(List<String> a) { |
| 476 | return Function.apply(argsAre, a); |
| 477 | } |
| 478 | |
| 479 | if (listArgsAre(FlutterVersion.gitLog(<String>['-n', '1', '--pretty=format:%ad', '--date=iso']))) { |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 480 | return success(localCommitDate.toString()); |
| 481 | } else if (argsAre('git', 'remote')) { |
| 482 | return success(''); |
| 483 | } else if (argsAre('git', 'remote', 'add', '__flutter_version_check__', 'https://github.com/flutter/flutter.git')) { |
| 484 | return success(''); |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 485 | } else if (argsAre('git', 'fetch', '__flutter_version_check__', channel)) { |
| 486 | if (!expectServerPing) { |
Yegor | 5efbe05 | 2017-04-10 13:21:02 -0700 | [diff] [blame] | 487 | fail('Did not expect server ping'); |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 488 | } |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 489 | return errorOnFetch ? failure(128) : success(''); |
∂ω∂ | 4277f36 | 2019-08-21 23:55:57 +0300 | [diff] [blame] | 490 | // Careful here! argsAre accepts 9 arguments and FlutterVersion.gitLog adds 4. |
| 491 | } else if (remoteCommitDate != null && listArgsAre(FlutterVersion.gitLog(<String>['__flutter_version_check__/$channel', '-n', '1', '--pretty=format:%ad', '--date=iso']))) { |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 492 | return success(remoteCommitDate.toString()); |
| 493 | } |
| 494 | |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 495 | throw StateError('Unexpected call to ProcessManager.run(${invocation.positionalArguments}, ${invocation.namedArguments})'); |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 496 | }; |
| 497 | |
Sam Rawlins | fba81d0 | 2018-05-31 08:24:26 -0700 | [diff] [blame] | 498 | when(pm.runSync(any, workingDirectory: anyNamed('workingDirectory'))).thenAnswer(syncAnswer); |
| 499 | when(pm.run(any, workingDirectory: anyNamed('workingDirectory'))).thenAnswer((Invocation invocation) async { |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 500 | return syncAnswer(invocation); |
| 501 | }); |
Alexander Aprelev | 7ebf272 | 2018-07-12 14:59:22 -0700 | [diff] [blame] | 502 | |
| 503 | when(pm.runSync( |
| 504 | <String>['git', 'rev-parse', '--abbrev-ref', '--symbolic', '@{u}'], |
| 505 | workingDirectory: anyNamed('workingDirectory'), |
| 506 | environment: anyNamed('environment'), |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 507 | )).thenReturn(ProcessResult(101, 0, channel, '')); |
Alexander Aprelev | 7ebf272 | 2018-07-12 14:59:22 -0700 | [diff] [blame] | 508 | when(pm.runSync( |
| 509 | <String>['git', 'rev-parse', '--abbrev-ref', 'HEAD'], |
| 510 | workingDirectory: anyNamed('workingDirectory'), |
| 511 | environment: anyNamed('environment'), |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 512 | )).thenReturn(ProcessResult(102, 0, 'branch', '')); |
Alexander Aprelev | 7ebf272 | 2018-07-12 14:59:22 -0700 | [diff] [blame] | 513 | when(pm.runSync( |
∂ω∂ | 4277f36 | 2019-08-21 23:55:57 +0300 | [diff] [blame] | 514 | FlutterVersion.gitLog(<String>['-n', '1', '--pretty=format:%H']), |
Alexander Aprelev | 7ebf272 | 2018-07-12 14:59:22 -0700 | [diff] [blame] | 515 | workingDirectory: anyNamed('workingDirectory'), |
| 516 | environment: anyNamed('environment'), |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 517 | )).thenReturn(ProcessResult(103, 0, '1234abcd', '')); |
Alexander Aprelev | 7ebf272 | 2018-07-12 14:59:22 -0700 | [diff] [blame] | 518 | when(pm.runSync( |
∂ω∂ | 4277f36 | 2019-08-21 23:55:57 +0300 | [diff] [blame] | 519 | FlutterVersion.gitLog(<String>['-n', '1', '--pretty=format:%ar']), |
Alexander Aprelev | 7ebf272 | 2018-07-12 14:59:22 -0700 | [diff] [blame] | 520 | workingDirectory: anyNamed('workingDirectory'), |
| 521 | environment: anyNamed('environment'), |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 522 | )).thenReturn(ProcessResult(104, 0, '1 second ago', '')); |
Alexander Aprelev | 7ebf272 | 2018-07-12 14:59:22 -0700 | [diff] [blame] | 523 | when(pm.runSync( |
| 524 | <String>['git', 'describe', '--match', 'v*.*.*', '--first-parent', '--long', '--tags'], |
| 525 | workingDirectory: anyNamed('workingDirectory'), |
| 526 | environment: anyNamed('environment'), |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 527 | )).thenReturn(ProcessResult(105, 0, 'v0.1.2-3-1234abcd', '')); |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | class MockProcessManager extends Mock implements ProcessManager {} |
Greg Spencer | eb35f89 | 2018-11-14 16:51:12 -0800 | [diff] [blame] | 531 | |
Yegor | 93126a8 | 2017-04-04 10:45:43 -0700 | [diff] [blame] | 532 | class MockCache extends Mock implements Cache {} |