Skip to content

Commit 041848d

Browse files
authored
test(create_dart_frog): post_gen tests (#208)
1 parent cf0323f commit 041848d

File tree

6 files changed

+93
-1
lines changed

6 files changed

+93
-1
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: create_dart_frog
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- ".github/workflows/create_dart_frog.yaml"
7+
- "bricks/create_dart_frog/hooks/**"
8+
push:
9+
branches:
10+
- main
11+
paths:
12+
- ".github/workflows/create_dart_frog.yaml"
13+
- "bricks/create_dart_frog/hooks/**"
14+
15+
jobs:
16+
build:
17+
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/dart_package.yml@v1
18+
with:
19+
working_directory: bricks/create_dart_frog/hooks
20+
analyze_directories: .
21+
report_on: post_gen.dart
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.dart_tool
22
.packages
33
pubspec.lock
4+
coverage
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include: package:very_good_analysis/analysis_options.3.0.1.yaml

bricks/create_dart_frog/hooks/post_gen.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'package:path/path.dart' as path;
44

55
Future<void> run(HookContext context) async {
66
final projectDirectory = path.canonicalize(
7-
context.vars['output_directory'] ?? Directory.current.path,
7+
context.vars['output_directory'] as String? ?? Directory.current.path,
88
);
99
final progress = context.logger.progress('Installing dependencies');
1010
await Process.run(

bricks/create_dart_frog/hooks/pubspec.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ environment:
66
dependencies:
77
mason: ^0.1.0-dev.30
88
path: ^1.8.1
9+
10+
dev_dependencies:
11+
mocktail: ^0.3.0
12+
test: ^1.19.2
13+
very_good_analysis: ^3.0.1
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import 'dart:io';
2+
3+
import 'package:mason/mason.dart';
4+
import 'package:mocktail/mocktail.dart';
5+
import 'package:test/test.dart';
6+
7+
import '../post_gen.dart';
8+
9+
class _FakeHookContext extends Fake implements HookContext {
10+
_FakeHookContext({Logger? logger}) : _logger = logger ?? _MockLogger();
11+
12+
final Logger _logger;
13+
14+
var _vars = <String, dynamic>{};
15+
16+
@override
17+
Map<String, dynamic> get vars => _vars;
18+
19+
@override
20+
set vars(Map<String, dynamic> value) => _vars = value;
21+
22+
@override
23+
Logger get logger => _logger;
24+
}
25+
26+
class _MockLogger extends Mock implements Logger {}
27+
28+
class _MockProgress extends Mock implements Progress {}
29+
30+
void main() {
31+
group('postGen', () {
32+
late HookContext context;
33+
late Logger logger;
34+
35+
setUp(() {
36+
logger = _MockLogger();
37+
context = _FakeHookContext(logger: logger);
38+
39+
when(() => logger.progress(any())).thenReturn(_MockProgress());
40+
});
41+
42+
test('run completes when pubspec.yaml exists', () async {
43+
const name = 'example';
44+
final projectDirectory = Directory.current.path;
45+
context.vars = {'name': name};
46+
await expectLater(run(context), completes);
47+
verifyInOrder([
48+
() => logger.info(''),
49+
() => logger
50+
.success('Created ${context.vars['name']} at $projectDirectory.'),
51+
() => logger.info(''),
52+
() => logger.info('Get started by typing:'),
53+
() => logger.info(''),
54+
() => logger.info('${lightCyan.wrap('cd')} $projectDirectory'),
55+
() => logger.info('${lightCyan.wrap('dart_frog dev')}'),
56+
]);
57+
});
58+
59+
test('run throws when pubspec.yaml does not exist', () async {
60+
context.vars = {'output_directory': '/invalid'};
61+
await expectLater(() => run(context), throwsA(isA<Exception>()));
62+
});
63+
});
64+
}

0 commit comments

Comments
 (0)