Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
d0526d3f
Unverified
Commit
d0526d3f
authored
Dec 10, 2019
by
Jonah Williams
Committed by
GitHub
Dec 10, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[flutter_tool] Prevent accidental calls to io.exit in unit tests (#46639)
parent
14145a4e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
1 deletion
+33
-1
io.dart
packages/flutter_tools/lib/src/base/io.dart
+23
-1
io_test.dart
packages/flutter_tools/test/general.shard/base/io_test.dart
+10
-0
No files found.
packages/flutter_tools/lib/src/base/io.dart
View file @
d0526d3f
...
...
@@ -30,6 +30,7 @@ import 'dart:io' as io show exit, IOSink, Process, ProcessInfo, ProcessSignal,
stderr
,
stdin
,
Stdin
,
StdinException
,
Stdout
,
stdout
;
import
'package:meta/meta.dart'
;
import
'package:test_api/test_api.dart'
;
// ignore: deprecated_member_use
import
'context.dart'
;
import
'platform.dart'
;
...
...
@@ -92,12 +93,33 @@ ExitFunction _exitFunction = _defaultExitFunction;
/// Exits the process.
///
/// Throws [AssertionError] 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 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
(
_exitFunction
!=
io
.
exit
||
!
_inUnitTest
(),
'io.exit was called with assertions active in a unit test'
,
);
return
_exitFunction
;
}
// Whether the tool is executing in a unit test.
bool
_inUnitTest
(
)
{
try
{
expect
(
true
,
true
);
}
on
StateError
{
// If a StateError is caught, then this is not a unit test.
return
false
;
}
return
true
;
}
/// Sets the [exit] function to a function that throws an exception rather
/// than exiting the process; this is intended for testing purposes.
...
...
packages/flutter_tools/test/general.shard/base/io_test.dart
View file @
d0526d3f
...
...
@@ -68,6 +68,16 @@ void main() {
testUsingContext
(
'ProcessSignal toString() works'
,
()
async
{
expect
(
io
.
ProcessSignal
.
sigint
.
toString
(),
ProcessSignal
.
SIGINT
.
toString
());
});
test
(
'exit throws a StateError if called without being overriden'
,
()
{
expect
(()
=>
exit
(
0
),
throwsA
(
isInstanceOf
<
AssertionError
>()));
});
test
(
'exit does not throw a StateError if overriden'
,
()
{
setExitFunctionForTests
((
int
value
)
{});
expect
(()
=>
exit
(
0
),
returnsNormally
);
});
}
class
MockIoProcessSignal
extends
Mock
implements
io
.
ProcessSignal
{}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment