Skip to content
This repository was archived by the owner on Nov 29, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions packages/simulators/lib/simulator_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class IosSimulatorManager {
await _listExistingSimulators(osMajorVersion, osMinorVersion);

// The simulator list, have the version string followed by a list of phone
// names along with their ids and their statuses. Example output:
// names along with their ids and their statuses. Example output 1:
// -- iOS 13.5 --
// iPhone 8 (2A437C91-3B85-4D7B-BB91-32561DA07B85) (Shutdown)
// iPhone 8 Plus (170207A8-7631-4CBE-940E-86A7815AEB2B) (Shutdown)
Expand All @@ -114,11 +114,34 @@ class IosSimulatorManager {
// iPhone 8 Plus (170207A8-7631-4CBE-940E-86A7815AEB2B) (Shutdown)
// iPhone 11 (7AEC5FB9-E08A-4F7F-8CA2-1518CE3A3E0D) (Booted)
// iPhone 11 Pro (D8074C8B-35A5-4DA5-9AB2-4CE738A5E5FC) (Shutdown)
// -- Device Pairs --
// Example output 2 (from Mac Web Engine try bots):
// == Devices ==
// -- iOS 13.0 --
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this example demonstrating the need for the new code? If so, then I'm not sure how it does that. restOfTheOutput.indexOf('--') is >0 (because the string does contain --), so the new code won't execute 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in one of them the version was finishing with '--' now it finishes with "==". let me write an explanation, but as mentioned in the earlier comment my next PR would be just removing these.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have local test for these things btw which I don't merge let me also send it!

// iPhone 8 (C142C9F5-C26E-4EB5-A2B8-915D5BD62FA5) (Shutdown)
// iPhone 8 Plus (C1FE8FAA-5797-478E-8BEE-D7AD4811F08C) (Shutdown)
// iPhone 11 (28A3E6C0-76E7-4EE3-9B34-B059C4BBE5CA) (Shutdown)
// iPhone 11 Pro (0AD4BBA5-7BE7-415D-B9FD-D962FA8E1782) (Shutdown)
// iPhone 11 Pro Max (1280DE05-B334-4E60-956F-4A62220DEFA3) (Shutdown)
// iPad Pro (9.7-inch) (EDE46501-CB2B-4EA4-8B5C-13FAC6F2EC91) (Shutdown)
// iPad Pro (11-inch) (E0B89C9C-6200-495C-B18B-0078CCAAC688) (Shutdown)
// iPad Pro (12.9-inch) (3rd generation) (DB3EB7A8-C4D2-4F86-AFC1-D652FB0579E8) (Shutdown)
// iPad Air (3rd generation) (9237DCD8-8F0E-40A6-96DF-B33C915AFE1B) (Shutdown)
// == Device Pairs ==
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this implementation can be simplified if it first split the output line by line, then inspected each line. There are only two interesting line formats:

  1. -- OS MAJOR.MINOR --, such as -- iOS 13.0
  2. DEVICE (HASH) (STATE), sush as iPhone 8 (C142C9F5-C26E-4EB5-A2B8-915D5BD62FA5) (Shutdown)

A single line-by-line for/in loop can detect both OS version and the requested device.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a lot for the suggestion, I think I'll used --json tag and will get rid of this parsing all together :)

final int indexOfVersionListStart =
simulatorsList.indexOf(simulatorVersion);
final String restOfTheOutput = simulatorsList
.substring(indexOfVersionListStart + simulatorVersion.length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if indexOfVersionListStart == -1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also fail an exception like now. This code relies on the current formatting very much: "--" or "==" kills all the code. I think @christopherfujino suggestion would be the best change.

final int indexOfNextVersion = restOfTheOutput.indexOf('--');
int indexOfNextVersion = restOfTheOutput.indexOf('--');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we do a more specific regex, rather than relying on --? I worry that a future change to this output could lead to a hard to spot regression.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, never mind...I see that would be difficult...

if (indexOfNextVersion == -1) {
// Search for `== Device Pairs ==`.
indexOfNextVersion = restOfTheOutput.indexOf('==');
}
if (indexOfNextVersion == -1) {
// Set to end of file.
indexOfNextVersion = restOfTheOutput.length;
}

final String listOfPhones =
restOfTheOutput.substring(0, indexOfNextVersion);

Expand Down Expand Up @@ -198,8 +221,7 @@ class IosSimulator {
await io.Process.run('xcrun', ['simctl', 'shutdown', '$id']);

if (versionResult.exitCode != 0) {
throw Exception(
'Failed to shutdown iOS simulators with id: $id.');
throw Exception('Failed to shutdown iOS simulators with id: $id.');
}

this._booted = false;
Expand Down
3 changes: 3 additions & 0 deletions packages/simulators/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ description: For creating and managing iOS simulators.

environment:
sdk: ">=2.2.2 <3.0.0"

dev_dependencies:
test: ^1.6.5
24 changes: 24 additions & 0 deletions packages/simulators/test/simulator_manager_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.6
import 'package:test/test.dart';

import '../lib/simulator_manager.dart';

void main() async {
test('boot simulator', () async {
IosSimulatorManager simulatorManager = IosSimulatorManager();
IosSimulator simulator =
await simulatorManager.getSimulator(13, 0, 'iPhone 11');
await simulator.boot();
});

test('shutdown simulator', () async {
IosSimulatorManager simulatorManager = IosSimulatorManager();
IosSimulator simulator =
await simulatorManager.getSimulator(13, 0, 'iPhone 11');
await simulator.shutdown();
});
}