asset_bundle_test.dart 6.35 KB
Newer Older
1 2 3 4 5
// 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:convert';
6 7

import 'package:file/file.dart';
8
import 'package:file/memory.dart';
9

10
import 'package:flutter_tools/src/asset.dart';
11
import 'package:flutter_tools/src/base/file_system.dart';
12
import 'package:flutter_tools/src/base/platform.dart';
13
import 'package:flutter_tools/src/cache.dart';
14

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

18
void main() {
19 20 21 22
  setUpAll(() {
    Cache.flutterRoot = getFlutterRoot();
  });

23
  group('AssetBundle.build', () {
24
    FileSystem testFileSystem;
25 26

    setUp(() async {
27
      testFileSystem = MemoryFileSystem(
28 29 30 31 32
        style: platform.isWindows
          ? FileSystemStyle.windows
          : FileSystemStyle.posix,
      );
      testFileSystem.currentDirectory = testFileSystem.systemTempDirectory.createTempSync('flutter_asset_bundle_test.');
33 34
    });

35 36
    testUsingContext('nonempty', () async {
      final AssetBundle ab = AssetBundleFactory.instance.createBundle();
37 38
      expect(await ab.build(), 0);
      expect(ab.entries.length, greaterThan(0));
39 40
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
41
      ProcessManager: () => FakeProcessManager(<FakeCommand>[]),
42
    });
43 44 45 46 47 48

    testUsingContext('empty pubspec', () async {
      fs.file('pubspec.yaml')
        ..createSync()
        ..writeAsStringSync('');

49
      final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
50 51
      await bundle.build(manifestPath: 'pubspec.yaml');
      expect(bundle.entries.length, 1);
52
      const String expectedAssetManifest = '{}';
53
      expect(
54
        utf8.decode(await bundle.entries['AssetManifest.json'].contentsAsBytes()),
55 56
        expectedAssetManifest,
      );
57 58
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
59
      ProcessManager: () => FakeProcessManager(<FakeCommand>[]),
60
    });
61 62

    testUsingContext('wildcard directories are updated when filesystem changes', () async {
63
      final File packageFile = fs.file('.packages')..createSync();
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
      fs.file(fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
      fs.file('pubspec.yaml')
        ..createSync()
        ..writeAsStringSync(r'''
name: example
flutter:
  assets:
    - assets/foo/
''');
      final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
      await bundle.build(manifestPath: 'pubspec.yaml');
      // Expected assets:
      //  - asset manifest
      //  - font manifest
      //  - license file
      //  - assets/foo/bar.txt
      expect(bundle.entries.length, 4);
      expect(bundle.needsBuild(manifestPath: 'pubspec.yaml'), false);

83 84 85 86
      // Simulate modifying the files by updating the filestat time manually.
      fs.file(fs.path.join('assets', 'foo', 'fizz.txt'))
        ..createSync(recursive: true)
        ..setLastModifiedSync(packageFile.lastModifiedSync().add(const Duration(hours: 1)));
87 88 89 90 91 92 93 94 95 96 97 98

      expect(bundle.needsBuild(manifestPath: 'pubspec.yaml'), true);
      await bundle.build(manifestPath: 'pubspec.yaml');
      // Expected assets:
      //  - asset manifest
      //  - font manifest
      //  - license file
      //  - assets/foo/bar.txt
      //  - assets/foo/fizz.txt
      expect(bundle.entries.length, 5);
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
99
      ProcessManager: () => FakeProcessManager(<FakeCommand>[]),
100
    });
101 102 103

    testUsingContext('handle removal of wildcard directories', () async {
      fs.file(fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
104
      final File pubspec = fs.file('pubspec.yaml')
105 106 107 108 109 110 111
        ..createSync()
        ..writeAsStringSync(r'''
name: example
flutter:
  assets:
    - assets/foo/
''');
112
      fs.file('.packages').createSync();
113 114 115 116 117 118 119 120 121 122 123
      final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
      await bundle.build(manifestPath: 'pubspec.yaml');
      // Expected assets:
      //  - asset manifest
      //  - font manifest
      //  - license file
      //  - assets/foo/bar.txt
      expect(bundle.entries.length, 4);
      expect(bundle.needsBuild(manifestPath: 'pubspec.yaml'), false);

      // Delete the wildcard directory and update pubspec file.
124
      final DateTime modifiedTime = pubspec.lastModifiedSync().add(const Duration(hours: 1));
125 126 127 128
      fs.directory(fs.path.join('assets', 'foo')).deleteSync(recursive: true);
      fs.file('pubspec.yaml')
        ..createSync()
        ..writeAsStringSync(r'''
129 130
name: example''')
        ..setLastModifiedSync(modifiedTime);
131

132
      // touch .packages to make sure its change time is after pubspec.yaml's
133 134
      fs.file('.packages')
        ..setLastModifiedSync(modifiedTime);
135

136 137 138 139 140 141 142 143 144 145 146 147 148
      // Even though the previous file was removed, it is left in the
      // asset manifest and not updated. This is due to the devfs not
      // supporting file deletion.
      expect(bundle.needsBuild(manifestPath: 'pubspec.yaml'), true);
      await bundle.build(manifestPath: 'pubspec.yaml');
      // Expected assets:
      //  - asset manifest
      //  - font manifest
      //  - license file
      //  - assets/foo/bar.txt
      expect(bundle.entries.length, 4);
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
149
      ProcessManager: () => FakeProcessManager(<FakeCommand>[]),
150
    });
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

    // https://github.com/flutter/flutter/issues/42723
    testUsingContext('Test regression for mistyped file', () async {
      fs.file(fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
      // Create a directory in the same path to test that we're only looking at File
      // objects.
      fs.directory(fs.path.join('assets', 'foo', 'bar')).createSync();
      fs.file('pubspec.yaml')
        ..createSync()
        ..writeAsStringSync(r'''
name: example
flutter:
  assets:
    - assets/foo/
''');
      fs.file('.packages').createSync();
      final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
      await bundle.build(manifestPath: 'pubspec.yaml');
      // Expected assets:
      //  - asset manifest
      //  - font manifest
      //  - license file
      //  - assets/foo/bar.txt
      expect(bundle.entries.length, 4);
      expect(bundle.needsBuild(manifestPath: 'pubspec.yaml'), false);
    }, overrides: <Type, Generator>{
      FileSystem: () => testFileSystem,
      ProcessManager: () => FakeProcessManager(<FakeCommand>[]),
    });
180
  });
181

182
}