From d5ce4e11101b20e735fd8faf89765019b079c0bc Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Sat, 15 Aug 2020 13:22:05 +0200 Subject: [PATCH] FlattenedNotesFolderTest: Fix it Occasionally the random number generator would generate the same number and we wouldn't check if the file existed. Also, we were accidentally creating all the notes in the root dir, so this test was a bit useless. --- test/flattened_notes_folder_test.dart | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/test/flattened_notes_folder_test.dart b/test/flattened_notes_folder_test.dart index 5a7eb80c..7dec704b 100644 --- a/test/flattened_notes_folder_test.dart +++ b/test/flattened_notes_folder_test.dart @@ -9,6 +9,20 @@ import 'package:gitjournal/core/note.dart'; import 'package:gitjournal/core/notes_folder_fs.dart'; void main() { + var random = Random(DateTime.now().millisecondsSinceEpoch); + + String _getRandomFilePath(String basePath) { + while (true) { + var filePath = p.join(basePath, "${random.nextInt(1000)}.md"); + if (File(filePath).existsSync()) { + filePath = null; + continue; + } + + return filePath; + } + } + group('Flattened Notes Folder Test', () { Directory tempDir; NotesFolderFS rootFolder; @@ -18,12 +32,8 @@ void main() { rootFolder = NotesFolderFS(null, tempDir.path); - var random = Random(); for (var i = 0; i < 3; i++) { - var note = Note( - rootFolder, - p.join(rootFolder.folderPath, "${random.nextInt(1000)}.md"), - ); + var note = Note(rootFolder, _getRandomFilePath(rootFolder.folderPath)); note.modified = DateTime(2020, 1, 10 + (i * 2)); note.body = "$i"; await note.save(); @@ -37,7 +47,7 @@ void main() { for (var i = 0; i < 2; i++) { var note = Note( sub1Folder, - p.join(rootFolder.folderPath, "${random.nextInt(1000)}.md"), + _getRandomFilePath(sub1Folder.folderPath), ); note.modified = DateTime(2020, 1, 10 + (i * 2)); note.body = "sub1-$i"; @@ -48,7 +58,7 @@ void main() { for (var i = 0; i < 2; i++) { var note = Note( sub2Folder, - p.join(rootFolder.folderPath, "${random.nextInt(1000)}.md"), + _getRandomFilePath(sub2Folder.folderPath), ); note.modified = DateTime(2020, 1, 10 + (i * 2)); note.body = "sub2-$i"; @@ -60,7 +70,7 @@ void main() { for (var i = 0; i < 2; i++) { var note = Note( p1Folder, - p.join(rootFolder.folderPath, "${random.nextInt(1000)}.md"), + _getRandomFilePath(p1Folder.folderPath), ); note.modified = DateTime(2020, 1, 10 + (i * 2)); note.body = "p1-$i";