-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutil.dart
116 lines (101 loc) · 3.02 KB
/
util.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import 'dart:ffi';
import 'dart:io';
import 'dart:isolate';
import 'package:glob/glob.dart';
import 'package:glob/list_local_fs.dart';
import 'package:sqlite3/open.dart' as sqlite_open;
import 'package:sqlite3/sqlite3.dart' as sqlite;
import 'package:sqlite_async/sqlite_async.dart';
import 'package:test_api/src/backend/invoker.dart';
const defaultSqlitePath = 'libsqlite3.so.0';
// const defaultSqlitePath = './sqlite-autoconf-3410100/.libs/libsqlite3.so.0';
class TestSqliteOpenFactory extends DefaultSqliteOpenFactory {
String sqlitePath;
List<String> initStatements;
TestSqliteOpenFactory(
{required super.path,
super.sqliteOptions,
this.sqlitePath = defaultSqlitePath,
this.initStatements = const []});
@override
sqlite.Database open(SqliteOpenOptions options) {
sqlite_open.open.overrideFor(sqlite_open.OperatingSystem.linux, () {
return DynamicLibrary.open(sqlitePath);
});
final db = super.open(options);
db.createFunction(
functionName: 'test_sleep',
argumentCount: const sqlite.AllowedArgumentCount(1),
function: (args) {
final millis = args[0] as int;
sleep(Duration(milliseconds: millis));
return millis;
},
);
db.createFunction(
functionName: 'test_connection_name',
argumentCount: const sqlite.AllowedArgumentCount(0),
function: (args) {
return Isolate.current.debugName;
},
);
db.createFunction(
functionName: 'test_connection_number',
argumentCount: const sqlite.AllowedArgumentCount(0),
function: (args) {
// write: 0, read: 1 - 5
final name = Isolate.current.debugName ?? '-0';
var nr = name.split('-').last;
return int.tryParse(nr) ?? 0;
},
);
for (var s in initStatements) {
db.execute(s);
}
return db;
}
}
SqliteOpenFactory testFactory(
{String? path, List<String> initStatements = const []}) {
return TestSqliteOpenFactory(
path: path ?? dbPath(), initStatements: initStatements);
}
Future<SqliteDatabase> setupDatabase({String? path}) async {
final db = SqliteDatabase.withFactory(testFactory(path: path));
await db.initialize();
return db;
}
Future<void> cleanDb({required String path}) async {
try {
await File(path).delete();
} on PathNotFoundException {
// Not an issue
}
try {
await File("$path-shm").delete();
} on PathNotFoundException {
// Not an issue
}
try {
await File("$path-wal").delete();
} on PathNotFoundException {
// Not an issue
}
}
List<String> findSqliteLibraries() {
var glob = Glob('sqlite-*/.libs/libsqlite3.so');
List<String> sqlites = [
'libsqlite3.so.0',
for (var sqlite in glob.listSync()) sqlite.path
];
return sqlites;
}
String dbPath() {
final test = Invoker.current!.liveTest;
var testName = test.test.name;
var testShortName =
testName.replaceAll(RegExp(r'[\s\./]'), '_').toLowerCase();
var dbName = "test-db/$testShortName.db";
Directory("test-db").createSync(recursive: false);
return dbName;
}