test_utils.dart 1.75 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
9
import 'package:flutter_tools/src/globals.dart' as globals;
10 11 12

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

13 14 15
/// 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
16
Directory createResolvedTempDirectorySync(String prefix) {
17
  assert(prefix.endsWith('.'));
18 19
  final Directory tempDirectory = globals.fs.systemTempDirectory.createTempSync('flutter_$prefix');
  return globals.fs.directory(tempDirectory.resolveSymbolicLinksSync());
20 21
}

22
void writeFile(String path, String content) {
23
  globals.fs.file(path)
24
    ..createSync(recursive: true)
25 26
    ..writeAsStringSync(content)
    ..setLastModifiedSync(DateTime.now().add(const Duration(seconds: 10)));
27 28 29
}

void writePackages(String folder) {
30 31
  writeFile(globals.fs.path.join(folder, '.packages'), '''
test:${globals.fs.path.join(globals.fs.currentDirectory.path, 'lib')}/
32 33 34 35
''');
}

void writePubspec(String folder) {
36
  writeFile(globals.fs.path.join(folder, 'pubspec.yaml'), '''
37 38 39 40 41 42 43 44 45
name: test
dependencies:
  flutter:
    sdk: flutter
''');
}

Future<void> getPackages(String folder) async {
  final List<String> command = <String>[
46
    globals.fs.path.join(getFlutterRoot(), 'bin', 'flutter'),
47
    'pub',
48
    'get',
49
  ];
50
  final ProcessResult result = await globals.processManager.run(command, workingDirectory: folder);
51 52
  if (result.exitCode != 0) {
    throw Exception('flutter pub get failed: ${result.stderr}\n${result.stdout}');
53
  }
54
}