2016-08-26 02:37:48 +00:00
|
|
|
// Copyright 2016 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 'package:args/command_runner.dart';
|
2017-09-06 09:53:39 +00:00
|
|
|
import 'package:flutter_tools/src/base/file_system.dart' hide IOSink;
|
2017-05-12 22:40:35 +00:00
|
|
|
import 'package:flutter_tools/src/base/io.dart';
|
2018-02-21 17:54:07 +00:00
|
|
|
import 'package:flutter_tools/src/base/utils.dart';
|
2017-03-10 17:39:01 +00:00
|
|
|
import 'package:flutter_tools/src/cache.dart';
|
2016-08-26 02:37:48 +00:00
|
|
|
import 'package:flutter_tools/src/commands/packages.dart';
|
2017-05-12 22:40:35 +00:00
|
|
|
import 'package:process/process.dart';
|
2016-08-26 02:37:48 +00:00
|
|
|
|
2017-05-12 17:44:52 +00:00
|
|
|
import '../src/common.dart';
|
|
|
|
import '../src/context.dart';
|
2018-01-25 01:16:30 +00:00
|
|
|
import '../src/mocks.dart' show MockProcessManager, MockStdio, PromptingProcess;
|
2016-08-26 02:37:48 +00:00
|
|
|
|
2018-02-21 17:54:07 +00:00
|
|
|
class AlwaysTrueBotDetector implements BotDetector {
|
|
|
|
const AlwaysTrueBotDetector();
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isRunningOnBot => true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class AlwaysFalseBotDetector implements BotDetector {
|
|
|
|
const AlwaysFalseBotDetector();
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isRunningOnBot => false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-26 02:37:48 +00:00
|
|
|
void main() {
|
2017-05-12 22:40:35 +00:00
|
|
|
Cache.disableLocking();
|
|
|
|
group('packages get/upgrade', () {
|
2018-08-17 20:17:23 +00:00
|
|
|
Directory tempDir;
|
2016-08-26 02:37:48 +00:00
|
|
|
|
|
|
|
setUp(() {
|
2018-08-17 20:17:23 +00:00
|
|
|
tempDir = fs.systemTempDirectory.createTempSync('flutter_tools_packages_test.');
|
2016-08-26 02:37:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() {
|
2018-08-17 20:17:23 +00:00
|
|
|
tryToDelete(tempDir);
|
2016-08-26 02:37:48 +00:00
|
|
|
});
|
|
|
|
|
2018-02-16 09:17:28 +00:00
|
|
|
Future<String> createProjectWithPlugin(String plugin) async {
|
2018-08-17 20:17:23 +00:00
|
|
|
final String projectPath = await createProject(tempDir);
|
2018-02-16 09:17:28 +00:00
|
|
|
final File pubspec = fs.file(fs.path.join(projectPath, 'pubspec.yaml'));
|
|
|
|
String content = await pubspec.readAsString();
|
|
|
|
content = content.replaceFirst(
|
|
|
|
'\ndependencies:\n',
|
|
|
|
'\ndependencies:\n $plugin:\n',
|
|
|
|
);
|
|
|
|
await pubspec.writeAsString(content, flush: true);
|
|
|
|
return projectPath;
|
|
|
|
}
|
2016-08-26 02:37:48 +00:00
|
|
|
|
2018-02-16 09:17:28 +00:00
|
|
|
Future<Null> runCommandIn(String projectPath, String verb, { List<String> args }) async {
|
2018-09-12 06:29:29 +00:00
|
|
|
final PackagesCommand command = PackagesCommand();
|
2017-03-04 01:50:46 +00:00
|
|
|
final CommandRunner<Null> runner = createTestCommandRunner(command);
|
2016-08-26 02:37:48 +00:00
|
|
|
|
2017-03-13 21:04:27 +00:00
|
|
|
final List<String> commandArgs = <String>['packages', verb];
|
|
|
|
if (args != null)
|
|
|
|
commandArgs.addAll(args);
|
2017-05-08 21:08:59 +00:00
|
|
|
commandArgs.add(projectPath);
|
2017-03-13 21:04:27 +00:00
|
|
|
|
|
|
|
await runner.run(commandArgs);
|
2016-08-26 02:37:48 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:08:59 +00:00
|
|
|
void expectExists(String projectPath, String relPath) {
|
2018-02-16 09:17:28 +00:00
|
|
|
expect(
|
|
|
|
fs.isFileSync(fs.path.join(projectPath, relPath)),
|
|
|
|
true,
|
|
|
|
reason: '$projectPath/$relPath should exist, but does not',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void expectContains(String projectPath, String relPath, String substring) {
|
|
|
|
expectExists(projectPath, relPath);
|
|
|
|
expect(
|
|
|
|
fs.file(fs.path.join(projectPath, relPath)).readAsStringSync(),
|
|
|
|
contains(substring),
|
|
|
|
reason: '$projectPath/$relPath has unexpected content'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void expectNotExists(String projectPath, String relPath) {
|
|
|
|
expect(
|
|
|
|
fs.isFileSync(fs.path.join(projectPath, relPath)),
|
|
|
|
false,
|
|
|
|
reason: '$projectPath/$relPath should not exist, but does',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void expectNotContains(String projectPath, String relPath, String substring) {
|
|
|
|
expectExists(projectPath, relPath);
|
|
|
|
expect(
|
|
|
|
fs.file(fs.path.join(projectPath, relPath)).readAsStringSync(),
|
|
|
|
isNot(contains(substring)),
|
|
|
|
reason: '$projectPath/$relPath has unexpected content',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-02 10:02:32 +00:00
|
|
|
const List<String> pubOutput = <String>[
|
2018-02-16 09:17:28 +00:00
|
|
|
'.packages',
|
|
|
|
'pubspec.lock',
|
|
|
|
];
|
|
|
|
|
2018-08-02 10:02:32 +00:00
|
|
|
const List<String> pluginRegistrants = <String>[
|
2018-02-16 09:17:28 +00:00
|
|
|
'ios/Runner/GeneratedPluginRegistrant.h',
|
|
|
|
'ios/Runner/GeneratedPluginRegistrant.m',
|
|
|
|
'android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java',
|
|
|
|
];
|
|
|
|
|
2018-08-02 10:02:32 +00:00
|
|
|
const List<String> pluginWitnesses = <String>[
|
2018-02-16 09:17:28 +00:00
|
|
|
'.flutter-plugins',
|
|
|
|
'ios/Podfile',
|
|
|
|
];
|
|
|
|
|
2018-08-02 10:02:32 +00:00
|
|
|
const Map<String, String> pluginContentWitnesses = <String, String>{
|
2018-02-16 09:17:28 +00:00
|
|
|
'ios/Flutter/Debug.xcconfig': '#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"',
|
|
|
|
'ios/Flutter/Release.xcconfig': '#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"',
|
|
|
|
};
|
|
|
|
|
|
|
|
void expectDependenciesResolved(String projectPath) {
|
|
|
|
for (String output in pubOutput) {
|
|
|
|
expectExists(projectPath, output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void expectZeroPluginsInjected(String projectPath) {
|
|
|
|
for (final String registrant in pluginRegistrants) {
|
|
|
|
expectExists(projectPath, registrant);
|
|
|
|
}
|
|
|
|
for (final String witness in pluginWitnesses) {
|
|
|
|
expectNotExists(projectPath, witness);
|
|
|
|
}
|
|
|
|
pluginContentWitnesses.forEach((String witness, String content) {
|
|
|
|
expectNotContains(projectPath, witness, content);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void expectPluginInjected(String projectPath) {
|
|
|
|
for (final String registrant in pluginRegistrants) {
|
|
|
|
expectExists(projectPath, registrant);
|
|
|
|
}
|
|
|
|
for (final String witness in pluginWitnesses) {
|
|
|
|
expectExists(projectPath, witness);
|
|
|
|
}
|
|
|
|
pluginContentWitnesses.forEach((String witness, String content) {
|
|
|
|
expectContains(projectPath, witness, content);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeGeneratedFiles(String projectPath) {
|
|
|
|
final Iterable<String> allFiles = <List<String>>[
|
|
|
|
pubOutput,
|
|
|
|
pluginRegistrants,
|
|
|
|
pluginWitnesses,
|
|
|
|
].expand((List<String> list) => list);
|
|
|
|
for (String path in allFiles) {
|
|
|
|
final File file = fs.file(fs.path.join(projectPath, path));
|
|
|
|
if (file.existsSync())
|
|
|
|
file.deleteSync();
|
|
|
|
}
|
2016-08-26 02:37:48 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 09:17:28 +00:00
|
|
|
testUsingContext('get fetches packages', () async {
|
2018-08-17 20:17:23 +00:00
|
|
|
final String projectPath = await createProject(tempDir);
|
2018-02-16 09:17:28 +00:00
|
|
|
removeGeneratedFiles(projectPath);
|
|
|
|
|
|
|
|
await runCommandIn(projectPath, 'get');
|
|
|
|
|
|
|
|
expectDependenciesResolved(projectPath);
|
|
|
|
expectZeroPluginsInjected(projectPath);
|
2018-02-15 21:17:12 +00:00
|
|
|
}, timeout: allowForRemotePubInvocation);
|
|
|
|
|
2018-02-16 09:17:28 +00:00
|
|
|
testUsingContext('get --offline fetches packages', () async {
|
2018-08-17 20:17:23 +00:00
|
|
|
final String projectPath = await createProject(tempDir);
|
2018-02-16 09:17:28 +00:00
|
|
|
removeGeneratedFiles(projectPath);
|
|
|
|
|
|
|
|
await runCommandIn(projectPath, 'get', args: <String>['--offline']);
|
|
|
|
|
|
|
|
expectDependenciesResolved(projectPath);
|
|
|
|
expectZeroPluginsInjected(projectPath);
|
|
|
|
}, timeout: allowForCreateFlutterProject);
|
|
|
|
|
|
|
|
testUsingContext('upgrade fetches packages', () async {
|
2018-08-17 20:17:23 +00:00
|
|
|
final String projectPath = await createProject(tempDir);
|
2018-02-16 09:17:28 +00:00
|
|
|
removeGeneratedFiles(projectPath);
|
|
|
|
|
|
|
|
await runCommandIn(projectPath, 'upgrade');
|
|
|
|
|
|
|
|
expectDependenciesResolved(projectPath);
|
|
|
|
expectZeroPluginsInjected(projectPath);
|
|
|
|
}, timeout: allowForRemotePubInvocation);
|
|
|
|
|
|
|
|
testUsingContext('get fetches packages and injects plugin', () async {
|
|
|
|
final String projectPath = await createProjectWithPlugin('path_provider');
|
|
|
|
removeGeneratedFiles(projectPath);
|
|
|
|
|
|
|
|
await runCommandIn(projectPath, 'get');
|
2017-03-13 21:04:27 +00:00
|
|
|
|
2018-02-16 09:17:28 +00:00
|
|
|
expectDependenciesResolved(projectPath);
|
|
|
|
expectPluginInjected(projectPath);
|
2018-02-16 11:05:37 +00:00
|
|
|
// TODO(mravn): This test fails on the Chrome windows bot only.
|
|
|
|
// Skipping until resolved.
|
|
|
|
}, timeout: allowForRemotePubInvocation, skip: true);
|
2018-02-17 20:25:13 +00:00
|
|
|
testUsingContext('get fetches packages and injects plugin in plugin project', () async {
|
|
|
|
final String projectPath = await createProject(
|
2018-08-17 20:17:23 +00:00
|
|
|
tempDir,
|
2018-02-17 20:25:13 +00:00
|
|
|
arguments: <String>['-t', 'plugin', '--no-pub'],
|
|
|
|
);
|
|
|
|
final String exampleProjectPath = fs.path.join(projectPath, 'example');
|
|
|
|
removeGeneratedFiles(projectPath);
|
|
|
|
removeGeneratedFiles(exampleProjectPath);
|
|
|
|
|
|
|
|
await runCommandIn(projectPath, 'get');
|
|
|
|
|
|
|
|
expectDependenciesResolved(projectPath);
|
|
|
|
|
|
|
|
await runCommandIn(exampleProjectPath, 'get');
|
|
|
|
|
|
|
|
expectDependenciesResolved(exampleProjectPath);
|
|
|
|
expectPluginInjected(exampleProjectPath);
|
|
|
|
}, timeout: allowForRemotePubInvocation);
|
2016-08-26 02:37:48 +00:00
|
|
|
});
|
2017-05-12 22:40:35 +00:00
|
|
|
|
|
|
|
group('packages test/pub', () {
|
2017-09-06 09:53:39 +00:00
|
|
|
MockProcessManager mockProcessManager;
|
|
|
|
MockStdio mockStdio;
|
|
|
|
|
|
|
|
setUp(() {
|
2018-09-12 06:29:29 +00:00
|
|
|
mockProcessManager = MockProcessManager();
|
|
|
|
mockStdio = MockStdio();
|
2017-09-06 09:53:39 +00:00
|
|
|
});
|
|
|
|
|
2018-02-21 17:54:07 +00:00
|
|
|
testUsingContext('test without bot', () async {
|
2018-09-12 06:29:29 +00:00
|
|
|
await createTestCommandRunner(PackagesCommand()).run(<String>['packages', 'test']);
|
2018-02-21 17:54:07 +00:00
|
|
|
final List<String> commands = mockProcessManager.commands;
|
|
|
|
expect(commands, hasLength(3));
|
|
|
|
expect(commands[0], matches(r'dart-sdk[\\/]bin[\\/]pub'));
|
|
|
|
expect(commands[1], 'run');
|
|
|
|
expect(commands[2], 'test');
|
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
ProcessManager: () => mockProcessManager,
|
|
|
|
Stdio: () => mockStdio,
|
|
|
|
BotDetector: () => const AlwaysFalseBotDetector(),
|
|
|
|
});
|
|
|
|
|
|
|
|
testUsingContext('test with bot', () async {
|
2018-09-12 06:29:29 +00:00
|
|
|
await createTestCommandRunner(PackagesCommand()).run(<String>['packages', 'test']);
|
2017-09-06 09:53:39 +00:00
|
|
|
final List<String> commands = mockProcessManager.commands;
|
2017-09-30 00:56:25 +00:00
|
|
|
expect(commands, hasLength(4));
|
2017-09-06 09:53:39 +00:00
|
|
|
expect(commands[0], matches(r'dart-sdk[\\/]bin[\\/]pub'));
|
2017-09-30 00:56:25 +00:00
|
|
|
expect(commands[1], '--trace');
|
|
|
|
expect(commands[2], 'run');
|
|
|
|
expect(commands[3], 'test');
|
2017-05-12 22:40:35 +00:00
|
|
|
}, overrides: <Type, Generator>{
|
2017-09-06 09:53:39 +00:00
|
|
|
ProcessManager: () => mockProcessManager,
|
|
|
|
Stdio: () => mockStdio,
|
2018-02-21 17:54:07 +00:00
|
|
|
BotDetector: () => const AlwaysTrueBotDetector(),
|
2017-05-12 22:40:35 +00:00
|
|
|
});
|
2017-09-06 09:53:39 +00:00
|
|
|
|
2017-05-12 22:40:35 +00:00
|
|
|
testUsingContext('run', () async {
|
2018-09-12 06:29:29 +00:00
|
|
|
await createTestCommandRunner(PackagesCommand()).run(<String>['packages', '--verbose', 'pub', 'run', '--foo', 'bar']);
|
2017-09-06 09:53:39 +00:00
|
|
|
final List<String> commands = mockProcessManager.commands;
|
|
|
|
expect(commands, hasLength(4));
|
|
|
|
expect(commands[0], matches(r'dart-sdk[\\/]bin[\\/]pub'));
|
|
|
|
expect(commands[1], 'run');
|
|
|
|
expect(commands[2], '--foo');
|
|
|
|
expect(commands[3], 'bar');
|
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
ProcessManager: () => mockProcessManager,
|
|
|
|
Stdio: () => mockStdio,
|
|
|
|
});
|
|
|
|
|
|
|
|
testUsingContext('publish', () async {
|
2018-09-12 06:29:29 +00:00
|
|
|
final PromptingProcess process = PromptingProcess();
|
2017-09-06 09:53:39 +00:00
|
|
|
mockProcessManager.processFactory = (List<String> commands) => process;
|
2018-09-12 06:29:29 +00:00
|
|
|
final Future<Null> runPackages = createTestCommandRunner(PackagesCommand()).run(<String>['packages', 'pub', 'publish']);
|
2017-09-06 09:53:39 +00:00
|
|
|
final Future<Null> runPrompt = process.showPrompt('Proceed (y/n)? ', <String>['hello', 'world']);
|
2018-09-12 06:29:29 +00:00
|
|
|
final Future<Null> simulateUserInput = Future<Null>(() {
|
2017-09-06 09:53:39 +00:00
|
|
|
mockStdio.simulateStdin('y');
|
|
|
|
});
|
|
|
|
await Future.wait(<Future<Null>>[runPackages, runPrompt, simulateUserInput]);
|
|
|
|
final List<String> commands = mockProcessManager.commands;
|
|
|
|
expect(commands, hasLength(2));
|
|
|
|
expect(commands[0], matches(r'dart-sdk[\\/]bin[\\/]pub'));
|
|
|
|
expect(commands[1], 'publish');
|
|
|
|
final List<String> stdout = mockStdio.writtenToStdout;
|
|
|
|
expect(stdout, hasLength(4));
|
|
|
|
expect(stdout.sublist(0, 2), contains('Proceed (y/n)? '));
|
|
|
|
expect(stdout.sublist(0, 2), contains('y\n'));
|
|
|
|
expect(stdout[2], 'hello\n');
|
|
|
|
expect(stdout[3], 'world\n');
|
2017-05-12 22:40:35 +00:00
|
|
|
}, overrides: <Type, Generator>{
|
2017-09-06 09:53:39 +00:00
|
|
|
ProcessManager: () => mockProcessManager,
|
|
|
|
Stdio: () => mockStdio,
|
2017-05-12 22:40:35 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|