|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:hooks/pre_gen.dart' as pre_gen; |
| 4 | +import 'package:mason/mason.dart'; |
| 5 | +import 'package:mocktail/mocktail.dart'; |
| 6 | +import 'package:path/path.dart' as path; |
| 7 | +import 'package:test/test.dart'; |
| 8 | + |
| 9 | +class MockProgress extends Mock implements Progress {} |
| 10 | + |
| 11 | +class MockLogger extends Mock implements Logger {} |
| 12 | + |
| 13 | +class FakeContext extends Fake implements HookContext { |
| 14 | + @override |
| 15 | + final logger = MockLogger(); |
| 16 | + |
| 17 | + @override |
| 18 | + Map<String, Object?> vars = {}; |
| 19 | +} |
| 20 | + |
| 21 | +void main() { |
| 22 | + late HookContext context; |
| 23 | + |
| 24 | + setUp(() { |
| 25 | + context = FakeContext(); |
| 26 | + }); |
| 27 | + |
| 28 | + group('Pre gen hook', () { |
| 29 | + group('Completes', () { |
| 30 | + test('with test files list', () async { |
| 31 | + final packageRoot = |
| 32 | + Directory.systemTemp.createTempSync('test_optimizer'); |
| 33 | + File(path.join(packageRoot.path, 'pubspec.yaml')).createSync(); |
| 34 | + |
| 35 | + final testDir = Directory(path.join(packageRoot.path, 'test')) |
| 36 | + ..createSync(); |
| 37 | + File(path.join(testDir.path, 'test1_test.dart')).createSync(); |
| 38 | + File(path.join(testDir.path, 'test2_test.dart')).createSync(); |
| 39 | + File(path.join(testDir.path, 'no_test_here.dart')).createSync(); |
| 40 | + |
| 41 | + context.vars['package-root'] = packageRoot.absolute.path; |
| 42 | + |
| 43 | + await pre_gen.run(context); |
| 44 | + |
| 45 | + final tests = context.vars['tests'] as List<String>; |
| 46 | + |
| 47 | + expect( |
| 48 | + tests, |
| 49 | + containsAll([ |
| 50 | + 'test2_test.dart', |
| 51 | + 'test1_test.dart', |
| 52 | + ]), |
| 53 | + ); |
| 54 | + expect(test, isNot(contains('no_test_here.dart'))); |
| 55 | + expect(context.vars['isFlutter'], false); |
| 56 | + }); |
| 57 | + |
| 58 | + test('with proper isFlutter identification', () async { |
| 59 | + final packageRoot = |
| 60 | + Directory.systemTemp.createTempSync('test_optimizer'); |
| 61 | + |
| 62 | + File(path.join(packageRoot.path, 'pubspec.yaml')) |
| 63 | + ..createSync() |
| 64 | + ..writeAsStringSync(''' |
| 65 | +dependencies: |
| 66 | + flutter: |
| 67 | + sdk: flutter'''); |
| 68 | + |
| 69 | + Directory(path.join(packageRoot.path, 'test')).createSync(); |
| 70 | + |
| 71 | + context.vars['package-root'] = packageRoot.absolute.path; |
| 72 | + |
| 73 | + await pre_gen.run(context); |
| 74 | + |
| 75 | + expect(context.vars['isFlutter'], true); |
| 76 | + }); |
| 77 | + }); |
| 78 | + group('Fails', () { |
| 79 | + setUp(() { |
| 80 | + pre_gen.exitFn = (code) { |
| 81 | + throw ProcessException('exit', [code.toString()]); |
| 82 | + }; |
| 83 | + }); |
| 84 | + |
| 85 | + tearDown(() { |
| 86 | + pre_gen.exitFn = exit; |
| 87 | + }); |
| 88 | + |
| 89 | + test('when target test dir does not exist', () async { |
| 90 | + final packageRoot = |
| 91 | + Directory.systemTemp.createTempSync('test_optimizer'); |
| 92 | + File(path.join(packageRoot.path, 'pubspec.yaml')).createSync(); |
| 93 | + |
| 94 | + final testDir = Directory(path.join(packageRoot.path, 'test')); |
| 95 | + |
| 96 | + context.vars['package-root'] = packageRoot.absolute.path; |
| 97 | + |
| 98 | + await expectLater( |
| 99 | + () => pre_gen.run(context), |
| 100 | + throwsA( |
| 101 | + isA<ProcessException>().having( |
| 102 | + (ex) => ex.arguments.first, |
| 103 | + 'error code', |
| 104 | + equals('1'), |
| 105 | + ), |
| 106 | + ), |
| 107 | + ); |
| 108 | + |
| 109 | + verify( |
| 110 | + () => context.logger.err('Could not find directory ${testDir.path}'), |
| 111 | + ).called(1); |
| 112 | + |
| 113 | + expect(context.vars['tests'], isNull); |
| 114 | + expect(context.vars['isFlutter'], isNull); |
| 115 | + }); |
| 116 | + test('when target dir does not contain a pubspec.yaml', () async { |
| 117 | + final packageRoot = |
| 118 | + Directory.systemTemp.createTempSync('test_optimizer'); |
| 119 | + |
| 120 | + final testDir = Directory(path.join(packageRoot.path, 'test')) |
| 121 | + ..createSync(); |
| 122 | + File(path.join(testDir.path, 'test1_test.dart')).createSync(); |
| 123 | + File(path.join(testDir.path, 'test2_test.dart')).createSync(); |
| 124 | + File(path.join(testDir.path, 'no_test_here.dart')).createSync(); |
| 125 | + |
| 126 | + context.vars['package-root'] = packageRoot.absolute.path; |
| 127 | + |
| 128 | + await expectLater( |
| 129 | + () => pre_gen.run(context), |
| 130 | + throwsA( |
| 131 | + isA<ProcessException>().having( |
| 132 | + (ex) => ex.arguments.first, |
| 133 | + 'error code', |
| 134 | + equals('1'), |
| 135 | + ), |
| 136 | + ), |
| 137 | + ); |
| 138 | + |
| 139 | + verify( |
| 140 | + () => context.logger.err( |
| 141 | + 'Could not find pubspec.yaml at ${testDir.path}', |
| 142 | + ), |
| 143 | + ).called(1); |
| 144 | + |
| 145 | + expect(context.vars['tests'], isNull); |
| 146 | + expect(context.vars['isFlutter'], isNull); |
| 147 | + }); |
| 148 | + }); |
| 149 | + }); |
| 150 | +} |
0 commit comments