From 387e765b3168836149e0f5aed2a320b98f089ba9 Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Mon, 4 Jul 2022 14:51:35 -0500 Subject: [PATCH] fix(dart_frog_gen): nested dynamic directory resolution (#138) --- .../lib/src/build_route_configuration.dart | 4 +- .../src/build_route_configuration_test.dart | 190 +++++++++++++++++- .../test/src/path_to_route_test.dart | 3 + 3 files changed, 194 insertions(+), 3 deletions(-) diff --git a/packages/dart_frog_gen/lib/src/build_route_configuration.dart b/packages/dart_frog_gen/lib/src/build_route_configuration.dart index 45343c2c2..92a723904 100644 --- a/packages/dart_frog_gen/lib/src/build_route_configuration.dart +++ b/packages/dart_frog_gen/lib/src/build_route_configuration.dart @@ -109,9 +109,9 @@ List _getRouteFilesForDynamicDirectories( .listSync() .sorted() .whereType() - .where((d) => d.isDynamicRoute) + .where((d) => prefix.isNotEmpty || d.isDynamicRoute) .forEach((dynamicDirectory) { - final newPrefix = '/${path.basename(dynamicDirectory.path)}$prefix'; + final newPrefix = '$prefix/${path.basename(dynamicDirectory.path)}'; final subset = _getRouteFiles( dynamicDirectory, onRoute: onRoute, diff --git a/packages/dart_frog_gen/test/src/build_route_configuration_test.dart b/packages/dart_frog_gen/test/src/build_route_configuration_test.dart index a9b87ab82..0ae5587f9 100644 --- a/packages/dart_frog_gen/test/src/build_route_configuration_test.dart +++ b/packages/dart_frog_gen/test/src/build_route_configuration_test.dart @@ -271,7 +271,7 @@ void main() { r'.._test_.fixtures_dynamic_nested_routes_$user_$id_index', 'path': '../test/.fixtures/dynamic_nested/routes/[user]/[id]/index.dart', - 'route': '//' + 'route': '//' } ] } @@ -299,5 +299,193 @@ void main() { equals(expected), ); }); + + test('supports /[id]/api/index.dart', () { + const expected = [ + { + 'name': '_', + 'route': '/', + 'middleware': false, + 'files': [ + { + 'name': '.._test_.fixtures_dynamic_static_nesting1_routes_index', + 'path': + '../test/.fixtures/dynamic_static_nesting1/routes/index.dart', + 'route': '/routes' + }, + { + 'name': + r'''.._test_.fixtures_dynamic_static_nesting1_routes_$id_api_index''', + 'path': + '../test/.fixtures/dynamic_static_nesting1/routes/[id]/api/index.dart', + 'route': '//api' + } + ] + } + ]; + final directory = Directory( + path.join( + Directory.current.path, + 'test', + '.fixtures', + 'dynamic_static_nesting1', + ), + )..createSync(recursive: true); + final routes = Directory(path.join(directory.path, 'routes')) + ..createSync(); + File(path.join(routes.path, 'index.dart')).createSync(); + final idDirectory = Directory(path.join(routes.path, '[id]')) + ..createSync(); + final apiDirectory = Directory(path.join(idDirectory.path, 'api')) + ..createSync(); + File(path.join(apiDirectory.path, 'index.dart')).createSync(); + final configuration = buildRouteConfiguration(directory); + expect( + configuration.directories.map((d) => d.toJson()).toList(), + equals(expected), + ); + }); + + test('supports /[id]/api/test.dart', () { + const expected = [ + { + 'name': '_', + 'route': '/', + 'middleware': false, + 'files': [ + { + 'name': '.._test_.fixtures_dynamic_static_nesting2_routes_index', + 'path': + '../test/.fixtures/dynamic_static_nesting2/routes/index.dart', + 'route': '/routes' + }, + { + 'name': + r'''.._test_.fixtures_dynamic_static_nesting2_routes_$id_api_test''', + 'path': + '../test/.fixtures/dynamic_static_nesting2/routes/[id]/api/test.dart', + 'route': '//api/test' + } + ] + } + ]; + final directory = Directory( + path.join( + Directory.current.path, + 'test', + '.fixtures', + 'dynamic_static_nesting2', + ), + )..createSync(recursive: true); + final routes = Directory(path.join(directory.path, 'routes')) + ..createSync(); + File(path.join(routes.path, 'index.dart')).createSync(); + final idDirectory = Directory(path.join(routes.path, '[id]')) + ..createSync(); + final apiDirectory = Directory(path.join(idDirectory.path, 'api')) + ..createSync(); + File(path.join(apiDirectory.path, 'test.dart')).createSync(); + final configuration = buildRouteConfiguration(directory); + expect( + configuration.directories.map((d) => d.toJson()).toList(), + equals(expected), + ); + }); + + test('supports /[id]/api/[name]/index.dart', () { + const expected = [ + { + 'name': '_', + 'route': '/', + 'middleware': false, + 'files': [ + { + 'name': '.._test_.fixtures_dynamic_static_nesting3_routes_index', + 'path': + '../test/.fixtures/dynamic_static_nesting3/routes/index.dart', + 'route': '/routes' + }, + { + 'name': + r'''.._test_.fixtures_dynamic_static_nesting3_routes_$id_api_$name_index''', + 'path': + '../test/.fixtures/dynamic_static_nesting3/routes/[id]/api/[name]/index.dart', + 'route': '//api/' + } + ] + }, + ]; + final directory = Directory( + path.join( + Directory.current.path, + 'test', + '.fixtures', + 'dynamic_static_nesting3', + ), + )..createSync(recursive: true); + final routes = Directory(path.join(directory.path, 'routes')) + ..createSync(); + File(path.join(routes.path, 'index.dart')).createSync(); + final idDirectory = Directory(path.join(routes.path, '[id]')) + ..createSync(); + final apiDirectory = Directory(path.join(idDirectory.path, 'api')) + ..createSync(); + final nameDirectory = Directory(path.join(apiDirectory.path, '[name]')) + ..createSync(); + File(path.join(nameDirectory.path, 'index.dart')).createSync(); + final configuration = buildRouteConfiguration(directory); + expect( + configuration.directories.map((d) => d.toJson()).toList(), + equals(expected), + ); + }); + + test('supports /[id]/api/[name]/test.dart', () { + const expected = [ + { + 'name': '_', + 'route': '/', + 'middleware': false, + 'files': [ + { + 'name': '.._test_.fixtures_dynamic_static_nesting4_routes_index', + 'path': + '../test/.fixtures/dynamic_static_nesting4/routes/index.dart', + 'route': '/routes' + }, + { + 'name': + r'''.._test_.fixtures_dynamic_static_nesting4_routes_$id_api_$name_test''', + 'path': + '../test/.fixtures/dynamic_static_nesting4/routes/[id]/api/[name]/test.dart', + 'route': '//api//test' + } + ] + }, + ]; + final directory = Directory( + path.join( + Directory.current.path, + 'test', + '.fixtures', + 'dynamic_static_nesting4', + ), + )..createSync(recursive: true); + final routes = Directory(path.join(directory.path, 'routes')) + ..createSync(); + File(path.join(routes.path, 'index.dart')).createSync(); + final idDirectory = Directory(path.join(routes.path, '[id]')) + ..createSync(); + final apiDirectory = Directory(path.join(idDirectory.path, 'api')) + ..createSync(); + final nameDirectory = Directory(path.join(apiDirectory.path, '[name]')) + ..createSync(); + File(path.join(nameDirectory.path, 'test.dart')).createSync(); + final configuration = buildRouteConfiguration(directory); + expect( + configuration.directories.map((d) => d.toJson()).toList(), + equals(expected), + ); + }); }); } diff --git a/packages/dart_frog_gen/test/src/path_to_route_test.dart b/packages/dart_frog_gen/test/src/path_to_route_test.dart index ca9cd959c..d2304b16a 100644 --- a/packages/dart_frog_gen/test/src/path_to_route_test.dart +++ b/packages/dart_frog_gen/test/src/path_to_route_test.dart @@ -8,6 +8,9 @@ void main() { '../routes/hello.dart': '/hello', '../routes/hello/world.dart': '/hello/world', '../routes/hello/[name].dart': '/hello/[name]', + '../routes/[id]/item.dart': '/[id]/item', + '../routes/[id]/part/item.dart': '/[id]/part/item', + '../routes/[id]/part/index.dart': '/[id]/part', '../routes/api/v1/index.dart': '/api/v1', r'..\routes\index.dart': '/', r'..\routes\hello.dart': '/hello',