From 2c69c99a33394275eba010c6698bf5fe7a2ee687 Mon Sep 17 00:00:00 2001 From: Renan Araujo Date: Mon, 4 Jul 2022 16:41:30 +0100 Subject: [PATCH 1/3] fix(#134): static dir inside dynamic dir --- .../lib/src/build_route_configuration.dart | 3 +- .../src/build_route_configuration_test.dart | 94 +++++++++++++++++++ .../test/src/path_to_route_test.dart | 3 + 3 files changed, 99 insertions(+), 1 deletion(-) 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..7381349cf 100644 --- a/packages/dart_frog_gen/lib/src/build_route_configuration.dart +++ b/packages/dart_frog_gen/lib/src/build_route_configuration.dart @@ -105,11 +105,12 @@ List _getRouteFilesForDynamicDirectories( String prefix = '', }) { final files = []; + final thisIsDynamic = directory.isDynamicRoute; directory .listSync() .sorted() .whereType() - .where((d) => d.isDynamicRoute) + .where((d) => thisIsDynamic || d.isDynamicRoute) .forEach((dynamicDirectory) { final newPrefix = '/${path.basename(dynamicDirectory.path)}$prefix'; final subset = _getRouteFiles( 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..915692285 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 @@ -299,5 +299,99 @@ void main() { equals(expected), ); }); + + test('includes static directory routes nested dynamic directory routes', + () { + const expected = [ + { + 'name': '_', + 'route': '/', + 'middleware': false, + 'files': [ + { + 'name': '.._test_.fixtures_static_dynamic_nested_routes_index', + 'path': + '../test/.fixtures/static_dynamic_nested/routes/index.dart', + 'route': '/routes' + }, + { + 'name': '.._test_.fixtures_static_dynamic_nested_routes' + r'_$user_item_index', + 'path': '../test/.fixtures/static_dynamic_nested/routes/' + '[user]/item/index.dart', + 'route': '/item/' + } + ] + } + ]; + final directory = Directory( + path.join( + Directory.current.path, + 'test', + '.fixtures', + 'static_dynamic_nested', + ), + )..createSync(recursive: true); + final routes = Directory(path.join(directory.path, 'routes')) + ..createSync(); + File(path.join(routes.path, 'index.dart')).createSync(); + final userDirectory = Directory(path.join(routes.path, '[user]')) + ..createSync(); + final itemDirectory = Directory(path.join(userDirectory.path, 'item')) + ..createSync(); + File(path.join(itemDirectory.path, 'index.dart')).createSync(); + final configuration = buildRouteConfiguration(directory); + expect( + configuration.directories.map((d) => d.toJson()).toList(), + equals(expected), + ); + }); + test('includes dynamic static dynamic nested directory routes', () { + const expected = [ + { + 'name': '_', + 'route': '/', + 'middleware': false, + 'files': [ + { + 'name': '.._test_.fixtures_dynamic_static_dynamic_routes_index', + 'path': + '../test/.fixtures/dynamic_static_dynamic/routes/index.dart', + 'route': '/routes' + }, + { + 'name': '.._test_.fixtures_dynamic_static_dynamic_routes' + r'_$user_item_$id_index', + 'path': '../test/.fixtures/dynamic_static_dynamic/' + 'routes/[user]/item/[id]/index.dart', + 'route': '//item/' + } + ] + } + ]; + final directory = Directory( + path.join( + Directory.current.path, + 'test', + '.fixtures', + 'dynamic_static_dynamic', + ), + )..createSync(recursive: true); + final routes = Directory(path.join(directory.path, 'routes')) + ..createSync(); + File(path.join(routes.path, 'index.dart')).createSync(); + final userDirectory = Directory(path.join(routes.path, '[user]')) + ..createSync(); + final itemDirectory = Directory(path.join(userDirectory.path, 'item')) + ..createSync(); + final idDirectory = Directory(path.join(itemDirectory.path, '[id]')) + ..createSync(); + File(path.join(idDirectory.path, 'index.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', From 5818f33b2dec17edde0bc9279f495453369c2763 Mon Sep 17 00:00:00 2001 From: Renan Araujo Date: Mon, 4 Jul 2022 17:10:49 +0100 Subject: [PATCH 2/3] rename isDynamic --- packages/dart_frog_gen/lib/src/build_route_configuration.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 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 7381349cf..eab54ba17 100644 --- a/packages/dart_frog_gen/lib/src/build_route_configuration.dart +++ b/packages/dart_frog_gen/lib/src/build_route_configuration.dart @@ -105,12 +105,12 @@ List _getRouteFilesForDynamicDirectories( String prefix = '', }) { final files = []; - final thisIsDynamic = directory.isDynamicRoute; + final isDynamic = directory.isDynamicRoute; directory .listSync() .sorted() .whereType() - .where((d) => thisIsDynamic || d.isDynamicRoute) + .where((d) => isDynamic || d.isDynamicRoute) .forEach((dynamicDirectory) { final newPrefix = '/${path.basename(dynamicDirectory.path)}$prefix'; final subset = _getRouteFiles( From 0b65c831e815fda3f6cabab9a70e92198e1202ad Mon Sep 17 00:00:00 2001 From: Renan <6718144+renancaraujo@users.noreply.github.com> Date: Mon, 4 Jul 2022 17:36:48 +0100 Subject: [PATCH 3/3] Update packages/dart_frog_gen/test/src/build_route_configuration_test.dart Co-authored-by: Felix Angelov --- .../dart_frog_gen/test/src/build_route_configuration_test.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 915692285..ac8edc1fe 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 @@ -346,7 +346,8 @@ void main() { equals(expected), ); }); - test('includes dynamic static dynamic nested directory routes', () { + + test('includes dynamic/static/dynamic nested directory routes', () { const expected = [ { 'name': '_',