diff --git a/.gitignore b/.gitignore index 17d453872..db5283476 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ .idea -.vscode/settings.json -.DS_Store +.vscode/settings.json \ No newline at end of file diff --git a/packages/dart_frog_gen/lib/dart_frog_gen.dart b/packages/dart_frog_gen/lib/dart_frog_gen.dart index 9d75cd5a1..9bd13af0e 100644 --- a/packages/dart_frog_gen/lib/dart_frog_gen.dart +++ b/packages/dart_frog_gen/lib/dart_frog_gen.dart @@ -2,4 +2,3 @@ library dart_frog_gen; export 'src/build_route_configuration.dart'; -export 'src/validate_route_configuration/validate_route_configuration.dart'; diff --git a/packages/dart_frog_gen/lib/src/validate_route_configuration/external_path_dependencies.dart b/packages/dart_frog_gen/lib/src/validate_route_configuration/external_path_dependencies.dart deleted file mode 100644 index 4c9781f49..000000000 --- a/packages/dart_frog_gen/lib/src/validate_route_configuration/external_path_dependencies.dart +++ /dev/null @@ -1,49 +0,0 @@ -import 'dart:io' as io; - -import 'package:path/path.dart' as path; -import 'package:pubspec_parse/pubspec_parse.dart'; - -/// Type definition for callbacks that report external path dependencies. -typedef OnExternalPathDependency = void Function( - String dependencyName, - String dependencyPath, -); - -/// Reports existence of external path dependencies on a [io.Directory]. -Future reportExternalPathDependencies( - io.Directory directory, { - /// Callback called when any external path dependency is found. - void Function()? onViolationStart, - - /// Callback called for each external path dependency found. - OnExternalPathDependency? onExternalPathDependency, - - /// Callback called when any external path dependency is found. - void Function()? onViolationEnd, -}) async { - final pubspec = Pubspec.parse( - await io.File(path.join(directory.path, 'pubspec.yaml')).readAsString(), - ); - - final dependencies = pubspec.dependencies; - final devDependencies = pubspec.devDependencies; - final pathDependencies = [...dependencies.entries, ...devDependencies.entries] - .where((entry) => entry.value is PathDependency) - .map((entry) { - final value = entry.value as PathDependency; - return [entry.key, value.path]; - }).toList(); - final externalDependencies = pathDependencies.where( - (dep) => !path.isWithin(directory.path, dep.last), - ); - - if (externalDependencies.isNotEmpty) { - onViolationStart?.call(); - for (final dependency in externalDependencies) { - final dependencyName = dependency.first; - final dependencyPath = path.normalize(dependency.last); - onExternalPathDependency?.call(dependencyName, dependencyPath); - } - onViolationEnd?.call(); - } -} diff --git a/packages/dart_frog_gen/lib/src/validate_route_configuration/rogue_routes.dart b/packages/dart_frog_gen/lib/src/validate_route_configuration/rogue_routes.dart deleted file mode 100644 index 2a153cfd7..000000000 --- a/packages/dart_frog_gen/lib/src/validate_route_configuration/rogue_routes.dart +++ /dev/null @@ -1,33 +0,0 @@ -import 'package:dart_frog_gen/dart_frog_gen.dart'; -import 'package:path/path.dart' as path; - -/// Type definition for callbacks that report rogue routes. -typedef OnRogueRoute = void Function(String filePath, String idealPath); - -/// Reports existence of rogue routes on a [RouteConfiguration]. -void reportRogueRoutes( - RouteConfiguration configuration, { - /// Callback called when any rogue route is found. - void Function()? onViolationStart, - - /// Callback called for each rogue route found. - OnRogueRoute? onRogueRoute, - - /// Callback called when any rogue route is found. - void Function()? onViolationEnd, -}) { - if (configuration.rogueRoutes.isNotEmpty) { - onViolationStart?.call(); - for (final route in configuration.rogueRoutes) { - final filePath = path.normalize(path.join('routes', route.path)); - final fileDirectory = path.dirname(filePath); - final idealPath = path.join( - fileDirectory, - path.basenameWithoutExtension(filePath), - 'index.dart', - ); - onRogueRoute?.call(filePath, idealPath); - } - onViolationEnd?.call(); - } -} diff --git a/packages/dart_frog_gen/lib/src/validate_route_configuration/route_conflicts.dart b/packages/dart_frog_gen/lib/src/validate_route_configuration/route_conflicts.dart deleted file mode 100644 index 55dd88685..000000000 --- a/packages/dart_frog_gen/lib/src/validate_route_configuration/route_conflicts.dart +++ /dev/null @@ -1,42 +0,0 @@ -import 'package:dart_frog_gen/dart_frog_gen.dart'; -import 'package:path/path.dart' as path; - -/// Type definition for callbacks that report route conflicts. -typedef OnRouteConflict = void Function( - String originalFilePath, - String conflictingFilePath, - String conflictingEndpoint, -); - -/// Reports existence of route conflicts on a [RouteConfiguration]. -void reportRouteConflicts( - RouteConfiguration configuration, { - /// Callback called when any route conflict is found. - void Function()? onViolationStart, - - /// Callback called for each route conflict found. - OnRouteConflict? onRouteConflict, - - /// Callback called when any route conflict is found. - void Function()? onViolationEnd, -}) { - final conflictingEndpoints = - configuration.endpoints.entries.where((entry) => entry.value.length > 1); - if (conflictingEndpoints.isNotEmpty) { - onViolationStart?.call(); - for (final conflict in conflictingEndpoints) { - final originalFilePath = path.normalize( - path.join('routes', conflict.value.first.path), - ); - final conflictingFilePath = path.normalize( - path.join('routes', conflict.value.last.path), - ); - onRouteConflict?.call( - originalFilePath, - conflictingFilePath, - conflict.key, - ); - } - onViolationEnd?.call(); - } -} diff --git a/packages/dart_frog_gen/lib/src/validate_route_configuration/validate_route_configuration.dart b/packages/dart_frog_gen/lib/src/validate_route_configuration/validate_route_configuration.dart deleted file mode 100644 index be9ae171a..000000000 --- a/packages/dart_frog_gen/lib/src/validate_route_configuration/validate_route_configuration.dart +++ /dev/null @@ -1,3 +0,0 @@ -export 'external_path_dependencies.dart'; -export 'rogue_routes.dart'; -export 'route_conflicts.dart'; diff --git a/packages/dart_frog_gen/pubspec.yaml b/packages/dart_frog_gen/pubspec.yaml index 4deb7f88e..7266715e8 100644 --- a/packages/dart_frog_gen/pubspec.yaml +++ b/packages/dart_frog_gen/pubspec.yaml @@ -11,7 +11,6 @@ environment: dependencies: path: ^1.8.1 - pubspec_parse: ^1.2.2 dev_dependencies: mocktail: ^0.3.0 diff --git a/packages/dart_frog_gen/test/src/validate_route_configuration/external_path_dependencies_test.dart b/packages/dart_frog_gen/test/src/validate_route_configuration/external_path_dependencies_test.dart deleted file mode 100644 index 781bc556f..000000000 --- a/packages/dart_frog_gen/test/src/validate_route_configuration/external_path_dependencies_test.dart +++ /dev/null @@ -1,138 +0,0 @@ -import 'dart:io'; - -import 'package:dart_frog_gen/dart_frog_gen.dart'; -import 'package:path/path.dart' as path; -import 'package:test/test.dart'; - -void main() { - group('reportExternalPathDependencies', () { - late bool violationStartCalled; - late bool violationEndCalled; - late List externalPathDependencies; - - setUp(() { - violationStartCalled = false; - violationEndCalled = false; - externalPathDependencies = []; - }); - - test('reports nothing when there are no external path dependencies', - () async { - final directory = Directory.systemTemp.createTempSync(); - File(path.join(directory.path, 'pubspec.yaml')).writeAsStringSync( - ''' -name: example -version: 0.1.0 -environment: - sdk: ^2.17.0 -dependencies: - mason: any -dev_dependencies: - test: any -''', - ); - - await expectLater( - reportExternalPathDependencies( - directory, - onViolationStart: () { - violationStartCalled = true; - }, - onViolationEnd: () { - violationEndCalled = true; - }, - onExternalPathDependency: (dependencyName, _) { - externalPathDependencies.add(dependencyName); - }, - ), - completes, - ); - - expect(violationStartCalled, isFalse); - expect(violationEndCalled, isFalse); - expect(externalPathDependencies, isEmpty); - }); - - test('reports when there is a single external path dependency', () async { - final directory = Directory.systemTemp.createTempSync(); - File(path.join(directory.path, 'pubspec.yaml')).writeAsStringSync( - ''' -name: example -version: 0.1.0 -environment: - sdk: ^2.17.0 -dependencies: - mason: any - foo: - path: ../../foo -dev_dependencies: - test: any -''', - ); - - await expectLater( - reportExternalPathDependencies( - directory, - onViolationStart: () { - violationStartCalled = true; - }, - onViolationEnd: () { - violationEndCalled = true; - }, - onExternalPathDependency: (dependencyName, _) { - externalPathDependencies.add(dependencyName); - }, - ), - completes, - ); - - expect(violationStartCalled, isTrue); - expect(violationEndCalled, isTrue); - expect(externalPathDependencies, ['foo']); - - directory.delete(recursive: true).ignore(); - }); - - test('reports when there are multiple external path dependencies', - () async { - final directory = Directory.systemTemp.createTempSync(); - File(path.join(directory.path, 'pubspec.yaml')).writeAsStringSync( - ''' -name: example -version: 0.1.0 -environment: - sdk: ^2.17.0 -dependencies: - mason: any - foo: - path: ../../foo -dev_dependencies: - test: any - bar: - path: ../../bar -''', - ); - await expectLater( - reportExternalPathDependencies( - directory, - onViolationStart: () { - violationStartCalled = true; - }, - onViolationEnd: () { - violationEndCalled = true; - }, - onExternalPathDependency: (dependencyName, _) { - externalPathDependencies.add(dependencyName); - }, - ), - completes, - ); - - expect(violationStartCalled, isTrue); - expect(violationEndCalled, isTrue); - expect(externalPathDependencies, ['foo', 'bar']); - - directory.delete(recursive: true).ignore(); - }); - }); -} diff --git a/packages/dart_frog_gen/test/src/validate_route_configuration/rogue_routes_test.dart b/packages/dart_frog_gen/test/src/validate_route_configuration/rogue_routes_test.dart deleted file mode 100644 index 1455478ba..000000000 --- a/packages/dart_frog_gen/test/src/validate_route_configuration/rogue_routes_test.dart +++ /dev/null @@ -1,114 +0,0 @@ -import 'package:dart_frog_gen/dart_frog_gen.dart'; -import 'package:mocktail/mocktail.dart'; -import 'package:path/path.dart' as p; -import 'package:test/test.dart'; - -class _MockRouteConfiguration extends Mock implements RouteConfiguration {} - -void main() { - group('reportRogueRoutes', () { - late RouteConfiguration configuration; - - late bool violationStartCalled; - late bool violationEndCalled; - late List rogueRoutes; - - setUp(() { - configuration = _MockRouteConfiguration(); - - violationStartCalled = false; - violationEndCalled = false; - rogueRoutes = []; - }); - - test('reports nothing when there are no rogue routes', () { - when(() => configuration.rogueRoutes).thenReturn([]); - - reportRogueRoutes( - configuration, - onViolationStart: () { - violationStartCalled = true; - }, - onRogueRoute: (filePath, idealPath) { - rogueRoutes.add(filePath); - }, - onViolationEnd: () { - violationEndCalled = true; - }, - ); - - expect(violationStartCalled, isFalse); - expect(violationEndCalled, isFalse); - expect(rogueRoutes, isEmpty); - }); - - test('reports single rogue route', () { - when(() => configuration.rogueRoutes).thenReturn( - const [ - RouteFile( - name: 'hello', - path: 'hello.dart', - route: '/hello', - params: [], - ), - ], - ); - - reportRogueRoutes( - configuration, - onViolationStart: () { - violationStartCalled = true; - }, - onRogueRoute: (filePath, idealPath) { - rogueRoutes.add(filePath); - }, - onViolationEnd: () { - violationEndCalled = true; - }, - ); - - expect(violationStartCalled, isTrue); - expect(violationEndCalled, isTrue); - expect(rogueRoutes, [p.join('routes', 'hello.dart')]); - }); - - test('reports multiple rogue routes', () { - when(() => configuration.rogueRoutes).thenReturn( - const [ - RouteFile( - name: 'hello', - path: 'hello.dart', - route: '/hello', - params: [], - ), - RouteFile( - name: 'hi', - path: 'hi.dart', - route: '/hi', - params: [], - ), - ], - ); - - reportRogueRoutes( - configuration, - onViolationStart: () { - violationStartCalled = true; - }, - onRogueRoute: (filePath, idealPath) { - rogueRoutes.add(filePath); - }, - onViolationEnd: () { - violationEndCalled = true; - }, - ); - - expect(violationStartCalled, isTrue); - expect(violationEndCalled, isTrue); - expect(rogueRoutes, [ - p.join('routes', 'hello.dart'), - p.join('routes', 'hi.dart'), - ]); - }); - }); -} diff --git a/packages/dart_frog_gen/test/src/validate_route_configuration/route_conflicts_test.dart b/packages/dart_frog_gen/test/src/validate_route_configuration/route_conflicts_test.dart deleted file mode 100644 index dbabad8b4..000000000 --- a/packages/dart_frog_gen/test/src/validate_route_configuration/route_conflicts_test.dart +++ /dev/null @@ -1,188 +0,0 @@ -import 'package:dart_frog_gen/dart_frog_gen.dart'; - -import 'package:mocktail/mocktail.dart'; -import 'package:test/test.dart'; - -class _MockRouteConfiguration extends Mock implements RouteConfiguration {} - -void main() { - group('reportRouteConflicts', () { - late RouteConfiguration configuration; - - late bool violationStartCalled; - late bool violationEndCalled; - late List conflicts; - - setUp(() { - configuration = _MockRouteConfiguration(); - - violationStartCalled = false; - violationEndCalled = false; - conflicts = []; - }); - - test('reports nothing when there are no endpoints', () { - when(() => configuration.endpoints).thenReturn({}); - - reportRouteConflicts( - configuration, - onViolationStart: () { - violationStartCalled = true; - }, - onRouteConflict: (_, __, conflictingEndpoint) { - conflicts.add(conflictingEndpoint); - }, - onViolationEnd: () { - violationEndCalled = true; - }, - ); - - expect(violationStartCalled, isFalse); - expect(violationEndCalled, isFalse); - expect(conflicts, isEmpty); - }); - - test('reports nothing when there are endpoints and no conflicts', () { - when(() => configuration.endpoints).thenReturn({ - '/': const [ - RouteFile( - name: 'index', - path: 'index.dart', - route: '/', - params: [], - ), - ], - '/hello': const [ - RouteFile( - name: 'hello', - path: 'hello.dart', - route: '/hello', - params: [], - ) - ] - }); - - reportRouteConflicts( - configuration, - onViolationStart: () { - violationStartCalled = true; - }, - onRouteConflict: (_, __, conflictingEndpoint) { - conflicts.add(conflictingEndpoint); - }, - onViolationEnd: () { - violationEndCalled = true; - }, - ); - - expect(violationStartCalled, isFalse); - expect(violationEndCalled, isFalse); - expect(conflicts, isEmpty); - }); - - test('reports single conflict when there is one endpoint with conflicts', - () { - when(() => configuration.endpoints).thenReturn({ - '/': const [ - RouteFile( - name: 'index', - path: 'index.dart', - route: '/', - params: [], - ), - ], - '/hello': const [ - RouteFile( - name: 'hello', - path: 'hello.dart', - route: '/hello', - params: [], - ), - RouteFile( - name: 'hello_index', - path: 'hello/index.dart', - route: '/', - params: [], - ) - ] - }); - - reportRouteConflicts( - configuration, - onViolationStart: () { - violationStartCalled = true; - }, - onRouteConflict: (_, __, conflictingEndpoint) { - conflicts.add(conflictingEndpoint); - }, - onViolationEnd: () { - violationEndCalled = true; - }, - ); - - expect(violationStartCalled, isTrue); - expect(violationEndCalled, isTrue); - expect(conflicts, ['/hello']); - }); - - test( - 'reports multiple conflicts ' - 'when there are multiple endpoint with conflicts', () { - when(() => configuration.endpoints).thenReturn({ - '/': const [ - RouteFile( - name: 'index', - path: 'index.dart', - route: '/', - params: [], - ), - ], - '/hello': const [ - RouteFile( - name: 'hello', - path: 'hello.dart', - route: '/hello', - params: [], - ), - RouteFile( - name: 'hello_index', - path: 'hello/index.dart', - route: '/', - params: [], - ) - ], - '/echo': const [ - RouteFile( - name: 'echo', - path: 'echo.dart', - route: '/echo', - params: [], - ), - RouteFile( - name: 'echo_index', - path: 'echo/index.dart', - route: '/', - params: [], - ) - ] - }); - - reportRouteConflicts( - configuration, - onViolationStart: () { - violationStartCalled = true; - }, - onRouteConflict: (_, __, conflictingEndpoint) { - conflicts.add(conflictingEndpoint); - }, - onViolationEnd: () { - violationEndCalled = true; - }, - ); - - expect(violationStartCalled, isTrue); - expect(violationEndCalled, isTrue); - expect(conflicts, ['/hello', '/echo']); - }); - }); -}