Skip to content

Commit aee41c8

Browse files
committed
Add mock classes for fs
1 parent 05c726c commit aee41c8

File tree

12 files changed

+584
-45
lines changed

12 files changed

+584
-45
lines changed

packages/keybinder/example/pubspec.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ packages:
139139
dependency: transitive
140140
description:
141141
name: meta
142-
sha256: e3641ec5d63ebf0d9b41bd43201a66e3fc79a65db5f61fc181f04cd27aab950c
142+
sha256: "23f08335362185a5ea2ad3a4e597f1375e78bce8a040df5c600c8d3552ef2394"
143143
url: "https://pub.dev"
144144
source: hosted
145-
version: "1.16.0"
145+
version: "1.17.0"
146146
path:
147147
dependency: transitive
148148
description:
@@ -200,10 +200,10 @@ packages:
200200
dependency: transitive
201201
description:
202202
name: test_api
203-
sha256: "522f00f556e73044315fa4585ec3270f1808a4b186c936e612cab0b565ff1e00"
203+
sha256: ab2726c1a94d3176a45960b6234466ec367179b87dd74f1611adb1f3b5fb9d55
204204
url: "https://pub.dev"
205205
source: hosted
206-
version: "0.7.6"
206+
version: "0.7.7"
207207
vector_math:
208208
dependency: transitive
209209
description:

packages/lw_file_system/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# https://dart.dev/guides/libraries/private-files
22
# Created by `dart pub`
33
.dart_tool/
4+
build/
45

56
# Avoid committing pubspec.lock for library packages; see
67
# https://dart.dev/guides/libraries/private-files#pubspeclock.

packages/lw_file_system/lib/lw_file_system.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ library;
55

66
export 'package:lw_file_system_api/lw_file_system_api.dart';
77
export 'src/api/file_system_base.dart';
8+
export 'src/api/file_system_mock.dart';
89
export 'src/api/file_system_remote.dart';
910
export 'src/api/typed.dart';
1011
export 'src/models/config.dart';

packages/lw_file_system/lib/src/api/file_system_dav.dart

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ class DavRemoteDirectoryFileSystem extends RemoteDirectoryFileSystem {
2525
}
2626

2727
@override
28-
Future<RawFileSystemDirectory> createDirectory(String path) async {
28+
Future<void> createRemoteDirectory(String path) async {
2929
path = _normalizePath(path);
30-
final location = AssetLocation(remote: storage.identifier, path: path);
3130
final response = await createRequest(path.split('/'), method: 'MKCOL');
3231
if (response == null) {
3332
throw FileSystemException(
@@ -36,14 +35,14 @@ class DavRemoteDirectoryFileSystem extends RemoteDirectoryFileSystem {
3635
);
3736
}
3837
if (response.statusCode == 201) {
39-
return RawFileSystemDirectory(location);
38+
return;
4039
} else if (response.statusCode == 405) {
41-
return RawFileSystemDirectory(location);
40+
return;
4241
} else if (response.statusCode == 409) {
4342
final parent = p.url.dirname(path);
4443
if (parent != '.' && parent != '/') {
45-
await createDirectory(parent);
46-
return createDirectory(path);
44+
await createRemoteDirectory(parent);
45+
return createRemoteDirectory(path);
4746
}
4847
}
4948

@@ -55,21 +54,17 @@ class DavRemoteDirectoryFileSystem extends RemoteDirectoryFileSystem {
5554
}
5655

5756
@override
58-
Future<FileSystemEntity<Uint8List>?> moveAsset(
59-
String path,
60-
String newPath, {
61-
bool forceSync = false,
62-
}) async {
57+
Future<void> moveRemoteAsset(String path, String newPath) async {
6358
path = _normalizePath(path);
6459
newPath = _normalizePath(newPath);
65-
if (path == newPath) return getAsset(path);
60+
if (path == newPath) return;
6661

6762
final destinationUri = storage.buildVariantUri(
6863
variant: config.currentPathVariant,
6964
path: newPath.split('/'),
7065
);
7166

72-
if (destinationUri == null) return null;
67+
if (destinationUri == null) return;
7368

7469
var response = await createRequest(
7570
path.split('/'),
@@ -79,15 +74,15 @@ class DavRemoteDirectoryFileSystem extends RemoteDirectoryFileSystem {
7974

8075
if (response != null && response.statusCode == 409) {
8176
final parent = p.url.dirname(newPath);
82-
await createDirectory(parent);
77+
await createRemoteDirectory(parent);
8378
response = await createRequest(
8479
path.split('/'),
8580
method: 'MOVE',
8681
headers: {'Destination': destinationUri.toString(), 'Overwrite': 'T'},
8782
);
8883
}
8984

90-
if (response == null) return null;
85+
if (response == null) return;
9186

9287
if (response.statusCode != 201 && response.statusCode != 204) {
9388
throw FileSystemException(
@@ -96,12 +91,10 @@ class DavRemoteDirectoryFileSystem extends RemoteDirectoryFileSystem {
9691
OSError('${response.statusCode} ${response.reasonPhrase}'),
9792
);
9893
}
99-
100-
return getAsset(newPath);
10194
}
10295

10396
@override
104-
Future<void> deleteAsset(String path) async {
97+
Future<void> deleteRemoteAsset(String path) async {
10598
path = _normalizePath(path);
10699
final response = await createRequest(path.split('/'), method: 'DELETE');
107100
if (response == null) return;
@@ -307,15 +300,8 @@ class DavRemoteDirectoryFileSystem extends RemoteDirectoryFileSystem {
307300
}
308301

309302
@override
310-
Future<void> updateFile(
311-
String path,
312-
Uint8List data, {
313-
bool forceSync = false,
314-
}) async {
303+
Future<void> uploadFile(String path, Uint8List data) async {
315304
path = _normalizePath(path);
316-
if (!forceSync && storage.hasDocumentCached(path)) {
317-
cacheContent(path, data);
318-
}
319305

320306
var response = await createRequest(
321307
path.split('/'),
@@ -325,7 +311,7 @@ class DavRemoteDirectoryFileSystem extends RemoteDirectoryFileSystem {
325311

326312
if (response != null && response.statusCode == 409) {
327313
final directoryPath = p.url.dirname(path);
328-
await createDirectory(directoryPath);
314+
await createRemoteDirectory(directoryPath);
329315
response = await createRequest(
330316
path.split('/'),
331317
method: 'PUT',
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
import 'dart:async';
2+
import 'dart:typed_data';
3+
4+
import 'package:lw_file_system/lw_file_system.dart';
5+
6+
class MockFileSystem extends DirectoryFileSystem {
7+
final Map<String, Uint8List> _files = {};
8+
bool _initialized = false;
9+
10+
MockFileSystem({required super.config, super.createDefault});
11+
12+
@override
13+
FutureOr<bool> isInitialized() => _initialized;
14+
15+
@override
16+
Future<void> runInitialize() async {
17+
_initialized = true;
18+
await runDefault();
19+
}
20+
21+
@override
22+
Future<void> reset() async {
23+
_files.clear();
24+
_initialized = false;
25+
}
26+
27+
@override
28+
Future<Uint8List?> loadAbsolute(String path) async {
29+
return _files[path];
30+
}
31+
32+
@override
33+
Future<void> saveAbsolute(String path, Uint8List bytes) async {
34+
_files[path] = bytes;
35+
}
36+
37+
@override
38+
Future<bool> moveAbsolute(String oldPath, String newPath) async {
39+
if (_files.containsKey(oldPath)) {
40+
_files[newPath] = _files[oldPath]!;
41+
_files.remove(oldPath);
42+
return true;
43+
}
44+
return false;
45+
}
46+
47+
@override
48+
Future<FileSystemDirectory<Uint8List>> createDirectory(String path) async {
49+
return FileSystemDirectory(
50+
AssetLocation(path: normalizePath(path), remote: ''),
51+
);
52+
}
53+
54+
@override
55+
Future<void> updateFile(
56+
String path,
57+
Uint8List data, {
58+
bool forceSync = false,
59+
}) async {
60+
_files[normalizePath(path)] = data;
61+
}
62+
63+
@override
64+
Future<void> deleteAsset(String path) async {
65+
path = normalizePath(path);
66+
_files.remove(path);
67+
final prefix = '$path/';
68+
_files.removeWhere((key, value) => key.startsWith(prefix));
69+
}
70+
71+
@override
72+
Future<FileSystemEntity<Uint8List>?> moveAsset(
73+
String path,
74+
String newPath, {
75+
bool forceSync = false,
76+
}) async {
77+
path = normalizePath(path);
78+
newPath = normalizePath(newPath);
79+
80+
if (_files.containsKey(path)) {
81+
final data = _files[path]!;
82+
_files.remove(path);
83+
_files[newPath] = data;
84+
return FileSystemFile(
85+
AssetLocation(path: newPath, remote: ''),
86+
data: data,
87+
);
88+
}
89+
90+
final prefix = '$path/';
91+
final keysToMove = _files.keys.where((k) => k.startsWith(prefix)).toList();
92+
if (keysToMove.isNotEmpty) {
93+
for (final key in keysToMove) {
94+
final suffix = key.substring(prefix.length);
95+
final newKey = '$newPath/$suffix';
96+
_files[newKey] = _files[key]!;
97+
_files.remove(key);
98+
}
99+
return FileSystemDirectory(AssetLocation(path: newPath, remote: ''));
100+
}
101+
102+
return null;
103+
}
104+
105+
@override
106+
Future<FileSystemEntity<Uint8List>?> readAsset(
107+
String path, {
108+
bool readData = true,
109+
bool forceRemote = false,
110+
}) async {
111+
path = normalizePath(path);
112+
113+
if (_files.containsKey(path)) {
114+
return FileSystemFile(
115+
AssetLocation(path: path, remote: ''),
116+
data: readData ? _files[path] : null,
117+
);
118+
}
119+
120+
final prefix = path.isEmpty ? '' : '$path/';
121+
final children = _files.keys
122+
.where((k) => k.startsWith(prefix) && k != path)
123+
.toList();
124+
125+
if (children.isEmpty && path.isNotEmpty) {
126+
return null;
127+
}
128+
129+
final assets = <FileSystemEntity<Uint8List>>[];
130+
final seen = <String>{};
131+
132+
for (final childPath in children) {
133+
final relative = childPath.substring(prefix.length);
134+
final parts = relative.split('/');
135+
final name = parts.first;
136+
if (seen.contains(name)) continue;
137+
seen.add(name);
138+
139+
final fullChildPath = prefix + name;
140+
if (_files.containsKey(fullChildPath)) {
141+
assets.add(
142+
FileSystemFile(
143+
AssetLocation(path: fullChildPath, remote: ''),
144+
data: readData ? _files[fullChildPath] : null,
145+
),
146+
);
147+
} else {
148+
assets.add(
149+
FileSystemDirectory(AssetLocation(path: fullChildPath, remote: '')),
150+
);
151+
}
152+
}
153+
154+
return FileSystemDirectory(
155+
AssetLocation(path: path, remote: ''),
156+
assets: assets,
157+
);
158+
}
159+
160+
void addFile(String path, Uint8List content) {
161+
_files[normalizePath(path)] = content;
162+
}
163+
}
164+
165+
class MockKeyFileSystem extends KeyFileSystem {
166+
final Map<String, Uint8List> _files = {};
167+
bool _initialized = false;
168+
169+
MockKeyFileSystem({required super.config, super.createDefault});
170+
171+
@override
172+
FutureOr<bool> isInitialized() => _initialized;
173+
174+
@override
175+
Future<void> runInitialize() async {
176+
_initialized = true;
177+
await runDefault();
178+
}
179+
180+
@override
181+
Future<void> reset() async {
182+
_files.clear();
183+
_initialized = false;
184+
}
185+
186+
@override
187+
Future<Uint8List?> loadAbsolute(String path) async {
188+
return _files[path];
189+
}
190+
191+
@override
192+
Future<void> saveAbsolute(String path, Uint8List bytes) async {
193+
_files[path] = bytes;
194+
}
195+
196+
@override
197+
Future<bool> moveAbsolute(String oldPath, String newPath) async {
198+
if (_files.containsKey(oldPath)) {
199+
_files[newPath] = _files[oldPath]!;
200+
_files.remove(oldPath);
201+
return true;
202+
}
203+
return false;
204+
}
205+
206+
@override
207+
Future<Uint8List?> getFile(String key) async {
208+
return _files[normalizePath(key)];
209+
}
210+
211+
@override
212+
Future<bool> hasKey(String key) async {
213+
return _files.containsKey(normalizePath(key));
214+
}
215+
216+
@override
217+
Future<void> updateFile(String key, Uint8List data) async {
218+
_files[normalizePath(key)] = data;
219+
}
220+
221+
@override
222+
Future<void> deleteFile(String key) async {
223+
_files.remove(normalizePath(key));
224+
}
225+
226+
@override
227+
Future<List<String>> getKeys() async {
228+
return _files.keys.toList();
229+
}
230+
}

0 commit comments

Comments
 (0)