From 75127a8007ce03a3ad530d0182b95d95af78b220 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20S=20Guerrero?= Date: Mon, 24 Jan 2022 17:10:16 -0800 Subject: [PATCH] [flutter_tools] support files in flutter analyze #96231 (#97021) --- .../lib/src/commands/analyze_once.dart | 32 +++++----- .../integration.shard/analyze_once_test.dart | 64 +++++++++++++++++-- 2 files changed, 75 insertions(+), 21 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/analyze_once.dart b/packages/flutter_tools/lib/src/commands/analyze_once.dart index f69f4c65ad..85d490a8db 100644 --- a/packages/flutter_tools/lib/src/commands/analyze_once.dart +++ b/packages/flutter_tools/lib/src/commands/analyze_once.dart @@ -48,17 +48,15 @@ class AnalyzeOnce extends AnalyzeBase { final String currentDirectory = (workingDirectory ?? fileSystem.currentDirectory).path; - // find directories from argResults.rest - final Set directories = Set.of(argResults.rest + // find directories or files from argResults.rest + final Set items = Set.of(argResults.rest .map((String path) => fileSystem.path.canonicalize(path))); - if (directories.isNotEmpty) { - for (final String directory in directories) { - final FileSystemEntityType type = fileSystem.typeSync(directory); + if (items.isNotEmpty) { + for (final String item in items) { + final FileSystemEntityType type = fileSystem.typeSync(item); if (type == FileSystemEntityType.notFound) { - throwToolExit("'$directory' does not exist"); - } else if (type != FileSystemEntityType.directory) { - throwToolExit("'$directory' is not a directory"); + throwToolExit("'$item' does not exist"); } } } @@ -67,17 +65,17 @@ class AnalyzeOnce extends AnalyzeBase { // check for conflicting dependencies final PackageDependencyTracker dependencies = PackageDependencyTracker(); dependencies.checkForConflictingDependencies(repoPackages, dependencies); - directories.addAll(repoRoots); + items.addAll(repoRoots); if (argResults.wasParsed('current-package') && (argResults['current-package'] as bool)) { - directories.add(currentDirectory); + items.add(currentDirectory); } } else { - if (argResults['current-package'] as bool) { - directories.add(currentDirectory); + if ((argResults['current-package'] as bool) && items.isEmpty) { + items.add(currentDirectory); } } - if (directories.isEmpty) { + if (items.isEmpty) { throwToolExit('Nothing to analyze.', exitCode: 0); } @@ -86,7 +84,7 @@ class AnalyzeOnce extends AnalyzeBase { final AnalysisServer server = AnalysisServer( sdkPath, - directories.toList(), + items.toList(), fileSystem: fileSystem, platform: platform, logger: logger, @@ -133,9 +131,9 @@ class AnalyzeOnce extends AnalyzeBase { // collect results timer = Stopwatch()..start(); - final String message = directories.length > 1 - ? '${directories.length} ${directories.length == 1 ? 'directory' : 'directories'}' - : fileSystem.path.basename(directories.first); + final String message = items.length > 1 + ? '${items.length} ${items.length == 1 ? 'item' : 'items'}' + : fileSystem.path.basename(items.first); progress = argResults['preamble'] == true ? logger.startProgress( 'Analyzing $message...', diff --git a/packages/flutter_tools/test/integration.shard/analyze_once_test.dart b/packages/flutter_tools/test/integration.shard/analyze_once_test.dart index 94f5ce735e..7418a848a7 100644 --- a/packages/flutter_tools/test/integration.shard/analyze_once_test.dart +++ b/packages/flutter_tools/test/integration.shard/analyze_once_test.dart @@ -13,6 +13,7 @@ void main() { late Directory tempDir; late String projectPath; late File libMain; + late File errorFile; Future runCommand({ List arguments = const [], @@ -79,6 +80,7 @@ void main() { setUp(() { tempDir = fileSystem.systemTempDirectory.createTempSync('flutter_analyze_once_test_1.').absolute; projectPath = fileSystem.path.join(tempDir.path, 'flutter_project'); + final String projectWithErrors = fileSystem.path.join(tempDir.path, 'flutter_project_errors'); fileSystem.file(fileSystem.path.join(projectPath, 'pubspec.yaml')) ..createSync(recursive: true) ..writeAsStringSync(pubspecYamlSrc); @@ -86,6 +88,9 @@ void main() { libMain = fileSystem.file(fileSystem.path.join(projectPath, 'lib', 'main.dart')) ..createSync(recursive: true) ..writeAsStringSync(mainDartSrc); + errorFile = fileSystem.file(fileSystem.path.join(projectWithErrors, 'other', 'error.dart')) + ..createSync(recursive: true) + ..writeAsStringSync(r"""import 'package:flutter/material.dart"""); }); tearDown(() { @@ -100,12 +105,63 @@ void main() { ); }); - // testWithoutContext a specific file outside the current directory - testWithoutContext('passing one file throws', () async { + testWithoutContext('passing one file works', () async { await runCommand( arguments: ['analyze', '--no-pub', libMain.path], - exitMessageContains: 'is not a directory', - exitCode: 1, + statusTextContains: ['No issues found!'] + ); + }); + + testWithoutContext('passing one file with errors are detected', () async { + await runCommand( + arguments: ['analyze', '--no-pub', errorFile.path], + statusTextContains: [ + 'Analyzing error.dart', + "error $analyzerSeparator Target of URI doesn't exist", + "error $analyzerSeparator Expected to find ';'", + 'error $analyzerSeparator Unterminated string literal' + ], + exitMessageContains: '3 issues found', + exitCode: 1 + ); + }); + + testWithoutContext('passing more than one file with errors', () async { + await runCommand( + arguments: ['analyze', '--no-pub', libMain.path, errorFile.path], + statusTextContains: [ + 'Analyzing 2 items', + "error $analyzerSeparator Target of URI doesn't exist", + "error $analyzerSeparator Expected to find ';'", + 'error $analyzerSeparator Unterminated string literal' + ], + exitMessageContains: '3 issues found', + exitCode: 1 + ); + }); + + testWithoutContext('passing more than one file success', () async { + final File secondFile = fileSystem.file(fileSystem.path.join(projectPath, 'lib', 'second.dart')) + ..createSync(recursive: true) + ..writeAsStringSync(''); + await runCommand( + arguments: ['analyze', '--no-pub', libMain.path, secondFile.path], + statusTextContains: ['No issues found!'] + ); + }); + + testWithoutContext('mixing directory and files success', () async { + await runCommand( + arguments: ['analyze', '--no-pub', libMain.path, projectPath], + statusTextContains: ['No issues found!'] + ); + }); + + testWithoutContext('file not found', () async { + await runCommand( + arguments: ['analyze', '--no-pub', 'not_found.abc'], + exitMessageContains: "not_found.abc' does not exist", + exitCode: 1 ); });