Skip to content

Commit

Permalink
Don't throw when files/ directory is missing
Browse files Browse the repository at this point in the history
Closes #123 where downloading some languages didn't work (because they
had only few worksheets that didn't have images, so there was no files/
directory)
  • Loading branch information
holybiber committed Oct 20, 2023
1 parent 64a78e9 commit 6d679f8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lib/data/globals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ class Globals {

/// File system path (relative to assets directory)
/// of the resources in a language
/// Must be the main folder name that is inside the zip file we download
/// Must be the main folder name that is inside the zip file we download,
/// e.g. 'html-en-main'
static String getLocalPath(String languageCode) {
return '$htmlPath-$languageCode-$branch';
}
Expand Down
16 changes: 9 additions & 7 deletions lib/data/languages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,15 @@ class LanguageController extends FamilyNotifier<Language, String> {
await _checkConsistency(dir, pages);

// Register available images
await for (var file in fileSystem
.directory(join(path, 'files'))
.list(recursive: false, followLinks: false)) {
if (file is File) {
images[basename(file.path)] = Image(basename(file.path));
} else {
debugPrint("Found unexpected element $file in files/ directory");
var filesDir = fileSystem.directory(join(path, 'files'));
if (await filesDir.exists()) {
await for (var file
in filesDir.list(recursive: false, followLinks: false)) {
if (file is File) {
images[basename(file.path)] = Image(basename(file.path));
} else {
debugPrint("Found unexpected element $file in files/ directory");
}
}
}
state = Language(
Expand Down
25 changes: 25 additions & 0 deletions test/languages_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,31 @@ void main() {
}
expect(deTest.state.downloaded, false);
});

test('A missing files/ dir should be no problem', () async {
// We construct a file system in memory with structure/contents.json
// filled correctly but where HTML files and the files/ dir are missing
var fileSystem = MemoryFileSystem();
await fileSystem
.directory('assets-de/html-de-main/structure')
.create(recursive: true);
var readFileSystem = ChrootFileSystem(
const LocalFileSystem(), path.canonicalize('test/'));
String jsonPath = 'assets-de/html-de-main/structure/contents.json';
var contentsJson = fileSystem.file(jsonPath);
contentsJson
.writeAsString(await readFileSystem.file(jsonPath).readAsString());
final container = ProviderContainer(overrides: [
languageProvider
.overrideWith(() => LanguageController(assetsController: mock)),
fileSystemProvider.overrideWith((ref) => fileSystem),
]);

// init() should work (even if expected HTML files are missing)
final deTest = container.read(languageProvider('de').notifier);
await deTest.init();
expect(deTest.state.downloaded, true);
});
});

test('Test everything with real content from test/assets-de/', () async {
Expand Down

0 comments on commit 6d679f8

Please sign in to comment.