Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Helps to add the new files
  • Loading branch information
stuartmorgan-g committed Jul 7, 2023
commit 68b67f539098d2e8b84d7584529612f5ffc1da38
44 changes: 44 additions & 0 deletions script/tool/lib/src/common/pub_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2013 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 'dart:io' as io;

import 'package:platform/platform.dart';

import 'process_runner.dart';
import 'repository_package.dart';

/// Runs either `dart pub get` or `flutter pub get` in [package], depending on
/// the package type.
///
/// If [alwaysUseFlutter] is true, it will use `flutter pub get` regardless.
/// This can be useful, for instance, to get the `flutter`-default behavior
/// of fetching example packages as well.
///
/// If [streamOutput] is false, output will only be printed if the command
/// fails.
Future<bool> runPubGet(
RepositoryPackage package, ProcessRunner processRunner, Platform platform,
{bool alwaysUseFlutter = false, bool streamOutput = true}) async {
// Running `dart pub get` on a Flutter package can fail if a non-Flutter Dart
// is first in the path, so use `flutter pub get` for any Flutter package.
final bool useFlutter = alwaysUseFlutter || package.requiresFlutter();
final String command =
useFlutter ? (platform.isWindows ? 'flutter.bat' : 'flutter') : 'dart';
final List<String> args = <String>['pub', 'get'];

final int exitCode;
if (streamOutput) {
exitCode = await processRunner.runAndStream(command, args,
workingDir: package.directory);
} else {
final io.ProcessResult result =
await processRunner.run(command, args, workingDir: package.directory);
exitCode = result.exitCode;
if (exitCode != 0) {
print('${result.stdout}\n${result.stderr}\n');
}
}
return exitCode == 0;
}
104 changes: 104 additions & 0 deletions script/tool/test/common/pub_utils_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright 2013 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/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_plugin_tools/src/common/pub_utils.dart';
import 'package:test/test.dart';

import '../mocks.dart';
import '../util.dart';

void main() {
late FileSystem fileSystem;
late Directory packagesDir;
late RecordingProcessRunner processRunner;

setUp(() {
fileSystem = MemoryFileSystem();
packagesDir = createPackagesDirectory(fileSystem: fileSystem);
processRunner = RecordingProcessRunner();
});

test('runs with Dart for a non-Flutter package by default', () async {
final RepositoryPackage package =
createFakePackage('a_package', packagesDir);
final MockPlatform platform = MockPlatform();

await runPubGet(package, processRunner, platform);

expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall('dart', const <String>['pub', 'get'], package.path),
]));
});

test('runs with Flutter for a Flutter package by default', () async {
final RepositoryPackage package =
createFakePackage('a_package', packagesDir, isFlutter: true);
final MockPlatform platform = MockPlatform();

await runPubGet(package, processRunner, platform);

expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall('flutter', const <String>['pub', 'get'], package.path),
]));
});

test('runs with Flutter for a Dart package when requested', () async {
final RepositoryPackage package =
createFakePackage('a_package', packagesDir);
final MockPlatform platform = MockPlatform();

await runPubGet(package, processRunner, platform, alwaysUseFlutter: true);

expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall('flutter', const <String>['pub', 'get'], package.path),
]));
});

test('uses the correct Flutter command on Windows', () async {
final RepositoryPackage package =
createFakePackage('a_package', packagesDir, isFlutter: true);
final MockPlatform platform = MockPlatform(isWindows: true);

await runPubGet(package, processRunner, platform);

expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall(
'flutter.bat', const <String>['pub', 'get'], package.path),
]));
});

test('reports success', () async {
final RepositoryPackage package =
createFakePackage('a_package', packagesDir);
final MockPlatform platform = MockPlatform();

final bool result = await runPubGet(package, processRunner, platform);

expect(result, true);
});

test('reports failure', () async {
final RepositoryPackage package =
createFakePackage('a_package', packagesDir);
final MockPlatform platform = MockPlatform();

processRunner.mockProcessesForExecutable['dart'] = <FakeProcessInfo>[
FakeProcessInfo(MockProcess(exitCode: 1), <String>['pub', 'get'])
];

final bool result = await runPubGet(package, processRunner, platform);

expect(result, false);
});
}