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

5 6
import 'dart:io';

7 8
import 'package:flutter_tools/src/build_system/build_system.dart';
import 'package:flutter_tools/src/build_system/targets/assets.dart';
9
import 'package:flutter_tools/src/globals.dart' as globals;
10

11 12
import '../../../src/common.dart';
import '../../../src/testbed.dart';
13 14

void main() {
15 16 17
  const BuildSystem buildSystem = BuildSystem();
  Environment environment;
  Testbed testbed;
18

19 20
  setUp(() {
    testbed = Testbed(setup: () {
21 22
      environment = Environment.test(
        globals.fs.currentDirectory,
23
      );
24
      globals.fs.file(globals.fs.path.join('packages', 'flutter_tools', 'lib', 'src',
25 26
          'build_system', 'targets', 'assets.dart'))
        ..createSync(recursive: true);
27
      globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.png'))
28
        ..createSync(recursive: true);
29
      globals.fs.file(globals.fs.path.join('assets', 'wildcard', '#bar.png'))
30
        ..createSync(recursive: true);
31
      globals.fs.file('.packages')
32
        ..createSync();
33
      globals.fs.file('pubspec.yaml')
34 35
        ..createSync()
        ..writeAsStringSync('''
36 37 38 39 40
name: example

flutter:
  assets:
    - assets/foo/bar.png
41
    - assets/wildcard/
42 43
''');
    });
44
  });
45

46 47
  test('Copies files to correct asset directory', () => testbed.run(() async {
    await buildSystem.build(const CopyAssets(), environment);
48

49 50 51
    expect(globals.fs.file(globals.fs.path.join(environment.buildDir.path, 'flutter_assets', 'AssetManifest.json')).existsSync(), true);
    expect(globals.fs.file(globals.fs.path.join(environment.buildDir.path, 'flutter_assets', 'FontManifest.json')).existsSync(), true);
    expect(globals.fs.file(globals.fs.path.join(environment.buildDir.path, 'flutter_assets', 'LICENSE')).existsSync(), true);
52
    // See https://github.com/flutter/flutter/issues/35293
53
    expect(globals.fs.file(globals.fs.path.join(environment.buildDir.path, 'flutter_assets', 'assets/foo/bar.png')).existsSync(), true);
54
    // See https://github.com/flutter/flutter/issues/46163
55
    expect(globals.fs.file(globals.fs.path.join(environment.buildDir.path, 'flutter_assets', 'assets/wildcard/%23bar.png')).existsSync(), true);
56
  }));
57

58 59
  test('Does not leave stale files in build directory', () => testbed.run(() async {
    await buildSystem.build(const CopyAssets(), environment);
60

61
    expect(globals.fs.file(globals.fs.path.join(environment.buildDir.path, 'flutter_assets', 'assets/foo/bar.png')).existsSync(), true);
62
    // Modify manifest to remove asset.
63
    globals.fs.file('pubspec.yaml')
64 65
      ..createSync()
      ..writeAsStringSync('''
66 67 68 69
name: example

flutter:
''');
70
    await buildSystem.build(const CopyAssets(), environment);
71

72
    // See https://github.com/flutter/flutter/issues/35293
73
    expect(globals.fs.file(globals.fs.path.join(environment.buildDir.path, 'flutter_assets', 'assets/foo/bar.png')).existsSync(), false);
74
  }), skip: Platform.isWindows); // See https://github.com/google/file.dart/issues/131
75 76

  test('FlutterPlugins updates required files as needed', () => testbed.run(() async {
77
    globals.fs.file('pubspec.yaml')
78 79
      ..writeAsStringSync('name: foo\ndependencies:\n  foo: any\n');

80 81
    await const FlutterPlugins().build(Environment.test(
      globals.fs.currentDirectory,
82 83
    ));

84
    expect(globals.fs.file('.flutter-plugins').existsSync(), true);
85
  }));
86
}