Unverified Commit 7b54a30e authored by Sigurd Meldgaard's avatar Sigurd Meldgaard Committed by GitHub

Notify about existing caches when preloading (#122592)

parent a72a8baa
...@@ -34,28 +34,6 @@ const String _kPubCacheEnvironmentKey = 'PUB_CACHE'; ...@@ -34,28 +34,6 @@ const String _kPubCacheEnvironmentKey = 'PUB_CACHE';
typedef MessageFilter = String? Function(String message); typedef MessageFilter = String? Function(String message);
/// Load any package-files stored in [preloadCacheDir] into the pub cache if it
/// exists.
///
/// Deletes the [preloadCacheDir].
@visibleForTesting
void preloadPubCache({
required Directory preloadCacheDir,
required ProcessManager processManager,
required Logger logger,
required List<String> pubCommand,
}) {
if (preloadCacheDir.existsSync()) {
final Iterable<String> cacheFiles =
preloadCacheDir
.listSync()
.map((FileSystemEntity f) => f.path)
.where((String path) => path.endsWith('.tar.gz'));
processManager.runSync(<String>[...pubCommand, 'cache', 'preload',...cacheFiles]);
_tryDeleteDirectory(preloadCacheDir, logger);
}
}
bool _tryDeleteDirectory(Directory directory, Logger logger) { bool _tryDeleteDirectory(Directory directory, Logger logger) {
try { try {
if (directory.existsSync()) { if (directory.existsSync()) {
...@@ -614,13 +592,76 @@ class _DefaultPub implements Pub { ...@@ -614,13 +592,76 @@ class _DefaultPub implements Pub {
if (_platform.environment.containsKey(_kPubCacheEnvironmentKey)) { if (_platform.environment.containsKey(_kPubCacheEnvironmentKey)) {
return _platform.environment[_kPubCacheEnvironmentKey]; return _platform.environment[_kPubCacheEnvironmentKey];
} }
_preloadPubCache();
// Use pub's default location by returning null.
return null;
}
/// Load any package-files stored in FLUTTER_ROOT/.pub-preload-cache into the
/// pub cache if it exists.
///
/// Deletes the [preloadCacheDir].
void _preloadPubCache() {
final String flutterRootPath = Cache.flutterRoot!; final String flutterRootPath = Cache.flutterRoot!;
final Directory flutterRoot = _fileSystem.directory(flutterRootPath); final Directory flutterRoot = _fileSystem.directory(flutterRootPath);
final Directory preloadCacheDir = flutterRoot.childDirectory('.pub-preload-cache'); final Directory preloadCacheDir = flutterRoot.childDirectory('.pub-preload-cache');
preloadPubCache(preloadCacheDir: preloadCacheDir,logger: _logger,processManager: _processManager, pubCommand: _pubCommand); if (preloadCacheDir.existsSync()) {
// Use pub's default location by returning null. /// We only want to inform about existing caches on first run of a freshly
return null; /// downloaded Flutter SDK. Therefore it is conditioned on the existence
/// of the .pub-preload-cache dir.
_informAboutExistingCaches();
final Iterable<String> cacheFiles =
preloadCacheDir
.listSync()
.map((FileSystemEntity f) => f.path)
.where((String path) => path.endsWith('.tar.gz'));
_processManager.runSync(<String>[..._pubCommand, 'cache', 'preload', ...cacheFiles]);
_tryDeleteDirectory(preloadCacheDir, _logger);
}
}
/// Issues a log message if there is an existing pub cache and or an existing
/// Dart Analysis Server cache.
void _informAboutExistingCaches() {
final String? pubCachePath = _pubCacheDefaultLocation();
if (pubCachePath != null) {
final Directory pubCacheDirectory = _fileSystem.directory(pubCachePath);
if (pubCacheDirectory.existsSync()) {
_logger.printStatus('''
Found an existing Pub cache at $pubCachePath.
It can be repaired by running `dart pub cache repair`.
It can be reset by running `dart pub cache clear`.''');
}
}
final String? home = _platform.environment['HOME'];
if (home != null) {
final String dartServerCachePath = _fileSystem.path.join(home, '.dartServer');
if (_fileSystem.directory(dartServerCachePath).existsSync()) {
_logger.printStatus('''
Found an existing Dart Analysis Server cache at $dartServerCachePath.
It can be reset by deleting $dartServerCachePath.''');
}
}
}
/// The default location of the Pub cache if the PUB_CACHE environment variable
/// is not set.
///
/// Returns null if the appropriate environment variables are unset.
String? _pubCacheDefaultLocation () {
if (_platform.isWindows) {
final String? localAppData = _platform.environment['LOCALAPPDATA'];
if (localAppData == null) {
return null;
}
return _fileSystem.path.join(localAppData, 'Pub', 'Cache');
} else {
final String? home = _platform.environment['HOME'];
if (home == null) {
return null;
}
return _fileSystem.path.join(home, '.pub-cache');
}
} }
/// The full environment used when running pub. /// The full environment used when running pub.
......
...@@ -43,16 +43,23 @@ void main() { ...@@ -43,16 +43,23 @@ void main() {
stdio: FakeStdio(), stdio: FakeStdio(),
); );
await expectLater(() => pub.get( await expectLater(
project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory), () => pub.get(
context: PubContext.pubGet, project:
checkUpToDate: true, FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
), throwsToolExit(message: 'Your Flutter SDK download may be corrupt or missing permissions to run')); context: PubContext.pubGet,
checkUpToDate: true,
),
throwsToolExit(
message:
'Your Flutter SDK download may be corrupt or missing permissions to run'));
}); });
group('shouldSkipThirdPartyGenerator', () { group('shouldSkipThirdPartyGenerator', () {
testWithoutContext('does not skip pub get the parameter is false', () async { testWithoutContext('does not skip pub get the parameter is false',
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[ () async {
final FakeProcessManager processManager =
FakeProcessManager.list(<FakeCommand>[
const FakeCommand(command: <String>[ const FakeCommand(command: <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
'pub', 'pub',
...@@ -88,7 +95,7 @@ void main() { ...@@ -88,7 +95,7 @@ void main() {
usage: TestUsage(), usage: TestUsage(),
platform: FakePlatform(), platform: FakePlatform(),
botDetector: const BotDetectorAlwaysNo(), botDetector: const BotDetectorAlwaysNo(),
stdio: FakeStdio(), stdio: FakeStdio(),
); );
await pub.get( await pub.get(
...@@ -102,8 +109,11 @@ void main() { ...@@ -102,8 +109,11 @@ void main() {
expect(fileSystem.file('.dart_tool/version').readAsStringSync(), 'b'); expect(fileSystem.file('.dart_tool/version').readAsStringSync(), 'b');
}); });
testWithoutContext('does not skip pub get if package_config.json has "generator": "pub"', () async { testWithoutContext(
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[ 'does not skip pub get if package_config.json has "generator": "pub"',
() async {
final FakeProcessManager processManager =
FakeProcessManager.list(<FakeCommand>[
const FakeCommand(command: <String>[ const FakeCommand(command: <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
'pub', 'pub',
...@@ -140,7 +150,7 @@ void main() { ...@@ -140,7 +150,7 @@ void main() {
usage: TestUsage(), usage: TestUsage(),
platform: FakePlatform(), platform: FakePlatform(),
botDetector: const BotDetectorAlwaysNo(), botDetector: const BotDetectorAlwaysNo(),
stdio: FakeStdio(), stdio: FakeStdio(),
); );
await pub.get( await pub.get(
...@@ -153,8 +163,11 @@ void main() { ...@@ -153,8 +163,11 @@ void main() {
expect(fileSystem.file('.dart_tool/version').readAsStringSync(), 'b'); expect(fileSystem.file('.dart_tool/version').readAsStringSync(), 'b');
}); });
testWithoutContext('does not skip pub get if package_config.json has "generator": "pub"', () async { testWithoutContext(
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[ 'does not skip pub get if package_config.json has "generator": "pub"',
() async {
final FakeProcessManager processManager =
FakeProcessManager.list(<FakeCommand>[
const FakeCommand(command: <String>[ const FakeCommand(command: <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
'pub', 'pub',
...@@ -191,7 +204,7 @@ void main() { ...@@ -191,7 +204,7 @@ void main() {
usage: TestUsage(), usage: TestUsage(),
platform: FakePlatform(), platform: FakePlatform(),
botDetector: const BotDetectorAlwaysNo(), botDetector: const BotDetectorAlwaysNo(),
stdio: FakeStdio(), stdio: FakeStdio(),
); );
await pub.get( await pub.get(
...@@ -204,8 +217,9 @@ void main() { ...@@ -204,8 +217,9 @@ void main() {
expect(fileSystem.file('.dart_tool/version').readAsStringSync(), 'b'); expect(fileSystem.file('.dart_tool/version').readAsStringSync(), 'b');
}); });
testWithoutContext('skips pub get if the package config "generator" is ' testWithoutContext(
'different than "pub"', () async { 'skips pub get if the package config "generator" is '
'different than "pub"', () async {
final FakeProcessManager processManager = FakeProcessManager.empty(); final FakeProcessManager processManager = FakeProcessManager.empty();
final BufferLogger logger = BufferLogger.test(); final BufferLogger logger = BufferLogger.test();
final MemoryFileSystem fileSystem = MemoryFileSystem.test(); final MemoryFileSystem fileSystem = MemoryFileSystem.test();
...@@ -223,7 +237,7 @@ void main() { ...@@ -223,7 +237,7 @@ void main() {
usage: TestUsage(), usage: TestUsage(),
platform: FakePlatform(), platform: FakePlatform(),
botDetector: const BotDetectorAlwaysNo(), botDetector: const BotDetectorAlwaysNo(),
stdio: FakeStdio(), stdio: FakeStdio(),
); );
await pub.get( await pub.get(
...@@ -239,15 +253,19 @@ void main() { ...@@ -239,15 +253,19 @@ void main() {
}); });
}); });
testWithoutContext('checkUpToDate skips pub get if the package config is newer than the pubspec ' testWithoutContext(
'and the current framework version is the same as the last version', () async { 'checkUpToDate skips pub get if the package config is newer than the pubspec '
'and the current framework version is the same as the last version',
() async {
final FakeProcessManager processManager = FakeProcessManager.empty(); final FakeProcessManager processManager = FakeProcessManager.empty();
final BufferLogger logger = BufferLogger.test(); final BufferLogger logger = BufferLogger.test();
final MemoryFileSystem fileSystem = MemoryFileSystem.test(); final MemoryFileSystem fileSystem = MemoryFileSystem.test();
fileSystem.file('pubspec.yaml').createSync(); fileSystem.file('pubspec.yaml').createSync();
fileSystem.file('pubspec.lock').createSync(); fileSystem.file('pubspec.lock').createSync();
fileSystem.file('.dart_tool/package_config.json').createSync(recursive: true); fileSystem
.file('.dart_tool/package_config.json')
.createSync(recursive: true);
fileSystem.file('.dart_tool/version').writeAsStringSync('a'); fileSystem.file('.dart_tool/version').writeAsStringSync('a');
fileSystem.file('version').writeAsStringSync('a'); fileSystem.file('version').writeAsStringSync('a');
...@@ -270,17 +288,20 @@ void main() { ...@@ -270,17 +288,20 @@ void main() {
expect(logger.traceText, contains('Skipping pub get: version match.')); expect(logger.traceText, contains('Skipping pub get: version match.'));
}); });
testWithoutContext('checkUpToDate does not skip pub get if the package config is newer than the pubspec ' testWithoutContext(
'but the current framework version is not the same as the last version', () async { 'checkUpToDate does not skip pub get if the package config is newer than the pubspec '
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[ 'but the current framework version is not the same as the last version',
() async {
final FakeProcessManager processManager =
FakeProcessManager.list(<FakeCommand>[
const FakeCommand(command: <String>[ const FakeCommand(command: <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
'pub', 'pub',
'--suppress-analytics', '--suppress-analytics',
'--directory', '--directory',
'.', '.',
'get', 'get',
'--example', '--example',
]), ]),
]); ]);
final BufferLogger logger = BufferLogger.test(); final BufferLogger logger = BufferLogger.test();
...@@ -288,7 +309,9 @@ void main() { ...@@ -288,7 +309,9 @@ void main() {
fileSystem.file('pubspec.yaml').createSync(); fileSystem.file('pubspec.yaml').createSync();
fileSystem.file('pubspec.lock').createSync(); fileSystem.file('pubspec.lock').createSync();
fileSystem.file('.dart_tool/package_config.json').createSync(recursive: true); fileSystem
.file('.dart_tool/package_config.json')
.createSync(recursive: true);
fileSystem.file('.dart_tool/version').writeAsStringSync('a'); fileSystem.file('.dart_tool/version').writeAsStringSync('a');
fileSystem.file('version').writeAsStringSync('b'); fileSystem.file('version').writeAsStringSync('b');
...@@ -312,17 +335,19 @@ void main() { ...@@ -312,17 +335,19 @@ void main() {
expect(fileSystem.file('.dart_tool/version').readAsStringSync(), 'b'); expect(fileSystem.file('.dart_tool/version').readAsStringSync(), 'b');
}); });
testWithoutContext('checkUpToDate does not skip pub get if the package config is newer than the pubspec ' testWithoutContext(
'but the current framework version does not exist yet', () async { 'checkUpToDate does not skip pub get if the package config is newer than the pubspec '
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[ 'but the current framework version does not exist yet', () async {
final FakeProcessManager processManager =
FakeProcessManager.list(<FakeCommand>[
const FakeCommand(command: <String>[ const FakeCommand(command: <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
'pub', 'pub',
'--suppress-analytics', '--suppress-analytics',
'--directory', '--directory',
'.', '.',
'get', 'get',
'--example', '--example',
]), ]),
]); ]);
final BufferLogger logger = BufferLogger.test(); final BufferLogger logger = BufferLogger.test();
...@@ -330,7 +355,9 @@ void main() { ...@@ -330,7 +355,9 @@ void main() {
fileSystem.file('pubspec.yaml').createSync(); fileSystem.file('pubspec.yaml').createSync();
fileSystem.file('pubspec.lock').createSync(); fileSystem.file('pubspec.lock').createSync();
fileSystem.file('.dart_tool/package_config.json').createSync(recursive: true); fileSystem
.file('.dart_tool/package_config.json')
.createSync(recursive: true);
fileSystem.file('version').writeAsStringSync('b'); fileSystem.file('version').writeAsStringSync('b');
final Pub pub = Pub.test( final Pub pub = Pub.test(
...@@ -353,20 +380,27 @@ void main() { ...@@ -353,20 +380,27 @@ void main() {
expect(fileSystem.file('.dart_tool/version').readAsStringSync(), 'b'); expect(fileSystem.file('.dart_tool/version').readAsStringSync(), 'b');
}); });
testWithoutContext('checkUpToDate does not skip pub get if the package config does not exist', () async { testWithoutContext(
'checkUpToDate does not skip pub get if the package config does not exist',
() async {
final MemoryFileSystem fileSystem = MemoryFileSystem.test(); final MemoryFileSystem fileSystem = MemoryFileSystem.test();
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[ final FakeProcessManager processManager =
FakeCommand(command: const <String>[ FakeProcessManager.list(<FakeCommand>[
'bin/cache/dart-sdk/bin/dart', FakeCommand(
'pub', command: const <String>[
'--suppress-analytics', 'bin/cache/dart-sdk/bin/dart',
'--directory', 'pub',
'.', '--suppress-analytics',
'get', '--directory',
'--example', '.',
], onRun: () { 'get',
fileSystem.file('.dart_tool/package_config.json').createSync(recursive: true); '--example',
}), ],
onRun: () {
fileSystem
.file('.dart_tool/package_config.json')
.createSync(recursive: true);
}),
]); ]);
final BufferLogger logger = BufferLogger.test(); final BufferLogger logger = BufferLogger.test();
...@@ -394,24 +428,29 @@ void main() { ...@@ -394,24 +428,29 @@ void main() {
expect(fileSystem.file('.dart_tool/version').readAsStringSync(), 'b'); expect(fileSystem.file('.dart_tool/version').readAsStringSync(), 'b');
}); });
testWithoutContext('checkUpToDate does not skip pub get if the pubspec.lock does not exist', () async { testWithoutContext(
'checkUpToDate does not skip pub get if the pubspec.lock does not exist',
() async {
final MemoryFileSystem fileSystem = MemoryFileSystem.test(); final MemoryFileSystem fileSystem = MemoryFileSystem.test();
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[ final FakeProcessManager processManager =
FakeProcessManager.list(<FakeCommand>[
const FakeCommand(command: <String>[ const FakeCommand(command: <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
'pub', 'pub',
'--suppress-analytics', '--suppress-analytics',
'--directory', '--directory',
'.', '.',
'get', 'get',
'--example', '--example',
]), ]),
]); ]);
final BufferLogger logger = BufferLogger.test(); final BufferLogger logger = BufferLogger.test();
fileSystem.file('pubspec.yaml').createSync(); fileSystem.file('pubspec.yaml').createSync();
fileSystem.file('version').writeAsStringSync('b'); fileSystem.file('version').writeAsStringSync('b');
fileSystem.file('.dart_tool/package_config.json').createSync(recursive: true); fileSystem
.file('.dart_tool/package_config.json')
.createSync(recursive: true);
fileSystem.file('.dart_tool/version').writeAsStringSync('b'); fileSystem.file('.dart_tool/version').writeAsStringSync('b');
final Pub pub = Pub.test( final Pub pub = Pub.test(
...@@ -434,16 +473,19 @@ void main() { ...@@ -434,16 +473,19 @@ void main() {
expect(fileSystem.file('.dart_tool/version').readAsStringSync(), 'b'); expect(fileSystem.file('.dart_tool/version').readAsStringSync(), 'b');
}); });
testWithoutContext('checkUpToDate does not skip pub get if the package config is older that the pubspec', () async { testWithoutContext(
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[ 'checkUpToDate does not skip pub get if the package config is older that the pubspec',
() async {
final FakeProcessManager processManager =
FakeProcessManager.list(<FakeCommand>[
const FakeCommand(command: <String>[ const FakeCommand(command: <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
'pub', 'pub',
'--suppress-analytics', '--suppress-analytics',
'--directory', '--directory',
'.', '.',
'get', 'get',
'--example', '--example',
]), ]),
]); ]);
final BufferLogger logger = BufferLogger.test(); final BufferLogger logger = BufferLogger.test();
...@@ -476,16 +518,19 @@ void main() { ...@@ -476,16 +518,19 @@ void main() {
expect(fileSystem.file('.dart_tool/version').readAsStringSync(), 'b'); expect(fileSystem.file('.dart_tool/version').readAsStringSync(), 'b');
}); });
testWithoutContext('checkUpToDate does not skip pub get if the pubspec.lock is older that the pubspec', () async { testWithoutContext(
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[ 'checkUpToDate does not skip pub get if the pubspec.lock is older that the pubspec',
() async {
final FakeProcessManager processManager =
FakeProcessManager.list(<FakeCommand>[
const FakeCommand(command: <String>[ const FakeCommand(command: <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
'pub', 'pub',
'--suppress-analytics', '--suppress-analytics',
'--directory', '--directory',
'.', '.',
'get', 'get',
'--example', '--example',
]), ]),
]); ]);
final BufferLogger logger = BufferLogger.test(); final BufferLogger logger = BufferLogger.test();
...@@ -495,8 +540,9 @@ void main() { ...@@ -495,8 +540,9 @@ void main() {
fileSystem.file('pubspec.lock') fileSystem.file('pubspec.lock')
..createSync() ..createSync()
..setLastModifiedSync(DateTime(1991)); ..setLastModifiedSync(DateTime(1991));
fileSystem.file('.dart_tool/package_config.json') fileSystem
.createSync(recursive: true); .file('.dart_tool/package_config.json')
.createSync(recursive: true);
fileSystem.file('version').writeAsStringSync('b'); fileSystem.file('version').writeAsStringSync('b');
fileSystem.file('.dart_tool/version').writeAsStringSync('b'); fileSystem.file('.dart_tool/version').writeAsStringSync('b');
...@@ -524,7 +570,8 @@ void main() { ...@@ -524,7 +570,8 @@ void main() {
final BufferLogger logger = BufferLogger.test(); final BufferLogger logger = BufferLogger.test();
final FileSystem fileSystem = MemoryFileSystem.test(); final FileSystem fileSystem = MemoryFileSystem.test();
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[ final FakeProcessManager processManager =
FakeProcessManager.list(<FakeCommand>[
const FakeCommand( const FakeCommand(
command: <String>[ command: <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
...@@ -538,7 +585,10 @@ void main() { ...@@ -538,7 +585,10 @@ void main() {
exitCode: 66, exitCode: 66,
stderr: 'err1\nerr2\nerr3\n', stderr: 'err1\nerr2\nerr3\n',
stdout: 'out1\nout2\nout3\n', stdout: 'out1\nout2\nout3\n',
environment: <String, String>{'FLUTTER_ROOT': '', 'PUB_ENVIRONMENT': 'flutter_cli:flutter_tests'}, environment: <String, String>{
'FLUTTER_ROOT': '',
'PUB_ENVIRONMENT': 'flutter_cli:flutter_tests'
},
), ),
]); ]);
final FakeStdio mockStdio = FakeStdio(); final FakeStdio mockStdio = FakeStdio();
...@@ -565,30 +615,27 @@ exit code: 66 ...@@ -565,30 +615,27 @@ exit code: 66
project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory), project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
context: PubContext.flutterTests, context: PubContext.flutterTests,
), ),
throwsA(isA<ToolExit>().having((ToolExit error) => error.message, 'message', null)), throwsA(isA<ToolExit>()
.having((ToolExit error) => error.message, 'message', null)),
); );
expect(logger.statusText, isEmpty); expect(logger.statusText, isEmpty);
expect(logger.traceText, contains(toolExitMessage)); expect(logger.traceText, contains(toolExitMessage));
expect( expect(mockStdio.stdout.writes.map(utf8.decode), <String>[
mockStdio.stdout.writes.map(utf8.decode), 'out1\nout2\nout3\n',
<String>[ ]);
'out1\nout2\nout3\n', expect(mockStdio.stderr.writes.map(utf8.decode), <String>[
] 'err1\nerr2\nerr3\n',
); ]);
expect(
mockStdio.stderr.writes.map(utf8.decode),
<String>[
'err1\nerr2\nerr3\n',
]
);
expect(processManager, hasNoRemainingExpectations); expect(processManager, hasNoRemainingExpectations);
}); });
testWithoutContext('pub get shows working directory on process exception', () async { testWithoutContext('pub get shows working directory on process exception',
() async {
final BufferLogger logger = BufferLogger.test(); final BufferLogger logger = BufferLogger.test();
final FileSystem fileSystem = MemoryFileSystem.test(); final FileSystem fileSystem = MemoryFileSystem.test();
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[ final FakeProcessManager processManager =
FakeProcessManager.list(<FakeCommand>[
FakeCommand( FakeCommand(
command: const <String>[ command: const <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
...@@ -617,7 +664,10 @@ exit code: 66 ...@@ -617,7 +664,10 @@ exit code: 66
exitCode: 66, exitCode: 66,
stderr: 'err1\nerr2\nerr3\n', stderr: 'err1\nerr2\nerr3\n',
stdout: 'out1\nout2\nout3\n', stdout: 'out1\nout2\nout3\n',
environment: const <String, String>{'FLUTTER_ROOT': '', 'PUB_ENVIRONMENT': 'flutter_cli:flutter_tests'}, environment: const <String, String>{
'FLUTTER_ROOT': '',
'PUB_ENVIRONMENT': 'flutter_cli:flutter_tests'
},
), ),
]); ]);
...@@ -636,15 +686,17 @@ exit code: 66 ...@@ -636,15 +686,17 @@ exit code: 66
context: PubContext.flutterTests, context: PubContext.flutterTests,
), ),
throwsA( throwsA(
isA<ProcessException>().having( isA<ProcessException>()
(ProcessException error) => error.message, .having(
'message', (ProcessException error) => error.message,
contains('Working directory: "/" (exists)'), 'message',
).having( contains('Working directory: "/" (exists)'),
(ProcessException error) => error.message, )
'message', .having(
contains('"PUB_ENVIRONMENT": "flutter_cli:flutter_tests"'), (ProcessException error) => error.message,
), 'message',
contains('"PUB_ENVIRONMENT": "flutter_cli:flutter_tests"'),
),
), ),
); );
expect(logger.statusText, isEmpty); expect(logger.statusText, isEmpty);
...@@ -657,7 +709,8 @@ exit code: 66 ...@@ -657,7 +709,8 @@ exit code: 66
final BufferLogger logger = BufferLogger.test(); final BufferLogger logger = BufferLogger.test();
final FileSystem fileSystem = MemoryFileSystem.test(); final FileSystem fileSystem = MemoryFileSystem.test();
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[ final FakeProcessManager processManager =
FakeProcessManager.list(<FakeCommand>[
const FakeCommand( const FakeCommand(
command: <String>[ command: <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
...@@ -670,7 +723,10 @@ exit code: 66 ...@@ -670,7 +723,10 @@ exit code: 66
], ],
stderr: 'err1\nerr2\nerr3\n', stderr: 'err1\nerr2\nerr3\n',
stdout: 'out1\nout2\nout3\n', stdout: 'out1\nout2\nout3\n',
environment: <String, String>{'FLUTTER_ROOT': '', 'PUB_ENVIRONMENT': 'flutter_cli:flutter_tests'}, environment: <String, String>{
'FLUTTER_ROOT': '',
'PUB_ENVIRONMENT': 'flutter_cli:flutter_tests'
},
), ),
]); ]);
...@@ -696,36 +752,33 @@ exit code: 66 ...@@ -696,36 +752,33 @@ exit code: 66
} }
expect( expect(
mockStdio.stdout.writes.map(utf8.decode), mockStdio.stdout.writes.map(utf8.decode),
isNot( isNot(<String>[
<String>[
'out1\nout2\nout3\n', 'out1\nout2\nout3\n',
] ]));
)
);
expect(processManager, hasNoRemainingExpectations); expect(processManager, hasNoRemainingExpectations);
}); });
testWithoutContext('pub cache in flutter root is ignored', () async { testWithoutContext('pub cache in flutter root is ignored', () async {
final FileSystem fileSystem = MemoryFileSystem.test(); final FileSystem fileSystem = MemoryFileSystem.test();
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[ final FakeProcessManager processManager =
FakeProcessManager.list(<FakeCommand>[
const FakeCommand( const FakeCommand(
command: <String>[ command: <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
'pub', 'pub',
'--suppress-analytics', '--suppress-analytics',
'--directory', '--directory',
'.', '.',
'get', 'get',
'--example', '--example',
], ],
exitCode: 69, exitCode: 69,
environment: <String, String>{ environment: <String, String>{
'FLUTTER_ROOT': '', 'FLUTTER_ROOT': '',
'PUB_ENVIRONMENT': 'flutter_cli:flutter_tests', 'PUB_ENVIRONMENT': 'flutter_cli:flutter_tests',
}, },
stdout: "FakeCommand's env successfully matched" stdout: "FakeCommand's env successfully matched"),
),
]); ]);
final FakeStdio mockStdio = FakeStdio(); final FakeStdio mockStdio = FakeStdio();
...@@ -741,29 +794,29 @@ exit code: 66 ...@@ -741,29 +794,29 @@ exit code: 66
try { try {
await pub.get( await pub.get(
project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory), project:
context: PubContext.flutterTests FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
); context: PubContext.flutterTests);
} on ToolExit { } on ToolExit {
// Ignore. // Ignore.
} }
expect( expect(mockStdio.stdout.writes.map(utf8.decode), <String>[
mockStdio.stdout.writes.map(utf8.decode), "FakeCommand's env successfully matched",
<String>[ ]);
"FakeCommand's env successfully matched",
]
);
expect(processManager, hasNoRemainingExpectations); expect(processManager, hasNoRemainingExpectations);
}); });
testWithoutContext('Preloaded packages are added to the pub cache', () async { testWithoutContext('Preloaded packages are added to the pub cache', () async {
final FileSystem fileSystem = MemoryFileSystem.test(); final FileSystem fileSystem = MemoryFileSystem.test();
final Directory preloadCache = fileSystem.currentDirectory.childDirectory('.pub-preload-cache'); final Directory preloadCache =
fileSystem.currentDirectory.childDirectory('.pub-preload-cache');
preloadCache.childFile('a.tar.gz').createSync(recursive: true); preloadCache.childFile('a.tar.gz').createSync(recursive: true);
preloadCache.childFile('b.tar.gz').createSync(); preloadCache.childFile('b.tar.gz').createSync();
fileSystem.currentDirectory.childFile('version').createSync();
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[ final FakeProcessManager processManager =
FakeProcessManager.list(<FakeCommand>[
const FakeCommand( const FakeCommand(
command: <String>[ command: <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
...@@ -775,8 +828,74 @@ exit code: 66 ...@@ -775,8 +828,74 @@ exit code: 66
'.pub-preload-cache/b.tar.gz', '.pub-preload-cache/b.tar.gz',
], ],
), ),
const FakeCommand( FakeCommand(
command: <String>[ command: const <String>[
'bin/cache/dart-sdk/bin/dart',
'pub',
'--suppress-analytics',
'--directory',
'.',
'get',
'--example',
],
environment: const <String, String>{
'FLUTTER_ROOT': '',
'PUB_ENVIRONMENT': 'flutter_cli:flutter_tests',
},
onRun: () {
fileSystem.currentDirectory
.childDirectory('.dart_tool')
.childFile('package_config.json')
.createSync(recursive: true);
}),
]);
final Platform platform =
FakePlatform(environment: <String, String>{'HOME': '/global'});
final BufferLogger logger = BufferLogger.test();
final Pub pub = Pub.test(
platform: platform,
usage: TestUsage(),
fileSystem: fileSystem,
logger: logger,
processManager: processManager,
botDetector: const BotDetectorAlwaysNo(),
stdio: FakeStdio(),
);
await pub.get(
project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
context: PubContext.flutterTests);
expect(logger.statusText, isNot(contains('Found an existing Pub cache')));
expect(logger.statusText,
isNot(contains('Found an existing Dart Analysis Server cache')));
expect(processManager, hasNoRemainingExpectations);
expect(preloadCache.existsSync(), false);
});
testWithoutContext('Notifies about existing caches, on first run only',
() async {
final FileSystem fileSystem = MemoryFileSystem.test();
final Directory preloadCache =
fileSystem.currentDirectory.childDirectory('.pub-preload-cache');
preloadCache.childFile('a.tar.gz').createSync(recursive: true);
fileSystem.currentDirectory.childFile('version').createSync();
fileSystem.directory('/global/.pub-cache').createSync(recursive: true);
fileSystem.directory('/global/.dartServer').createSync(recursive: true);
const FakeCommand dartPreloadCommand = FakeCommand(
command: <String>[
'bin/cache/dart-sdk/bin/dart',
'pub',
'--suppress-analytics',
'cache',
'preload',
'.pub-preload-cache/a.tar.gz',
],
);
final FakeCommand dartPubGetCommand = FakeCommand(
command: const <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
'pub', 'pub',
'--suppress-analytics', '--suppress-analytics',
...@@ -785,62 +904,80 @@ exit code: 66 ...@@ -785,62 +904,80 @@ exit code: 66
'get', 'get',
'--example', '--example',
], ],
exitCode: 69, environment: const <String, String>{
environment: <String, String>{
'FLUTTER_ROOT': '', 'FLUTTER_ROOT': '',
'PUB_ENVIRONMENT': 'flutter_cli:flutter_tests', 'PUB_ENVIRONMENT': 'flutter_cli:flutter_tests',
}, },
), onRun: () {
fileSystem.currentDirectory
.childDirectory('.dart_tool')
.childFile('package_config.json')
.createSync(recursive: true);
});
final FakeProcessManager processManager =
FakeProcessManager.list(<FakeCommand>[
dartPreloadCommand,
dartPubGetCommand,
dartPubGetCommand,
]); ]);
final Platform platform = FakePlatform( final Platform platform =
environment: <String, String>{'HOME': '/global'} FakePlatform(environment: <String, String>{'HOME': '/global'});
); final BufferLogger logger = BufferLogger.test();
final Pub pub = Pub.test( final Pub pub = Pub.test(
platform: platform, platform: platform,
usage: TestUsage(), usage: TestUsage(),
fileSystem: fileSystem, fileSystem: fileSystem,
logger: BufferLogger.test(), logger: logger,
processManager: processManager, processManager: processManager,
botDetector: const BotDetectorAlwaysNo(), botDetector: const BotDetectorAlwaysNo(),
stdio: FakeStdio(), stdio: FakeStdio(),
); );
try { await pub.get(
await pub.get(
project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory), project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
context: PubContext.flutterTests context: PubContext.flutterTests);
); expect(logger.statusText,
} on ToolExit { contains('Found an existing Pub cache at /global/.pub-cache'));
// Ignore. expect(
} logger.statusText,
contains(
expect(processManager, hasNoRemainingExpectations); 'Found an existing Dart Analysis Server cache at /global/.dartServer'),
);
expect(preloadCache.existsSync(), false); expect(preloadCache.existsSync(), false);
logger.clear();
await pub.get(
project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
context: PubContext.flutterTests);
expect(logger.statusText, isNot(contains('Found an existing Pub cache')));
expect(logger.statusText,
isNot(contains('Found an existing Dart Analysis Server cache')));
expect(processManager, hasNoRemainingExpectations);
}); });
testWithoutContext('pub cache in environment is used', () async { testWithoutContext('pub cache in environment is used', () async {
final FileSystem fileSystem = MemoryFileSystem.test(); final FileSystem fileSystem = MemoryFileSystem.test();
fileSystem.directory('custom/pub-cache/path').createSync(recursive: true); fileSystem.directory('custom/pub-cache/path').createSync(recursive: true);
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[ final FakeProcessManager processManager =
FakeProcessManager.list(<FakeCommand>[
const FakeCommand( const FakeCommand(
command: <String>[ command: <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
'pub', 'pub',
'--suppress-analytics', '--suppress-analytics',
'--directory', '--directory',
'.', '.',
'get', 'get',
'--example', '--example',
], ],
exitCode: 69, exitCode: 69,
environment: <String, String>{ environment: <String, String>{
'FLUTTER_ROOT': '', 'FLUTTER_ROOT': '',
'PUB_ENVIRONMENT': 'flutter_cli:flutter_tests', 'PUB_ENVIRONMENT': 'flutter_cli:flutter_tests',
'PUB_CACHE': 'custom/pub-cache/path', 'PUB_CACHE': 'custom/pub-cache/path',
}, },
stdout: "FakeCommand's env successfully matched" stdout: "FakeCommand's env successfully matched"),
),
]); ]);
final FakeStdio mockStdio = FakeStdio(); final FakeStdio mockStdio = FakeStdio();
...@@ -860,19 +997,16 @@ exit code: 66 ...@@ -860,19 +997,16 @@ exit code: 66
try { try {
await pub.get( await pub.get(
project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory), project:
context: PubContext.flutterTests FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
); context: PubContext.flutterTests);
} on ToolExit { } on ToolExit {
// Ignore. // Ignore.
} }
expect( expect(mockStdio.stdout.writes.map(utf8.decode), <String>[
mockStdio.stdout.writes.map(utf8.decode), "FakeCommand's env successfully matched",
<String>[ ]);
"FakeCommand's env successfully matched",
]
);
expect(processManager, hasNoRemainingExpectations); expect(processManager, hasNoRemainingExpectations);
}); });
...@@ -886,11 +1020,9 @@ exit code: 66 ...@@ -886,11 +1020,9 @@ exit code: 66
botDetector: const BotDetectorAlwaysNo(), botDetector: const BotDetectorAlwaysNo(),
stdio: FakeStdio(), stdio: FakeStdio(),
usage: usage, usage: usage,
platform: FakePlatform( platform: FakePlatform(environment: const <String, String>{
environment: const <String, String>{ 'PUB_CACHE': 'custom/pub-cache/path',
'PUB_CACHE': 'custom/pub-cache/path', }),
}
),
); );
fileSystem.file('version').createSync(); fileSystem.file('version').createSync();
fileSystem.file('pubspec.yaml').createSync(); fileSystem.file('pubspec.yaml').createSync();
...@@ -902,12 +1034,16 @@ exit code: 66 ...@@ -902,12 +1034,16 @@ exit code: 66
project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory), project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
context: PubContext.flutterTests, context: PubContext.flutterTests,
); );
expect(usage.events, contains( expect(
const TestUsageEvent('pub-result', 'flutter-tests', label: 'success'), usage.events,
)); contains(
const TestUsageEvent('pub-result', 'flutter-tests', label: 'success'),
));
}); });
testWithoutContext('package_config_subset file is generated from packages and not timestamp', () async { testWithoutContext(
'package_config_subset file is generated from packages and not timestamp',
() async {
final FileSystem fileSystem = MemoryFileSystem.test(); final FileSystem fileSystem = MemoryFileSystem.test();
final TestUsage usage = TestUsage(); final TestUsage usage = TestUsage();
final Pub pub = Pub.test( final Pub pub = Pub.test(
...@@ -917,11 +1053,9 @@ exit code: 66 ...@@ -917,11 +1053,9 @@ exit code: 66
botDetector: const BotDetectorAlwaysNo(), botDetector: const BotDetectorAlwaysNo(),
stdio: FakeStdio(), stdio: FakeStdio(),
usage: usage, usage: usage,
platform: FakePlatform( platform: FakePlatform(environment: const <String, String>{
environment: const <String, String>{ 'PUB_CACHE': 'custom/pub-cache/path',
'PUB_CACHE': 'custom/pub-cache/path', }),
}
),
); );
fileSystem.file('version').createSync(); fileSystem.file('version').createSync();
fileSystem.file('pubspec.yaml').createSync(); fileSystem.file('pubspec.yaml').createSync();
...@@ -958,7 +1092,8 @@ exit code: 66 ...@@ -958,7 +1092,8 @@ exit code: 66
fileSystem.directory('custom/pub-cache/path').createSync(recursive: true); fileSystem.directory('custom/pub-cache/path').createSync(recursive: true);
final TestUsage usage = TestUsage(); final TestUsage usage = TestUsage();
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[ final FakeProcessManager processManager =
FakeProcessManager.list(<FakeCommand>[
const FakeCommand( const FakeCommand(
command: <String>[ command: <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
...@@ -995,31 +1130,34 @@ exit code: 66 ...@@ -995,31 +1130,34 @@ exit code: 66
// Ignore. // Ignore.
} }
expect(usage.events, contains( expect(
const TestUsageEvent('pub-result', 'flutter-tests', label: 'failure'), usage.events,
)); contains(
const TestUsageEvent('pub-result', 'flutter-tests', label: 'failure'),
));
expect(processManager, hasNoRemainingExpectations); expect(processManager, hasNoRemainingExpectations);
}); });
testWithoutContext('Pub error handling', () async { testWithoutContext('Pub error handling', () async {
final BufferLogger logger = BufferLogger.test(); final BufferLogger logger = BufferLogger.test();
final MemoryFileSystem fileSystem = MemoryFileSystem.test(); final MemoryFileSystem fileSystem = MemoryFileSystem.test();
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[ final FakeProcessManager processManager =
FakeProcessManager.list(<FakeCommand>[
FakeCommand( FakeCommand(
command: const <String>[ command: const <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
'pub', 'pub',
'--suppress-analytics', '--suppress-analytics',
'--directory', '--directory',
'.', '.',
'get', 'get',
'--example', '--example',
], ],
onRun: () { onRun: () {
fileSystem.file('.dart_tool/package_config.json') fileSystem
.setLastModifiedSync(DateTime(2002)); .file('.dart_tool/package_config.json')
} .setLastModifiedSync(DateTime(2002));
), }),
const FakeCommand( const FakeCommand(
command: <String>[ command: <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
...@@ -1032,20 +1170,18 @@ exit code: 66 ...@@ -1032,20 +1170,18 @@ exit code: 66
], ],
), ),
FakeCommand( FakeCommand(
command: const <String>[ command: const <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
'pub', 'pub',
'--suppress-analytics', '--suppress-analytics',
'--directory', '--directory',
'.', '.',
'get', 'get',
'--example', '--example',
], ],
onRun: () { onRun: () {
fileSystem.file('pubspec.yaml') fileSystem.file('pubspec.yaml').setLastModifiedSync(DateTime(2002));
.setLastModifiedSync(DateTime(2002)); }),
}
),
const FakeCommand( const FakeCommand(
command: <String>[ command: <String>[
'bin/cache/dart-sdk/bin/dart', 'bin/cache/dart-sdk/bin/dart',
...@@ -1059,16 +1195,15 @@ exit code: 66 ...@@ -1059,16 +1195,15 @@ exit code: 66
), ),
]); ]);
final Pub pub = Pub.test( final Pub pub = Pub.test(
usage: TestUsage(), usage: TestUsage(),
fileSystem: fileSystem, fileSystem: fileSystem,
logger: logger, logger: logger,
processManager: processManager, processManager: processManager,
platform: FakePlatform( platform: FakePlatform(
environment: <String, String>{}, environment: <String, String>{},
), ),
botDetector: const BotDetectorAlwaysNo(), botDetector: const BotDetectorAlwaysNo(),
stdio: FakeStdio() stdio: FakeStdio());
);
fileSystem.file('version').createSync(); fileSystem.file('version').createSync();
// the good scenario: .packages is old, pub updates the file. // the good scenario: .packages is old, pub updates the file.
...@@ -1084,14 +1219,15 @@ exit code: 66 ...@@ -1084,14 +1219,15 @@ exit code: 66
); // pub sets date of .packages to 2002 ); // pub sets date of .packages to 2002
expect(logger.errorText, isEmpty); expect(logger.errorText, isEmpty);
expect(fileSystem.file('pubspec.yaml').lastModifiedSync(), DateTime(2001)); // because nothing should touch it expect(fileSystem.file('pubspec.yaml').lastModifiedSync(),
DateTime(2001)); // because nothing should touch it
logger.clear(); logger.clear();
// bad scenario 1: pub doesn't update file; doesn't matter, because we do instead // bad scenario 1: pub doesn't update file; doesn't matter, because we do instead
fileSystem.file('.dart_tool/package_config.json') fileSystem
.setLastModifiedSync(DateTime(2000)); .file('.dart_tool/package_config.json')
fileSystem.file('pubspec.yaml') .setLastModifiedSync(DateTime(2000));
.setLastModifiedSync(DateTime(2001)); fileSystem.file('pubspec.yaml').setLastModifiedSync(DateTime(2001));
await pub.get( await pub.get(
project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory), project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
context: PubContext.flutterTests, context: PubContext.flutterTests,
...@@ -1099,7 +1235,8 @@ exit code: 66 ...@@ -1099,7 +1235,8 @@ exit code: 66
expect(logger.statusText, isEmpty); expect(logger.statusText, isEmpty);
expect(logger.errorText, isEmpty); expect(logger.errorText, isEmpty);
expect(fileSystem.file('pubspec.yaml').lastModifiedSync(), DateTime(2001)); // because nothing should touch it expect(fileSystem.file('pubspec.yaml').lastModifiedSync(),
DateTime(2001)); // because nothing should touch it
logger.clear(); logger.clear();
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment