Skip to content

Commit

Permalink
Null safety migration of packages/flutter_tools/test/commands.shard/h…
Browse files Browse the repository at this point in the history
…ermetic, part 1/3 (#110707)

* Migrate packages/flutter_tools/test/commands.shard/hermetic, part 1/3

* Fix tests

* Fix analysis

* Fix analyze_test

* Make AnalysisServer a local variable

* Chris's comments
  • Loading branch information
liamappelbe committed Sep 6, 2022
1 parent c035499 commit 8da0432
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 104 deletions.
2 changes: 1 addition & 1 deletion packages/flutter_tools/lib/src/commands/analyze_base.dart
Expand Up @@ -87,7 +87,7 @@ abstract class AnalyzeBase {
return artifacts.getHostArtifact(HostArtifact.engineDartSdkPath).path;
}
bool get isBenchmarking => argResults['benchmark'] as bool;
String get protocolTrafficLog => argResults['protocol-traffic-log'] as String;
String? get protocolTrafficLog => argResults['protocol-traffic-log'] as String?;

/// Generate an analysis summary for both [AnalyzeOnce], [AnalyzeContinuously].
static String generateErrorsMessage({
Expand Down
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.8

import 'dart:async';

import 'package:fake_async/fake_async.dart';
Expand Down Expand Up @@ -32,13 +30,12 @@ void main() {
Cache.flutterRoot = getFlutterRoot();
});

AnalysisServer server;
Directory tempDir;
FileSystem fileSystem;
Platform platform;
ProcessManager processManager;
AnsiTerminal terminal;
Logger logger;
late Directory tempDir;
late FileSystem fileSystem;
late Platform platform;
late ProcessManager processManager;
late AnsiTerminal terminal;
late Logger logger;

setUp(() {
fileSystem = globals.localFileSystem;
Expand All @@ -51,7 +48,6 @@ void main() {

tearDown(() {
tryToDelete(tempDir);
return server?.dispose();
});


Expand Down Expand Up @@ -88,11 +84,10 @@ void main() {
await pub.get(
context: PubContext.flutterTests,
directory: tempDir.path,
generateSyntheticPackage: false,
);

server = AnalysisServer(
globals.artifacts.getHostArtifact(HostArtifact.engineDartSdkPath).path,
final AnalysisServer server = AnalysisServer(
globals.artifacts!.getHostArtifact(HostArtifact.engineDartSdkPath).path,
<String>[tempDir.path],
fileSystem: fileSystem,
platform: platform,
Expand All @@ -109,6 +104,8 @@ void main() {
await onDone;

expect(errorCount, 0);

await server.dispose();
});
});

Expand All @@ -126,18 +123,17 @@ void main() {
await pub.get(
context: PubContext.flutterTests,
directory: tempDir.path,
generateSyntheticPackage: false,
);

server = AnalysisServer(
globals.artifacts.getHostArtifact(HostArtifact.engineDartSdkPath).path,
<String>[tempDir.path],
fileSystem: fileSystem,
platform: platform,
processManager: processManager,
logger: logger,
terminal: terminal,
);
final AnalysisServer server = AnalysisServer(
globals.artifacts!.getHostArtifact(HostArtifact.engineDartSdkPath).path,
<String>[tempDir.path],
fileSystem: fileSystem,
platform: platform,
processManager: processManager,
logger: logger,
terminal: terminal,
);

int errorCount = 0;
final Future<bool> onDone = server.onAnalyzing.where((bool analyzing) => analyzing == false).first;
Expand All @@ -149,13 +145,15 @@ void main() {
await onDone;

expect(errorCount, greaterThan(0));

await server.dispose();
});

testUsingContext('Returns no errors when source is error-free', () async {
const String contents = "StringBuffer bar = StringBuffer('baz');";
tempDir.childFile('main.dart').writeAsStringSync(contents);
server = AnalysisServer(
globals.artifacts.getHostArtifact(HostArtifact.engineDartSdkPath).path,
final AnalysisServer server = AnalysisServer(
globals.artifacts!.getHostArtifact(HostArtifact.engineDartSdkPath).path,
<String>[tempDir.path],
fileSystem: fileSystem,
platform: platform,
Expand All @@ -172,6 +170,7 @@ void main() {
await server.start();
await onDone;
expect(errorCount, 0);
await server.dispose();
});

testUsingContext('Can run AnalysisService with customized cache location', () async {
Expand Down
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.8

import 'package:args/command_runner.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/artifacts.dart';
Expand All @@ -22,7 +20,7 @@ import '../../src/test_flutter_command_runner.dart';

class ProjectValidatorDummy extends ProjectValidator {
@override
Future<List<ProjectValidatorResult>> start(FlutterProject project, {Logger logger, FileSystem fileSystem}) async{
Future<List<ProjectValidatorResult>> start(FlutterProject project, {Logger? logger, FileSystem? fileSystem}) async{
return <ProjectValidatorResult>[
const ProjectValidatorResult(name: 'pass', value: 'value', status: StatusProjectValidator.success),
const ProjectValidatorResult(name: 'fail', value: 'my error', status: StatusProjectValidator.error),
Expand All @@ -41,7 +39,7 @@ class ProjectValidatorDummy extends ProjectValidator {

class ProjectValidatorSecondDummy extends ProjectValidator {
@override
Future<List<ProjectValidatorResult>> start(FlutterProject project, {Logger logger, FileSystem fileSystem}) async{
Future<List<ProjectValidatorResult>> start(FlutterProject project, {Logger? logger, FileSystem? fileSystem}) async{
return <ProjectValidatorResult>[
const ProjectValidatorResult(name: 'second', value: 'pass', status: StatusProjectValidator.success),
const ProjectValidatorResult(name: 'other fail', value: 'second fail', status: StatusProjectValidator.error),
Expand All @@ -59,7 +57,7 @@ class ProjectValidatorSecondDummy extends ProjectValidator {

class ProjectValidatorCrash extends ProjectValidator {
@override
Future<List<ProjectValidatorResult>> start(FlutterProject project, {Logger logger, FileSystem fileSystem}) async{
Future<List<ProjectValidatorResult>> start(FlutterProject project, {Logger? logger, FileSystem? fileSystem}) async{
throw Exception('my exception');
}

Expand All @@ -73,10 +71,10 @@ class ProjectValidatorCrash extends ProjectValidator {
}

void main() {
FileSystem fileSystem;
Terminal terminal;
ProcessManager processManager;
Platform platform;
late FileSystem fileSystem;
late Terminal terminal;
late ProcessManager processManager;
late Platform platform;

group('analyze --suggestions command', () {

Expand Down
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.8

import 'package:args/command_runner.dart';
import 'package:file/file.dart';
import 'package:file/memory.dart';
Expand Down Expand Up @@ -46,13 +44,13 @@ void main() {
});

group('analyze command', () {
FileSystem fileSystem;
Platform platform;
BufferLogger logger;
FakeProcessManager processManager;
Terminal terminal;
AnalyzeCommand command;
CommandRunner<void> runner;
late FileSystem fileSystem;
late Platform platform;
late BufferLogger logger;
late FakeProcessManager processManager;
late Terminal terminal;
late AnalyzeCommand command;
late CommandRunner<void> runner;

setUpAll(() {
Cache.disableLocking();
Expand Down Expand Up @@ -130,8 +128,8 @@ void main() {
// Absolute paths
expect(inRepo(<String>[tempDir.path], fileSystem), isFalse);
expect(inRepo(<String>[fileSystem.path.join(tempDir.path, 'foo')], fileSystem), isFalse);
expect(inRepo(<String>[Cache.flutterRoot], fileSystem), isTrue);
expect(inRepo(<String>[fileSystem.path.join(Cache.flutterRoot, 'foo')], fileSystem), isTrue);
expect(inRepo(<String>[Cache.flutterRoot!], fileSystem), isTrue);
expect(inRepo(<String>[fileSystem.path.join(Cache.flutterRoot!, 'foo')], fileSystem), isTrue);

// Relative paths
fileSystem.currentDirectory = Cache.flutterRoot;
Expand All @@ -158,18 +156,19 @@ void main() {
'startColumn': 4,
},
'message': 'Prefer final for variable declarations if they are not reassigned.',
'code': 'var foo = 123;',
'hasFix': false,
};
expect(WrittenError.fromJson(json).toString(),
'[info] Prefer final for variable declarations if they are not reassigned (/Users/.../lib/test.dart:15:4)');
});
}

bool inRepo(List<String> fileList, FileSystem fileSystem) {
bool inRepo(List<String>? fileList, FileSystem fileSystem) {
if (fileList == null || fileList.isEmpty) {
fileList = <String>[fileSystem.path.current];
}
final String root = fileSystem.path.normalize(fileSystem.path.absolute(Cache.flutterRoot));
final String root = fileSystem.path.normalize(fileSystem.path.absolute(Cache.flutterRoot!));
final String prefix = root + fileSystem.path.separator;
for (String file in fileList) {
file = fileSystem.path.normalize(fileSystem.path.absolute(file));
Expand Down
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.8

import 'package:args/command_runner.dart';
import 'package:file/memory.dart';
import 'package:file_testing/file_testing.dart';
Expand Down
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.8

import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/platform.dart';
Expand All @@ -21,9 +19,9 @@ import '../../src/fakes.dart';
import '../../src/test_build_system.dart';

void main() {
MemoryFileSystem memoryFileSystem;
Directory outputDirectory;
FakePlatform fakePlatform;
late MemoryFileSystem memoryFileSystem;
late Directory outputDirectory;
late FakePlatform fakePlatform;

setUpAll(() {
Cache.disableLocking();
Expand All @@ -48,7 +46,7 @@ void main() {
group('build ios-framework', () {
group('podspec', () {
const String engineRevision = '0123456789abcdef';
Cache cache;
late Cache cache;

setUp(() {
final Directory rootOverride = memoryFileSystem.directory('cache');
Expand Down Expand Up @@ -181,7 +179,7 @@ void main() {
});

group('not on master channel', () {
FakeFlutterVersion fakeFlutterVersion;
late FakeFlutterVersion fakeFlutterVersion;
setUp(() {
const GitTagVersion gitTagVersion = GitTagVersion(
x: 1,
Expand Down Expand Up @@ -277,7 +275,7 @@ void main() {
group('build macos-framework', () {
group('podspec', () {
const String engineRevision = '0123456789abcdef';
Cache cache;
late Cache cache;

setUp(() {
final Directory rootOverride = memoryFileSystem.directory('cache');
Expand Down Expand Up @@ -410,7 +408,7 @@ void main() {
});

group('not on master channel', () {
FakeFlutterVersion fakeFlutterVersion;
late FakeFlutterVersion fakeFlutterVersion;
setUp(() {
const GitTagVersion gitTagVersion = GitTagVersion(
x: 1,
Expand Down Expand Up @@ -504,8 +502,8 @@ void main() {
});

group('XCFrameworks', () {
MemoryFileSystem fileSystem;
FakeProcessManager fakeProcessManager;
late MemoryFileSystem fileSystem;
late FakeProcessManager fakeProcessManager;

setUp(() {
fileSystem = MemoryFileSystem.test();
Expand Down
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.8

import 'package:args/command_runner.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
Expand All @@ -28,21 +26,21 @@ class FakeXcodeProjectInterpreterWithBuildSettings extends FakeXcodeProjectInter
@override
Future<Map<String, String>> getBuildSettings(
String projectPath, {
XcodeProjectBuildContext buildContext,
XcodeProjectBuildContext? buildContext,
Duration timeout = const Duration(minutes: 1),
}) async {
return <String, String>{
'PRODUCT_BUNDLE_IDENTIFIER': productBundleIdentifier ?? 'io.flutter.someProject',
'TARGET_BUILD_DIR': 'build/ios/Release-iphoneos',
'WRAPPER_NAME': 'Runner.app',
if (developmentTeam != null) 'DEVELOPMENT_TEAM': developmentTeam,
if (developmentTeam != null) 'DEVELOPMENT_TEAM': developmentTeam!,
};
}

/// The value of 'PRODUCT_BUNDLE_IDENTIFIER'.
final String productBundleIdentifier;
final String? productBundleIdentifier;

final String developmentTeam;
final String? developmentTeam;
}

final Platform macosPlatform = FakePlatform(
Expand All @@ -59,8 +57,8 @@ final Platform notMacosPlatform = FakePlatform(
);

void main() {
FileSystem fileSystem;
TestUsage usage;
late FileSystem fileSystem;
late TestUsage usage;

setUpAll(() {
Cache.disableLocking();
Expand Down Expand Up @@ -90,7 +88,7 @@ void main() {
'xattr', '-r', '-d', 'com.apple.FinderInfo', '/',
]);

FakeCommand setUpRsyncCommand({void Function() onRun}) {
FakeCommand setUpRsyncCommand({void Function()? onRun}) {
return FakeCommand(
command: const <String>[
'rsync',
Expand All @@ -104,7 +102,7 @@ void main() {
);
}

FakeCommand setUpXCResultCommand({String stdout = '', void Function() onRun}) {
FakeCommand setUpXCResultCommand({String stdout = '', void Function()? onRun}) {
return FakeCommand(
command: const <String>[
'xcrun',
Expand All @@ -125,10 +123,10 @@ void main() {
FakeCommand setUpFakeXcodeBuildHandler({
bool verbose = false,
bool simulator = false,
String deviceId,
String? deviceId,
int exitCode = 0,
String stdout,
void Function() onRun,
String? stdout,
void Function()? onRun,
}) {
return FakeCommand(
command: <String>[
Expand Down

0 comments on commit 8da0432

Please sign in to comment.