template_test.dart 3.11 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:file_testing/file_testing.dart';
7
import 'package:flutter_tools/src/base/file_system.dart';
8 9
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/template.dart';
10
import 'package:flutter_tools/src/template.dart';
11
import '../src/common.dart';
12 13

void main() {
14 15 16 17 18 19 20 21 22 23 24 25 26 27
  testWithoutContext('Template constructor throws ToolExit when source directory is missing', () {
    final FileExceptionHandler handler = FileExceptionHandler();
    final MemoryFileSystem fileSystem = MemoryFileSystem.test(opHandle: handler.opHandle);

    expect(() => Template(
      fileSystem.directory('doesNotExist'),
      fileSystem.currentDirectory,
      fileSystem: fileSystem,
      logger: BufferLogger.test(),
      templateRenderer: FakeTemplateRenderer(),
      templateManifest: null,
    ), throwsToolExit());
  });

28
  testWithoutContext('Template.render throws ToolExit when FileSystem exception is raised', () {
29 30
    final FileExceptionHandler handler = FileExceptionHandler();
    final MemoryFileSystem fileSystem = MemoryFileSystem.test(opHandle: handler.opHandle);
31
    final Template template = Template(
32
      fileSystem.directory('examples')..createSync(recursive: true),
33
      fileSystem.currentDirectory,
34 35 36
      fileSystem: fileSystem,
      logger: BufferLogger.test(),
      templateRenderer: FakeTemplateRenderer(),
37 38
      templateManifest: null,
    );
39 40
    final Directory directory = fileSystem.directory('foo');
    handler.addError(directory, FileSystemOp.create, const FileSystemException());
41

42 43
    expect(() => template.render(directory, <String, Object>{}),
      throwsToolExit());
44
  });
45

46
  testWithoutContext('Template.render replaces .img.tmpl files with files from the image source', () {
47
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
48 49 50 51 52 53 54
    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);
55
    sourceImage.writeAsStringSync("Ceci n'est pas une pipe");
56

57 58 59 60 61
    final Template template = Template(
      templateDir,
      imageSourceDir,
      fileSystem: fileSystem,
      templateManifest: null,
62 63
      logger: BufferLogger.test(),
      templateRenderer: FakeTemplateRenderer(),
64
    );
65 66 67
    template.render(destination, <String, Object>{});

    final File destinationImage = destination.childFile(imageName);
68
    expect(destinationImage, exists);
69
    expect(destinationImage.readAsBytesSync(), equals(sourceImage.readAsBytesSync()));
70
  });
71 72
}

73 74 75 76 77 78
class FakeTemplateRenderer extends TemplateRenderer {
  @override
  String renderString(String template, dynamic context, {bool htmlEscapeValues = false}) {
    return '';
  }
}