forked from git-connected/flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippets_test.dart
334 lines (293 loc) · 10.6 KB
/
snippets_test.dart
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// 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 'dart:convert';
import 'dart:io' show Directory, File, Process, ProcessResult, ProcessSignal, ProcessStartMode, SystemEncoding;
import 'package:path/path.dart' as path;
import 'package:platform/platform.dart';
import 'package:process/process.dart';
import 'package:snippets/configuration.dart';
import 'package:snippets/main.dart' show getChannelName;
import 'package:snippets/snippets.dart';
import 'package:test/test.dart' hide TypeMatcher, isInstanceOf;
void main() {
group('Generator', () {
late Configuration configuration;
late SnippetGenerator generator;
late Directory tmpDir;
late File template;
setUp(() {
tmpDir = Directory.systemTemp.createTempSync('flutter_snippets_test.');
configuration = Configuration(flutterRoot: Directory(path.join(
tmpDir.absolute.path, 'flutter')));
configuration.createOutputDirectory();
configuration.templatesDirectory.createSync(recursive: true);
configuration.skeletonsDirectory.createSync(recursive: true);
template = File(path.join(configuration.templatesDirectory.path, 'template.tmpl'));
template.writeAsStringSync('''
// Flutter code sample for {{element}}
{{description}}
import 'package:flutter/material.dart';
import '../foo.dart';
{{code-imports}}
{{code-my-preamble}}
main() {
{{code}}
}
''');
configuration.getHtmlSkeletonFile(SnippetType.sample).writeAsStringSync('''
<div>HTML Bits</div>
{{description}}
<pre>{{code}}</pre>
<pre>{{app}}</pre>
<div>More HTML Bits</div>
''');
configuration.getHtmlSkeletonFile(SnippetType.snippet).writeAsStringSync('''
<div>HTML Bits</div>
{{description}}
<pre>{{code}}</pre>
<div>More HTML Bits</div>
''');
configuration.getHtmlSkeletonFile(SnippetType.sample, showDartPad: true).writeAsStringSync('''
<div>HTML Bits (DartPad-style)</div>
<iframe class="snippet-dartpad" src="https://dartpad.dev/embed-flutter.html?split=60&run=true&sample_id={{id}}&sample_channel={{channel}}"></iframe>
<div>More HTML Bits</div>
''');
generator = SnippetGenerator(configuration: configuration);
});
tearDown(() {
tmpDir.deleteSync(recursive: true);
});
test('generates samples', () async {
final File inputFile = File(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
..createSync(recursive: true)
..writeAsStringSync(r'''
A description of the snippet.
On several lines.
```dart imports
import 'dart:ui';
```
```my-dart_language my-preamble
const String name = 'snippet';
```
```dart
void main() {
print('The actual $name.');
}
```
''');
final File outputFile = File(path.join(tmpDir.absolute.path, 'snippet_out.txt'));
final String html = generator.generate(
inputFile,
SnippetType.sample,
template: 'template',
metadata: <String, Object>{
'id': 'id',
'channel': 'stable',
'element': 'MyElement',
},
output: outputFile,
);
expect(html, contains('<div>HTML Bits</div>'));
expect(html, contains('<div>More HTML Bits</div>'));
expect(html, contains(r'print('The actual $name.');'));
expect(html, contains('A description of the snippet.\n'));
expect(html, isNot(contains('sample_channel=stable')));
expect(
html,
contains('// A description of the snippet.\n'
'//\n'
'// On several lines.\n'));
expect(html, contains('void main() {'));
final String outputContents = outputFile.readAsStringSync();
expect(outputContents, contains('// Flutter code sample for MyElement'));
expect(outputContents, contains('A description of the snippet.'));
expect(outputContents, contains('void main() {'));
expect(outputContents, contains("const String name = 'snippet';"));
final List<String> lines = outputContents.split('\n');
final int dartUiLine = lines.indexOf("import 'dart:ui';");
final int materialLine = lines.indexOf("import 'package:flutter/material.dart';");
final int otherLine = lines.indexOf("import '../foo.dart';");
expect(dartUiLine, lessThan(materialLine));
expect(materialLine, lessThan(otherLine));
});
test('generates snippets', () async {
final File inputFile = File(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
..createSync(recursive: true)
..writeAsStringSync(r'''
A description of the snippet.
On several lines.
```code
void main() {
print('The actual $name.');
}
```
''');
final String html = generator.generate(
inputFile,
SnippetType.snippet,
metadata: <String, Object>{'id': 'id'},
);
expect(html, contains('<div>HTML Bits</div>'));
expect(html, contains('<div>More HTML Bits</div>'));
expect(html, contains(r' print('The actual $name.');'));
expect(html, contains('<div class="snippet-description">{@end-inject-html}A description of the snippet.\n\n'
'On several lines.{@inject-html}</div>\n'));
expect(html, contains('main() {'));
});
test('generates dartpad samples', () async {
final File inputFile = File(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
..createSync(recursive: true)
..writeAsStringSync(r'''
A description of the snippet.
On several lines.
```code
void main() {
print('The actual $name.');
}
```
''');
final String html = generator.generate(
inputFile,
SnippetType.sample,
showDartPad: true,
template: 'template',
metadata: <String, Object>{'id': 'id', 'channel': 'stable'},
);
expect(html, contains('<div>HTML Bits (DartPad-style)</div>'));
expect(html, contains('<div>More HTML Bits</div>'));
expect(html, contains('<iframe class="snippet-dartpad" src="https://dartpad.dev/embed-flutter.html?split=60&run=true&sample_id=id&sample_channel=stable"></iframe>'));
});
test('generates sample metadata', () async {
final File inputFile = File(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
..createSync(recursive: true)
..writeAsStringSync(r'''
A description of the snippet.
On several lines.
```code
void main() {
print('The actual $name.');
}
```
''');
final File outputFile = File(path.join(tmpDir.absolute.path, 'snippet_out.dart'));
final File expectedMetadataFile = File(path.join(tmpDir.absolute.path, 'snippet_out.json'));
generator.generate(
inputFile,
SnippetType.sample,
template: 'template',
output: outputFile,
metadata: <String, Object>{'sourcePath': 'some/path.dart', 'id': 'id', 'channel': 'stable'},
);
expect(expectedMetadataFile.existsSync(), isTrue);
final Map<String, dynamic> json = jsonDecode(expectedMetadataFile.readAsStringSync()) as Map<String, dynamic>;
expect(json['id'], equals('id'));
expect(json['channel'], equals('stable'));
expect(json['file'], equals('snippet_out.dart'));
expect(json['description'], equals('A description of the snippet.\n\nOn several lines.'));
// Ensure any passed metadata is included in the output JSON too.
expect(json['sourcePath'], equals('some/path.dart'));
});
});
group('getChannelName()', () {
test('does not call git if LUCI_BRANCH env var provided', () {
const String branch = 'stable';
final FakePlatform platform = FakePlatform(
environment: <String, String>{'LUCI_BRANCH': branch},
);
final FakeProcessManager processManager = FakeProcessManager(<FakeCommand>[]);
expect(
getChannelName(
platform: platform,
processManager: processManager,
),
branch,
);
expect(processManager.hasRemainingExpectations, false);
});
test('calls git if LUCI_BRANCH env var is not provided', () {
const String branch = 'stable';
final FakePlatform platform = FakePlatform(
environment: <String, String>{},
);
final ProcessResult result = ProcessResult(0, 0, '## $branch...refs/heads/master', '');
final FakeProcessManager processManager = FakeProcessManager(
<FakeCommand>[FakeCommand('git status -b --porcelain', result)],
);
expect(
getChannelName(
platform: platform,
processManager: processManager,
),
branch,
);
expect(processManager.hasRemainingExpectations, false);
});
});
}
const SystemEncoding systemEncoding = SystemEncoding();
class FakeCommand {
FakeCommand(this.command, [ProcessResult? result]) : _result = result;
final String command;
final ProcessResult? _result;
ProcessResult get result => _result ?? ProcessResult(0, 0, '', '');
}
class FakeProcessManager implements ProcessManager {
FakeProcessManager(this.remainingExpectations);
final List<FakeCommand> remainingExpectations;
@override
bool canRun(dynamic command, {String? workingDirectory}) => true;
@override
Future<Process> start(
List<Object> command, {
String? workingDirectory,
Map<String, String>? environment,
bool includeParentEnvironment = true,
bool runInShell = false,
ProcessStartMode mode = ProcessStartMode.normal,
}) {
throw Exception('not implemented');
}
@override
Future<ProcessResult> run(
List<Object> command, {
String? workingDirectory,
Map<String, String>? environment,
bool includeParentEnvironment = true,
bool runInShell = false,
Encoding stdoutEncoding = systemEncoding,
Encoding stderrEncoding = systemEncoding,
}) {
throw Exception('not implemented');
}
@override
ProcessResult runSync(
List<Object> command, {
String? workingDirectory,
Map<String, String>? environment,
bool includeParentEnvironment = true,
bool runInShell = false,
Encoding stdoutEncoding = systemEncoding,
Encoding stderrEncoding = systemEncoding,
}) {
if (remainingExpectations.isEmpty) {
fail(
'Called FakeProcessManager with $command when no further commands were expected!',
);
}
final FakeCommand expectedCommand = remainingExpectations.removeAt(0);
final String expectedName = expectedCommand.command;
final String actualName = command.join(' ');
if (expectedName != actualName) {
fail(
'FakeProcessManager expected the command $expectedName but received $actualName',
);
}
return expectedCommand.result;
}
bool get hasRemainingExpectations => remainingExpectations.isNotEmpty;
@override
bool killPid(int pid, [ProcessSignal signal = ProcessSignal.sigterm]) {
throw Exception('not implemented');
}
}