analyze_suggestions_integration_test.dart 8.29 KB
Newer Older
1 2 3 4
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:convert';

7 8
import 'package:args/command_runner.dart';
import 'package:flutter_tools/src/base/file_system.dart';
9
import 'package:flutter_tools/src/base/io.dart';
10
import 'package:flutter_tools/src/base/logger.dart';
11
import 'package:flutter_tools/src/base/platform.dart';
12
import 'package:flutter_tools/src/commands/analyze.dart';
13
import 'package:flutter_tools/src/globals.dart' as globals;
14
import 'package:flutter_tools/src/project.dart';
15 16
import 'package:flutter_tools/src/project_validator.dart';

17
import '../src/common.dart';
18 19
import '../src/context.dart';
import '../src/test_flutter_command_runner.dart';
20
import 'test_utils.dart';
21 22

void main() {
23
  late FileSystem fileSystem;
24

25
  group('analyze --suggestions command integration', () {
26 27 28 29 30 31 32

    setUp(() {
      fileSystem = globals.localFileSystem;
    });

    testUsingContext('General Info Project Validator', () async {
      final BufferLogger loggerTest = BufferLogger.test();
33 34
      final AnalyzeCommand command = AnalyzeCommand(
          artifacts: globals.artifacts!,
35 36
          fileSystem: fileSystem,
          logger: loggerTest,
37 38 39 40
          platform: globals.platform,
          terminal: globals.terminal,
          processManager: globals.processManager,
          allProjectValidators: <ProjectValidator>[GeneralInfoProjectValidator()],
41
          suppressAnalytics: true,
42 43 44
      );
      final CommandRunner<void> runner = createTestCommandRunner(command);

45 46 47 48 49 50 51
      await runner.run(<String>[
        'analyze',
        '--no-pub',
        '--no-current-package',
        '--suggestions',
        '../../dev/integration_tests/flutter_gallery',
      ]);
52 53

      const String expected = '\n'
54 55 56 57 58 59 60
      '┌───────────────────────────────────────────────────────────────────┐\n'
      '│ General Info                                                      │\n'
      '│ [✓] App Name: flutter_gallery                                     │\n'
      '│ [✓] Supported Platforms: android, ios, web, macos, linux, windows │\n'
      '│ [✓] Is Flutter Package: yes                                       │\n'
      '│ [✓] Uses Material Design: yes                                     │\n'
      '│ [✓] Is Plugin: no                                                 │\n'
61
      '│ [✓] Java/Gradle/Android Gradle Plugin: ${AndroidProject.validJavaGradleAgpString}\n'
62
      '└───────────────────────────────────────────────────────────────────┘\n';
63 64 65

      expect(loggerTest.statusText, contains(expected));
    });
66 67 68 69 70 71 72 73 74 75 76 77 78

    testUsingContext('PubDependenciesProjectValidator success ', () async {
      final BufferLogger loggerTest = BufferLogger.test();
      final AnalyzeCommand command = AnalyzeCommand(
        artifacts: globals.artifacts!,
        fileSystem: fileSystem,
        logger: loggerTest,
        platform: globals.platform,
        terminal: globals.terminal,
        processManager: globals.processManager,
        allProjectValidators: <ProjectValidator>[
          PubDependenciesProjectValidator(globals.processManager),
        ],
79
        suppressAnalytics: true,
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
      );
      final CommandRunner<void> runner = createTestCommandRunner(command);

      await runner.run(<String>[
        'analyze',
        '--no-pub',
        '--no-current-package',
        '--suggestions',
        '../../dev/integration_tests/flutter_gallery',
      ]);

      const String expected = '\n'
        '┌────────────────────────────────────────────────────────────────────────────────────┐\n'
        '│ Pub dependencies                                                                   │\n'
        '│ [✓] Dart dependencies: All pub dependencies are hosted on https://pub.dartlang.org │\n'
        '└────────────────────────────────────────────────────────────────────────────────────┘\n';
      expect(loggerTest.statusText, contains(expected));
    });
98
  });
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

  group('analyze --suggestions --machine command integration', () {
    late Directory tempDir;
    late Platform platform;

    setUpAll(() async {
      platform = const LocalPlatform();
      tempDir = createResolvedTempDirectorySync('run_test.');
      await globals.processManager.run(<String>['flutter', 'create', 'test_project'], workingDirectory: tempDir.path);
    });

    tearDown(() async {
      tryToDelete(tempDir);
    });

Lioness100's avatar
Lioness100 committed
114
    testUsingContext('analyze --suggestions --machine produces expected values', () async {
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
      final ProcessResult result = await globals.processManager.run(<String>['flutter', 'analyze', '--suggestions', '--machine'], workingDirectory: tempDir.childDirectory('test_project').path);

      expect(result.stdout is String, true);
      expect((result.stdout as String).startsWith('{\n'), true);
      expect(result.stdout, isNot(contains(',\n}'))); // No trailing commas allowed in JSON
      expect((result.stdout as String).endsWith('}\n'), true);

      final Map<String, dynamic> decoded = jsonDecode(result.stdout as String) as Map<String, dynamic>;

      expect(decoded.containsKey('FlutterProject.android.exists'), true);
      expect(decoded.containsKey('FlutterProject.ios.exists'), true);
      expect(decoded.containsKey('FlutterProject.web.exists'), true);
      expect(decoded.containsKey('FlutterProject.macos.exists'), true);
      expect(decoded.containsKey('FlutterProject.linux.exists'), true);
      expect(decoded.containsKey('FlutterProject.windows.exists'), true);
      expect(decoded.containsKey('FlutterProject.fuchsia.exists'), true);
      expect(decoded.containsKey('FlutterProject.android.isKotlin'), true);
      expect(decoded.containsKey('FlutterProject.ios.isSwift'), true);
      expect(decoded.containsKey('FlutterProject.isModule'), true);
      expect(decoded.containsKey('FlutterProject.isPlugin'), true);
      expect(decoded.containsKey('FlutterProject.manifest.appname'), true);
      expect(decoded.containsKey('FlutterVersion.frameworkRevision'), true);

      expect(decoded.containsKey('FlutterProject.directory'), true);
      expect(decoded.containsKey('FlutterProject.metadataFile'), true);
      expect(decoded.containsKey('Platform.operatingSystem'), true);
      expect(decoded.containsKey('Platform.isAndroid'), true);
      expect(decoded.containsKey('Platform.isIOS'), true);
      expect(decoded.containsKey('Platform.isWindows'), true);
      expect(decoded.containsKey('Platform.isMacOS'), true);
      expect(decoded.containsKey('Platform.isFuchsia'), true);
      expect(decoded.containsKey('Platform.pathSeparator'), true);
      expect(decoded.containsKey('Cache.flutterRoot'), true);

      expect(decoded['FlutterProject.android.exists'], true);
      expect(decoded['FlutterProject.ios.exists'], true);
      expect(decoded['FlutterProject.web.exists'], true);
      expect(decoded['FlutterProject.macos.exists'], true);
      expect(decoded['FlutterProject.linux.exists'], true);
      expect(decoded['FlutterProject.windows.exists'], true);
      expect(decoded['FlutterProject.fuchsia.exists'], false);
      expect(decoded['FlutterProject.android.isKotlin'], true);
      expect(decoded['FlutterProject.ios.isSwift'], true);
      expect(decoded['FlutterProject.isModule'], false);
      expect(decoded['FlutterProject.isPlugin'], false);
      expect(decoded['FlutterProject.manifest.appname'], 'test_project');

      expect(decoded['Platform.isAndroid'], false);
      expect(decoded['Platform.isIOS'], false);
      expect(decoded['Platform.isWindows'], platform.isWindows);
      expect(decoded['Platform.isMacOS'], platform.isMacOS);
      expect(decoded['Platform.isFuchsia'], platform.isFuchsia);
      expect(decoded['Platform.pathSeparator'], platform.pathSeparator);
    }, overrides: <Type, Generator>{});
  });
170
}