-
-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only read text files that look like entries when opening folder journ…
…al (#1697) * Add text file that should be ignored to basic test folder journal. Makes tons of tests fail * Add additional files that should be ignored by FolderJournal * Ignore all files in folder journal except year/month/day.txt * Completely remake get_files in FolderJournal: - move get_files into FolderJournal class and add underscore prefix - create iterables to get for year/month folders and day files - make year/month/day file reading strict: only exact expected months and days out of all possible months and days * Restore accidentally-deleted self.sort() line * Use match instead of string comparison to be os-agnostic * Explicitly declare static methods * Filter with glob first for max performance * Explicitly check for valid dates in FolderJournal and add unit test * Remove unneeded jrnl import * Clean up method comment and add type hints * Add is_valid_date unit test * Elucidate comment Co-authored-by: Jonathan Wren <jonathan@nowandwren.com>
- Loading branch information
1 parent
88aa249
commit 95836a7
Showing
7 changed files
with
153 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
tests/data/journals/basic_folder/2020/09/should-be-ignored.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[2022-03-02 9:25:00 AM] This file should be ignored (month) | ||
This text file is in a folder journal's month directory ("2020/09"), but it's not in the file name format used by jrnl for folder journal entries, so it should be ignored. | ||
|
||
This file should not ever appear in a test. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[2022-03-02 9:25:00 AM] This file should be ignored (year) | ||
This text file is in a folder journal's year directory ("2020"), but it's not in the file name format used by jrnl for folder journal entries, so it should be ignored. | ||
|
||
This file should not ever appear in a test. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[2022-03-02 9:25:00 AM] This file should be ignored (root) | ||
This text file is in a folder journal's root directory, but it's not in the file name format used by jrnl for folder journal entries, so it should be ignored. | ||
|
||
This file should not ever appear in a test. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright © 2012-2023 jrnl contributors | ||
# License: https://www.gnu.org/licenses/gpl-3.0.html | ||
|
||
import pathlib | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from jrnl.journals.FolderJournal import Folder | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"inputs_and_outputs", | ||
[ | ||
[ | ||
"/2020/01", | ||
["02.txt", "03.txt", "31.txt"], | ||
["/2020/01/02.txt", "/2020/01/03.txt", "/2020/01/31.txt"], | ||
], | ||
[ | ||
"/2020/02", # leap year | ||
["02.txt", "03.txt", "28.txt", "29.txt", "31.txt", "39.txt"], | ||
[ | ||
"/2020/02/02.txt", | ||
"/2020/02/03.txt", | ||
"/2020/02/28.txt", | ||
"/2020/02/29.txt", | ||
], | ||
], | ||
[ | ||
"/2100/02", # not a leap year | ||
["01.txt", "28.txt", "29.txt", "39.txt"], | ||
["/2100/02/01.txt", "/2100/02/28.txt"], | ||
], | ||
[ | ||
"/2023/04", | ||
["29.txt", "30.txt", "31.txt", "39.txt"], | ||
["/2023/04/29.txt", "/2023/04/30.txt"], | ||
], | ||
], | ||
) | ||
def test_get_day_files_expected_filtering(inputs_and_outputs): | ||
year_month_path, glob_filenames, expected_output = inputs_and_outputs | ||
|
||
year_month_path = pathlib.Path(year_month_path) | ||
|
||
glob_files = map(lambda x: year_month_path / x, glob_filenames) | ||
expected_output = list(map(lambda x: str(pathlib.PurePath(x)), expected_output)) | ||
|
||
with ( | ||
mock.patch("pathlib.Path.glob", return_value=glob_files), | ||
mock.patch.object(pathlib.Path, "is_file", return_value=True), | ||
): | ||
actual_output = list(Folder._get_day_files(year_month_path)) | ||
actual_output.sort() | ||
|
||
expected_output.sort() | ||
|
||
assert actual_output == expected_output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters