Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 464303f

Browse files
[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.
1 parent 996a4a9 commit 464303f

16 files changed

+456
-537
lines changed

script/tool/test/analyze_command_test.dart

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:args/command_runner.dart';
66
import 'package:file/file.dart';
7+
import 'package:file/memory.dart';
78
import 'package:flutter_plugin_tools/src/analyze_command.dart';
89
import 'package:flutter_plugin_tools/src/common.dart';
910
import 'package:test/test.dart';
@@ -12,26 +13,25 @@ import 'mocks.dart';
1213
import 'util.dart';
1314

1415
void main() {
16+
late FileSystem fileSystem;
17+
late Directory packagesDir;
1518
late RecordingProcessRunner processRunner;
1619
late CommandRunner<void> runner;
1720

1821
setUp(() {
19-
initializeFakePackages();
22+
fileSystem = MemoryFileSystem();
23+
packagesDir = createPackagesDirectory(fileSystem: fileSystem);
2024
processRunner = RecordingProcessRunner();
2125
final AnalyzeCommand analyzeCommand =
22-
AnalyzeCommand(mockPackagesDir, processRunner: processRunner);
26+
AnalyzeCommand(packagesDir, processRunner: processRunner);
2327

2428
runner = CommandRunner<void>('analyze_command', 'Test for analyze_command');
2529
runner.addCommand(analyzeCommand);
2630
});
2731

28-
tearDown(() {
29-
mockPackagesDir.deleteSync(recursive: true);
30-
});
31-
3232
test('analyzes all packages', () async {
33-
final Directory plugin1Dir = createFakePlugin('a');
34-
final Directory plugin2Dir = createFakePlugin('b');
33+
final Directory plugin1Dir = createFakePlugin('a', packagesDir);
34+
final Directory plugin2Dir = createFakePlugin('b', packagesDir);
3535

3636
final MockProcess mockProcess = MockProcess();
3737
mockProcess.exitCodeCompleter.complete(0);
@@ -53,7 +53,8 @@ void main() {
5353
});
5454

5555
test('skips flutter pub get for examples', () async {
56-
final Directory plugin1Dir = createFakePlugin('a', withSingleExample: true);
56+
final Directory plugin1Dir =
57+
createFakePlugin('a', packagesDir, withSingleExample: true);
5758

5859
final MockProcess mockProcess = MockProcess();
5960
mockProcess.exitCodeCompleter.complete(0);
@@ -71,8 +72,8 @@ void main() {
7172
});
7273

7374
test('don\'t elide a non-contained example package', () async {
74-
final Directory plugin1Dir = createFakePlugin('a');
75-
final Directory plugin2Dir = createFakePlugin('example');
75+
final Directory plugin1Dir = createFakePlugin('a', packagesDir);
76+
final Directory plugin2Dir = createFakePlugin('example', packagesDir);
7677

7778
final MockProcess mockProcess = MockProcess();
7879
mockProcess.exitCodeCompleter.complete(0);
@@ -94,7 +95,7 @@ void main() {
9495
});
9596

9697
test('uses a separate analysis sdk', () async {
97-
final Directory pluginDir = createFakePlugin('a');
98+
final Directory pluginDir = createFakePlugin('a', packagesDir);
9899

99100
final MockProcess mockProcess = MockProcess();
100101
mockProcess.exitCodeCompleter.complete(0);
@@ -120,7 +121,7 @@ void main() {
120121

121122
group('verifies analysis settings', () {
122123
test('fails analysis_options.yaml', () async {
123-
createFakePlugin('foo', withExtraFiles: <List<String>>[
124+
createFakePlugin('foo', packagesDir, withExtraFiles: <List<String>>[
124125
<String>['analysis_options.yaml']
125126
]);
126127

@@ -129,7 +130,7 @@ void main() {
129130
});
130131

131132
test('fails .analysis_options', () async {
132-
createFakePlugin('foo', withExtraFiles: <List<String>>[
133+
createFakePlugin('foo', packagesDir, withExtraFiles: <List<String>>[
133134
<String>['.analysis_options']
134135
]);
135136

@@ -139,7 +140,7 @@ void main() {
139140

140141
test('takes an allow list', () async {
141142
final Directory pluginDir =
142-
createFakePlugin('foo', withExtraFiles: <List<String>>[
143+
createFakePlugin('foo', packagesDir, withExtraFiles: <List<String>>[
143144
<String>['analysis_options.yaml']
144145
]);
145146

@@ -160,7 +161,7 @@ void main() {
160161

161162
// See: https://github.com/flutter/flutter/issues/78994
162163
test('takes an empty allow list', () async {
163-
createFakePlugin('foo', withExtraFiles: <List<String>>[
164+
createFakePlugin('foo', packagesDir, withExtraFiles: <List<String>>[
164165
<String>['analysis_options.yaml']
165166
]);
166167

0 commit comments

Comments
 (0)