drift/moor_generator/test/cli/migrate_test.dart

108 lines
2.6 KiB
Dart
Raw Normal View History

2021-10-07 13:16:05 -07:00
// @dart=2.9
import 'dart:async';
import 'dart:io';
2021-10-07 13:55:54 -07:00
import 'dart:isolate';
2021-10-07 13:16:05 -07:00
import 'package:meta/meta.dart';
import 'package:moor_generator/src/cli/cli.dart';
2021-10-07 13:55:54 -07:00
import 'package:package_config/package_config_types.dart';
import 'package:path/path.dart' as p;
2021-10-07 13:16:05 -07:00
import 'package:test/scaffolding.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;
@isTest
void _test(String desc, Function() body) {
test(desc, () {
return IOOverrides.runZoned(
body,
getCurrentDirectory: () => Directory('${d.sandbox}/app'),
);
});
}
Future<void> _apply() {
return MoorCli().run(['migrate']);
}
Future<void> _setup(Iterable<d.Descriptor> lib,
2021-10-07 13:55:54 -07:00
{String pubspec, Iterable<d.Descriptor> additional}) async {
// Copy and patch moor_generator's package config instead of running `pub get`
// in each test.
final uri = await Isolate.packageConfig;
final config =
PackageConfig.parseBytes(await File.fromUri(uri).readAsBytes(), uri);
final appUri = '${File(p.join(d.sandbox, 'app')).absolute.uri}/';
final newConfig = PackageConfig([
...config.packages,
Package('app', Uri.parse(appUri),
packageUriRoot: Uri.parse('${appUri}lib/')),
]);
final configBuffer = StringBuffer();
PackageConfig.writeString(newConfig, configBuffer);
2021-10-07 13:16:05 -07:00
pubspec ??= '''
name: app
environment:
sdk: ^2.12.0
dependencies:
moor: ^4.4.0
dev_dependencies:
moor_generator: ^4.4.0
''';
2021-10-07 13:55:54 -07:00
await d.dir('app', [
2021-10-07 13:16:05 -07:00
d.dir('lib', lib),
d.file('pubspec.yaml', pubspec),
2021-10-07 13:55:54 -07:00
d.dir('.dart_tool', [
d.file('package_config.json', configBuffer.toString()),
]),
...?additional,
2021-10-07 13:16:05 -07:00
]).create();
}
void main() {
_test('renames moor files', () async {
await _setup([
d.file('a.moor', "import 'b.moor';"),
d.file('b.moor', 'CREATE TABLE foo (x TEXT);'),
]);
await _apply();
await d.dir('app/lib', [
d.file('a.drift', "import 'b.drift';"),
d.file('b.drift', 'CREATE TABLE foo (x TEXT);'),
2021-10-07 13:55:54 -07:00
]).validate();
});
_test('patches moor imports', () async {
await _setup([
d.file('a.dart', '''
import 'package:moor/moor.dart' as moor;
import 'package:moor/extensions/moor_ffi.dart';
import 'package:moor/src/some/internal/file.dart';
export 'package:moor/moor_web.dart';
export 'package:moor/fFI.dart';
'''),
]);
await _apply();
await d.dir('app/lib', [
d.file('a.dart', '''
import 'package:drift/drift.dart' as moor;
import 'package:drift/extensions/native.dart';
import 'package:drift/src/some/internal/file.dart';
export 'package:drift/web.dart';
export 'package:drift/native.dart';
'''),
]).validate();
2021-10-07 13:16:05 -07:00
});
}