[flutter_tools] add a mechanism to turn off immediate tool exit (#66787)

Instead of always exiting the tool, provide a mechanism to turn off this behavior for non-critical functionality like configuration and analytics settings.

Fixes #66786 Fixes #4674
This commit is contained in:
Jonah Williams
2020-09-28 13:04:38 -07:00
committed by GitHub
parent 5e651b969d
commit 549de844ed
7 changed files with 205 additions and 35 deletions

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
import 'package:file/file.dart';
import 'package:flutter_tools/src/base/common.dart';
import 'package:flutter_tools/src/base/error_handling_io.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
@@ -71,6 +72,18 @@ void setupWriteMocks({
)).thenThrow(FileSystemException('', '', OSError('', errorCode)));
}
void setupReadMocks({
FileSystem mockFileSystem,
ErrorHandlingFileSystem fs,
int errorCode,
}) {
final MockFile mockFile = MockFile();
when(mockFileSystem.file(any)).thenReturn(mockFile);
when(mockFile.readAsStringSync(
encoding: anyNamed('encoding'),
)).thenThrow(FileSystemException('', '', OSError('', errorCode)));
}
void setupDirectoryMocks({
FileSystem mockFileSystem,
ErrorHandlingFileSystem fs,
@@ -114,6 +127,30 @@ void main() {
when(mockFileSystem.path).thenReturn(MockPathContext());
});
testWithoutContext('bypasses error handling when withAllowedFailure is used', () {
setupWriteMocks(
mockFileSystem: mockFileSystem,
fs: fs,
errorCode: kUserPermissionDenied,
);
final File file = fs.file('file');
expect(() => ErrorHandlingFileSystem.noExitOnFailure(
() => file.writeAsStringSync('')), throwsA(isA<Exception>()));
// nesting does not unconditionally re-enable errors.
expect(() {
ErrorHandlingFileSystem.noExitOnFailure(() {
ErrorHandlingFileSystem.noExitOnFailure(() { });
file.writeAsStringSync('');
});
}, throwsA(isA<Exception>()));
// Check that state does not leak.
expect(() => file.writeAsStringSync(''), throwsA(isA<ToolExit>()));
});
testWithoutContext('when access is denied', () async {
setupWriteMocks(
mockFileSystem: mockFileSystem,
@@ -219,6 +256,20 @@ void main() {
expect(() => directory.existsSync(),
throwsToolExit(message: expectedMessage));
});
testWithoutContext('When reading from a file without permission', () {
setupReadMocks(
mockFileSystem: mockFileSystem,
fs: fs,
errorCode: kUserPermissionDenied,
);
final File file = fs.file('file');
const String expectedMessage = 'Flutter failed to read a file at';
expect(() => file.readAsStringSync(),
throwsToolExit(message: expectedMessage));
});
});
group('throws ToolExit on Linux', () {
@@ -437,6 +488,20 @@ void main() {
expect(() => directory.existsSync(),
throwsToolExit(message: expectedMessage));
});
testWithoutContext('When reading from a file without permission', () {
setupReadMocks(
mockFileSystem: mockFileSystem,
fs: fs,
errorCode: eacces,
);
final File file = fs.file('file');
const String expectedMessage = 'Flutter failed to read a file at';
expect(() => file.readAsStringSync(),
throwsToolExit(message: expectedMessage));
});
});
testWithoutContext('Caches path context correctly', () {

View File

@@ -4,10 +4,12 @@
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/config.dart';
import 'package:flutter_tools/src/base/error_handling_io.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/base/terminal.dart';
import 'package:flutter_tools/src/convert.dart';
import 'package:test/fake.dart';
import '../src/common.dart';
@@ -62,13 +64,7 @@ void main() {
});
testWithoutContext('Config parse error', () {
final BufferLogger bufferLogger = BufferLogger(
terminal: AnsiTerminal(
stdio: null,
platform: const LocalPlatform(),
),
outputPreferences: OutputPreferences.test(),
);
final BufferLogger bufferLogger = BufferLogger.test();
final File file = memoryFileSystem.file('example')
..writeAsStringSync('{"hello":"bar');
config = Config(
@@ -81,4 +77,50 @@ void main() {
expect(file.existsSync(), false);
expect(bufferLogger.errorText, contains('Failed to decode preferences'));
});
testWithoutContext('Config does not error on missing file', () {
final BufferLogger bufferLogger = BufferLogger.test();
final File file = memoryFileSystem.file('example');
config = Config(
'example',
fileSystem: memoryFileSystem,
logger: bufferLogger,
platform: fakePlatform,
);
expect(file.existsSync(), false);
expect(bufferLogger.errorText, isEmpty);
});
testWithoutContext('Config does not error on a normally fatal file system exception', () {
final BufferLogger bufferLogger = BufferLogger.test();
final File file = ErrorHandlingFile(
platform: FakePlatform(operatingSystem: 'linux'),
fileSystem: MemoryFileSystem.test(),
delegate: FakeFile('testfile'),
);
config = Config.createForTesting(file, bufferLogger);
expect(bufferLogger.errorText, contains('Could not read preferences in testfile'));
// Also contains original error message:
expect(bufferLogger.errorText, contains('The flutter tool cannot access the file or directory'));
});
}
class FakeFile extends Fake implements File {
FakeFile(this.path);
@override
final String path;
@override
bool existsSync() {
return true;
}
@override
String readAsStringSync({Encoding encoding = utf8ForTesting}) {
throw const FileSystemException('', '', OSError('', 13)); // EACCES error on linux
}
}