Report error when tables can't be read

This commit is contained in:
Simon Binder 2021-05-03 18:06:19 +02:00
parent 878af3add8
commit c5d696a9eb
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
2 changed files with 45 additions and 4 deletions

View File

@ -11,9 +11,17 @@ class UseMoorParser {
Future<Database> parseDatabase(
ClassElement element, ConstantReader annotation) async {
// the types declared in UseMoor.tables
final tableTypes =
annotation.peek('tables')?.listValue?.map((obj) => obj.toTypeValue()) ??
[];
final tablesOrNull =
annotation.peek('tables')?.listValue?.map((obj) => obj.toTypeValue());
if (tablesOrNull == null) {
step.reportError(ErrorInDartCode(
message: 'Could not read tables from @UseMoor annotation! \n'
'Please make sure that all table classes exist.',
affectedElement: element,
));
}
final tableTypes = tablesOrNull ?? [];
final queryStrings = annotation.peek('queries')?.mapValue ?? {};
final includes = annotation
.read('include')

View File

@ -93,7 +93,17 @@ void main() {
@override
Set<Column> get primaryKey => {other};
}
'''
''',
AssetId.parse('test_lib|lib/invalid_reference.dart'): '''
import 'package:moor/moor.dart';
class Foo extends Table {
IntColumn get id => integer().autoIncrement()();
}
@UseMoor(tables: [Foo, DoesNotExist])
class Database {}
''',
});
});
tearDownAll(() {
@ -284,4 +294,27 @@ void main() {
),
);
});
test('reports errors for unknown classes in UseMoor', () async {
final session = MoorSession(backend);
final uri = Uri.parse('package:test_lib/invalid_reference.dart');
final backendTask = backend.startTask(uri);
final task = session.startTask(backendTask);
await task.runTask();
final file = session.registerFile(uri);
expect(
file.errors.errors,
contains(
isA<ErrorInDartCode>().having(
(e) => e.message,
'message',
allOf(
contains('Could not read tables from @UseMoor annotation!'),
contains('Please make sure that all table classes exist.'),
),
),
),
);
});
}