local_engine_test.dart 14.5 KB
Newer Older
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
// 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:flutter_tools/src/artifacts.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/base/user_messages.dart';
import 'package:flutter_tools/src/runner/local_engine.dart';

import '../../src/common.dart';

const String kEngineRoot = '/flutter/engine';
const String kArbitraryEngineRoot = '/arbitrary/engine';
const String kDotPackages = '.packages';

void main() {
  testWithoutContext('works if --local-engine is specified and --local-engine-src-path '
    'is determined by sky_engine', () async {
    final FileSystem fileSystem = MemoryFileSystem.test();
    fileSystem
      .directory('$kArbitraryEngineRoot/src/out/ios_debug/gen/dart-pkg/sky_engine/lib/')
      .createSync(recursive: true);
    fileSystem
      .directory('$kArbitraryEngineRoot/src/out/host_debug')
      .createSync(recursive: true);
    fileSystem
      .file(kDotPackages)
      .writeAsStringSync('sky_engine:file://$kArbitraryEngineRoot/src/out/ios_debug/gen/dart-pkg/sky_engine/lib/');
    fileSystem
      .file('bin/cache/pkg/sky_engine/lib')
      .createSync(recursive: true);

36
    final BufferLogger logger = BufferLogger.test();
37 38 39
    final LocalEngineLocator localEngineLocator = LocalEngineLocator(
      fileSystem: fileSystem,
      flutterRoot: '',
40
      logger: logger,
41 42 43 44 45
      userMessages: UserMessages(),
      platform: FakePlatform(environment: <String, String>{}),
    );

    expect(
46
      await localEngineLocator.findEnginePath(localEngine: 'ios_debug'),
47 48 49 50 51
      matchesEngineBuildPaths(
        hostEngine: '/arbitrary/engine/src/out/host_debug',
        targetEngine: '/arbitrary/engine/src/out/ios_debug',
      ),
    );
52
    expect(logger.traceText, contains('Local engine source at /arbitrary/engine/src'));
53 54 55 56 57 58 59 60

    // Verify that this also works if the sky_engine path is a symlink to the engine root.
    fileSystem.link('/symlink').createSync(kArbitraryEngineRoot);
    fileSystem
      .file(kDotPackages)
      .writeAsStringSync('sky_engine:file:///symlink/src/out/ios_debug/gen/dart-pkg/sky_engine/lib/');

    expect(
61
      await localEngineLocator.findEnginePath(localEngine: 'ios_debug'),
62 63 64 65 66
      matchesEngineBuildPaths(
        hostEngine: '/symlink/src/out/host_debug',
        targetEngine: '/symlink/src/out/ios_debug',
      ),
    );
67
    expect(logger.traceText, contains('Local engine source at /symlink/src'));
68 69 70 71 72 73 74 75 76
  });

  testWithoutContext('works if --local-engine is specified and --local-engine-src-path '
    'is specified', () async {
    final FileSystem fileSystem = MemoryFileSystem.test();
    // Intentionally do not create a package_config to verify that it is not required.
    fileSystem.directory('$kArbitraryEngineRoot/src/out/ios_debug').createSync(recursive: true);
    fileSystem.directory('$kArbitraryEngineRoot/src/out/host_debug').createSync(recursive: true);

77
    final BufferLogger logger = BufferLogger.test();
78 79 80
    final LocalEngineLocator localEngineLocator = LocalEngineLocator(
      fileSystem: fileSystem,
      flutterRoot: '',
81
      logger: logger,
82 83 84 85 86
      userMessages: UserMessages(),
      platform: FakePlatform(environment: <String, String>{}),
    );

    expect(
87
      await localEngineLocator.findEnginePath(engineSourcePath: '$kArbitraryEngineRoot/src', localEngine: 'ios_debug'),
88 89 90 91 92
      matchesEngineBuildPaths(
        hostEngine: '/arbitrary/engine/src/out/host_debug',
        targetEngine: '/arbitrary/engine/src/out/ios_debug',
      ),
    );
93
    expect(logger.traceText, contains('Local engine source at /arbitrary/engine/src'));
94 95
  });

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  testWithoutContext('works if --local-engine is specified and --local-engine-src-path '
      'is determined by --local-engine', () async {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final Directory localEngine = fileSystem
        .directory('$kArbitraryEngineRoot/src/out/ios_debug/')
        ..createSync(recursive: true);
    fileSystem.directory('$kArbitraryEngineRoot/src/out/host_debug/').createSync(recursive: true);

    final BufferLogger logger = BufferLogger.test();
    final LocalEngineLocator localEngineLocator = LocalEngineLocator(
      fileSystem: fileSystem,
      flutterRoot: 'flutter/flutter',
      logger: logger,
      userMessages: UserMessages(),
      platform: FakePlatform(environment: <String, String>{}),
    );

    expect(
114
      await localEngineLocator.findEnginePath(localEngine: localEngine.path),
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
      matchesEngineBuildPaths(
        hostEngine: '/arbitrary/engine/src/out/host_debug',
        targetEngine: '/arbitrary/engine/src/out/ios_debug',
      ),
    );
    expect(logger.traceText, contains('Parsed engine source from local engine as /arbitrary/engine/src'));
    expect(logger.traceText, contains('Local engine source at /arbitrary/engine/src'));
  });

  testWithoutContext('works if local engine is host engine', () async {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final Directory localEngine = fileSystem
        .directory('$kArbitraryEngineRoot/src/out/host_debug/')
      ..createSync(recursive: true);

    final BufferLogger logger = BufferLogger.test();
    final LocalEngineLocator localEngineLocator = LocalEngineLocator(
      fileSystem: fileSystem,
      flutterRoot: 'flutter/flutter',
      logger: logger,
      userMessages: UserMessages(),
      platform: FakePlatform(environment: <String, String>{}),
    );

    expect(
140
      await localEngineLocator.findEnginePath(localEngine: localEngine.path),
141 142 143 144 145 146 147 148
      matchesEngineBuildPaths(
        hostEngine: '/arbitrary/engine/src/out/host_debug',
        targetEngine: '/arbitrary/engine/src/out/host_debug',
      ),
    );
    expect(logger.traceText, contains('Local engine source at /arbitrary/engine/src'));
  });

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  testWithoutContext('works if local engine is host engine with suffixes', () async {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final Directory localEngine = fileSystem
        .directory('$kArbitraryEngineRoot/src/out/host_debug_unopt_arm64/')
      ..createSync(recursive: true);

    final BufferLogger logger = BufferLogger.test();
    final LocalEngineLocator localEngineLocator = LocalEngineLocator(
      fileSystem: fileSystem,
      flutterRoot: 'flutter/flutter',
      logger: logger,
      userMessages: UserMessages(),
      platform: FakePlatform(environment: <String, String>{}),
    );

    expect(
165
      await localEngineLocator.findEnginePath(localEngine: localEngine.path),
166 167 168 169 170 171 172
      matchesEngineBuildPaths(
        hostEngine: '/arbitrary/engine/src/out/host_debug_unopt_arm64',
        targetEngine: '/arbitrary/engine/src/out/host_debug_unopt_arm64',
      ),
    );
  });

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
  testWithoutContext('works if local engine is simulator', () async {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final Directory localEngine = fileSystem
        .directory('$kArbitraryEngineRoot/src/out/ios_debug_sim/')
      ..createSync(recursive: true);
    fileSystem
        .directory('$kArbitraryEngineRoot/src/out/host_debug/')
        .createSync(recursive: true);

    final BufferLogger logger = BufferLogger.test();
    final LocalEngineLocator localEngineLocator = LocalEngineLocator(
      fileSystem: fileSystem,
      flutterRoot: 'flutter/flutter',
      logger: logger,
      userMessages: UserMessages(),
      platform: FakePlatform(environment: <String, String>{}),
    );

    expect(
      await localEngineLocator.findEnginePath(localEngine: localEngine.path),
      matchesEngineBuildPaths(
        hostEngine: '/arbitrary/engine/src/out/host_debug',
        targetEngine: '/arbitrary/engine/src/out/ios_debug_sim',
      ),
    );
  });

  testWithoutContext('works if local engine is simulator unoptimized',
      () async {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final Directory localEngine = fileSystem
        .directory('$kArbitraryEngineRoot/src/out/ios_debug_sim_unopt/')
      ..createSync(recursive: true);
    fileSystem
        .directory('$kArbitraryEngineRoot/src/out/host_debug_unopt/')
        .createSync(recursive: true);

    final BufferLogger logger = BufferLogger.test();
    final LocalEngineLocator localEngineLocator = LocalEngineLocator(
      fileSystem: fileSystem,
      flutterRoot: 'flutter/flutter',
      logger: logger,
      userMessages: UserMessages(),
      platform: FakePlatform(environment: <String, String>{}),
    );

    expect(
      await localEngineLocator.findEnginePath(localEngine: localEngine.path),
      matchesEngineBuildPaths(
        hostEngine: '/arbitrary/engine/src/out/host_debug_unopt',
        targetEngine: '/arbitrary/engine/src/out/ios_debug_sim_unopt',
      ),
    );
  });

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
  testWithoutContext('fails if host_debug does not exist', () async {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final Directory localEngine = fileSystem
        .directory('$kArbitraryEngineRoot/src/out/ios_debug/')
      ..createSync(recursive: true);

    final LocalEngineLocator localEngineLocator = LocalEngineLocator(
      fileSystem: fileSystem,
      flutterRoot: 'flutter/flutter',
      logger: BufferLogger.test(),
      userMessages: UserMessages(),
      platform: FakePlatform(environment: <String, String>{}),
    );

    await expectToolExitLater(
243
      localEngineLocator.findEnginePath(localEngine: localEngine.path),
244 245 246 247
      contains('No Flutter engine build found at /arbitrary/engine/src/out/host_debug'),
    );
  });

248 249 250 251 252 253 254 255 256 257 258 259 260 261
  testWithoutContext('works if --local-engine is specified and --local-engine-src-path '
    'is determined by flutter root', () async {
    final FileSystem fileSystem = MemoryFileSystem.test();
    fileSystem.file(kDotPackages).writeAsStringSync('\n');
    fileSystem
      .directory('$kEngineRoot/src/out/ios_debug')
      .createSync(recursive: true);
    fileSystem
      .directory('$kEngineRoot/src/out/host_debug')
      .createSync(recursive: true);
    fileSystem
      .file('bin/cache/pkg/sky_engine/lib')
      .createSync(recursive: true);

262
    final BufferLogger logger = BufferLogger.test();
263 264 265
    final LocalEngineLocator localEngineLocator = LocalEngineLocator(
      fileSystem: fileSystem,
      flutterRoot: 'flutter/flutter',
266
      logger: logger,
267 268 269 270 271
      userMessages: UserMessages(),
      platform: FakePlatform(environment: <String, String>{}),
    );

    expect(
272
      await localEngineLocator.findEnginePath(localEngine: 'ios_debug'),
273 274 275 276 277
      matchesEngineBuildPaths(
        hostEngine: 'flutter/engine/src/out/host_debug',
        targetEngine: 'flutter/engine/src/out/ios_debug',
      ),
    );
278
    expect(logger.traceText, contains('Local engine source at flutter/engine/src'));
279
  });
280 281 282 283 284 285 286 287 288 289 290 291 292 293

  testWithoutContext('fails if --local-engine is specified and --local-engine-src-path '
      'cannot be determined', () async {
    final FileSystem fileSystem = MemoryFileSystem.test();

    final LocalEngineLocator localEngineLocator = LocalEngineLocator(
      fileSystem: fileSystem,
      flutterRoot: 'flutter/flutter',
      logger: BufferLogger.test(),
      userMessages: UserMessages(),
      platform: FakePlatform(environment: <String, String>{}),
    );

    await expectToolExitLater(
294
      localEngineLocator.findEnginePath(localEngine: '/path/to/nothing'),
295 296 297
      contains('Unable to detect local Flutter engine src directory'),
    );
  });
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

  testWithoutContext('works for local web engine', () async {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final Directory localWasmEngine = fileSystem
        .directory('$kArbitraryEngineRoot/src/out/wasm_whatever/')
      ..createSync(recursive: true);
    final Directory localWebEngine = fileSystem
        .directory('$kArbitraryEngineRoot/src/out/web_whatever/')
      ..createSync(recursive: true);

    final BufferLogger wasmLogger = BufferLogger.test();
    final LocalEngineLocator localWasmEngineLocator = LocalEngineLocator(
      fileSystem: fileSystem,
      flutterRoot: 'flutter/flutter',
      logger: wasmLogger,
      userMessages: UserMessages(),
      platform: FakePlatform(environment: <String, String>{}),
    );

    expect(
318
      await localWasmEngineLocator.findEnginePath(localEngine: localWasmEngine.path),
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
      matchesEngineBuildPaths(
        hostEngine: '/arbitrary/engine/src/out/wasm_whatever',
        targetEngine: '/arbitrary/engine/src/out/wasm_whatever',
      ),
    );
    expect(wasmLogger.traceText, contains('Local engine source at /arbitrary/engine/src'));

    final BufferLogger webLogger = BufferLogger.test();
    final LocalEngineLocator localWebEngineLocator = LocalEngineLocator(
      fileSystem: fileSystem,
      flutterRoot: 'flutter/flutter',
      logger: webLogger,
      userMessages: UserMessages(),
      platform: FakePlatform(environment: <String, String>{}),
    );

    expect(
336
      await localWebEngineLocator.findEnginePath(localEngine: localWebEngine.path),
337 338 339 340 341 342 343
      matchesEngineBuildPaths(
        hostEngine: '/arbitrary/engine/src/out/web_whatever',
        targetEngine: '/arbitrary/engine/src/out/web_whatever',
      ),
    );
    expect(webLogger.traceText, contains('Local engine source at /arbitrary/engine/src'));
  });
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

  test('returns null without throwing if nothing is specified', () async {
    final LocalEngineLocator localWebEngineLocator = LocalEngineLocator(
      fileSystem: MemoryFileSystem.test(),
      flutterRoot: 'flutter/flutter',
      logger: BufferLogger.test(),
      userMessages: UserMessages(),
      platform: FakePlatform(environment: <String, String>{}),
    );

    final EngineBuildPaths? paths = await localWebEngineLocator.findEnginePath();
    expect(paths, isNull);
  });

  test('throws if nothing is specified but the FLUTTER_ENGINE environment variable is set', () async {
    final LocalEngineLocator localWebEngineLocator = LocalEngineLocator(
      fileSystem: MemoryFileSystem.test(),
      flutterRoot: 'flutter/flutter',
      logger: BufferLogger.test(),
      userMessages: UserMessages(),
      platform: FakePlatform(environment: <String, String>{'FLUTTER_ENGINE': 'blah'}),
    );

    await expectToolExitLater(
      localWebEngineLocator.findEnginePath(),
      contains('Unable to detect a Flutter engine build directory in blah'),
    );
  });
372 373 374
}

Matcher matchesEngineBuildPaths({
375 376
  String? hostEngine,
  String? targetEngine,
377 378 379 380 381
}) {
  return const TypeMatcher<EngineBuildPaths>()
    .having((EngineBuildPaths paths) => paths.hostEngine, 'hostEngine', hostEngine)
    .having((EngineBuildPaths paths) => paths.targetEngine, 'targetEngine', targetEngine);
}