template_test.dart 4.22 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
import 'package:file/memory.dart';
6
import 'package:flutter_tools/src/base/file_system.dart';
7
import 'package:flutter_tools/src/cache.dart';
8
import 'package:flutter_tools/src/dart/pub.dart';
9
import 'package:flutter_tools/src/template.dart';
10
import 'package:flutter_tools/src/globals.dart' as globals;
11 12 13 14 15 16 17 18 19 20 21 22 23
import 'package:mockito/mockito.dart';

import 'src/common.dart';
import 'src/testbed.dart';

void main() {
  Testbed testbed;

  setUp(() {
    testbed = Testbed();
  });

  test('Template.render throws ToolExit when FileSystem exception is raised', () => testbed.run(() {
24 25 26 27 28 29 30
    final Template template = Template(
      globals.fs.directory('examples'),
      globals.fs.currentDirectory,
      null,
      fileSystem: globals.fs,
      templateManifest: null,
    );
31 32 33 34
    final MockDirectory mockDirectory = MockDirectory();
    when(mockDirectory.createSync(recursive: true)).thenThrow(const FileSystemException());

    expect(() => template.render(mockDirectory, <String, Object>{}),
Dan Field's avatar
Dan Field committed
35
        throwsToolExit());
36
  }));
37 38 39 40 41 42 43 44 45 46 47 48

  test('Template.render replaces .img.tmpl files with files from the image source', () => testbed.run(() {
    final MemoryFileSystem fileSystem = MemoryFileSystem();
    final Directory templateDir = fileSystem.directory('templates');
    final Directory imageSourceDir = fileSystem.directory('template_images');
    final Directory destination = fileSystem.directory('target');
    const String imageName = 'some_image.png';
    templateDir.childFile('$imageName.img.tmpl').createSync(recursive: true);
    final File sourceImage = imageSourceDir.childFile(imageName);
    sourceImage.createSync(recursive: true);
    sourceImage.writeAsStringSync('Ceci n\'est pas une pipe');

49 50 51 52 53 54 55
    final Template template = Template(
      templateDir,
      templateDir,
      imageSourceDir,
      fileSystem: fileSystem,
      templateManifest: null,
    );
56 57 58 59 60 61 62 63 64 65 66
    template.render(destination, <String, Object>{});

    final File destinationImage = destination.childFile(imageName);
    expect(destinationImage.existsSync(), true);
    expect(destinationImage.readAsBytesSync(), equals(sourceImage.readAsBytesSync()));
  }));

  test('Template.fromName runs pub get if .packages is missing', () => testbed.run(() async {
    final MemoryFileSystem fileSystem = MemoryFileSystem();

    // Attempting to run pub in a test throws.
67
    await expectLater(Template.fromName('app', fileSystem: fileSystem, templateManifest: null),
68 69 70 71 72 73 74 75 76 77 78 79 80
      throwsUnsupportedError);
  }));

  test('Template.fromName runs pub get if .packages is missing flutter_template_images', () => testbed.run(() async {
    final MemoryFileSystem fileSystem = MemoryFileSystem();
    Cache.flutterRoot = '/flutter';
    final File packagesFile = fileSystem.directory(Cache.flutterRoot)
        .childDirectory('packages')
        .childDirectory('flutter_tools')
        .childFile('.packages');
    packagesFile.createSync(recursive: true);

    // Attempting to run pub in a test throws.
81
    await expectLater(Template.fromName('app', fileSystem: fileSystem, templateManifest: null),
82 83 84 85 86 87 88 89 90 91 92
      throwsUnsupportedError);
  }));

  test('Template.fromName runs pub get if flutter_template_images directory is missing', () => testbed.run(() async {
    final MemoryFileSystem fileSystem = MemoryFileSystem();
    Cache.flutterRoot = '/flutter';
    final File packagesFile = fileSystem.directory(Cache.flutterRoot)
        .childDirectory('packages')
        .childDirectory('flutter_tools')
        .childFile('.packages');
    packagesFile.createSync(recursive: true);
93 94 95 96 97
    packagesFile.writeAsStringSync('\n');

    when(pub.get(
      context: PubContext.pubGet,
      directory: anyNamed('directory'),
98
      generateSyntheticPackage: false,
99 100 101 102 103
    )).thenAnswer((Invocation invocation) async {
      // Create valid package entry.
      packagesFile.writeAsStringSync('flutter_template_images:file:///flutter_template_images');
    });

104
    await Template.fromName('app', fileSystem: fileSystem, templateManifest: null);
105 106
  }, overrides: <Type, Generator>{
    Pub: () => MockPub(),
107
  }));
108 109
}

110
class MockPub extends Mock implements Pub {}
111
class MockDirectory extends Mock implements Directory {}