Skip to content

Commit 0e70716

Browse files
authored
fix(dart_frog_gen): do not report rogue route if index.dart already exists (#264)
1 parent 8fa32c3 commit 0e70716

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

packages/dart_frog_gen/lib/src/build_route_configuration.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,12 @@ List<RouteFile> _getRouteFiles({
225225
onRoute(route);
226226
files.add(route);
227227

228-
if (subDirectories.contains(path.basenameWithoutExtension(filePath))) {
229-
onRogueRoute(route);
230-
}
228+
final fileBasename = path.basenameWithoutExtension(filePath);
229+
final isRogueRoute = subDirectories.contains(fileBasename) &&
230+
!File(path.join(directory.path, fileBasename, 'index.dart'))
231+
.existsSync();
232+
233+
if (isRogueRoute) onRogueRoute(route);
231234
});
232235
return files;
233236
}

packages/dart_frog_gen/test/src/build_route_configuration_test.dart

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,5 +805,67 @@ void main() {
805805
]),
806806
);
807807
});
808+
809+
test('does not report rogue route when index.dart already exists', () {
810+
const expected = [
811+
{
812+
'name': '_',
813+
'route': '/',
814+
'middleware': false,
815+
'files': [
816+
{'name': 'api', 'path': '../routes/api.dart', 'route': '/api'}
817+
]
818+
},
819+
{
820+
'name': '_api',
821+
'route': '/api',
822+
'middleware': false,
823+
'files': [
824+
{
825+
'name': 'api_index',
826+
'path': '../routes/api/index.dart',
827+
'route': '/'
828+
}
829+
]
830+
}
831+
];
832+
final directory = Directory(
833+
path.join(
834+
Directory.current.path,
835+
'test',
836+
'.fixtures',
837+
'rogue_routes_with_conflict',
838+
),
839+
)..createSync(recursive: true);
840+
final routes = Directory(path.join(directory.path, 'routes'))
841+
..createSync();
842+
File(path.join(routes.path, 'api.dart')).createSync();
843+
final apiDirectory = Directory(path.join(routes.path, 'api'))
844+
..createSync();
845+
File(path.join(apiDirectory.path, 'index.dart')).createSync();
846+
final configuration = buildRouteConfiguration(directory);
847+
expect(
848+
configuration.directories.map((d) => d.toJson()).toList(),
849+
equals(expected),
850+
);
851+
expect(
852+
configuration.endpoints,
853+
equals({
854+
'/api': [
855+
isA<RouteFile>().having(
856+
(r) => r.path,
857+
'path',
858+
'../routes/api.dart',
859+
),
860+
isA<RouteFile>().having(
861+
(r) => r.path,
862+
'path',
863+
'../routes/api/index.dart',
864+
),
865+
],
866+
}),
867+
);
868+
expect(configuration.rogueRoutes, isEmpty);
869+
});
808870
});
809871
}

0 commit comments

Comments
 (0)