Skip to content

Commit b2ae22b

Browse files
authored
refactor: ensure temporary test directories are deleted (#713)
1 parent 402b279 commit b2ae22b

29 files changed

+389
-295
lines changed

bricks/test_optimizer/hooks/test/pre_gen_test.dart

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ class _FakeContext extends Fake implements HookContext {
1717
}
1818

1919
void main() {
20+
late Directory tempDirectory;
21+
22+
setUp(() {
23+
tempDirectory = Directory.systemTemp.createTempSync('test_optimizer');
24+
});
25+
26+
tearDown(() {
27+
tempDirectory.deleteSync(recursive: true);
28+
});
29+
2030
group('Pre gen hook', () {
2131
late HookContext context;
2232

@@ -26,17 +36,15 @@ void main() {
2636

2737
group('Completes', () {
2838
test('with test files list', () async {
29-
final packageRoot =
30-
Directory.systemTemp.createTempSync('test_optimizer');
31-
File(path.join(packageRoot.path, 'pubspec.yaml')).createSync();
39+
File(path.join(tempDirectory.path, 'pubspec.yaml')).createSync();
3240

33-
final testDir = Directory(path.join(packageRoot.path, 'test'))
41+
final testDir = Directory(path.join(tempDirectory.path, 'test'))
3442
..createSync();
3543
File(path.join(testDir.path, 'test1_test.dart')).createSync();
3644
File(path.join(testDir.path, 'test2_test.dart')).createSync();
3745
File(path.join(testDir.path, 'no_test_here.dart')).createSync();
3846

39-
context.vars['package-root'] = packageRoot.absolute.path;
47+
context.vars['package-root'] = tempDirectory.absolute.path;
4048

4149
await pre_gen.run(context);
4250

@@ -63,25 +71,23 @@ void main() {
6371
});
6472

6573
test('with proper isFlutter identification', () async {
66-
final packageRoot =
67-
Directory.systemTemp.createTempSync('test_optimizer');
68-
69-
File(path.join(packageRoot.path, 'pubspec.yaml'))
74+
File(path.join(tempDirectory.path, 'pubspec.yaml'))
7075
..createSync()
7176
..writeAsStringSync('''
7277
dependencies:
7378
flutter:
7479
sdk: flutter''');
7580

76-
Directory(path.join(packageRoot.path, 'test')).createSync();
81+
Directory(path.join(tempDirectory.path, 'test')).createSync();
7782

78-
context.vars['package-root'] = packageRoot.absolute.path;
83+
context.vars['package-root'] = tempDirectory.absolute.path;
7984

8085
await pre_gen.run(context);
8186

8287
expect(context.vars['isFlutter'], true);
8388
});
8489
});
90+
8591
group('Fails', () {
8692
setUp(() {
8793
pre_gen.exitFn = (code) {
@@ -94,13 +100,11 @@ dependencies:
94100
});
95101

96102
test('when target test dir does not exist', () async {
97-
final packageRoot =
98-
Directory.systemTemp.createTempSync('test_optimizer');
99-
File(path.join(packageRoot.path, 'pubspec.yaml')).createSync();
103+
File(path.join(tempDirectory.path, 'pubspec.yaml')).createSync();
100104

101-
final testDir = Directory(path.join(packageRoot.path, 'test'));
105+
final testDir = Directory(path.join(tempDirectory.path, 'test'));
102106

103-
context.vars['package-root'] = packageRoot.absolute.path;
107+
context.vars['package-root'] = tempDirectory.absolute.path;
104108

105109
await expectLater(
106110
() => pre_gen.run(context),
@@ -122,16 +126,13 @@ dependencies:
122126
});
123127

124128
test('when target dir does not contain a pubspec.yaml', () async {
125-
final packageRoot =
126-
Directory.systemTemp.createTempSync('test_optimizer');
127-
128-
final testDir = Directory(path.join(packageRoot.path, 'test'))
129+
final testDir = Directory(path.join(tempDirectory.path, 'test'))
129130
..createSync();
130131
File(path.join(testDir.path, 'test1_test.dart')).createSync();
131132
File(path.join(testDir.path, 'test2_test.dart')).createSync();
132133
File(path.join(testDir.path, 'no_test_here.dart')).createSync();
133134

134-
context.vars['package-root'] = packageRoot.absolute.path;
135+
context.vars['package-root'] = tempDirectory.absolute.path;
135136

136137
await expectLater(
137138
() => pre_gen.run(context),

lib/src/commands/test/templates/test_optimizer_bundle.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/helpers/test_multi_template_commands.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ Future<void> testMultiTemplateCommand({
2121
required Map<String, dynamic> expectedVars,
2222
required String expectedLogSummary,
2323
}) async {
24-
final tempDir = Directory.systemTemp.createTempSync();
25-
addTearDown(() => tempDir.deleteSync(recursive: true));
24+
final tempDirectory = Directory.systemTemp.createTempSync();
25+
addTearDown(() => tempDirectory.deleteSync(recursive: true));
26+
2627
final argResults = _MockArgResults();
2728
final command = multiTemplatesCommand..argResultOverrides = argResults;
2829

2930
when(() => argResults['template'] as String?).thenReturn(templateName);
3031
when(() => argResults['output-directory'] as String?)
31-
.thenReturn(tempDir.path);
32+
.thenReturn(tempDirectory.path);
3233

3334
for (final entry in mockArgs.entries) {
3435
when(() => argResults[entry.key]).thenReturn(entry.value);

0 commit comments

Comments
 (0)