test_utils.dart 1.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright 2018 The Chromium 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:async';
import 'dart:convert';

import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/process_manager.dart';

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

14 15 16
/// Creates a temporary directory but resolves any symlinks to return the real
/// underlying path to avoid issues with breakpoints/hot reload.
/// https://github.com/flutter/flutter/pull/21741
17 18
Directory createResolvedTempDirectorySync(String prefix) {
  final Directory tempDir = fs.systemTempDirectory.createTempSync('flutter_$prefix');
19 20 21
  return fs.directory(tempDir.resolveSymbolicLinksSync());
}

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
void writeFile(String path, String content) {
  fs.file(path)
    ..createSync(recursive: true)
    ..writeAsStringSync(content);
}

void writePackages(String folder) {
  writeFile(fs.path.join(folder, '.packages'), '''
test:${fs.path.join(fs.currentDirectory.path, 'lib')}/
''');
}

void writePubspec(String folder) {
  writeFile(fs.path.join(folder, 'pubspec.yaml'), '''
name: test
dependencies:
  flutter:
    sdk: flutter
''');
}

Future<void> getPackages(String folder) async {
  final List<String> command = <String>[
    fs.path.join(getFlutterRoot(), 'bin', 'flutter'),
46
    'pub',
47
    'get',
48 49
  ];
  final Process process = await processManager.start(command, workingDirectory: folder);
50
  final StringBuffer errorOutput = StringBuffer();
51 52
  process.stderr.transform(utf8.decoder).listen(errorOutput.write);
  final int exitCode = await process.exitCode;
53
  if (exitCode != 0) {
54
    throw Exception('flutter pub get failed: $errorOutput');
55
  }
56
}