Skip to content

Commit 3d0c708

Browse files
authored
feat(dart_frog_prod_server): report rogue routes (#253)
1 parent ab612b4 commit 3d0c708

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

bricks/dart_frog_prod_server/hooks/pre_gen.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Future<void> preGen(
3333
}
3434

3535
reportRouteConflicts(context, configuration, _exit);
36+
reportRogueRoutes(context, configuration, _exit);
3637

3738
context.vars = {
3839
'directories': configuration.directories
@@ -120,6 +121,28 @@ void reportRouteConflicts(
120121
}
121122
}
122123

124+
void reportRogueRoutes(
125+
HookContext context,
126+
RouteConfiguration configuration,
127+
void Function(int exitCode) exit,
128+
) {
129+
if (configuration.rogueRoutes.isNotEmpty) {
130+
for (final route in configuration.rogueRoutes) {
131+
final filePath = path.normalize(path.join('routes', route.path));
132+
final fileDirectory = path.dirname(filePath);
133+
final idealPath = path.join(
134+
fileDirectory,
135+
path.basenameWithoutExtension(filePath),
136+
'index.dart',
137+
);
138+
context.logger.err(
139+
'''Rogue route detected.${defaultForeground.wrap(' ')}Rename ${lightCyan.wrap(filePath)} to ${lightCyan.wrap(idealPath)}.''',
140+
);
141+
}
142+
exit(1);
143+
}
144+
}
145+
123146
const _asyncRunZoned = runZoned;
124147

125148
abstract class ExitOverrides {

bricks/dart_frog_prod_server/hooks/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ environment:
55
sdk: ">=2.12.0 <3.0.0"
66

77
dependencies:
8-
dart_frog_gen: ^0.0.2-dev.6
8+
dart_frog_gen: ^0.0.2-dev.7
99
io: ^1.0.3
1010
mason: ^0.1.0-dev.31
1111
path: ^1.8.1

bricks/dart_frog_prod_server/hooks/test/pre_gen_test.dart

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import 'dart:io';
22

33
import 'package:dart_frog_gen/dart_frog_gen.dart';
4-
import 'package:mason/mason.dart' show HookContext, Logger, Progress, lightCyan;
4+
import 'package:mason/mason.dart'
5+
show HookContext, Logger, Progress, defaultForeground, lightCyan;
56
import 'package:mocktail/mocktail.dart';
67
import 'package:path/path.dart' as path;
78
import 'package:test/test.dart';
@@ -95,6 +96,7 @@ void main() {
9596
RouteFile(name: 'index', path: 'index.dart', route: '/'),
9697
RouteFile(name: 'hello', path: 'hello.dart', route: '/hello'),
9798
],
99+
rogueRoutes: [],
98100
endpoints: {
99101
'/': [
100102
RouteFile(name: 'index', path: 'index.dart', route: '/'),
@@ -302,6 +304,66 @@ dev_dependencies:
302304
expect(exitCalls, equals([1]));
303305
});
304306
});
307+
308+
group('reportRogueRoutes', () {
309+
late HookContext context;
310+
late Logger logger;
311+
late RouteConfiguration configuration;
312+
313+
setUp(() {
314+
context = _MockHookContext();
315+
logger = _MockLogger();
316+
configuration = _MockRouteConfiguration();
317+
318+
when(() => context.logger).thenReturn(logger);
319+
});
320+
321+
test('reports nothing when there are no rogue routes', () {
322+
final exitCalls = <int>[];
323+
when(() => configuration.rogueRoutes).thenReturn([]);
324+
reportRogueRoutes(context, configuration, exitCalls.add);
325+
verifyNever(() => logger.err(any()));
326+
expect(exitCalls, isEmpty);
327+
});
328+
329+
test('reports single rogue route', () {
330+
final exitCalls = <int>[];
331+
when(() => configuration.rogueRoutes).thenReturn(
332+
const [
333+
RouteFile(name: 'hello', path: 'hello.dart', route: '/hello'),
334+
],
335+
);
336+
reportRogueRoutes(context, configuration, exitCalls.add);
337+
verify(
338+
() => logger.err(
339+
'''Rogue route detected.${defaultForeground.wrap(' ')}Rename ${lightCyan.wrap('routes/hello.dart')} to ${lightCyan.wrap('routes/hello/index.dart')}.''',
340+
),
341+
);
342+
expect(exitCalls, equals([1]));
343+
});
344+
345+
test('reports multiple rogue routes', () {
346+
final exitCalls = <int>[];
347+
when(() => configuration.rogueRoutes).thenReturn(
348+
const [
349+
RouteFile(name: 'hello', path: 'hello.dart', route: '/hello'),
350+
RouteFile(name: 'hi', path: 'hi.dart', route: '/hi'),
351+
],
352+
);
353+
reportRogueRoutes(context, configuration, exitCalls.add);
354+
verify(
355+
() => logger.err(
356+
'''Rogue route detected.${defaultForeground.wrap(' ')}Rename ${lightCyan.wrap('routes/hello.dart')} to ${lightCyan.wrap('routes/hello/index.dart')}.''',
357+
),
358+
);
359+
verify(
360+
() => logger.err(
361+
'''Rogue route detected.${defaultForeground.wrap(' ')}Rename ${lightCyan.wrap('routes/hi.dart')} to ${lightCyan.wrap('routes/hi/index.dart')}.''',
362+
),
363+
);
364+
expect(exitCalls, equals([1]));
365+
});
366+
});
305367
});
306368

307369
group('ExitOverrides', () {

0 commit comments

Comments
 (0)