Make sure all notes end with a \n

Fixes #383
This commit is contained in:
Vishesh Handa
2021-03-26 10:09:15 +01:00
parent ebd225c3b5
commit d2d9504dc9
6 changed files with 80 additions and 64 deletions

View File

@ -1,3 +1,8 @@
- version: "1.76"
draft: true
bugs:
- text: "MD files should end with a single newline character #383"
- version: "1.74"
date: 2021-02-23
reddit: https://www.reddit.com/r/GitJournal/comments/lr9n0r/gitjournal_v174/

View File

@ -421,6 +421,11 @@ class Note with NotesNotifier {
var file = File(filePath);
var contents = _serializer.encode(data);
// Make sure all docs end with a \n
if (!contents.endsWith('\n')) {
contents += '\n';
}
await file.writeAsString(contents, flush: true);
}

View File

@ -36,7 +36,7 @@ void main() {
for (var i = 0; i < 3; i++) {
var note = Note(rootFolder, _getRandomFilePath(rootFolder.folderPath));
note.modified = DateTime(2020, 1, 10 + (i * 2));
note.body = "$i";
note.body = "$i\n";
await note.save();
}
@ -52,7 +52,7 @@ void main() {
_getRandomFilePath(sub1Folder.folderPath),
);
note.modified = DateTime(2020, 1, 10 + (i * 2));
note.body = "sub1-$i";
note.body = "sub1-$i\n";
await note.save();
}
@ -64,7 +64,7 @@ void main() {
_getRandomFilePath(sub2Folder.folderPath),
);
note.modified = DateTime(2020, 1, 10 + (i * 2));
note.body = "sub2-$i";
note.body = "sub2-$i\n";
await note.save();
}
@ -76,7 +76,7 @@ void main() {
_getRandomFilePath(p1Folder.folderPath),
);
note.modified = DateTime(2020, 1, 10 + (i * 2));
note.body = "p1-$i";
note.body = "p1-$i\n";
await note.save();
}
@ -98,15 +98,15 @@ void main() {
var notes = List<Note>.from(f.notes);
notes.sort((Note n1, Note n2) => n1.body.compareTo(n2.body));
expect(notes[0].body, "0");
expect(notes[1].body, "1");
expect(notes[2].body, "2");
expect(notes[3].body, "p1-0");
expect(notes[4].body, "p1-1");
expect(notes[5].body, "sub1-0");
expect(notes[6].body, "sub1-1");
expect(notes[7].body, "sub2-0");
expect(notes[8].body, "sub2-1");
expect(notes[0].body, "0\n");
expect(notes[1].body, "1\n");
expect(notes[2].body, "2\n");
expect(notes[3].body, "p1-0\n");
expect(notes[4].body, "p1-1\n");
expect(notes[5].body, "sub1-0\n");
expect(notes[6].body, "sub1-1\n");
expect(notes[7].body, "sub2-0\n");
expect(notes[8].body, "sub2-1\n");
});
test('Should add a note properly', () async {
@ -115,7 +115,7 @@ void main() {
var p1 = (f.fsFolder as NotesFolderFS).getFolderWithSpec("sub1/p1");
var note = Note(p1, p.join(p1.folderPath, "new.md"));
note.modified = DateTime(2020, 2, 1);
note.body = "new";
note.body = "new\n";
await note.save();
p1.add(note);
@ -124,16 +124,16 @@ void main() {
var notes = List<Note>.from(f.notes);
notes.sort((Note n1, Note n2) => n1.body.compareTo(n2.body));
expect(notes[0].body, "0");
expect(notes[1].body, "1");
expect(notes[2].body, "2");
expect(notes[3].body, "new");
expect(notes[4].body, "p1-0");
expect(notes[5].body, "p1-1");
expect(notes[6].body, "sub1-0");
expect(notes[7].body, "sub1-1");
expect(notes[8].body, "sub2-0");
expect(notes[9].body, "sub2-1");
expect(notes[0].body, "0\n");
expect(notes[1].body, "1\n");
expect(notes[2].body, "2\n");
expect(notes[3].body, "new\n");
expect(notes[4].body, "p1-0\n");
expect(notes[5].body, "p1-1\n");
expect(notes[6].body, "sub1-0\n");
expect(notes[7].body, "sub1-1\n");
expect(notes[8].body, "sub2-0\n");
expect(notes[9].body, "sub2-1\n");
// FIXME: Check if the callback for added is called with the correct index
});

View File

@ -30,11 +30,11 @@ void main() {
var parent = NotesFolderFS(null, tempDir.path, Settings(''));
var n1 = Note(parent, n1Path);
n1.body = "test";
n1.body = "test\n";
n1.created = dt;
var n2 = Note(parent, n2Path);
n2.data = MdYamlDoc(body: "test2", props: props);
n2.data = MdYamlDoc(body: "test2\n", props: props);
notes = [n1, n2];
});

View File

@ -25,7 +25,8 @@ bar: Foo
modified: 2017-02-15T22:41:19+01:00
---
Hello""";
Hello
""";
var notePath = p.join(tempDir.path, "note.md");
await File(notePath).writeAsString(content);
@ -43,7 +44,8 @@ bar: Foo
modified: 2019-12-02T04:00:00+00:00
---
Hello""";
Hello
""";
var actualContent = File(notePath).readAsStringSync();
expect(actualContent, equals(expectedContent));
@ -55,7 +57,8 @@ bar: Foo
mod: 2017-02-15T22:41:19+01:00
---
Hello""";
Hello
""";
var notePath = p.join(tempDir.path, "note.md");
await File(notePath).writeAsString(content);
@ -73,7 +76,8 @@ bar: Foo
mod: 2019-12-02T04:00:00+00:00
---
Hello""";
Hello
""";
var actualContent = File(notePath).readAsStringSync();
expect(actualContent, equals(expectedContent));
@ -85,7 +89,8 @@ bar: Foo
tags: [A, B]
---
Hello""";
Hello
""";
var notePath = p.join(tempDir.path, "note5.md");
await File(notePath).writeAsString(content);
@ -109,7 +114,8 @@ bar: Foo
tags: [A, C, D]
---
Hello""";
Hello
""";
var actualContent = File(notePath).readAsStringSync();
expect(actualContent, equals(expectedContent));
@ -143,7 +149,7 @@ bar: Foo
});
test('Should parse wiki style links', () async {
var content = "[[GitJournal]] needs some [[Wild Fire]]";
var content = "[[GitJournal]] needs some [[Wild Fire]]\n";
var notePath = p.join(tempDir.path, "note63.md");
await File(notePath).writeAsString(content);

View File

@ -27,7 +27,7 @@ void main() {
p.join(folder.folderPath, "${random.nextInt(1000)}.md"),
);
note.modified = DateTime(2020, 1, 10 + (i * 2));
note.body = "$i";
note.body = "$i\n";
await note.save();
}
await folder.loadRecursively();
@ -49,11 +49,11 @@ void main() {
expect(sf.subFolders.length, 0);
expect(sf.notes.length, 5);
expect(sf.notes[0].body, "4");
expect(sf.notes[1].body, "3");
expect(sf.notes[2].body, "2");
expect(sf.notes[3].body, "1");
expect(sf.notes[4].body, "0");
expect(sf.notes[0].body, "4\n");
expect(sf.notes[1].body, "3\n");
expect(sf.notes[2].body, "2\n");
expect(sf.notes[3].body, "1\n");
expect(sf.notes[4].body, "0\n");
});
test('Should on modification remains sorted', () async {
@ -63,14 +63,14 @@ void main() {
SortingMode(SortingField.Modified, SortingOrder.Descending),
);
var i = sf.notes.indexWhere((n) => n.body == "1");
var i = sf.notes.indexWhere((n) => n.body == "1\n");
sf.notes[i].modified = DateTime(2020, 2, 1);
expect(sf.notes[0].body, "1");
expect(sf.notes[1].body, "4");
expect(sf.notes[2].body, "3");
expect(sf.notes[3].body, "2");
expect(sf.notes[4].body, "0");
expect(sf.notes[0].body, "1\n");
expect(sf.notes[1].body, "4\n");
expect(sf.notes[2].body, "3\n");
expect(sf.notes[3].body, "2\n");
expect(sf.notes[4].body, "0\n");
});
test('Should add new note correctly', () async {
@ -82,19 +82,19 @@ void main() {
var note = Note(folder, p.join(folder.folderPath, "new.md"));
note.modified = DateTime(2020, 2, 1);
note.body = "new";
note.body = "new\n";
await note.save();
folder.add(note);
expect(sf.notes.length, 6);
expect(sf.notes[0].body, "new");
expect(sf.notes[1].body, "4");
expect(sf.notes[2].body, "3");
expect(sf.notes[3].body, "2");
expect(sf.notes[4].body, "1");
expect(sf.notes[5].body, "0");
expect(sf.notes[0].body, "new\n");
expect(sf.notes[1].body, "4\n");
expect(sf.notes[2].body, "3\n");
expect(sf.notes[3].body, "2\n");
expect(sf.notes[4].body, "1\n");
expect(sf.notes[5].body, "0\n");
});
test('Should add new note to end works correctly', () async {
@ -106,19 +106,19 @@ void main() {
var note = Note(folder, p.join(folder.folderPath, "new.md"));
note.modified = DateTime(2020, 1, 1);
note.body = "new";
note.body = "new\n";
await note.save();
folder.add(note);
expect(sf.notes.length, 6);
expect(sf.notes[0].body, "4");
expect(sf.notes[1].body, "3");
expect(sf.notes[2].body, "2");
expect(sf.notes[3].body, "1");
expect(sf.notes[4].body, "0");
expect(sf.notes[5].body, "new");
expect(sf.notes[0].body, "4\n");
expect(sf.notes[1].body, "3\n");
expect(sf.notes[2].body, "2\n");
expect(sf.notes[3].body, "1\n");
expect(sf.notes[4].body, "0\n");
expect(sf.notes[5].body, "new\n");
});
test('If still sorted while loading the notes', () async {
@ -137,11 +137,11 @@ void main() {
expect(sf.subFolders.length, 0);
expect(sf.notes.length, 5);
expect(sf.notes[0].body, "4");
expect(sf.notes[1].body, "3");
expect(sf.notes[2].body, "2");
expect(sf.notes[3].body, "1");
expect(sf.notes[4].body, "0");
expect(sf.notes[0].body, "4\n");
expect(sf.notes[1].body, "3\n");
expect(sf.notes[2].body, "2\n");
expect(sf.notes[3].body, "1\n");
expect(sf.notes[4].body, "0\n");
});
});
}