devfs_test.dart 3.34 KB
Newer Older
1 2 3 4 5 6
// 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.

import 'dart:io';

7
import 'package:flutter_tools/src/asset.dart';
8
import 'package:flutter_tools/src/build_info.dart';
9 10 11 12 13 14 15 16 17 18 19 20 21 22
import 'package:flutter_tools/src/devfs.dart';
import 'package:path/path.dart' as path;
import 'package:test/test.dart';

import 'src/context.dart';
import 'src/mocks.dart';

void main() {
  String filePath = 'bar/foo.txt';
  String filePath2 = 'foo/bar.txt';
  Directory tempDir;
  String basePath;
  MockDevFSOperations devFSOperations = new MockDevFSOperations();
  DevFS devFS;
23 24
  AssetBundle assetBundle = new AssetBundle();
  assetBundle.entries.add(new AssetBundleEntry.fromString('a.txt', ''));
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  group('devfs', () {
    testUsingContext('create local file system', () async {
      tempDir = Directory.systemTemp.createTempSync();
      basePath = tempDir.path;
      File file = new File(path.join(basePath, filePath));
      await file.parent.create(recursive: true);
      file.writeAsBytesSync(<int>[1, 2, 3]);
    });
    testUsingContext('create dev file system', () async {
      devFS = new DevFS.operations(devFSOperations, 'test', tempDir);
      await devFS.create();
      expect(devFSOperations.contains('create test'), isTrue);
    });
    testUsingContext('populate dev file system', () async {
      await devFS.update();
      expect(devFSOperations.contains('writeFile test bar/foo.txt'), isTrue);
    });
    testUsingContext('modify existing file on local file system', () async {
      File file = new File(path.join(basePath, filePath));
      file.writeAsBytesSync(<int>[1, 2, 3, 4, 5, 6]);
      await devFS.update();
      expect(devFSOperations.contains('writeFile test bar/foo.txt'), isTrue);
    });
    testUsingContext('add new file to local file system', () async {
      File file = new File(path.join(basePath, filePath2));
      await file.parent.create(recursive: true);
      file.writeAsBytesSync(<int>[1, 2, 3, 4, 5, 6, 7]);
      await devFS.update();
      expect(devFSOperations.contains('writeFile test foo/bar.txt'), isTrue);
    });
55 56 57 58 59 60 61
    testUsingContext('delete a file from the local file system', () async {
      File file = new File(path.join(basePath, filePath));
      await file.delete();
      await devFS.update();
      expect(devFSOperations.contains('deleteFile test bar/foo.txt'), isTrue);
    });
    testUsingContext('add file in an asset bundle', () async {
62
      await devFS.update(bundle: assetBundle, bundleDirty: true);
63 64
      expect(devFSOperations.contains(
          'writeFile test ${getAssetBuildDirectory()}/a.txt'), isTrue);
65 66 67
    });
    testUsingContext('add a file to the asset bundle', () async {
      assetBundle.entries.add(new AssetBundleEntry.fromString('b.txt', ''));
68
      await devFS.update(bundle: assetBundle, bundleDirty: true);
69 70
      expect(devFSOperations.contains(
          'writeFile test ${getAssetBuildDirectory()}/b.txt'), isTrue);
71 72 73
    });
    testUsingContext('delete a file from the asset bundle', () async {
      assetBundle.entries.clear();
74
      await devFS.update(bundle: assetBundle, bundleDirty: true);
75 76
      expect(devFSOperations.contains(
          'deleteFile test ${getAssetBuildDirectory()}/b.txt'), isTrue);
77
    });
78 79 80 81 82
    testUsingContext('delete dev file system', () async {
      await devFS.destroy();
    });
  });
}