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

import 'dart:async';

import 'package:args/command_runner.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/template.dart';
import 'package:flutter_tools/src/commands/ide_config.dart';
12
import 'package:flutter_tools/src/globals.dart' as globals;
13

14 15
import '../../src/common.dart';
import '../../src/context.dart';
16 17 18

void main() {
  group('ide_config', () {
19
    Directory tempDir;
20 21 22 23
    Directory templateDir;
    Directory intellijDir;
    Directory toolsDir;

24
    Map<String, String> _getFilesystemContents([ Directory root ]) {
25
      final String tempPath = tempDir.absolute.path;
26
      final List<String> paths =
27
        (root ?? tempDir).listSync(recursive: true).map((FileSystemEntity entity) {
28
          final String relativePath = globals.fs.path.relative(entity.path, from: tempPath);
29 30
          return relativePath;
        }).toList();
31
      final Map<String, String> contents = <String, String>{};
32
      for (final String path in paths) {
33 34
        final String absPath = globals.fs.path.join(tempPath, path);
        if (globals.fs.isDirectorySync(absPath)) {
35
          contents[path] = 'dir';
36 37
        } else if (globals.fs.isFileSync(absPath)) {
          contents[path] = globals.fs.file(absPath).readAsStringSync();
38 39 40 41 42
        }
      }
      return contents;
    }

43
    Map<String, String> _getManifest(Directory base, String marker, { bool isTemplate = false }) {
44
      final String basePath = globals.fs.path.relative(base.path, from: tempDir.absolute.path);
45 46
      final String suffix = isTemplate ? Template.copyTemplateExtension : '';
      return <String, String>{
47 48 49 50 51 52 53 54
        globals.fs.path.join(basePath, '.idea'): 'dir',
        globals.fs.path.join(basePath, '.idea', 'modules.xml$suffix'): 'modules $marker',
        globals.fs.path.join(basePath, '.idea', 'vcs.xml$suffix'): 'vcs $marker',
        globals.fs.path.join(basePath, '.idea', '.name$suffix'): 'codeStyleSettings $marker',
        globals.fs.path.join(basePath, '.idea', 'runConfigurations'): 'dir',
        globals.fs.path.join(basePath, '.idea', 'runConfigurations', 'hello_world.xml$suffix'): 'hello_world $marker',
        globals.fs.path.join(basePath, 'flutter.iml$suffix'): 'flutter $marker',
        globals.fs.path.join(basePath, 'packages', 'new', 'deep.iml$suffix'): 'deep $marker',
55
        globals.fs.path.join(basePath, 'example', 'gallery', 'android.iml$suffix'): 'android $marker',
56 57 58 59
      };
    }

    void _populateDir(Map<String, String> manifest) {
60
      for (final String key in manifest.keys) {
61
        if (manifest[key] == 'dir') {
62
          tempDir.childDirectory(key).createSync(recursive: true);
63 64
        }
      }
65
      for (final String key in manifest.keys) {
66
        if (manifest[key] != 'dir') {
67
          tempDir.childFile(key)
68 69 70 71 72 73 74
            ..createSync(recursive: true)
            ..writeAsStringSync(manifest[key]);
        }
      }
    }

    bool _fileOrDirectoryExists(String path) {
75 76
      final String absPath = globals.fs.path.join(tempDir.absolute.path, path);
      return globals.fs.file(absPath).existsSync() || globals.fs.directory(absPath).existsSync();
77 78
    }

79
    Future<void> _updateIdeConfig({
80 81 82 83 84
      Directory dir,
      List<String> args = const <String>[],
      Map<String, String> expectedContents = const <String, String>{},
      List<String> unexpectedPaths = const <String>[],
    }) async {
85
      dir ??= tempDir;
86
      final IdeConfigCommand command = IdeConfigCommand();
87
      final CommandRunner<void> runner = createTestCommandRunner(command);
88 89 90 91 92
      await runner.run(<String>[
        '--flutter-root=${tempDir.absolute.path}',
        'ide-config',
        ...args,
      ]);
93

94
      for (final String path in expectedContents.keys) {
95 96
        final String absPath = globals.fs.path.join(tempDir.absolute.path, path);
        expect(_fileOrDirectoryExists(globals.fs.path.join(dir.path, path)), true,
97
            reason: "$path doesn't exist");
98 99
        if (globals.fs.file(absPath).existsSync()) {
          expect(globals.fs.file(absPath).readAsStringSync(), equals(expectedContents[path]),
100 101 102
              reason: "$path contents don't match");
        }
      }
103
      for (final String path in unexpectedPaths) {
104
        expect(_fileOrDirectoryExists(globals.fs.path.join(dir.path, path)), false, reason: '$path exists');
105 106 107 108 109 110 111 112
      }
    }

    setUpAll(() {
      Cache.disableLocking();
    });

    setUp(() {
113
      tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_tools_ide_config_test.');
114
      final Directory packagesDir = tempDir.childDirectory('packages')..createSync(recursive: true);
115 116 117 118 119 120
      toolsDir = packagesDir.childDirectory('flutter_tools')..createSync();
      templateDir = toolsDir.childDirectory('ide_templates')..createSync();
      intellijDir = templateDir.childDirectory('intellij')..createSync();
    });

    tearDown(() {
121
      tryToDelete(tempDir);
122 123 124 125 126 127 128 129 130
    });

    testUsingContext("doesn't touch existing files without --overwrite", () async {
      final Map<String, String> templateManifest = _getManifest(
        intellijDir,
        'template',
        isTemplate: true,
      );
      final Map<String, String> flutterManifest = _getManifest(
131
        tempDir,
132 133 134 135 136 137 138 139
        'existing',
      );
      _populateDir(templateManifest);
      _populateDir(flutterManifest);
      final Map<String, String> expectedContents = _getFilesystemContents();
      return _updateIdeConfig(
        expectedContents: expectedContents,
      );
140
    });
141 142 143 144 145 146 147 148

    testUsingContext('creates non-existent files', () async {
      final Map<String, String> templateManifest = _getManifest(
        intellijDir,
        'template',
        isTemplate: true,
      );
      final Map<String, String> flutterManifest = _getManifest(
149
        tempDir,
150 151 152
        'template',
      );
      _populateDir(templateManifest);
153 154 155 156
      final Map<String, String> expectedContents = <String, String>{
        ...templateManifest,
        ...flutterManifest,
      };
157 158 159
      return _updateIdeConfig(
        expectedContents: expectedContents,
      );
160
    });
161 162 163 164 165 166 167 168

    testUsingContext('overwrites existing files with --overwrite', () async {
      final Map<String, String> templateManifest = _getManifest(
        intellijDir,
        'template',
        isTemplate: true,
      );
      final Map<String, String> flutterManifest = _getManifest(
169
        tempDir,
170 171 172 173 174
        'existing',
      );
      _populateDir(templateManifest);
      _populateDir(flutterManifest);
      final Map<String, String> overwrittenManifest = _getManifest(
175
        tempDir,
176 177
        'template',
      );
178 179 180 181
      final Map<String, String> expectedContents = <String, String>{
        ...templateManifest,
        ...overwrittenManifest,
      };
182 183 184 185
      return _updateIdeConfig(
        args: <String>['--overwrite'],
        expectedContents: expectedContents,
      );
186
    });
187 188 189 190 191 192 193

    testUsingContext('only adds new templates without --overwrite', () async {
      final Map<String, String> templateManifest = _getManifest(
        intellijDir,
        'template',
        isTemplate: true,
      );
194
      final String flutterIml = globals.fs.path.join(
195 196 197 198 199 200 201 202 203 204
        'packages',
        'flutter_tools',
        'ide_templates',
        'intellij',
        'flutter.iml${Template.copyTemplateExtension}',
      );
      templateManifest.remove(flutterIml);
      _populateDir(templateManifest);
      templateManifest[flutterIml] = 'flutter existing';
      final Map<String, String> flutterManifest = _getManifest(
205
        tempDir,
206 207 208
        'existing',
      );
      _populateDir(flutterManifest);
209 210 211 212
      final Map<String, String> expectedContents = <String, String>{
        ...flutterManifest,
        ...templateManifest,
      };
213 214 215 216
      return _updateIdeConfig(
        args: <String>['--update-templates'],
        expectedContents: expectedContents,
      );
217
    });
218 219 220 221 222 223 224 225 226

    testUsingContext('update all templates with --overwrite', () async {
      final Map<String, String> templateManifest = _getManifest(
        intellijDir,
        'template',
        isTemplate: true,
      );
      _populateDir(templateManifest);
      final Map<String, String> flutterManifest = _getManifest(
227
        tempDir,
228 229 230 231 232 233 234 235
        'existing',
      );
      _populateDir(flutterManifest);
      final Map<String, String> updatedTemplates = _getManifest(
        intellijDir,
        'existing',
        isTemplate: true,
      );
236 237 238 239
      final Map<String, String> expectedContents = <String, String>{
        ...flutterManifest,
        ...updatedTemplates,
      };
240 241 242 243
      return _updateIdeConfig(
        args: <String>['--update-templates', '--overwrite'],
        expectedContents: expectedContents,
      );
244
    });
245

246
    testUsingContext('removes deleted imls with --overwrite', () async {
247 248 249 250 251 252 253
      final Map<String, String> templateManifest = _getManifest(
        intellijDir,
        'template',
        isTemplate: true,
      );
      _populateDir(templateManifest);
      final Map<String, String> flutterManifest = _getManifest(
254
        tempDir,
255 256 257 258 259 260 261 262 263
        'existing',
      );
      flutterManifest.remove('flutter.iml');
      _populateDir(flutterManifest);
      final Map<String, String> updatedTemplates = _getManifest(
        intellijDir,
        'existing',
        isTemplate: true,
      );
264
      final String flutterIml = globals.fs.path.join(
265 266 267 268 269 270 271
        'packages',
        'flutter_tools',
        'ide_templates',
        'intellij',
        'flutter.iml${Template.copyTemplateExtension}',
      );
      updatedTemplates.remove(flutterIml);
272 273 274 275
      final Map<String, String> expectedContents = <String, String>{
        ...flutterManifest,
        ...updatedTemplates,
      };
276 277 278 279
      return _updateIdeConfig(
        args: <String>['--update-templates', '--overwrite'],
        expectedContents: expectedContents,
      );
280
    });
281

282
    testUsingContext('removes deleted imls with --overwrite, including empty parent dirs', () async {
283 284 285 286 287 288 289
      final Map<String, String> templateManifest = _getManifest(
        intellijDir,
        'template',
        isTemplate: true,
      );
      _populateDir(templateManifest);
      final Map<String, String> flutterManifest = _getManifest(
290
        tempDir,
291 292
        'existing',
      );
293
      flutterManifest.remove(globals.fs.path.join('packages', 'new', 'deep.iml'));
294 295 296 297 298 299
      _populateDir(flutterManifest);
      final Map<String, String> updatedTemplates = _getManifest(
        intellijDir,
        'existing',
        isTemplate: true,
      );
300
      String deepIml = globals.fs.path.join(
301 302 303 304 305 306
        'packages',
        'flutter_tools',
        'ide_templates',
        'intellij');
      // Remove the all the dir entries too.
      updatedTemplates.remove(deepIml);
307
      deepIml = globals.fs.path.join(deepIml, 'packages');
308
      updatedTemplates.remove(deepIml);
309
      deepIml = globals.fs.path.join(deepIml, 'new');
310
      updatedTemplates.remove(deepIml);
311
      deepIml = globals.fs.path.join(deepIml, 'deep.iml');
312
      updatedTemplates.remove(deepIml);
313 314 315 316
      final Map<String, String> expectedContents = <String, String>{
        ...flutterManifest,
        ...updatedTemplates,
      };
317 318 319 320
      return _updateIdeConfig(
        args: <String>['--update-templates', '--overwrite'],
        expectedContents: expectedContents,
      );
321
    });
322 323 324

  });
}