Skip to content

Commit

Permalink
fix(dart_frog_gen): nested dynamic directory resolution (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel committed Jul 4, 2022
1 parent 41eaea3 commit 387e765
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/dart_frog_gen/lib/src/build_route_configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ List<RouteFile> _getRouteFilesForDynamicDirectories(
.listSync()
.sorted()
.whereType<Directory>()
.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,
Expand Down
190 changes: 189 additions & 1 deletion packages/dart_frog_gen/test/src/build_route_configuration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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': '/<id>/<user>'
'route': '/<user>/<id>'
}
]
}
Expand Down Expand Up @@ -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': '/<id>/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': '/<id>/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': '/<id>/api/<name>'
}
]
},
];
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': '/<id>/api/<name>/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),
);
});
});
}
3 changes: 3 additions & 0 deletions packages/dart_frog_gen/test/src/path_to_route_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 387e765

Please sign in to comment.