File size: 5,445 Bytes
e0a2378
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// 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' as io;

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

final ArgParser _argParser =
    ArgParser()
      ..addFlag('help', abbr: 'h', help: 'Display usage information.', negatable: false)
      ..addFlag('verbose', abbr: 'v', help: 'Show noisy output while running', negatable: false)
      ..addFlag(
        'generate-initial-golden',
        help:
            'Whether an initial run (not part of "runs") should generate the '
            'base golden file. If false, it is assumed the golden file wasl already generated.',
        defaultsTo: true,
      )
      ..addFlag(
        'build-app-once',
        help:
            'Whether to use flutter build and --use-application-binary instead of rebuilding every iteration.',
        defaultsTo: true,
      )
      ..addOption('runs', abbr: 'n', help: 'How many times to run the test.', defaultsTo: '10');

/// Builds, establishes a baseline, and runs a golden-file test N number of times.
///
/// Example use:
/// ```sh
/// dart ./tool/deflake.dart lib/external_texture/surface_texture_smiley_face_main.dart
/// ```
///
/// By default it will:
/// - Build the app once (and reuse the APK);
/// - Generate a baseline (local) golden-file, overwriting your local file system;
/// - Run N (by default, 10) subsequent tests, asserting the generated golden exactly matches.
///
/// For advanced usage, see `dart ./tool/deflake.dart --help`.
void main(List<String> args) async {
  final ArgResults argResults = _argParser.parse(args);
  if (argResults.flag('help')) {
    return _printUsage();
  }

  final List<String> testFiles = argResults.rest;
  if (testFiles.length != 1) {
    io.stderr.writeln('Exactly one test-file must be specified');
    _printUsage();
    io.exitCode = 1;
    return;
  }

  final io.File testFile = io.File(testFiles.single);
  if (!testFile.existsSync()) {
    io.stderr.writeln('Not a file: ${testFile.path}');
    _printUsage();
    io.exitCode = 1;
    return;
  }

  final bool generateInitialGolden = argResults.flag('generate-initial-golden');
  final bool buildAppOnce = argResults.flag('build-app-once');
  final bool verbose = argResults.flag('verbose');
  final int runs;
  {
    final String rawRuns = argResults.option('runs')!;
    final int? parsedRuns = int.tryParse(rawRuns);
    if (parsedRuns == null || parsedRuns < 1) {
      io.stderr.writeln('--runs must be a positive number: "$rawRuns".');
      io.exitCode = 1;
      return;
    }
    runs = parsedRuns;
  }

  final List<String> driverArgs;
  if (buildAppOnce) {
    io.stderr.writeln('Building initial app with "flutter build apk --debug...');
    final io.Process proccess = await io.Process.start('flutter', <String>[
      'build',
      'apk',
      '--debug',
      testFile.path,
    ], mode: verbose ? io.ProcessStartMode.inheritStdio : io.ProcessStartMode.normal);
    if (await proccess.exitCode case final int exitCode when exitCode != 0) {
      io.stderr.writeln('Failed to build (exit code = $exitCode).');
      io.stderr.writeln(_collectStdOut(proccess));
      io.exitCode = 1;
      return;
    }

    // Strictly speaking, it would be better to parse stdout for:
    // "✓ Built build/app/outputs/flutter-apk/app-debug.apk"
    //
    // ... _or_ specify the expected out ourselves and rely on that.
    driverArgs = <String>[
      'drive',
      '--use-application-binary',
      p.join('build', 'app', 'outputs', 'flutter-apk', 'app-debug.apk'),
      testFile.path,
    ];
  } else {
    // I can't imagine wanting to do this, but here is the option anyway!
    driverArgs = <String>['drive', testFile.path];
  }

  Future<bool> runDriverTest({Map<String, String>? environment}) async {
    final io.Process proccess = await io.Process.start(
      'flutter',
      driverArgs,
      mode: verbose ? io.ProcessStartMode.inheritStdio : io.ProcessStartMode.normal,
      environment: environment,
    );
    if (await proccess.exitCode case final int exitCode when exitCode != 0) {
      io.stderr.writeln('Failed to build (exit code = $exitCode).');
      io.stderr.writeln(_collectStdOut(proccess));
      return false;
    }
    return true;
  }

  // Do an initial baseline run.
  if (generateInitialGolden) {
    io.stderr.writeln('Generating a baseline set of golden-files...');
    await runDriverTest(environment: <String, String>{'UPDATE_GOLDENS': '1'});
  }

  // Now run.
  int totalFailed = 0;
  for (int i = 0; i < runs; i++) {
    io.stderr.writeln('RUN ${i + 1} of $runs');
    final bool result = await runDriverTest();
    if (!result) {
      totalFailed++;
      io.stderr.writeln('FAIL');
    } else {
      io.stderr.writeln('PASS');
    }
  }

  io.stderr.writeln('PASSED: ${runs - totalFailed} / $runs');
  if (totalFailed != 0) {
    io.exitCode = 1;
  }
}

void _printUsage() {
  io.stdout.writeln('Usage: dart tool/deflake.dart lib/<path-to-main>.dart');
  io.stdout.writeln(_argParser.usage);
}

Future<String> _collectStdOut(io.Process process) async {
  final StringBuffer buffer = StringBuffer();
  buffer.writeln('stdout:');
  buffer.writeln(await utf8.decodeStream(process.stdout));
  buffer.writeln('stderr:');
  buffer.writeln(await utf8.decodeStream(process.stderr));
  return buffer.toString();
}