Skip to content

Commit ab612b4

Browse files
authored
feat(dart_frog_dev_server): report rogue routes (#251)
1 parent 0b32fca commit ab612b4

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

bricks/dart_frog_dev_server/hooks/pre_gen.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Future<void> preGen(
2626
}
2727

2828
reportRouteConflicts(context, configuration);
29+
reportRogueRoutes(context, configuration);
2930

3031
context.vars = {
3132
'port': context.vars['port'] ?? '8080',
@@ -62,7 +63,27 @@ void reportRouteConflicts(
6263
'''Route conflict detected. ${lightCyan.wrap(originalFilePath)} and ${lightCyan.wrap(conflictingFilePath)} both resolve to ${lightCyan.wrap(conflict.key)}.''',
6364
);
6465
}
66+
}
67+
}
68+
69+
void reportRogueRoutes(
70+
HookContext context,
71+
RouteConfiguration configuration,
72+
) {
73+
if (configuration.rogueRoutes.isNotEmpty) {
6574
context.logger.info('');
75+
for (final route in configuration.rogueRoutes) {
76+
final filePath = path.normalize(path.join('routes', route.path));
77+
final fileDirectory = path.dirname(filePath);
78+
final idealPath = path.join(
79+
fileDirectory,
80+
path.basenameWithoutExtension(filePath),
81+
'index.dart',
82+
);
83+
context.logger.err(
84+
'''Rogue route detected.${defaultForeground.wrap(' ')}Rename ${lightCyan.wrap(filePath)} to ${lightCyan.wrap(idealPath)}.''',
85+
);
86+
}
6687
}
6788
}
6889

bricks/dart_frog_dev_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
mason: ^0.1.0-dev.31
1010

1111
dev_dependencies:

bricks/dart_frog_dev_server/hooks/test/pre_gen_test.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ void main() {
6969
middleware: [],
7070
directories: [],
7171
routes: [],
72+
rogueRoutes: [],
7273
endpoints: {},
7374
);
7475
final exitCalls = <int>[];
@@ -128,6 +129,7 @@ void main() {
128129
RouteFile(name: 'hello', path: 'hello.dart', route: '/hello'),
129130
]
130131
},
132+
rogueRoutes: [],
131133
serveStaticFiles: true,
132134
);
133135
final exitCalls = <int>[];
@@ -248,6 +250,60 @@ void main() {
248250
});
249251
});
250252

253+
group('reportRogueRoutes', () {
254+
late HookContext context;
255+
late Logger logger;
256+
late RouteConfiguration configuration;
257+
258+
setUp(() {
259+
context = _MockHookContext();
260+
logger = _MockLogger();
261+
configuration = _MockRouteConfiguration();
262+
263+
when(() => context.logger).thenReturn(logger);
264+
});
265+
266+
test('reports nothing when there are no rogue routes', () {
267+
when(() => configuration.rogueRoutes).thenReturn([]);
268+
reportRogueRoutes(context, configuration);
269+
verifyNever(() => logger.err(any()));
270+
});
271+
272+
test('reports single rogue route', () {
273+
when(() => configuration.rogueRoutes).thenReturn(
274+
const [
275+
RouteFile(name: 'hello', path: 'hello.dart', route: '/hello'),
276+
],
277+
);
278+
reportRogueRoutes(context, configuration);
279+
verify(
280+
() => logger.err(
281+
'''Rogue route detected.${defaultForeground.wrap(' ')}Rename ${lightCyan.wrap('routes/hello.dart')} to ${lightCyan.wrap('routes/hello/index.dart')}.''',
282+
),
283+
);
284+
});
285+
286+
test('reports multiple rogue routes', () {
287+
when(() => configuration.rogueRoutes).thenReturn(
288+
const [
289+
RouteFile(name: 'hello', path: 'hello.dart', route: '/hello'),
290+
RouteFile(name: 'hi', path: 'hi.dart', route: '/hi'),
291+
],
292+
);
293+
reportRogueRoutes(context, configuration);
294+
verify(
295+
() => logger.err(
296+
'''Rogue route detected.${defaultForeground.wrap(' ')}Rename ${lightCyan.wrap('routes/hello.dart')} to ${lightCyan.wrap('routes/hello/index.dart')}.''',
297+
),
298+
);
299+
verify(
300+
() => logger.err(
301+
'''Rogue route detected.${defaultForeground.wrap(' ')}Rename ${lightCyan.wrap('routes/hi.dart')} to ${lightCyan.wrap('routes/hi/index.dart')}.''',
302+
),
303+
);
304+
});
305+
});
306+
251307
group('ExitOverrides', () {
252308
group('runZoned', () {
253309
test('uses default exit when not specified', () {

0 commit comments

Comments
 (0)