intellij_test.dart 7.11 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
Ian Hickson's avatar
Ian Hickson committed
4

5
import 'dart:convert';
6

7
import 'package:archive/archive.dart';
8 9
import 'package:file/file.dart';
import 'package:file/memory.dart';
10 11 12 13
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/doctor.dart';
import 'package:flutter_tools/src/intellij/intellij.dart';

14
import '../../src/common.dart';
15 16

void main() {
17
  FileSystem fileSystem;
18

19
  void writeFileCreatingDirectories(String path, List<int> bytes) {
20
    final File file = fileSystem.file(path);
21 22 23
    file.parent.createSync(recursive: true);
    file.writeAsBytesSync(bytes);
  }
24

25
  setUp(() {
26
    fileSystem = MemoryFileSystem.test();
27
  });
28

29 30
  testWithoutContext('IntelliJPlugins found', () async {
    final IntelliJPlugins plugins = IntelliJPlugins(_kPluginsPath, fileSystem: fileSystem);
31

32 33
    final Archive dartJarArchive =
        buildSingleFileArchive('META-INF/plugin.xml', r'''
34
<idea-plugin version="2">
35 36
<name>Dart</name>
<version>162.2485</version>
37 38
</idea-plugin>
''');
39 40 41 42
    writeFileCreatingDirectories(
      fileSystem.path.join(_kPluginsPath, 'Dart', 'lib', 'Dart.jar'),
      ZipEncoder().encode(dartJarArchive),
    );
43

44
    final Archive flutterJarArchive = buildSingleFileArchive('META-INF/plugin.xml', r'''
45
<idea-plugin version="2">
46 47
<name>Flutter</name>
<version>0.1.3</version>
48 49
</idea-plugin>
''');
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    writeFileCreatingDirectories(
      fileSystem.path.join(_kPluginsPath, 'flutter-intellij.jar'),
      ZipEncoder().encode(flutterJarArchive),
    );

    final List<ValidationMessage> messages = <ValidationMessage>[];
    plugins.validatePackage(messages, <String>['Dart'], 'Dart', 'download-Dart');
    plugins.validatePackage(messages,
      <String>['flutter-intellij', 'flutter-intellij.jar'], 'Flutter', 'download-Flutter',
      minVersion: IntelliJPlugins.kMinFlutterPluginVersion,
    );

    ValidationMessage message = messages
        .firstWhere((ValidationMessage m) => m.message.startsWith('Dart '));
    expect(message.message, 'Dart plugin version 162.2485');

    message = messages.firstWhere(
        (ValidationMessage m) => m.message.startsWith('Flutter '));
    expect(message.message, contains('Flutter plugin version 0.1.3'));
    expect(message.message, contains('recommended minimum version'));
  });

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 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
  testWithoutContext('IntelliJPlugins can read the package version of the flutter-intellij 50.0+/IntelliJ 2020.2+ layout', () async {
    final IntelliJPlugins plugins = IntelliJPlugins(_kPluginsPath, fileSystem: fileSystem);

    final Archive flutterIdeaJarArchive = buildSingleFileArchive('META-INF/plugin.xml', r'''
<idea-plugin version="2">
<name>Flutter</name>
<version>50.0</version>
</idea-plugin>
''');
    writeFileCreatingDirectories(
      fileSystem.path.join(_kPluginsPath, 'flutter-intellij', 'lib', 'flutter-idea-50.0.jar'),
      ZipEncoder().encode(flutterIdeaJarArchive),
    );
    final Archive flutterIntellijJarArchive = buildSingleFileArchive('META-INF/MANIFEST.MF', r'''
Manifest-Version: 1.0
''');
    writeFileCreatingDirectories(
      fileSystem.path.join(_kPluginsPath, 'flutter-intellij', 'lib', 'flutter-intellij-50.0.jar'),
      ZipEncoder().encode(flutterIntellijJarArchive),
    );

    final List<ValidationMessage> messages = <ValidationMessage>[];
    plugins.validatePackage(messages,
      <String>[
        'flutter-intellij',
        'flutter-intellij.jar',
      ],
      'Flutter', 'download-Flutter',
      minVersion: IntelliJPlugins.kMinFlutterPluginVersion,
    );

    final ValidationMessage message = messages.firstWhere(
            (ValidationMessage m) => m.message.startsWith('Flutter '));
    expect(message.message, contains('Flutter plugin version 50.0'));
  });

  testWithoutContext('IntelliJPlugins can read the package version of the flutter-intellij 50.0+/IntelliJ 2020.2+ layout(priority is given to packages with the same prefix as packageName)', () async {
    final IntelliJPlugins plugins = IntelliJPlugins(_kPluginsPath, fileSystem: fileSystem);

    final Archive flutterIdeaJarArchive = buildSingleFileArchive('META-INF/plugin.xml', r'''
<idea-plugin version="2">
<name>Flutter</name>
<version>50.0</version>
</idea-plugin>
''');
    writeFileCreatingDirectories(
      fileSystem.path.join(_kPluginsPath, 'flutter-intellij', 'lib', 'flutter-idea-50.0.jar'),
      ZipEncoder().encode(flutterIdeaJarArchive),
    );
    final Archive flutterIntellijJarArchive = buildSingleFileArchive('META-INF/plugin.xml', r'''
<idea-plugin version="2">
<name>Flutter</name>
<version>51.0</version>
</idea-plugin>
''');
    writeFileCreatingDirectories(
      fileSystem.path.join(_kPluginsPath, 'flutter-intellij', 'lib', 'flutter-intellij-50.0.jar'),
      ZipEncoder().encode(flutterIntellijJarArchive),
    );

    final List<ValidationMessage> messages = <ValidationMessage>[];
    plugins.validatePackage(messages,
      <String>[
        'flutter-intellij',
        'flutter-intellij.jar',
      ],
      'Flutter', 'download-Flutter',
      minVersion: IntelliJPlugins.kMinFlutterPluginVersion,
    );

    final ValidationMessage message = messages.firstWhere(
            (ValidationMessage m) => m.message.startsWith('Flutter '));
    expect(message.message, contains('Flutter plugin version 51.0'));
  });

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  testWithoutContext('IntelliJPlugins not found displays a link to their download site', () async {
    final IntelliJPlugins plugins = IntelliJPlugins(_kPluginsPath, fileSystem: fileSystem);

    final List<ValidationMessage> messages = <ValidationMessage>[];
    plugins.validatePackage(messages, <String>['Dart'], 'Dart', 'download-Dart');
    plugins.validatePackage(messages,
      <String>['flutter-intellij', 'flutter-intellij.jar'], 'Flutter', 'download-Flutter',
      minVersion: IntelliJPlugins.kMinFlutterPluginVersion,
    );

    ValidationMessage message = messages
        .firstWhere((ValidationMessage m) => m.message.startsWith('Dart '));
    expect(message.message, contains('Dart plugin can be installed from'));
    expect(message.contextUrl, isNotNull);

    message = messages.firstWhere(
        (ValidationMessage m) => m.message.startsWith('Flutter '));
    expect(message.message, contains('Flutter plugin can be installed from'));
    expect(message.contextUrl, isNotNull);
166
  });
167 168 169 170 171

  testWithoutContext('IntelliJPlugins does not crash if no plugin file found', () async {
    final IntelliJPlugins plugins = IntelliJPlugins(_kPluginsPath, fileSystem: fileSystem);

    final Archive dartJarArchive =
172 173
    buildSingleFileArchive('META-INF/MANIFEST.MF', r'''
Manifest-Version: 1.0
174 175 176 177 178 179 180 181 182 183 184
''');
    writeFileCreatingDirectories(
      fileSystem.path.join(_kPluginsPath, 'Dart', 'lib', 'Other.jar'),
      ZipEncoder().encode(dartJarArchive),
    );

    expect(
      () => plugins.validatePackage(<ValidationMessage>[], <String>['Dart'], 'Dart', 'download-Dart'),
      returnsNormally,
    );
  });
185
}
186

187
const String _kPluginsPath = '/data/intellij/plugins';
188

189 190
Archive buildSingleFileArchive(String path, String content) {
  final Archive archive = Archive();
191

192 193
  final List<int> bytes = utf8.encode(content);
  archive.addFile(ArchiveFile(path, bytes.length, bytes));
194

195
  return archive;
196
}