Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dart_frog_gen): static dir inside dynamic dir #135

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ List<RouteFile> _getRouteFilesForDynamicDirectories(
String prefix = '',
}) {
final files = <RouteFile>[];
final isDynamic = directory.isDynamicRoute;
directory
.listSync()
.sorted()
.whereType<Directory>()
.where((d) => d.isDynamicRoute)
.where((d) => isDynamic || d.isDynamicRoute)
.forEach((dynamicDirectory) {
final newPrefix = '/${path.basename(dynamicDirectory.path)}$prefix';
final subset = _getRouteFiles(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,5 +299,100 @@ 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/<user>'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be /<user>/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': '/<id>/item/<user>'
}
]
}
];
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),
);
});
});
}
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