flutter_build_null_unsafe_test.dart 2.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// 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/file.dart';
import 'package:flutter_tools/src/base/io.dart';

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

void main() {
12 13 14
  late Directory tempDir;
  late Directory projectRoot;
  late String flutterBin;
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
  final List<String> targetPlatforms = <String>[
    'apk',
    'web',
    if (platform.isWindows) 'windows',
    if (platform.isMacOS) ...<String>['macos', 'ios'],
  ];

  setUpAll(() {
    tempDir = createResolvedTempDirectorySync('build_null_unsafe_test.');
    flutterBin = fileSystem.path.join(
      getFlutterRoot(),
      'bin',
      'flutter',
    );
    processManager.runSync(<String>[
      flutterBin,
      'config',
      '--enable-macos-desktop',
      '--enable-windows-desktop',
      '--enable-web',
    ]);

    processManager.runSync(<String>[
      flutterBin,
      ...getLocalEngineArguments(),
      'create',
      'hello',
    ], workingDirectory: tempDir.path);

    projectRoot = tempDir.childDirectory('hello');
    writeFile(fileSystem.path.join(projectRoot.path, 'pubspec.yaml'), '''
name: hello
environment:
  sdk: '>=2.12.0 <3.0.0'
''');
    writeFile(fileSystem.path.join(projectRoot.path, 'lib', 'main.dart'), '''
import 'unsafe.dart';
void main() {
  print(unsafeString);
}
''');
    writeFile(fileSystem.path.join(projectRoot.path, 'lib', 'unsafe.dart'), '''
// @dart=2.9
String unsafeString = null;
''');
  });

  tearDownAll(() {
    tryToDelete(tempDir);
  });

  for (final String targetPlatform in targetPlatforms) {
    testWithoutContext('flutter build $targetPlatform --no-sound-null-safety', () {
      final ProcessResult result = processManager.runSync(<String>[
        flutterBin,
        ...getLocalEngineArguments(),
        'build',
        targetPlatform,
        '--no-pub',
        '--no-sound-null-safety',
        if (targetPlatform == 'ios') '--no-codesign',
      ], workingDirectory: projectRoot.path);
77 78 79 80

      if (result.exitCode != 0) {
        fail('build --no-sound-null-safety failed: ${result.exitCode}\n${result.stderr}\n${result.stdout}');
      }
81
    });
82 83
  }
}