[flutter_plugin_tools] Remove global state from tests (#4018)

Eliminates the global test filesystem and global test packages directory, in favor of local versions. This guarantees that each test runs with a clean filesystem state, rather than relying on cleanup. It also simplifies understanding the tests, since everything is done via params and return values instead of needing to know about the magic global variables and which methods mutate them.
This commit is contained in:
stuartmorgan
2021-06-07 10:04:43 -07:00
committed by GitHub
parent bb0a1ea161
commit 74d03857f8
16 changed files with 456 additions and 537 deletions

View File

@ -22,9 +22,10 @@ import 'util.dart';
void main() {
const String testPluginName = 'foo';
final List<String> printedMessages = <String>[];
List<String> printedMessages;
Directory parentDir;
Directory testRoot;
Directory packagesDir;
Directory pluginDir;
GitDir gitDir;
TestProcessRunner processRunner;
@ -43,34 +44,34 @@ void main() {
}
setUp(() async {
parentDir = fileSystem.systemTempDirectory
testRoot = fileSystem.systemTempDirectory
.createTempSync('publish_plugin_command_test-');
// The temp directory can have symbolic links, which won't match git output;
// use a fully resolved version to avoid potential path comparison issues.
parentDir = fileSystem.directory(parentDir.resolveSymbolicLinksSync());
initializeFakePackages(parentDir: parentDir);
pluginDir = createFakePlugin(testPluginName,
withSingleExample: false, packagesDirectory: parentDir);
testRoot = fileSystem.directory(testRoot.resolveSymbolicLinksSync());
packagesDir = createPackagesDirectory(parentDir: testRoot);
pluginDir =
createFakePlugin(testPluginName, packagesDir, withSingleExample: false);
assert(pluginDir != null && pluginDir.existsSync());
createFakePubspec(pluginDir, includeVersion: true);
io.Process.runSync('git', <String>['init'],
workingDirectory: parentDir.path);
gitDir = await GitDir.fromExisting(parentDir.path);
workingDirectory: testRoot.path);
gitDir = await GitDir.fromExisting(testRoot.path);
await gitDir.runCommand(<String>['add', '-A']);
await gitDir.runCommand(<String>['commit', '-m', 'Initial commit']);
processRunner = TestProcessRunner();
mockStdin = MockStdin();
printedMessages = <String>[];
commandRunner = CommandRunner<void>('tester', '')
..addCommand(PublishPluginCommand(parentDir,
..addCommand(PublishPluginCommand(packagesDir,
processRunner: processRunner,
print: (Object message) => printedMessages.add(message.toString()),
stdinput: mockStdin,
gitDir: await GitDir.fromExisting(parentDir.path)));
gitDir: gitDir));
});
tearDown(() {
parentDir.deleteSync(recursive: true);
printedMessages.clear();
testRoot.deleteSync(recursive: true);
});
group('Initial validation', () {
@ -109,7 +110,7 @@ void main() {
expect(
printedMessages,
containsAllInOrder(<String>[
'There are files in the package directory that haven\'t been saved in git. Refusing to publish these files:\n\n?? foo/tmp\n\nIf the directory should be clean, you can run `git clean -xdf && git reset --hard HEAD` to wipe all local changes.',
'There are files in the package directory that haven\'t been saved in git. Refusing to publish these files:\n\n?? packages/foo/tmp\n\nIf the directory should be clean, you can run `git clean -xdf && git reset --hard HEAD` to wipe all local changes.',
'Failed, see above for details.',
]));
});
@ -140,8 +141,8 @@ void main() {
test('can publish non-flutter package', () async {
createFakePubspec(pluginDir, includeVersion: true, isFlutter: false);
io.Process.runSync('git', <String>['init'],
workingDirectory: parentDir.path);
gitDir = await GitDir.fromExisting(parentDir.path);
workingDirectory: testRoot.path);
gitDir = await GitDir.fromExisting(testRoot.path);
await gitDir.runCommand(<String>['add', '-A']);
await gitDir.runCommand(<String>['commit', '-m', 'Initial commit']);
// Immediately return 0 when running `pub publish`.
@ -420,21 +421,19 @@ void main() {
group('Auto release (all-changed flag)', () {
setUp(() async {
io.Process.runSync('git', <String>['init'],
workingDirectory: parentDir.path);
gitDir = await GitDir.fromExisting(parentDir.path);
workingDirectory: testRoot.path);
gitDir = await GitDir.fromExisting(testRoot.path);
await gitDir.runCommand(
<String>['remote', 'add', 'upstream', 'http://localhost:8000']);
});
test('can release newly created plugins', () async {
// Non-federated
final Directory pluginDir1 = createFakePlugin('plugin1',
withSingleExample: true, packagesDirectory: parentDir);
final Directory pluginDir1 =
createFakePlugin('plugin1', packagesDir, withSingleExample: true);
// federated
final Directory pluginDir2 = createFakePlugin('plugin2',
withSingleExample: true,
parentDirectoryName: 'plugin2',
packagesDirectory: parentDir);
final Directory pluginDir2 = createFakePlugin('plugin2', packagesDir,
withSingleExample: true, parentDirectoryName: 'plugin2');
createFakePubspec(pluginDir1,
name: 'plugin1',
includeVersion: true,
@ -475,8 +474,8 @@ void main() {
test('can release newly created plugins, while there are existing plugins',
() async {
// Prepare an exiting plugin and tag it
final Directory pluginDir0 = createFakePlugin('plugin0',
withSingleExample: true, packagesDirectory: parentDir);
final Directory pluginDir0 =
createFakePlugin('plugin0', packagesDir, withSingleExample: true);
createFakePubspec(pluginDir0,
name: 'plugin0',
includeVersion: true,
@ -492,13 +491,11 @@ void main() {
processRunner.pushTagsArgs.clear();
// Non-federated
final Directory pluginDir1 = createFakePlugin('plugin1',
withSingleExample: true, packagesDirectory: parentDir);
final Directory pluginDir1 =
createFakePlugin('plugin1', packagesDir, withSingleExample: true);
// federated
final Directory pluginDir2 = createFakePlugin('plugin2',
withSingleExample: true,
parentDirectoryName: 'plugin2',
packagesDirectory: parentDir);
final Directory pluginDir2 = createFakePlugin('plugin2', packagesDir,
withSingleExample: true, parentDirectoryName: 'plugin2');
createFakePubspec(pluginDir1,
name: 'plugin1',
includeVersion: true,
@ -536,13 +533,11 @@ void main() {
test('can release newly created plugins, dry run', () async {
// Non-federated
final Directory pluginDir1 = createFakePlugin('plugin1',
withSingleExample: true, packagesDirectory: parentDir);
final Directory pluginDir1 =
createFakePlugin('plugin1', packagesDir, withSingleExample: true);
// federated
final Directory pluginDir2 = createFakePlugin('plugin2',
withSingleExample: true,
parentDirectoryName: 'plugin2',
packagesDirectory: parentDir);
final Directory pluginDir2 = createFakePlugin('plugin2', packagesDir,
withSingleExample: true, parentDirectoryName: 'plugin2');
createFakePubspec(pluginDir1,
name: 'plugin1',
includeVersion: true,
@ -585,13 +580,11 @@ void main() {
test('version change triggers releases.', () async {
// Non-federated
final Directory pluginDir1 = createFakePlugin('plugin1',
withSingleExample: true, packagesDirectory: parentDir);
final Directory pluginDir1 =
createFakePlugin('plugin1', packagesDir, withSingleExample: true);
// federated
final Directory pluginDir2 = createFakePlugin('plugin2',
withSingleExample: true,
parentDirectoryName: 'plugin2',
packagesDirectory: parentDir);
final Directory pluginDir2 = createFakePlugin('plugin2', packagesDir,
withSingleExample: true, parentDirectoryName: 'plugin2');
createFakePubspec(pluginDir1,
name: 'plugin1',
includeVersion: true,
@ -676,13 +669,11 @@ void main() {
'delete package will not trigger publish but exit the command successfully.',
() async {
// Non-federated
final Directory pluginDir1 = createFakePlugin('plugin1',
withSingleExample: true, packagesDirectory: parentDir);
final Directory pluginDir1 =
createFakePlugin('plugin1', packagesDir, withSingleExample: true);
// federated
final Directory pluginDir2 = createFakePlugin('plugin2',
withSingleExample: true,
parentDirectoryName: 'plugin2',
packagesDirectory: parentDir);
final Directory pluginDir2 = createFakePlugin('plugin2', packagesDir,
withSingleExample: true, parentDirectoryName: 'plugin2');
createFakePubspec(pluginDir1,
name: 'plugin1',
includeVersion: true,
@ -764,13 +755,11 @@ void main() {
'versions revert do not trigger releases. Also prints out warning message.',
() async {
// Non-federated
final Directory pluginDir1 = createFakePlugin('plugin1',
withSingleExample: true, packagesDirectory: parentDir);
final Directory pluginDir1 =
createFakePlugin('plugin1', packagesDir, withSingleExample: true);
// federated
final Directory pluginDir2 = createFakePlugin('plugin2',
withSingleExample: true,
parentDirectoryName: 'plugin2',
packagesDirectory: parentDir);
final Directory pluginDir2 = createFakePlugin('plugin2', packagesDir,
withSingleExample: true, parentDirectoryName: 'plugin2');
createFakePubspec(pluginDir1,
name: 'plugin1',
includeVersion: true,
@ -846,13 +835,11 @@ void main() {
test('No version change does not release any plugins', () async {
// Non-federated
final Directory pluginDir1 = createFakePlugin('plugin1',
withSingleExample: true, packagesDirectory: parentDir);
final Directory pluginDir1 =
createFakePlugin('plugin1', packagesDir, withSingleExample: true);
// federated
final Directory pluginDir2 = createFakePlugin('plugin2',
withSingleExample: true,
parentDirectoryName: 'plugin2',
packagesDirectory: parentDir);
final Directory pluginDir2 = createFakePlugin('plugin2', packagesDir,
withSingleExample: true, parentDirectoryName: 'plugin2');
createFakePubspec(pluginDir1,
name: 'plugin1',
includeVersion: true,
@ -865,8 +852,8 @@ void main() {
version: '0.0.1');
io.Process.runSync('git', <String>['init'],
workingDirectory: parentDir.path);
gitDir = await GitDir.fromExisting(parentDir.path);
workingDirectory: testRoot.path);
gitDir = await GitDir.fromExisting(testRoot.path);
await gitDir.runCommand(<String>['add', '-A']);
await gitDir.runCommand(<String>['commit', '-m', 'Add plugins']);