-
Notifications
You must be signed in to change notification settings - Fork 925
feat: add fallback behavior when running a default simulator on iOS #758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add fallback behavior when running a default simulator on iOS #758
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for tackling this! Looks good, left some feedback
@@ -492,7 +507,7 @@ export default { | |||
description: | |||
'Explicitly set simulator to use. Optionally include iOS version between' + | |||
'parenthesis at the end to match an exact version: "iPhone 6 (10.0)"', | |||
default: 'iPhone X', | |||
default: '', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can default to undefined (or just don't provide the default) – can we change that and adjust docs as well?
: 'iPhone 8'; | ||
} | ||
|
||
const selectedSimulator = findMatchingSimulator(simulators, simulator); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is calling findMatchingSimulator
twice for iPhone 11 and iPhone X. Can we move this call to line 143 (iPhone 8) and get rid of selectedSimulator
variable?
Thanks for the feedback! I've made some modification to reduce the number of times |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for sending this over! I think this is a really good idea. Left some feedback as well, on top of what @thymikee already wrote.
* - iPhone X | ||
* - iPhone 8 | ||
*/ | ||
if (!simulator) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this could be replaced with a simple while
loop or a for
loop and break
statement. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the suggestion!
I modified it to use array.some
so that loop would break when simulator is found.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a good improvement, but still, I find doing side effects inside some
a bit of a code smell.
How about:
for (const defaultSimulator of defaultSimulators) {
selectedSimulator = findMatchingSimulator(simulators, defaultSimulator);
if (selectedSimulator) {
break;
}
}
or
const selectedSimulator = args.simulator
? findMatchingSimulator(simulators, args.simulator)
: defaultSimulators.reduce(
(selectedSimulator, simulator) => selectedSimulator || findMatchingSimulator(simulators, simulator),
null
);
The below (in my opinion) would be the most concise:
const selectedSimulator = defaultSimulators.reduce(
(selectedSimulator, simulator) => selectedSimulator || findMatchingSimulator(simulators, simulator),
findMatchingSimulator(simulators, args.simulator)
);
but it would require we bring: args.simulator
default value to be iPhone 11
and rename defaultSimulators
to be fallbackSimulators
(in other words, try these ones in case user provided/default value does not exist on the machine).
Leaving that up to further discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the feedback and suggestions!
I find doing side effects inside some
feels iffy as well, but was not able to come up with a solution at the moment.
I like the last suggestion of providing fallback when args.simulator
is not available. It let me remove multiple if
s.
I've modified the default simulator to be iPhone 11
and modified the document to match the code.
} | ||
} else { | ||
selectedSimulator = findMatchingSimulator(simulators, simulator); | ||
if (!selectedSimulator) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we can move this and another if
statement at line 152
outside of this block. If we would place it below line 163, we would be able to keep only one if
statement, instead of the two.
* - iPhone X | ||
* - iPhone 8 | ||
*/ | ||
if (!simulator) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a good improvement, but still, I find doing side effects inside some
a bit of a code smell.
How about:
for (const defaultSimulator of defaultSimulators) {
selectedSimulator = findMatchingSimulator(simulators, defaultSimulator);
if (selectedSimulator) {
break;
}
}
or
const selectedSimulator = args.simulator
? findMatchingSimulator(simulators, args.simulator)
: defaultSimulators.reduce(
(selectedSimulator, simulator) => selectedSimulator || findMatchingSimulator(simulators, simulator),
null
);
The below (in my opinion) would be the most concise:
const selectedSimulator = defaultSimulators.reduce(
(selectedSimulator, simulator) => selectedSimulator || findMatchingSimulator(simulators, simulator),
findMatchingSimulator(simulators, args.simulator)
);
but it would require we bring: args.simulator
default value to be iPhone 11
and rename defaultSimulators
to be fallbackSimulators
(in other words, try these ones in case user provided/default value does not exist on the machine).
Leaving that up to further discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's ship it
const fallbackSimulators = ['iPhone X', 'iPhone 8']; | ||
const selectedSimulator = fallbackSimulators.reduce((simulator, fallback) => { | ||
return simulator || findMatchingSimulator(simulators, fallback); | ||
}, findMatchingSimulator(simulators, args.simulator)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This loop always run through all simulators from fallbackSimulators
, but since the data set is small and we avoid running findMatchingSimulator
if necessary, I'm fine with that.
… (#782) Co-authored-by: Jakob Krigovsky <jakob@krigovsky.com>
Summary:
This is an attempt to solve problem on #739, based on idea provided at #739 (comment)
This changes the behavior of runIOS when no simulator argument is provided.
When no simulator argument is provided, the cli will look for simulators in the following order
iPhone 11
iPhone X
iPhone 8
Test Plan:
yarn
yarn test
yarn lint