[tool] Allow running from anywhere (#4199)

Now that the repo tooling is always run from source, not via `pub global`, we no longer need to infer the repo location from the current directory. Instead, hard-code knowledge of where the repository root is. This makes it much easier to run the tooling, since it's common to be in a package directory rather than the repo root.

To make it even easier to run from within a package, this also adds a `--current-package` as an alternative to `--packages`. This makes it possible to, e.g., write local wrapper scripts that run a specific set of commands on whatever the current package happens to be (such as a generic version of the script discussed in https://github.com/flutter/packages/pull/4129).

As related cleanup, makes the tool non-publishable (we haven't been publishing it since the repo merge, but I never made it unpublishable; this is important now that it would not work if published) and remove the LICENSE and CHANGELOG since it's no longer a stand-alone package.

Fixes https://github.com/flutter/flutter/issues/128231
Fixes https://github.com/flutter/flutter/issues/128232
This commit is contained in:
stuartmorgan
2023-06-13 13:24:56 -04:00
committed by GitHub
parent 1dad75928f
commit eed9b68284
7 changed files with 182 additions and 734 deletions

View File

@ -319,8 +319,7 @@ packages/plugin1/plugin1/plugin1.dart
expect(
output,
containsAllInOrder(<Matcher>[
contains('Only one of --packages, --run-on-changed-packages, or '
'--packages-for-branch can be provided.')
contains('Only one of the package selection arguments')
]));
});
@ -338,8 +337,25 @@ packages/plugin1/plugin1/plugin1.dart
expect(
output,
containsAllInOrder(<Matcher>[
contains('Only one of --packages, --run-on-changed-packages, or '
'--packages-for-branch can be provided.')
contains('Only one of the package selection arguments')
]));
});
test('does not allow --packages with --current-package', () async {
Error? commandError;
final List<String> output = await runCapturingPrint(runner, <String>[
'sample',
'--current-package',
'--packages=plugin1',
], errorHandler: (Error e) {
commandError = e;
});
expect(commandError, isA<ToolExit>());
expect(
output,
containsAllInOrder(<Matcher>[
contains('Only one of the package selection arguments')
]));
});
@ -359,12 +375,96 @@ packages/plugin1/plugin1/plugin1.dart
expect(
output,
containsAllInOrder(<Matcher>[
contains('Only one of --packages, --run-on-changed-packages, or '
'--packages-for-branch can be provided.')
contains('Only one of the package selection arguments')
]));
});
});
group('current-package', () {
test('throws when run from outside of the packages directory', () async {
fileSystem.currentDirectory = packagesDir.parent;
Error? commandError;
final List<String> output = await runCapturingPrint(runner, <String>[
'sample',
'--current-package',
], errorHandler: (Error e) {
commandError = e;
});
expect(commandError, isA<ToolExit>());
expect(
output,
containsAllInOrder(<Matcher>[
contains('--current-package can only be used within a repository '
'package or package group')
]));
});
test('throws when run directly in the packages directory', () async {
fileSystem.currentDirectory = packagesDir;
Error? commandError;
final List<String> output = await runCapturingPrint(runner, <String>[
'sample',
'--current-package',
], errorHandler: (Error e) {
commandError = e;
});
expect(commandError, isA<ToolExit>());
expect(
output,
containsAllInOrder(<Matcher>[
contains('--current-package can only be used within a repository '
'package or package group')
]));
});
test('runs on a package when run from the package directory', () async {
final RepositoryPackage package =
createFakePlugin('a_package', packagesDir);
createFakePlugin('another_package', packagesDir);
fileSystem.currentDirectory = package.directory;
await runCapturingPrint(
runner, <String>['sample', '--current-package']);
expect(command.plugins, unorderedEquals(<String>[package.path]));
});
test('runs on a package when run from a package example directory',
() async {
final RepositoryPackage package = createFakePlugin(
'a_package', packagesDir,
examples: <String>['a', 'b', 'c']);
createFakePlugin('another_package', packagesDir);
fileSystem.currentDirectory = package.getExamples().first.directory;
await runCapturingPrint(
runner, <String>['sample', '--current-package']);
expect(command.plugins, unorderedEquals(<String>[package.path]));
});
test('runs on a package group when run from the group directory',
() async {
final Directory pluginGroup = packagesDir.childDirectory('a_plugin');
final RepositoryPackage plugin1 =
createFakePlugin('a_plugin_foo', pluginGroup);
final RepositoryPackage plugin2 =
createFakePlugin('a_plugin_bar', pluginGroup);
createFakePlugin('unrelated_plugin', packagesDir);
fileSystem.currentDirectory = pluginGroup;
await runCapturingPrint(
runner, <String>['sample', '--current-package']);
expect(command.plugins,
unorderedEquals(<String>[plugin1.path, plugin2.path]));
});
});
group('test run-on-changed-packages', () {
test('all plugins should be tested if there are no changes.', () async {
final RepositoryPackage plugin1 =