2019-08-30 14:09:22 -07:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:analyzer/dart/element/element.dart';
|
|
|
|
import 'package:build/build.dart';
|
|
|
|
import 'package:build_test/build_test.dart';
|
2021-10-08 09:10:48 -07:00
|
|
|
import 'package:drift_dev/src/backends/backend.dart';
|
2021-10-08 12:51:01 -07:00
|
|
|
import 'package:logging/logging.dart';
|
2019-08-30 14:09:22 -07:00
|
|
|
|
|
|
|
class TestBackend extends Backend {
|
2019-09-02 11:28:57 -07:00
|
|
|
final Map<AssetId, String> fakeContent;
|
2021-07-26 10:48:26 -07:00
|
|
|
late Resolver _resolver;
|
2019-08-30 14:09:22 -07:00
|
|
|
|
|
|
|
final Completer _initCompleter = Completer();
|
|
|
|
final Completer _finish = Completer();
|
|
|
|
|
|
|
|
/// Future that completes when this backend is ready, which happens when all
|
|
|
|
/// input files have been parsed and analyzed by the Dart analyzer.
|
|
|
|
Future get _ready => _initCompleter.future;
|
|
|
|
|
2020-03-04 07:52:08 -08:00
|
|
|
TestBackend(this.fakeContent, {bool enableDartAnalyzer = true}) {
|
2020-02-25 12:54:12 -08:00
|
|
|
if (enableDartAnalyzer) {
|
|
|
|
_init();
|
|
|
|
} else {
|
|
|
|
_initCompleter.complete();
|
|
|
|
}
|
2019-08-30 14:09:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void _init() {
|
2019-09-02 11:28:57 -07:00
|
|
|
resolveSources(fakeContent.map((k, v) => MapEntry(k.toString(), v)), (r) {
|
2019-08-30 14:09:22 -07:00
|
|
|
_resolver = r;
|
|
|
|
_initCompleter.complete();
|
|
|
|
return _finish.future;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-02 11:28:57 -07:00
|
|
|
BackendTask startTask(Uri uri) {
|
|
|
|
return _TestBackendTask(this, uri);
|
2019-08-30 14:09:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void finish() {
|
|
|
|
_finish.complete();
|
|
|
|
}
|
2019-09-12 07:01:28 -07:00
|
|
|
|
|
|
|
@override
|
|
|
|
Uri resolve(Uri base, String import) {
|
2021-03-16 01:47:09 -07:00
|
|
|
final from = AssetId.resolve(base);
|
|
|
|
return AssetId.resolve(Uri.parse(import), from: from).uri;
|
2019-09-12 07:01:28 -07:00
|
|
|
}
|
2019-08-30 14:09:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class _TestBackendTask extends BackendTask {
|
|
|
|
final TestBackend backend;
|
|
|
|
|
|
|
|
@override
|
2019-09-02 11:28:57 -07:00
|
|
|
final Uri entrypoint;
|
|
|
|
|
|
|
|
@override
|
2021-07-26 10:48:26 -07:00
|
|
|
Logger get log => Logger.root;
|
2019-08-30 14:09:22 -07:00
|
|
|
|
|
|
|
_TestBackendTask(this.backend, this.entrypoint);
|
|
|
|
|
|
|
|
@override
|
2019-09-02 11:28:57 -07:00
|
|
|
Future<String> readMoor(Uri path) async {
|
2019-08-30 14:09:22 -07:00
|
|
|
await backend._ready;
|
2021-07-26 10:48:26 -07:00
|
|
|
return backend.fakeContent[AssetId.resolve(path)]!;
|
2019-08-30 14:09:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2019-09-02 11:28:57 -07:00
|
|
|
Future<LibraryElement> resolveDart(Uri path) async {
|
2019-08-30 14:09:22 -07:00
|
|
|
await backend._ready;
|
2021-04-11 10:55:44 -07:00
|
|
|
try {
|
|
|
|
return await backend._resolver.libraryFor(AssetId.resolve(path));
|
|
|
|
} on NonLibraryAssetException catch (_) {
|
|
|
|
throw NotALibraryException(path);
|
|
|
|
}
|
2019-08-30 14:09:22 -07:00
|
|
|
}
|
2019-09-06 13:23:44 -07:00
|
|
|
|
2019-09-12 07:01:28 -07:00
|
|
|
@override
|
|
|
|
Future<bool> exists(Uri uri) async {
|
2021-03-16 01:47:09 -07:00
|
|
|
return backend.fakeContent.containsKey(AssetId.resolve(uri));
|
2019-09-12 07:01:28 -07:00
|
|
|
}
|
2019-08-30 14:09:22 -07:00
|
|
|
}
|