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
 
 
 
 
 
7d80aa8
e0a2378
 
 
 
 
 
7d80aa8
 
e0a2378
 
7d80aa8
 
 
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// 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:io';

import 'package:path/path.dart' as path;

import '../analyze.dart';
import '../custom_rules/analyze.dart';
import '../custom_rules/no_double_clamp.dart';
import '../custom_rules/no_stop_watches.dart';
import '../custom_rules/render_box_intrinsics.dart';
import '../utils.dart';
import 'common.dart';

typedef AsyncVoidCallback = Future<void> Function();

Future<String> capture(AsyncVoidCallback callback, {bool shouldHaveErrors = false}) async {
  final StringBuffer buffer = StringBuffer();
  final PrintCallback oldPrint = print;
  try {
    print = (Object? line) {
      buffer.writeln(line);
    };
    await callback();
    expect(
      hasError,
      shouldHaveErrors,
      reason:
          buffer.isEmpty
              ? '(No output to report.)'
              : hasError
              ? 'Unexpected errors:\n$buffer'
              : 'Unexpected success:\n$buffer',
    );
  } finally {
    print = oldPrint;
    resetErrorStatus();
  }
  if (stdout.supportsAnsiEscapes) {
    // Remove ANSI escapes when this test is running on a terminal.
    return buffer.toString().replaceAll(RegExp(r'(\x9B|\x1B\[)[0-?]{1,3}[ -/]*[@-~]'), '');
  } else {
    return buffer.toString();
  }
}

void main() {
  final String testRootPath = path.join('test', 'analyze-test-input', 'root');
  final String dartName = Platform.isWindows ? 'dart.exe' : 'dart';
  final String dartPath = path.canonicalize(
    path.join('..', '..', 'bin', 'cache', 'dart-sdk', 'bin', dartName),
  );
  final String testGenDefaultsPath = path.join('test', 'analyze-gen-defaults');

  test('matchesErrorsInFile matcher basic test', () async {
    final String result = await capture(() async {
      foundError(<String>[
        'meta.dart:5: error #1',
        'meta.dart:5: error #2',
        'meta.dart:6: error #3',
        '',
        'Error summary',
      ]);
    }, shouldHaveErrors: true);
    final File fixture = File(path.join(testRootPath, 'packages', 'foo', 'meta.dart'));
    expect(result, matchesErrorsInFile(fixture, endsWith: <String>['', 'Error summary']));
  });

  test('analyze.dart - verifyDeprecations', () async {
    final String result = await capture(
      () => verifyDeprecations(testRootPath, minimumMatches: 2),
      shouldHaveErrors: true,
    );
    final File fixture = File(path.join(testRootPath, 'packages', 'foo', 'deprecation.dart'));
    expect(
      result,
      matchesErrorsInFile(
        fixture,
        endsWith: <String>[
          'See: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes',
        ],
      ),
    );
  });

  test('analyze.dart - verifyGoldenTags', () async {
    final List<String> result = (await capture(
      () => verifyGoldenTags(testRootPath, minimumMatches: 6),
      shouldHaveErrors: true,
    )).split('\n');
    const String noTag =
        "Files containing golden tests must be tagged using @Tags(<String>['reduced-test-set']) "
        'at the top of the file before import statements.';
    const String missingTag =
        "Files containing golden tests must be tagged with 'reduced-test-set'.";
    final List<String> lines =
        <String>[
          'β•‘ test/analyze-test-input/root/packages/foo/golden_missing_tag.dart: $missingTag',
          'β•‘ test/analyze-test-input/root/packages/foo/golden_no_tag.dart: $noTag',
        ].map((String line) => line.replaceAll('/', Platform.isWindows ? r'\' : '/')).toList();
    expect(
      result.length,
      4 + lines.length,
      reason: 'output had unexpected number of lines:\n${result.join('\n')}',
    );
    expect(
      result[0],
      '╔═║ERROR #1β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•',
    );
    expect(result.getRange(1, result.length - 3).toSet(), lines.toSet());
    expect(
      result[result.length - 3],
      'β•‘ See: https://github.com/flutter/flutter/blob/main/docs/contributing/testing/Writing-a-golden-file-test-for-package-flutter.md',
    );
    expect(
      result[result.length - 2],
      'β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•',
    );
    expect(result[result.length - 1], ''); // trailing newline
  });

  test('analyze.dart - verifyNoMissingLicense', () async {
    final String result = await capture(
      () => verifyNoMissingLicense(testRootPath, checkMinimums: false),
      shouldHaveErrors: true,
    );
    final String file = 'test/analyze-test-input/root/packages/foo/foo.dart'.replaceAll(
      '/',
      Platform.isWindows ? r'\' : '/',
    );
    expect(
      result,
      '╔═║ERROR #1β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•\n'
      'β•‘ The following file does not have the right license header for dart files:\n'
      'β•‘   $file\n'
      'β•‘ The expected license header is:\n'
      'β•‘ // Copyright 2014 The Flutter Authors. All rights reserved.\n'
      'β•‘ // Use of this source code is governed by a BSD-style license that can be\n'
      'β•‘ // found in the LICENSE file.\n'
      'β•‘ ...followed by a blank line.\n'
      'β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•\n',
    );
  });

  test('analyze.dart - verifyNoTrailingSpaces', () async {
    final String result = await capture(
      () => verifyNoTrailingSpaces(testRootPath, minimumMatches: 2),
      shouldHaveErrors: true,
    );
    final String lines = <String>[
      'β•‘ test/analyze-test-input/root/packages/foo/spaces.txt:5: trailing U+0020 space character',
      'β•‘ test/analyze-test-input/root/packages/foo/spaces.txt:9: trailing blank line',
    ].map((String line) => line.replaceAll('/', Platform.isWindows ? r'\' : '/')).join('\n');
    expect(
      result,
      '╔═║ERROR #1β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•\n'
      '$lines\n'
      'β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•\n',
    );
  });

  test('analyze.dart - verifyRepositoryLinks', () async {
    final String result = await capture(
      () => verifyRepositoryLinks(testRootPath),
      shouldHaveErrors: true,
    );
    const String bannedBranch = 'master';
    final String file =
        Platform.isWindows
            ? r'test\analyze-test-input\root\packages\foo\bad_repository_links.dart'
            : 'test/analyze-test-input/root/packages/foo/bad_repository_links.dart';
    final String lines = <String>[
      'β•‘ $file contains https://android.googlesource.com/+/$bannedBranch/file1, which uses the banned "master" branch.',
      'β•‘ $file contains https://chromium.googlesource.com/+/$bannedBranch/file1, which uses the banned "master" branch.',
      'β•‘ $file contains https://cs.opensource.google.com/+/$bannedBranch/file1, which uses the banned "master" branch.',
      'β•‘ $file contains https://dart.googlesource.com/+/$bannedBranch/file1, which uses the banned "master" branch.',
      'β•‘ $file contains https://flutter.googlesource.com/+/$bannedBranch/file1, which uses the banned "master" branch.',
      'β•‘ $file contains https://source.chromium.org/+/$bannedBranch/file1, which uses the banned "master" branch.',
      'β•‘ $file contains https://github.com/flutter/flutter/tree/$bannedBranch/file1, which uses the banned "master" branch.',
      'β•‘ $file contains https://raw.githubusercontent.com/flutter/flutter/blob/$bannedBranch/file1, which uses the banned "master" branch.',
      'β•‘ Change the URLs above to the expected pattern by using the "main" branch if it exists, otherwise adding the repository to the list of exceptions in analyze.dart.',
    ].join('\n');
    expect(
      result,
      '╔═║ERROR #1β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•\n'
      '$lines\n'
      'β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•\n',
    );
  });

  test('analyze.dart - verifyNoBinaries - positive', () async {
    final String result = await capture(
      () => verifyNoBinaries(
        testRootPath,
        legacyBinaries: <Hash256>{const Hash256(0x39A050CD69434936, 0, 0, 0)},
      ),
      shouldHaveErrors: !Platform.isWindows,
    );
    if (!Platform.isWindows) {
      expect(
        result,
        '╔═║ERROR #1β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•\n'
        'β•‘ test/analyze-test-input/root/packages/foo/serviceaccount.enc:0: file is not valid UTF-8\n'
        'β•‘ All files in this repository must be UTF-8. In particular, images and other binaries\n'
        'β•‘ must not be checked into this repository. This is because we are very sensitive to the\n'
        'β•‘ size of the repository as it is distributed to all our developers. If you have a binary\n'
        'β•‘ to which you need access, you should consider how to fetch it from another repository;\n'
        'β•‘ for example, the "assets-for-api-docs" repository is used for images in API docs.\n'
        'β•‘ To add assets to flutter_tools templates, see the instructions in the wiki:\n'
        'β•‘ https://github.com/flutter/flutter/blob/main/docs/tool/Managing-template-image-assets.md\n'
        'β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•\n',
      );
    }
  });

  test('analyze.dart - verifyInternationalizations - comparison fails', () async {
    final String result = await capture(
      () => verifyInternationalizations(testRootPath, dartPath),
      shouldHaveErrors: true,
    );
    final String genLocalizationsScript = path.join(
      'dev',
      'tools',
      'localization',
      'bin',
      'gen_localizations.dart',
    );
    expect(result, contains('$dartName $genLocalizationsScript --cupertino'));
    expect(result, contains('$dartName $genLocalizationsScript --material'));
    final String generatedFile = path.join(
      testRootPath,
      'packages',
      'flutter_localizations',
      'lib',
      'src',
      'l10n',
      'generated_material_localizations.dart',
    );
    expect(
      result,
      contains(
        'The contents of $generatedFile are different from that produced by gen_localizations.',
      ),
    );
    expect(
      result,
      contains(r'Did you forget to run gen_localizations.dart after updating a .arb file?'),
    );
  });

  test('analyze.dart - verifyNoBinaries - negative', () async {
    await capture(
      () => verifyNoBinaries(
        testRootPath,
        legacyBinaries: <Hash256>{
          const Hash256(
            0xA8100AE6AA1940D0,
            0xB663BB31CD466142,
            0xEBBDBD5187131B92,
            0xD93818987832EB89,
          ), // sha256("\xff")
          const Hash256(0x155644D3F13D98BF, 0, 0, 0),
        },
      ),
    );
  });

  test('analyze.dart - verifyNullInitializedDebugExpensiveFields', () async {
    final String result = await capture(
      () => verifyNullInitializedDebugExpensiveFields(testRootPath, minimumMatches: 1),
      shouldHaveErrors: true,
    );

    final File fixture = File(path.join(testRootPath, 'packages', 'flutter', 'lib', 'bar.dart'));
    expect(
      result,
      matchesErrorsInFile(
        fixture,
        endsWith: <String>[
          '',
          'Fields annotated with @_debugOnly must null initialize,',
          'to ensure both the field and initializer are removed from profile/release mode.',
          'These fields should be written as:',
          'field = kDebugMode ? <DebugValue> : null;',
        ],
      ),
    );
  });

  test('analyze.dart - verifyTabooDocumentation', () async {
    final String result = await capture(
      () => verifyTabooDocumentation(testRootPath, minimumMatches: 1),
      shouldHaveErrors: true,
    );

    final File fixture = File(
      path.join(testRootPath, 'packages', 'flutter', 'lib', 'taboo_words.dart'),
    );
    expect(
      result,
      matchesErrorsInFile(
        fixture,
        endsWith: <String>[
          '',
          'Avoid the word "simply" in documentation. See https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#use-the-passive-voice-recommend-do-not-require-never-say-things-are-simple for details.',
          'In many cases these words can be omitted without loss of generality; in other cases it may require a bit of rewording to avoid implying that the task is simple.',
          'Similarly, avoid using "note:" or the phrase "note that". See https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#avoid-empty-prose for details.',
        ],
      ),
    );
  });

  test('analyze.dart - clampDouble', () async {
    final String result = await capture(
      () => analyzeWithRules(
        testRootPath,
        <AnalyzeRule>[noDoubleClamp],
        includePaths: <String>['packages/flutter/lib'],
      ),
      shouldHaveErrors: true,
    );

    final File fixture = File(
      path.join(testRootPath, 'packages', 'flutter', 'lib', 'double_clamp.dart'),
    );
    expect(
      result,
      matchesErrorsInFile(
        fixture,
        endsWith: <String>[
          '', // empty line before the last sentence.
          'For performance reasons, we use a custom "clampDouble" function instead of using "double.clamp".',
        ],
      ),
    );
  });

  test('analyze.dart - stopwatch', () async {
    final String result = await capture(
      () => analyzeWithRules(
        testRootPath,
        <AnalyzeRule>[noStopwatches],
        includePaths: <String>['packages/flutter/lib'],
      ),
      shouldHaveErrors: true,
    );

    final File fixture = File(
      path.join(testRootPath, 'packages', 'flutter', 'lib', 'stopwatch.dart'),
    );
    expect(
      result,
      matchesErrorsInFile(
        fixture,
        endsWith: <String>[
          '',
          'Stopwatches introduce flakes by falling out of sync with the FakeAsync used in testing.',
          'A Stopwatch that stays in sync with FakeAsync is available through the Gesture or Test bindings, through samplingClock.',
        ],
      ),
    );
  });

  test('analyze.dart - RenderBox intrinsics', () async {
    final String result = await capture(
      () => analyzeWithRules(
        testRootPath,
        <AnalyzeRule>[renderBoxIntrinsicCalculation],
        includePaths: <String>['packages/flutter/lib'],
      ),
      shouldHaveErrors: true,
    );
    final File fixture = File(
      path.join(testRootPath, 'packages', 'flutter', 'lib', 'renderbox_intrinsics.dart'),
    );
    expect(
      result,
      matchesErrorsInFile(
        fixture,
        endsWith: <String>[
          '',
          'Typically the get* methods should be used to obtain the intrinsics of a RenderBox.',
        ],
      ),
    );
  });

  test('analyze.dart - verifyMaterialFilesAreUpToDateWithTemplateFiles', () async {
    String result = await capture(
      () => verifyMaterialFilesAreUpToDateWithTemplateFiles(testGenDefaultsPath, dartPath),
      shouldHaveErrors: true,
    );
    final String lines = <String>[
      'β•‘ chip.dart is not up-to-date with the token template file.',
    ].map((String line) => line.replaceAll('/', Platform.isWindows ? r'\' : '/')).join('\n');
    const String errorStart = '╔═';
    result = result.substring(result.indexOf(errorStart));
    expect(
      result,
      '╔═║ERROR #1β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•\n'
      '$lines\n'
      'β•‘ See: https://github.com/flutter/flutter/blob/main/dev/tools/gen_defaults to update the token template files.\n'
      'β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•\n',
    );
  });
}