Skip to content

Commit fbec383

Browse files
committed
test: bootstrap the bundled Isar core for real-database tests
Isar.open only looks for libisar.so on the system loader path and in the working directory, and neither holds it on a fresh checkout or in CI. Resolve the binary shipped with isar_community_flutter_libs instead, so a test can open a real Isar with no manual step and no network download.
1 parent c8d2364 commit fbec383

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

‎test/utilities/isar_test_core.dart‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import 'dart:convert';
2+
import 'dart:ffi';
3+
import 'dart:io';
4+
5+
import 'package:isar_community/isar.dart';
6+
7+
/// Point Isar at the core binary bundled in `isar_community_flutter_libs` so
8+
/// tests can open a real Isar instance anywhere, including CI.
9+
///
10+
/// Without this, `Isar.open` only looks for `libisar.so` on the system loader
11+
/// path or in the working directory; neither exists on a fresh checkout, and
12+
/// the alternative (`download: true`) would make the test suite hit the
13+
/// network. Call once from `setUpAll` before opening Isar. Repeat calls and
14+
/// calls from other test files are no-ops.
15+
Future<void> initializeIsarCoreForTests() async {
16+
final String? relativeLibPath = switch (Abi.current()) {
17+
Abi.linuxX64 || Abi.linuxArm64 => "linux/libisar.so",
18+
Abi.macosX64 || Abi.macosArm64 => "macos/libisar.dylib",
19+
Abi.windowsX64 || Abi.windowsArm64 => "windows/libisar.dll",
20+
_ => null,
21+
};
22+
if (relativeLibPath == null) {
23+
throw UnsupportedError(
24+
"isar_community_flutter_libs bundles no Isar core for ${Abi.current()}",
25+
);
26+
}
27+
28+
await Isar.initializeIsarCore(
29+
libraries: {
30+
Abi.current(): _packageRoot(
31+
"isar_community_flutter_libs",
32+
).resolve(relativeLibPath).toFilePath(),
33+
},
34+
);
35+
}
36+
37+
Uri _packageRoot(String packageName) {
38+
final configFile = File(".dart_tool/package_config.json");
39+
if (!configFile.existsSync()) {
40+
throw StateError(
41+
"${configFile.path} not found; run `flutter pub get` before the tests",
42+
);
43+
}
44+
45+
final config =
46+
jsonDecode(configFile.readAsStringSync()) as Map<String, dynamic>;
47+
final package = (config["packages"] as List)
48+
.cast<Map<String, dynamic>>()
49+
.firstWhere(
50+
(p) => p["name"] == packageName,
51+
orElse: () => throw StateError("package $packageName not resolved"),
52+
);
53+
54+
// rootUri may be relative to .dart_tool/ and carries no trailing slash;
55+
// without one, resolve() would replace its last segment.
56+
final rootUri = package["rootUri"] as String;
57+
return configFile.absolute.parent.uri.resolve(
58+
rootUri.endsWith("/") ? rootUri : "$rootUri/",
59+
);
60+
}

0 commit comments

Comments
 (0)