Skip to content
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

prevent accidental calls to io.exit when asserts are active. #46210

Merged
merged 9 commits into from
Dec 9, 2019

Conversation

jonahwilliams
Copy link
Member

@jonahwilliams jonahwilliams commented Dec 6, 2019

Description

To prevent a scenario such as #46142 for occurring again in the future, prevent calls to dart:io's exit when asserts are enabled. Allow an env variable to override to allow for integration tests, which run the full flutter process with asserts enabled

@fluttergithubbot fluttergithubbot added the tool Affects the "flutter" command-line tool. See also t: labels. label Dec 6, 2019
@jonahwilliams
Copy link
Member Author

Errm, right - assertions in integration tests. If we could detect that we are in a package:test isolate maybe this could be more narrowly focused.

final ProcessResult result = await processManager.run(command,
workingDirectory: folder,
environment: <String, String>{
'FLUTTER_INTEGRATION_TEST': 'true'
Copy link
Member

Choose a reason for hiding this comment

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

In the previous two instances of this it's set to 'test' rather than 'true'.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

/// Throws [StateError] if assertions are enabled and the dart:io exit
/// is still active when called. This may indicate exit was called in
/// a test without being configured correctly. This behavior can be
/// removed by setting the `FLUTTER_INTEGRATION_TEST` environment
Copy link
Member

Choose a reason for hiding this comment

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

Is FLUTTER_INTEGRATION_TEST new? Is there any pre-existing one that could work?

Copy link
Member Author

Choose a reason for hiding this comment

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

there is a FLUTTER_TEST value that is set by flutter test and for some reason by the test_driver.dart. I could reuse that field since I don't think it is used otherwise for pub run test

Copy link
Member Author

Choose a reason for hiding this comment

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

Switched to FLUTTER_TEST

/// This is analogous to the `exit` function in `dart:io`, except that this
/// function may be set to a testing-friendly value by calling
/// [setExitFunctionForTests] (and then restored to its default implementation
/// with [restoreExitFunction]). The default implementation delegates to
/// `dart:io`.
ExitFunction get exit => _exitFunction;
ExitFunction get exit {
assert(() {
Copy link
Member

Choose a reason for hiding this comment

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

Does this work?

assert(
  _exitFunction != io.exit || io.Platform.environment['FLUTTER_TEST'] != null,
  'io.exit was called with assertions active. If this is an integration test, '
  'ensure that the environment variable FLUTTER_TEST is set '
  'to a non-null String.',
);

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea

@jonahwilliams jonahwilliams merged commit b698c9d into flutter:master Dec 9, 2019
@jonahwilliams jonahwilliams deleted the never_again_ branch December 9, 2019 21:01
@jonahwilliams jonahwilliams restored the never_again_ branch December 9, 2019 21:12
jonahwilliams added a commit that referenced this pull request Dec 9, 2019
…ts are active in unit tests (#46210)"

This reverts commit b698c9d.
jonahwilliams added a commit that referenced this pull request Dec 9, 2019
…ts are active in unit tests (#46210)" (#46630)

This reverts commit b698c9d.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
tool Affects the "flutter" command-line tool. See also t: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants