devfs_test.dart 21.7 KB
Newer Older
1 2 3 4
// Copyright 2016 The Chromium 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 7
import 'dart:async';
import 'dart:convert';

8 9
import 'package:file/file.dart';
import 'package:file/memory.dart';
10
import 'package:flutter_tools/src/asset.dart';
11
import 'package:flutter_tools/src/base/io.dart';
12
import 'package:flutter_tools/src/base/file_system.dart';
13
import 'package:flutter_tools/src/build_info.dart';
14
import 'package:flutter_tools/src/devfs.dart';
15
import 'package:flutter_tools/src/vmservice.dart';
16
import 'package:json_rpc_2/json_rpc_2.dart' as rpc;
17

18
import 'src/common.dart';
19 20 21 22
import 'src/context.dart';
import 'src/mocks.dart';

void main() {
23 24 25
  FileSystem fs;
  String filePath;
  String filePath2;
26 27 28
  Directory tempDir;
  String basePath;
  DevFS devFS;
29
  final AssetBundle assetBundle = AssetBundleFactory.defaultInstance.createBundle();
30

31
  setUpAll(() {
32
    fs = MemoryFileSystem();
33 34 35 36
    filePath = fs.path.join('lib', 'foo.txt');
    filePath2 = fs.path.join('foo', 'bar.txt');
  });

37 38
  group('DevFSContent', () {
    test('bytes', () {
39
      final DevFSByteContent content = DevFSByteContent(<int>[4, 5, 6]);
40 41 42 43 44 45 46 47 48
      expect(content.bytes, orderedEquals(<int>[4, 5, 6]));
      expect(content.isModified, isTrue);
      expect(content.isModified, isFalse);
      content.bytes = <int>[7, 8, 9, 2];
      expect(content.bytes, orderedEquals(<int>[7, 8, 9, 2]));
      expect(content.isModified, isTrue);
      expect(content.isModified, isFalse);
    });
    test('string', () {
49
      final DevFSStringContent content = DevFSStringContent('some string');
50
      expect(content.string, 'some string');
51
      expect(content.bytes, orderedEquals(utf8.encode('some string')));
52 53 54 55
      expect(content.isModified, isTrue);
      expect(content.isModified, isFalse);
      content.string = 'another string';
      expect(content.string, 'another string');
56
      expect(content.bytes, orderedEquals(utf8.encode('another string')));
57 58
      expect(content.isModified, isTrue);
      expect(content.isModified, isFalse);
59
      content.bytes = utf8.encode('foo bar');
60
      expect(content.string, 'foo bar');
61
      expect(content.bytes, orderedEquals(utf8.encode('foo bar')));
62 63 64
      expect(content.isModified, isTrue);
      expect(content.isModified, isFalse);
    });
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    testUsingContext('file', () async {
      final File file = fs.file(filePath);
      final DevFSFileContent content = DevFSFileContent(file);
      expect(content.isModified, isFalse);
      expect(content.isModified, isFalse);

      file.parent.createSync(recursive: true);
      file.writeAsBytesSync(<int>[1, 2, 3]);

      final DateTime fiveSecondsAgo = DateTime.now().subtract(Duration(seconds:5));
      expect(content.isModifiedAfter(fiveSecondsAgo), isTrue);
      expect(content.isModifiedAfter(fiveSecondsAgo), isTrue);
      expect(content.isModifiedAfter(null), isTrue);

      file.writeAsBytesSync(<int>[2, 3, 4]);
      expect(content.fileDependencies, <String>[filePath]);
      expect(content.isModified, isTrue);
      expect(content.isModified, isFalse);
      expect(await content.contentsAsBytes(), <int>[2, 3, 4]);
      updateFileModificationTime(file.path, fiveSecondsAgo, 0);
      expect(content.isModified, isFalse);
      expect(content.isModified, isFalse);

      file.deleteSync();
      expect(content.isModified, isTrue);
      expect(content.isModified, isFalse);
      expect(content.isModified, isFalse);
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
    });
95 96 97
  });

  group('devfs local', () {
98 99
    final MockDevFSOperations devFSOperations = MockDevFSOperations();
    final MockResidentCompiler residentCompiler = MockResidentCompiler();
100 101

    setUpAll(() {
102
      tempDir = _newTempDir(fs);
103
      basePath = tempDir.path;
104 105 106 107 108
    });
    tearDownAll(_cleanupTempDirs);

    testUsingContext('create dev file system', () async {
      // simulate workspace
109
      final File file = fs.file(fs.path.join(basePath, filePath));
110 111
      await file.parent.create(recursive: true);
      file.writeAsBytesSync(<int>[1, 2, 3]);
112
      _packages['my_project'] = fs.path.toUri('lib');
113 114

      // simulate package
115
      await _createPackage(fs, 'somepkg', 'somefile.txt');
116

117
      devFS = DevFS.operations(devFSOperations, 'test', tempDir);
118
      await devFS.create();
119 120 121
      devFSOperations.expectMessages(<String>['create test']);
      expect(devFS.assetPathsToEvict, isEmpty);

122
      int bytes = await devFS.update(
123 124 125
        mainPath: 'lib/foo.txt',
        generator: residentCompiler,
        pathToReload: 'lib/foo.txt.dill',
126
        trackWidgetCreation: false,
127
      );
128
      devFSOperations.expectMessages(<String>[
129 130 131 132 133 134 135 136 137 138 139 140
        'writeFile test lib/foo.txt.dill build/app.dill',
      ]);
      expect(devFS.assetPathsToEvict, isEmpty);

      bytes = await devFS.update(
        mainPath: 'lib/foo.txt',
        generator: residentCompiler,
        pathToReload: 'lib/foo.txt.dill',
        trackWidgetCreation: true,
      );
      devFSOperations.expectMessages(<String>[
        'writeFile test lib/foo.txt.dill build/app.dill.track.dill',
141 142
      ]);
      expect(devFS.assetPathsToEvict, isEmpty);
143

144
      expect(bytes, 22);
145 146
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
147
    });
148

149
    testUsingContext('add new file to local file system', () async {
150
      final File file = fs.file(fs.path.join(basePath, filePath2));
151 152
      await file.parent.create(recursive: true);
      file.writeAsBytesSync(<int>[1, 2, 3, 4, 5, 6, 7]);
153 154 155 156
      final int bytes = await devFS.update(
        mainPath: 'lib/foo.txt',
        generator: residentCompiler,
        pathToReload: 'lib/foo.txt.dill',
157
        trackWidgetCreation: false,
158
      );
159
      devFSOperations.expectMessages(<String>[
160
        'writeFile test lib/foo.txt.dill build/app.dill',
161 162
      ]);
      expect(devFS.assetPathsToEvict, isEmpty);
163
      expect(bytes, 22);
164 165 166 167
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
    });

168 169 170 171 172
    testUsingContext('modify existing file on local file system', () async {
      int bytes = await devFS.update(
        mainPath: 'lib/foo.txt',
        generator: residentCompiler,
        pathToReload: 'lib/foo.txt.dill',
173
        trackWidgetCreation: false,
174
      );
175
      devFSOperations.expectMessages(<String>[
176
        'writeFile test lib/foo.txt.dill build/app.dill',
177 178
      ]);
      expect(devFS.assetPathsToEvict, isEmpty);
179
      expect(bytes, 22);
180

181
      final File file = fs.file(fs.path.join(basePath, filePath));
182
      // Set the last modified time to 5 seconds in the past.
183
      updateFileModificationTime(file.path, DateTime.now(), -5);
184 185 186 187
      bytes = await devFS.update(
        mainPath: 'lib/foo.txt',
        generator: residentCompiler,
        pathToReload: 'lib/foo.txt.dill',
188
        trackWidgetCreation: false,
189 190
      );
      devFSOperations.expectMessages(<String>[
191
        'writeFile test lib/foo.txt.dill build/app.dill',
192
      ]);
193
      expect(devFS.assetPathsToEvict, isEmpty);
194
      expect(bytes, 22);
195

196
      await file.writeAsBytes(<int>[1, 2, 3, 4, 5, 6]);
197 198 199 200
      bytes = await devFS.update(
        mainPath: 'lib/foo.txt',
        generator: residentCompiler,
        pathToReload: 'lib/foo.txt.dill',
201
        trackWidgetCreation: false,
202
      );
203
      devFSOperations.expectMessages(<String>[
204
        'writeFile test lib/foo.txt.dill build/app.dill',
205 206
      ]);
      expect(devFS.assetPathsToEvict, isEmpty);
207
      expect(bytes, 22);
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

      // Set the last modified time to 5 seconds in the past.
      updateFileModificationTime(file.path, DateTime.now(), -5);
      bytes = await devFS.update(
        mainPath: 'lib/foo.txt',
        generator: residentCompiler,
        pathToReload: 'lib/foo.txt.dill',
        trackWidgetCreation: true,
      );
      devFSOperations.expectMessages(<String>[
        'writeFile test lib/foo.txt.dill build/app.dill.track.dill',
      ]);
      expect(devFS.assetPathsToEvict, isEmpty);
      expect(bytes, 22);

      await file.writeAsBytes(<int>[1, 2, 3, 4, 5, 6]);
      bytes = await devFS.update(
        mainPath: 'lib/foo.txt',
        generator: residentCompiler,
        pathToReload: 'lib/foo.txt.dill',
        trackWidgetCreation: true,
      );
      devFSOperations.expectMessages(<String>[
        'writeFile test lib/foo.txt.dill build/app.dill.track.dill',
      ]);
      expect(devFS.assetPathsToEvict, isEmpty);
      expect(bytes, 22);

236 237
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
238
    });
239

240
    testUsingContext('delete a file from the local file system', () async {
241
      final File file = fs.file(fs.path.join(basePath, filePath));
242
      await file.delete();
243 244 245 246
      final int bytes = await devFS.update(
        mainPath: 'lib/foo.txt',
        generator: residentCompiler,
        pathToReload: 'lib/foo.txt.dill',
247
        trackWidgetCreation: false,
248
      );
249
      devFSOperations.expectMessages(<String>[
250
        'deleteFile test lib/foo.txt',
251
        'writeFile test lib/foo.txt.dill build/app.dill',
252 253
      ]);
      expect(devFS.assetPathsToEvict, isEmpty);
254
      expect(bytes, 22);
255 256
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
257
    });
258

259
    testUsingContext('add new package', () async {
260
      await _createPackage(fs, 'newpkg', 'anotherfile.txt');
261
      int bytes = await devFS.update(
262 263 264
        mainPath: 'lib/foo.txt',
        generator: residentCompiler,
        pathToReload: 'lib/foo.txt.dill',
265
        trackWidgetCreation: false,
266
      );
267
      devFSOperations.expectMessages(<String>[
268 269 270 271 272 273 274 275 276 277 278 279 280
        'writeFile test lib/foo.txt.dill build/app.dill',
      ]);
      expect(devFS.assetPathsToEvict, isEmpty);
      expect(bytes, 22);

      bytes = await devFS.update(
        mainPath: 'lib/foo.txt',
        generator: residentCompiler,
        pathToReload: 'lib/foo.txt.dill',
        trackWidgetCreation: true,
      );
      devFSOperations.expectMessages(<String>[
        'writeFile test lib/foo.txt.dill build/app.dill.track.dill',
281 282
      ]);
      expect(devFS.assetPathsToEvict, isEmpty);
283
      expect(bytes, 22);
284

285 286
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
287
    });
288

289
    testUsingContext('add new package with double slashes in URI', () async {
290
      const String packageName = 'doubleslashpkg';
291
      await _createPackage(fs, packageName, 'somefile.txt', doubleSlash: true);
292

293
      final Set<String> fileFilter = Set<String>();
294
      final List<Uri> pkgUris = <Uri>[fs.path.toUri(basePath)]..addAll(_packages.values);
295 296 297 298 299 300
      for (Uri pkgUri in pkgUris) {
        if (!pkgUri.isAbsolute) {
          pkgUri = fs.path.toUri(fs.path.join(basePath, pkgUri.path));
        }
        fileFilter.addAll(fs.directory(pkgUri)
            .listSync(recursive: true)
301 302
            .whereType<File>()
            .map<String>((File file) => canonicalizePath(file.path))
303 304
            .toList());
      }
305 306 307 308 309
      final int bytes = await devFS.update(
        mainPath: 'lib/foo.txt',
        fileFilter: fileFilter,
        generator: residentCompiler,
        pathToReload: 'lib/foo.txt.dill',
310
        trackWidgetCreation: false,
311
      );
312
      devFSOperations.expectMessages(<String>[
313
        'writeFile test lib/foo.txt.dill build/app.dill',
314 315
      ]);
      expect(devFS.assetPathsToEvict, isEmpty);
316
      expect(bytes, 22);
317 318
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
319
    });
320

321
    testUsingContext('add an asset bundle', () async {
322
      assetBundle.entries['a.txt'] = DevFSStringContent('abc');
323 324 325 326 327 328
      final int bytes = await devFS.update(
        mainPath: 'lib/foo.txt',
        bundle: assetBundle,
        bundleDirty: true,
        generator: residentCompiler,
        pathToReload: 'lib/foo.txt.dill',
329
        trackWidgetCreation: false,
330
      );
331
      devFSOperations.expectMessages(<String>[
332
        'writeFile test ${_inAssetBuildDirectory(fs, 'a.txt')}',
333
        'writeFile test lib/foo.txt.dill build/app.dill',
334 335 336
      ]);
      expect(devFS.assetPathsToEvict, unorderedMatches(<String>['a.txt']));
      devFS.assetPathsToEvict.clear();
337
      expect(bytes, 25);
338 339
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
340
    });
341

342
    testUsingContext('add a file to the asset bundle - bundleDirty', () async {
343
      assetBundle.entries['b.txt'] = DevFSStringContent('abcd');
344 345 346 347 348 349
      final int bytes = await devFS.update(
        mainPath: 'lib/foo.txt',
        bundle: assetBundle,
        bundleDirty: true,
        generator: residentCompiler,
        pathToReload: 'lib/foo.txt.dill',
350
        trackWidgetCreation: false,
351
      );
352 353
      // Expect entire asset bundle written because bundleDirty is true
      devFSOperations.expectMessages(<String>[
354 355
        'writeFile test ${_inAssetBuildDirectory(fs, 'a.txt')}',
        'writeFile test ${_inAssetBuildDirectory(fs, 'b.txt')}',
356
        'writeFile test lib/foo.txt.dill build/app.dill',
357 358 359 360
      ]);
      expect(devFS.assetPathsToEvict, unorderedMatches(<String>[
        'a.txt', 'b.txt']));
      devFS.assetPathsToEvict.clear();
361
      expect(bytes, 29);
362 363
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
364
    });
365

366
    testUsingContext('add a file to the asset bundle', () async {
367
      assetBundle.entries['c.txt'] = DevFSStringContent('12');
368 369 370 371 372
      final int bytes = await devFS.update(
        mainPath: 'lib/foo.txt',
        bundle: assetBundle,
        generator: residentCompiler,
        pathToReload: 'lib/foo.txt.dill',
373
        trackWidgetCreation: false,
374
      );
375
      devFSOperations.expectMessages(<String>[
376
        'writeFile test ${_inAssetBuildDirectory(fs, 'c.txt')}',
377
        'writeFile test lib/foo.txt.dill build/app.dill',
378 379 380 381
      ]);
      expect(devFS.assetPathsToEvict, unorderedMatches(<String>[
        'c.txt']));
      devFS.assetPathsToEvict.clear();
382
      expect(bytes, 24);
383 384
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
385
    });
386

387
    testUsingContext('delete a file from the asset bundle', () async {
388
      assetBundle.entries.remove('c.txt');
389 390 391 392 393
      final int bytes = await devFS.update(
        mainPath: 'lib/foo.txt',
        bundle: assetBundle,
        generator: residentCompiler,
        pathToReload: 'lib/foo.txt.dill',
394
        trackWidgetCreation: false,
395
      );
396
      devFSOperations.expectMessages(<String>[
397
        'deleteFile test ${_inAssetBuildDirectory(fs, 'c.txt')}',
398
        'writeFile test lib/foo.txt.dill build/app.dill',
399 400 401
      ]);
      expect(devFS.assetPathsToEvict, unorderedMatches(<String>['c.txt']));
      devFS.assetPathsToEvict.clear();
402
      expect(bytes, 22);
403 404
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
405
    });
406

407
    testUsingContext('delete all files from the asset bundle', () async {
408
      assetBundle.entries.clear();
409 410 411 412 413 414
      final int bytes = await devFS.update(
        mainPath: 'lib/foo.txt',
        bundle: assetBundle,
        bundleDirty: true,
        generator: residentCompiler,
        pathToReload: 'lib/foo.txt.dill',
415
        trackWidgetCreation: false,
416
      );
417
      devFSOperations.expectMessages(<String>[
418 419
        'deleteFile test ${_inAssetBuildDirectory(fs, 'a.txt')}',
        'deleteFile test ${_inAssetBuildDirectory(fs, 'b.txt')}',
420
        'writeFile test lib/foo.txt.dill build/app.dill',
421 422 423 424 425
      ]);
      expect(devFS.assetPathsToEvict, unorderedMatches(<String>[
        'a.txt', 'b.txt'
      ]));
      devFS.assetPathsToEvict.clear();
426
      expect(bytes, 22);
427 428
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
429
    });
430

431 432
    testUsingContext('delete dev file system', () async {
      await devFS.destroy();
433 434
      devFSOperations.expectMessages(<String>['destroy test']);
      expect(devFS.assetPathsToEvict, isEmpty);
435 436
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
437
    });
438
  });
439 440 441

  group('devfs remote', () {
    MockVMService vmService;
442
    final MockResidentCompiler residentCompiler = MockResidentCompiler();
443 444

    setUpAll(() async {
445
      tempDir = _newTempDir(fs);
446
      basePath = tempDir.path;
447
      vmService = MockVMService();
448 449 450 451 452
      await vmService.setUp();
    });
    tearDownAll(() async {
      await vmService.tearDown();
      _cleanupTempDirs();
453
    });
454 455 456

    testUsingContext('create dev file system', () async {
      // simulate workspace
457
      final File file = fs.file(fs.path.join(basePath, filePath));
458 459 460 461
      await file.parent.create(recursive: true);
      file.writeAsBytesSync(<int>[1, 2, 3]);

      // simulate package
462
      await _createPackage(fs, 'somepkg', 'somefile.txt');
463

464
      devFS = DevFS(vmService, 'test', tempDir);
465 466 467 468
      await devFS.create();
      vmService.expectMessages(<String>['create test']);
      expect(devFS.assetPathsToEvict, isEmpty);

469 470 471 472
      final int bytes = await devFS.update(
        mainPath: 'lib/foo.txt',
        generator: residentCompiler,
        pathToReload: 'lib/foo.txt.dill',
473
        trackWidgetCreation: false,
474
      );
475
      vmService.expectMessages(<String>[
476
        'writeFile test lib/foo.txt.dill',
477 478
      ]);
      expect(devFS.assetPathsToEvict, isEmpty);
479
      expect(bytes, 22);
480 481
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
Dan Rubel's avatar
Dan Rubel committed
482
    });
483 484

    testUsingContext('delete dev file system', () async {
Dan Rubel's avatar
Dan Rubel committed
485
      expect(vmService.messages, isEmpty, reason: 'prior test timeout');
486
      await devFS.destroy();
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
      vmService.expectMessages(<String>['destroy test']);
      expect(devFS.assetPathsToEvict, isEmpty);
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
    });

    testUsingContext('cleanup preexisting file system', () async {
      // simulate workspace
      final File file = fs.file(fs.path.join(basePath, filePath));
      await file.parent.create(recursive: true);
      file.writeAsBytesSync(<int>[1, 2, 3]);

      // simulate package
      await _createPackage(fs, 'somepkg', 'somefile.txt');

502
      devFS = DevFS(vmService, 'test', tempDir);
503 504 505 506 507 508 509 510 511 512 513 514
      await devFS.create();
      vmService.expectMessages(<String>['create test']);
      expect(devFS.assetPathsToEvict, isEmpty);

      // Try to create again.
      await devFS.create();
      vmService.expectMessages(<String>['create test', 'destroy test', 'create test']);
      expect(devFS.assetPathsToEvict, isEmpty);

      // Really destroy.
      await devFS.destroy();
      vmService.expectMessages(<String>['destroy test']);
515
      expect(devFS.assetPathsToEvict, isEmpty);
516 517
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
518
    });
519
  });
520 521 522 523
}

class MockVMService extends BasicMock implements VMService {
  MockVMService() {
524
    _vm = MockVM(this);
525 526
  }

527 528 529 530
  Uri _httpAddress;
  HttpServer _server;
  MockVM _vm;

531 532 533 534 535 536
  @override
  Uri get httpAddress => _httpAddress;

  @override
  VM get vm => _vm;

537
  Future<void> setUp() async {
538
    try {
539
      _server = await HttpServer.bind(InternetAddress.loopbackIPv6, 0);
540 541 542
      _httpAddress = Uri.parse('http://[::1]:${_server.port}');
    } on SocketException {
      // Fall back to IPv4 if the host doesn't support binding to IPv6 localhost
543
      _server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
544 545
      _httpAddress = Uri.parse('http://127.0.0.1:${_server.port}');
    }
546
    _server.listen((HttpRequest request) {
547
      final String fsName = request.headers.value('dev_fs_name');
548
      final String devicePath = utf8.decode(base64.decode(request.headers.value('dev_fs_uri_b64')));
549
      messages.add('writeFile $fsName $devicePath');
550
      request.drain<List<int>>().then<void>((List<int> value) {
551 552 553 554 555 556 557
        request.response
          ..write('Got it')
          ..close();
      });
    });
  }

558
  Future<void> tearDown() async {
559
    await _server?.close();
560 561 562 563 564 565 566
  }

  @override
  dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}

class MockVM implements VM {
567 568
  MockVM(this._service);

569 570
  final MockVMService _service;
  final Uri _baseUri = Uri.parse('file:///tmp/devfs/test');
571 572 573
  bool _devFSExists = false;

  static const int kFileSystemAlreadyExists = 1001;
574 575 576 577

  @override
  Future<Map<String, dynamic>> createDevFS(String fsName) async {
    _service.messages.add('create $fsName');
578
    if (_devFSExists) {
579
      throw rpc.RpcException(kFileSystemAlreadyExists, 'File system already exists');
580 581
    }
    _devFSExists = true;
582 583 584
    return <String, dynamic>{'uri': '$_baseUri'};
  }

585 586 587 588 589 590 591
  @override
  Future<Map<String, dynamic>> deleteDevFS(String fsName) async {
    _service.messages.add('destroy $fsName');
    _devFSExists = false;
    return <String, dynamic>{'type': 'Success'};
  }

592
  @override
593
  Future<Map<String, dynamic>> invokeRpcRaw(String method, {
594
    Map<String, dynamic> params = const <String, dynamic>{},
595
    Duration timeout,
596
    bool timeoutFatal = true,
597
  }) async {
598 599 600 601 602 603 604 605 606 607
    _service.messages.add('$method $params');
    return <String, dynamic>{'success': true};
  }

  @override
  dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}


final List<Directory> _tempDirs = <Directory>[];
608
final Map <String, Uri> _packages = <String, Uri>{};
609

610
Directory _newTempDir(FileSystem fs) {
611
  final Directory tempDir = fs.systemTempDirectory.createTempSync('flutter_devfs${_tempDirs.length}_test.');
612 613 614 615 616
  _tempDirs.add(tempDir);
  return tempDir;
}

void _cleanupTempDirs() {
617 618
  while (_tempDirs.isNotEmpty)
    tryToDelete(_tempDirs.removeLast());
619 620
}

621
Future<void> _createPackage(FileSystem fs, String pkgName, String pkgFileName, { bool doubleSlash = false }) async {
622
  final Directory pkgTempDir = _newTempDir(fs);
623 624 625
  String pkgFilePath = fs.path.join(pkgTempDir.path, pkgName, 'lib', pkgFileName);
  if (doubleSlash) {
    // Force two separators into the path.
626
    final String doubleSlash = fs.path.separator + fs.path.separator;
627
    pkgFilePath = pkgTempDir.path + doubleSlash + fs.path.join(pkgName, 'lib', pkgFileName);
628 629
  }
  final File pkgFile = fs.file(pkgFilePath);
630 631
  await pkgFile.parent.create(recursive: true);
  pkgFile.writeAsBytesSync(<int>[11, 12, 13]);
632
  _packages[pkgName] = fs.path.toUri(pkgFile.parent.path);
633
  final StringBuffer sb = StringBuffer();
634 635
  _packages.forEach((String pkgName, Uri pkgUri) {
    sb.writeln('$pkgName:$pkgUri');
636
  });
637
  fs.file(fs.path.join(_tempDirs[0].path, '.packages')).writeAsStringSync(sb.toString());
638
}
639

640
String _inAssetBuildDirectory(FileSystem fs, String filename) {
641 642
  return '${fs.path.toUri(getAssetBuildDirectory()).path}/$filename';
}