flutter/packages/flutter_tools/test/general.shard/commands/flutter_root_test.dart
Jonah Williams 9e5e763ebe
[flutter_tools] document flutter root initialization (#67884)
Moves the flutter root initialization to a static method on the cache. This is a small step towards making this functionality non-static and instead injected like normal members - however, completely removing all of the static-ness at once was too large of a change.

Instead document and add unit tests and change existing code as little as possible.

#47161
2020-10-27 15:20:07 -07:00

105 lines
3.7 KiB
Dart

// 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/base/file_system.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/base/user_messages.dart';
import 'package:flutter_tools/src/cache.dart';
import '../../src/common.dart';
void main() {
testWithoutContext('Cache can initialize flutter root from environment variable', () {
final String defaultFlutterRoot = Cache.defaultFlutterRoot(
fileSystem: MemoryFileSystem.test(),
userMessages: UserMessages(),
platform: FakePlatform(
environment: <String, String>{
'FLUTTER_ROOT': 'path/to/flutter'
}
)
);
expect(defaultFlutterRoot, '/path/to/flutter');
});
testWithoutContext('Cache can initialize flutter root data-scheme platform script', () {
final FileSystem fileSystem = MemoryFileSystem.test();
// For data-uri, the root is initialized to ../.. and then normalized. Change the
// current directory to verify this.
final Directory directory = fileSystem.directory('foo/bar/baz/')
..createSync(recursive: true);
fileSystem.currentDirectory = directory;
final String defaultFlutterRoot = Cache.defaultFlutterRoot(
fileSystem: fileSystem,
userMessages: UserMessages(),
platform: FakePlatform(
environment: <String, String>{},
script: Uri.parse('data:,Hello%2C%20World!'),
)
);
expect(defaultFlutterRoot, '/foo');
});
testWithoutContext('Cache can initialize flutter root package-scheme platform script', () {
final FileSystem fileSystem = MemoryFileSystem.test();
final String defaultFlutterRoot = Cache.defaultFlutterRoot(
fileSystem: fileSystem,
userMessages: UserMessages(),
platform: FakePlatform(
environment: <String, String>{},
script: Uri.parse('package:flutter_tools/flutter_tools.dart'),
packageConfig: 'flutter/packages/flutter_tools/.packages'
)
);
expect(defaultFlutterRoot, '/flutter');
});
testWithoutContext('Cache can initialize flutter root from snapshot location', () {
final FileSystem fileSystem = MemoryFileSystem.test();
final String defaultFlutterRoot = Cache.defaultFlutterRoot(
fileSystem: fileSystem,
userMessages: UserMessages(),
platform: FakePlatform(
environment: <String, String>{},
script: Uri.parse('file:///flutter/bin/cache/flutter_tools.snapshot'),
)
);
expect(defaultFlutterRoot, '/flutter');
});
testWithoutContext('Cache can initialize flutter root from script file', () {
final FileSystem fileSystem = MemoryFileSystem.test();
final String defaultFlutterRoot = Cache.defaultFlutterRoot(
fileSystem: fileSystem,
userMessages: UserMessages(),
platform: FakePlatform(
environment: <String, String>{},
script: Uri.parse('file:///flutter/packages/flutter_tools/bin/flutter_tools.dart'),
)
);
expect(defaultFlutterRoot, '/flutter');
});
testWithoutContext('Cache will default to current directory if there are no matches', () {
final FileSystem fileSystem = MemoryFileSystem.test();
final String defaultFlutterRoot = Cache.defaultFlutterRoot(
fileSystem: fileSystem,
userMessages: UserMessages(),
platform: FakePlatform(
environment: <String, String>{},
script: Uri.parse('http://foo.bar'), // does not match any heuristics.
)
);
expect(defaultFlutterRoot, '/');
});
}