1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2014 The Flutter 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 'package:file/memory.dart';
import 'package:file_testing/file_testing.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/template.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/dart/pub.dart';
import 'package:flutter_tools/src/template.dart';
import 'package:mockito/mockito.dart';
import 'src/common.dart';
void main() {
testWithoutContext('Template.render throws ToolExit when FileSystem exception is raised', () {
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
final Template template = Template(
fileSystem.directory('examples'),
fileSystem.currentDirectory,
null,
fileSystem: fileSystem,
logger: BufferLogger.test(),
templateRenderer: FakeTemplateRenderer(),
templateManifest: null,
);
final MockDirectory mockDirectory = MockDirectory();
when(mockDirectory.createSync(recursive: true)).thenThrow(const FileSystemException());
expect(() => template.render(mockDirectory, <String, Object>{}),
throwsToolExit());
});
testWithoutContext('Template.render attempts to read byte from template file before copying', () {
final MemoryFileSystem baseFileSystem = MemoryFileSystem.test();
baseFileSystem.file('templates/foo.copy.tmpl').createSync(recursive: true);
final ConfiguredFileSystem fileSystem = ConfiguredFileSystem(
baseFileSystem,
entities: <String, FileSystemEntity>{
'/templates/foo.copy.tmpl': FakeFile('/templates/foo.copy.tmpl'),
},
);
final Template template = Template(
fileSystem.directory('templates'),
fileSystem.currentDirectory,
null,
fileSystem: fileSystem,
logger: BufferLogger.test(),
templateRenderer: FakeTemplateRenderer(),
templateManifest: null,
);
expect(() => template.render(fileSystem.directory('out'), <String, Object>{}),
throwsA(isA<FileSystemException>()));
});
testWithoutContext('Template.render replaces .img.tmpl files with files from the image source', () {
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');
final Template template = Template(
templateDir,
templateDir,
imageSourceDir,
fileSystem: fileSystem,
templateManifest: null,
logger: BufferLogger.test(),
templateRenderer: FakeTemplateRenderer(),
);
template.render(destination, <String, Object>{});
final File destinationImage = destination.childFile(imageName);
expect(destinationImage, exists);
expect(destinationImage.readAsBytesSync(), equals(sourceImage.readAsBytesSync()));
});
testWithoutContext('Template.fromName runs pub get if .packages is missing', () async {
final MemoryFileSystem fileSystem = MemoryFileSystem();
final MockPub pub = MockPub();
when(pub.get(
context: PubContext.pubGet,
directory: anyNamed('directory'),
generateSyntheticPackage: false,
)).thenThrow(UnsupportedError(''));
Cache.flutterRoot = '/flutter';
// Attempting to run pub in a test throws.
await expectLater(Template.fromName(
'app',
fileSystem: fileSystem,
templateManifest: null,
logger: BufferLogger.test(),
pub: pub,
templateRenderer: FakeTemplateRenderer(),
),
throwsUnsupportedError,
);
});
testWithoutContext('Template.fromName runs pub get if .packages is missing flutter_template_images', () async {
final MemoryFileSystem fileSystem = MemoryFileSystem();
final MockPub pub = MockPub();
when(pub.get(
context: PubContext.pubGet,
directory: anyNamed('directory'),
generateSyntheticPackage: false,
)).thenThrow(UnsupportedError(''));
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.
await expectLater(
Template.fromName(
'app',
fileSystem: fileSystem,
templateManifest: null,
logger: BufferLogger.test(),
pub: pub,
templateRenderer: FakeTemplateRenderer(),
),
throwsUnsupportedError,
);
});
testWithoutContext('Template.fromName runs pub get if flutter_template_images directory is missing', () async {
final MemoryFileSystem fileSystem = MemoryFileSystem();
final MockPub pub = MockPub();
Cache.flutterRoot = '/flutter';
final File packagesFile = fileSystem.directory(Cache.flutterRoot)
.childDirectory('packages')
.childDirectory('flutter_tools')
.childFile('.packages');
packagesFile.createSync(recursive: true);
packagesFile.writeAsStringSync('\n');
when(pub.get(
context: PubContext.pubGet,
directory: anyNamed('directory'),
generateSyntheticPackage: false,
)).thenAnswer((Invocation invocation) async {
// Create valid package entry.
packagesFile.writeAsStringSync('flutter_template_images:file:///flutter_template_images');
});
await Template.fromName(
'app',
fileSystem: fileSystem,
templateManifest: null,
logger: BufferLogger.test(),
templateRenderer: FakeTemplateRenderer(),
pub: pub,
);
});
}
class MockPub extends Mock implements Pub {}
class MockDirectory extends Mock implements Directory {}
class FakeFile extends Fake implements File {
FakeFile(this.path);
@override
final String path;
@override
int lengthSync() {
throw const FileSystemException('', '', OSError('', 5));
}
}
class FakeTemplateRenderer extends TemplateRenderer {
@override
String renderString(String template, dynamic context, {bool htmlEscapeValues = false}) {
return '';
}
}