diff --git a/Makefile b/Makefile index 8130dade..454702c6 100644 --- a/Makefile +++ b/Makefile @@ -21,9 +21,16 @@ lint: ## Check style with various tools poetry run pyflakes jrnl tests poetry run black --check --diff . -test: lint ## Run unit tests and behave tests - poetry run pytest - poetry run behave --no-skipped --format progress2 +unit: # unit tests + poetry run pytest tests/unit + +e2e: # end-to-end tests + poetry run pytest tests/step_defs --gherkin-terminal-reporter --tb=native --diff-type=unified + +e2e-debug: # end-to-end tests + poetry run pytest tests/step_defs --gherkin-terminal-reporter --tb=native --diff-type=unified -x -vv + +test: lint unit e2e ## Run unit tests and behave tests build: poetry build diff --git a/jrnl/DayOneJournal.py b/jrnl/DayOneJournal.py index 61a60ca0..00271875 100644 --- a/jrnl/DayOneJournal.py +++ b/jrnl/DayOneJournal.py @@ -1,4 +1,4 @@ -from datetime import datetime +import datetime import fnmatch import os from pathlib import Path @@ -116,7 +116,7 @@ class DayOne(Journal.Journal): """Writes only the entries that have been modified into plist files.""" for entry in self.entries: if entry.modified: - utc_time = datetime.utcfromtimestamp( + utc_time = datetime.datetime.utcfromtimestamp( time.mktime(entry.date.timetuple()) ) diff --git a/jrnl/Entry.py b/jrnl/Entry.py index e227794f..56347770 100644 --- a/jrnl/Entry.py +++ b/jrnl/Entry.py @@ -2,7 +2,7 @@ # License: https://www.gnu.org/licenses/gpl-3.0.html -from datetime import datetime +import datetime import re import ansiwrap @@ -14,7 +14,7 @@ from .color import highlight_tags_with_background_color class Entry: def __init__(self, journal, date=None, text="", starred=False): self.journal = journal # Reference to journal mainly to access its config - self.date = date or datetime.now() + self.date = date or datetime.datetime.now() self.text = text self._title = None self._body = None diff --git a/jrnl/Journal.py b/jrnl/Journal.py index b889c0d3..181d85c4 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -2,7 +2,7 @@ # License: https://www.gnu.org/licenses/gpl-3.0.html -from datetime import datetime +import datetime import logging import os import re @@ -134,7 +134,9 @@ class Journal: for match in date_blob_re.finditer(journal_txt): date_blob = match.groups()[0] try: - new_date = datetime.strptime(date_blob, self.config["timeformat"]) + new_date = datetime.datetime.strptime( + date_blob, self.config["timeformat"] + ) except ValueError: # Passing in a date that had brackets around it new_date = time.parse(date_blob, bracketed=True) @@ -347,7 +349,7 @@ class LegacyJournal(Journal): """Parses a journal that's stored in a string and returns a list of entries""" # Entries start with a line that looks like 'date title' - let's figure out how # long the date will be by constructing one - date_length = len(datetime.today().strftime(self.config["timeformat"])) + date_length = len(datetime.datetime.today().strftime(self.config["timeformat"])) # Initialise our current entry entries = [] @@ -357,7 +359,7 @@ class LegacyJournal(Journal): line = line.rstrip() try: # try to parse line as date => new entry begins - new_date = datetime.strptime( + new_date = datetime.datetime.strptime( line[:date_length], self.config["timeformat"] ) diff --git a/jrnl/time.py b/jrnl/time.py index b9ea8e84..f4e7319d 100644 --- a/jrnl/time.py +++ b/jrnl/time.py @@ -1,11 +1,11 @@ # Copyright (C) 2012-2021 jrnl contributors # License: https://www.gnu.org/licenses/gpl-3.0.html -from datetime import datetime +import datetime FAKE_YEAR = 9999 -DEFAULT_FUTURE = datetime(FAKE_YEAR, 12, 31, 23, 59, 59) -DEFAULT_PAST = datetime(FAKE_YEAR, 1, 1, 0, 0) +DEFAULT_FUTURE = datetime.datetime(FAKE_YEAR, 12, 31, 23, 59, 59) +DEFAULT_PAST = datetime.datetime(FAKE_YEAR, 1, 1, 0, 0) def __get_pdt_calendar(): @@ -27,7 +27,7 @@ def parse( """Parses a string containing a fuzzy date and returns a datetime.datetime object""" if not date_str: return None - elif isinstance(date_str, datetime): + elif isinstance(date_str, datetime.datetime): return date_str # Don't try to parse anything with 6 or less characters and was parsed from the existing journal. @@ -44,7 +44,9 @@ def parse( date = dateparse(date_str, default=default_date) if date.year == FAKE_YEAR: - date = datetime(datetime.now().year, date.timetuple()[1:6]) + date = datetime.datetime( + datetime.datetime.now().year, date.timetuple()[1:6] + ) else: year_present = True flag = 1 if date.hour == date.minute == 0 else 2 @@ -52,7 +54,7 @@ def parse( except Exception as e: if e.args[0] == "day is out of range for month": y, m, d, H, M, S = default_date.timetuple()[:6] - default_date = datetime(y, m, d - 1, H, M, S) + default_date = datetime.datetime(y, m, d - 1, H, M, S) else: calendar = __get_pdt_calendar() date, flag = calendar.parse(date_str) @@ -60,26 +62,26 @@ def parse( if not flag: # Oops, unparsable. try: # Try and parse this as a single year year = int(date_str) - return datetime(year, 1, 1) + return datetime.datetime(year, 1, 1) except ValueError: return None except TypeError: return None if flag == 1: # Date found, but no time. Use the default time. - date = datetime( + date = datetime.datetime( *date[:3], hour=23 if inclusive else default_hour or 0, minute=59 if inclusive else default_minute or 0, second=59 if inclusive else 0 ) else: - date = datetime(*date[:6]) + date = datetime.datetime(*date[:6]) # Ugly heuristic: if the date is more than 4 weeks in the future, we got the year wrong. # Rather then this, we would like to see parsedatetime patched so we can tell it to prefer # past dates - dt = datetime.now() - date + dt = datetime.datetime.now() - date if dt.days < -28 and not year_present: date = date.replace(date.year - 1) return date diff --git a/tests/features/datetime.feature b/tests/features/datetime.feature index 0a3f5155..0da3027f 100644 --- a/tests/features/datetime.feature +++ b/tests/features/datetime.feature @@ -36,10 +36,10 @@ Feature: Reading and writing to journal with custom date formats When we run "jrnl " Then we should see the message "Entry added" When we run "jrnl -n 1" - Then the output should contain "" + Then the output should contain "" Examples: Day-first Dates - | config_file | command | output | + | config_file | command | expected_output | | little_endian_dates.yaml | 2020-09-19: My first entry. | 19.09.2020 09:00 My first entry. | | little_endian_dates.yaml | 2020-08-09: My second entry. | 09.08.2020 09:00 My second entry. | | little_endian_dates.yaml | 2020-02-29: Test. | 29.02.2020 09:00 Test. | @@ -53,10 +53,10 @@ Feature: Reading and writing to journal with custom date formats Scenario Outline: Searching for dates with custom date Given we use the config "" When we run "jrnl " - Then the output should be "" + Then the output should be "" Examples: Day-first Dates - | config_file | command | output | + | config_file | command | expected_output | | little_endian_dates.yaml | -on '2013-07-10' --short | 10.07.2013 15:40 Life is good. | | little_endian_dates.yaml | -on 'june 9 2013' --short | 09.06.2013 15:39 My first entry. | | little_endian_dates.yaml | -on 'july 10 2013' --short | 10.07.2013 15:40 Life is good. | @@ -83,47 +83,48 @@ Feature: Reading and writing to journal with custom date formats Then the output should not contain "Life is good" And the output should not contain "But I'm better." - - Scenario Outline: Create entry using day of the week as entry date. + Scenario Outline: Create entry using day of the week as entry date one. Given we use the config "simple.yaml" + And now is "2019-03-12 01:30:32 PM" When we run "jrnl " Then we should see the message "Entry added" When we run "jrnl -1" - Then the output should contain "" + Then the output should contain "" Then the output should contain the date "" Examples: Days of the week - | command | output | date | - | Monday: entry on a monday | entry on a monday | monday at 9am | - | Tuesday: entry on a tuesday | entry on a tuesday | tuesday at 9am | - | Wednesday: entry on a wednesday | entry on a wednesday | wednesday at 9am | - | Thursday: entry on a thursday | entry on a thursday | thursday at 9am | - | Friday: entry on a friday | entry on a friday | friday at 9am | - | Saturday: entry on a saturday | entry on a saturday | saturday at 9am | - | Sunday: entry on a sunday | entry on a sunday | sunday at 9am | - | sunday: entry on a sunday | entry on a sunday | sunday at 9am | - | sUndAy: entry on a sunday | entry on a sunday | sunday at 9am | + | command | expected_output | date | + | Monday: entry on a monday | entry on a monday | 2019-03-11 09:00 | + | Tuesday: entry on a tuesday | entry on a tuesday | 2019-03-05 09:00 | + | Wednesday: entry on a wednesday | entry on a wednesday | 2019-03-06 09:00 | + | Thursday: entry on a thursday | entry on a thursday | 2019-03-07 09:00 | + | Friday: entry on a friday | entry on a friday | 2019-03-08 09:00 | + | Saturday: entry on a saturday | entry on a saturday | 2019-03-09 09:00 | + | Sunday: entry on a sunday | entry on a sunday | 2019-03-10 09:00 | + | sunday: entry on a sunday | entry on a sunday | 2019-03-10 09:00 | + | sUndAy: entry on a sunday | entry on a sunday | 2019-03-10 09:00 | - Scenario Outline: Create entry using day of the week as entry date. + Scenario Outline: Create entry using day of the week as entry date two. Given we use the config "simple.yaml" + And now is "2019-03-12 01:30:32 PM" When we run "jrnl " Then we should see the message "Entry added" When we run "jrnl -1" - Then the output should contain "" + Then the output should contain "" Then the output should contain the date "" Examples: Days of the week - | command | output | date | - | Mon: entry on a monday | entry on a monday | monday at 9am | - | Tue: entry on a tuesday | entry on a tuesday | tuesday at 9am | - | Wed: entry on a wednesday | entry on a wednesday | wednesday at 9am | - | Thu: entry on a thursday | entry on a thursday | thursday at 9am | - | Fri: entry on a friday | entry on a friday | friday at 9am | - | Sat: entry on a saturday | entry on a saturday | saturday at 9am | - | Sun: entry on a sunday | entry on a sunday | sunday at 9am | - | sun: entry on a sunday | entry on a sunday | sunday at 9am | - | sUn: entry on a sunday | entry on a sunday | sunday at 9am | + | command | expected_output | date | + | Mon: entry on a monday | entry on a monday | 2019-03-11 09:00 | + | Tue: entry on a tuesday | entry on a tuesday | 2019-03-05 09:00 | + | Wed: entry on a wednesday | entry on a wednesday | 2019-03-06 09:00 | + | Thu: entry on a thursday | entry on a thursday | 2019-03-07 09:00 | + | Fri: entry on a friday | entry on a friday | 2019-03-08 09:00 | + | Sat: entry on a saturday | entry on a saturday | 2019-03-09 09:00 | + | Sun: entry on a sunday | entry on a sunday | 2019-03-10 09:00 | + | sun: entry on a sunday | entry on a sunday | 2019-03-10 09:00 | + | sUn: entry on a sunday | entry on a sunday | 2019-03-10 09:00 | Scenario: Journals with unreadable dates should still be loaded diff --git a/tests/step_defs/conftest.py b/tests/step_defs/conftest.py index ea561ba0..e49b8217 100644 --- a/tests/step_defs/conftest.py +++ b/tests/step_defs/conftest.py @@ -4,6 +4,7 @@ import ast import json import os +from datetime import datetime from collections import defaultdict from keyring import backend from keyring import set_keyring @@ -15,6 +16,7 @@ import re import shutil import tempfile from unittest.mock import patch +from unittest.mock import MagicMock from xml.etree import ElementTree from pytest_bdd import given @@ -31,6 +33,7 @@ from jrnl.cli import cli from jrnl.config import load_config from jrnl.os_compat import split_args from jrnl.os_compat import on_windows +from jrnl.time import __get_pdt_calendar class TestKeyring(backend.KeyringBackend): @@ -99,10 +102,6 @@ def pytest_bdd_apply_tag(tag, function): # ----- UTILS ----- # -def failed_msg(msg, expected, actual): - return f"{msg}\nExpected:\n{expected}\n---end---\nActual:\n{actual}\n---end---\n" - - def read_value_from_string(string): if string[0] == "{": # Handle value being a dictionary @@ -142,6 +141,11 @@ def password(): return "" +@fixture +def now_date(): + return {"datetime": datetime, "calendar_parse": __get_pdt_calendar()} + + @fixture def cache_dir(): return {"exists": False, "path": ""} @@ -251,6 +255,29 @@ def we_enter_editor(editor_method, editor_input, editor_state): editor_state["intent"] = {"method": file_method, "input": editor_input} +@given(parse('now is ""'), target_fixture="now_date") +@given(parse('now is "{date_str}"'), target_fixture="now_date") +def now_is_str(date_str): + class DatetimeMagicMock(MagicMock): + # needed because jrnl does some reflection on datetime + def __instancecheck__(self, subclass): + return isinstance(subclass, datetime) + + my_date = datetime.strptime(date_str, "%Y-%m-%d %I:%M:%S %p") + + # jrnl uses two different classes to parse dates, so both must be mocked + datetime_mock = DatetimeMagicMock(wraps=datetime) + datetime_mock.now.return_value = my_date + + pdt = __get_pdt_calendar() + calendar_mock = MagicMock(wraps=pdt) + calendar_mock.parse.side_effect = lambda date_str_input: pdt.parse( + date_str_input, my_date + ) + + return {"datetime": datetime_mock, "calendar_parse": calendar_mock} + + @then(parse("the editor should have been called")) @then(parse("the editor should have been called with {num_args} arguments")) def count_editor_args(num_args, cli_run, editor_state): @@ -328,9 +355,9 @@ def we_run( cli_run, capsys, password, - keyring, cache_dir, editor, + now_date, ): if cache_dir["exists"]: command = command.format(cache_dir=cache_dir["path"]) @@ -354,6 +381,8 @@ def we_run( patch("sys.stdin.read", side_effect=user_input) as mock_stdin, \ patch("builtins.input", side_effect=user_input) as mock_input, \ patch("getpass.getpass", side_effect=password) as mock_getpass, \ + patch("datetime.datetime", new=now_date["datetime"]), \ + patch("jrnl.time.__get_pdt_calendar", return_value=now_date["calendar_parse"]), \ patch("jrnl.install.get_config_path", return_value=config_path), \ patch("jrnl.config.get_config_path", return_value=config_path), \ patch("subprocess.call", side_effect=editor) as mock_editor \ @@ -392,48 +421,53 @@ def output_should_match(regex, cli_run): assert matches, f"\nRegex didn't match:\n{regex}\n{str(out)}\n{str(matches)}" -@then(parse("the output should contain\n{output}")) -@then(parse('the output should contain "{output}"')) -@then('the output should contain ""') -@then(parse("the {which_output_stream} output should contain\n{output}")) -@then(parse('the {which_output_stream} output should contain "{output}"')) -def output_should_contain(output, which_output_stream, cli_run): - assert output +@then(parse("the output should contain\n{expected_output}")) +@then(parse('the output should contain "{expected_output}"')) +@then('the output should contain ""') +@then(parse("the {which_output_stream} output should contain\n{expected_output}")) +@then(parse('the {which_output_stream} output should contain "{expected_output}"')) +def output_should_contain(expected_output, which_output_stream, cli_run): + assert expected_output if which_output_stream is None: - assert (output in cli_run["stdout"]) or (output in cli_run["stderr"]) + assert (expected_output in cli_run["stdout"]) or ( + expected_output in cli_run["stderr"] + ) elif which_output_stream == "standard": - assert output in cli_run["stdout"] + assert expected_output in cli_run["stdout"] elif which_output_stream == "error": - assert output in cli_run["stderr"] + assert expected_output in cli_run["stderr"] else: - assert output in cli_run[which_output_stream] + assert expected_output in cli_run[which_output_stream] -@then(parse("the output should not contain\n{output}")) -@then(parse('the output should not contain "{output}"')) -@then('the output should not contain ""') -def output_should_not_contain(output, cli_run): - assert output not in cli_run["stdout"] +@then(parse("the output should not contain\n{expected_output}")) +@then(parse('the output should not contain "{expected_output}"')) +@then('the output should not contain ""') +def output_should_not_contain(expected_output, cli_run): + assert expected_output not in cli_run["stdout"] + + +@then(parse("the output should be\n{expected_output}")) +@then(parse('the output should be "{expected_output}"')) +@then('the output should be ""') +def output_should_be(expected_output, cli_run): + actual = cli_run["stdout"].strip() + expected = expected_output.strip() + assert expected == actual -@then(parse("the output should be\n{str_value}")) -@then(parse('the output should be "{str_value}"')) -@then('the output should be ""') @then("the output should be empty") -def output_should_be(str_value, cli_run): - actual_out = cli_run["stdout"].strip() - expected = str_value.strip() - assert expected == actual_out, failed_msg( - "Output does not match.", expected, actual_out - ) +def output_should_be_empty(cli_run): + actual = cli_run["stdout"].strip() + assert actual == "" @then('the output should contain the date ""') -def output_should_contain_date(output, cli_run): - assert output and output in cli_run["stdout"] +def output_should_contain_date(date, cli_run): + assert date and date in cli_run["stdout"] @then("the output should contain pyproject.toml version") @@ -574,12 +608,12 @@ def assert_output_is_valid_language(cli_run, language_name): @given(parse("we parse the output as {language_name}"), target_fixture="parsed_output") def parse_output_as_language(cli_run, language_name): language_name = language_name.upper() - output = cli_run["stdout"] + actual_output = cli_run["stdout"] if language_name == "XML": - parsed_output = ElementTree.fromstring(output) + parsed_output = ElementTree.fromstring(actual_output) elif language_name == "JSON": - parsed_output = json.loads(output) + parsed_output = json.loads(actual_output) else: assert False, f"Language name {language_name} not recognized" @@ -669,6 +703,6 @@ def assert_output_field_content( @then(parse('there should be {number:d} "{item}" elements')) def count_elements(number, item, cli_run): - output = cli_run["stdout"] - xml_tree = ElementTree.fromstring(output) + actual_output = cli_run["stdout"] + xml_tree = ElementTree.fromstring(actual_output) assert len(xml_tree.findall(".//" + item)) == number diff --git a/tests/test_color.py b/tests/unit/test_color.py similarity index 100% rename from tests/test_color.py rename to tests/unit/test_color.py diff --git a/tests/test_display.py b/tests/unit/test_display.py similarity index 100% rename from tests/test_display.py rename to tests/unit/test_display.py diff --git a/tests/test_exception.py b/tests/unit/test_exception.py similarity index 100% rename from tests/test_exception.py rename to tests/unit/test_exception.py diff --git a/tests/test_export.py b/tests/unit/test_export.py similarity index 100% rename from tests/test_export.py rename to tests/unit/test_export.py diff --git a/tests/test_install.py b/tests/unit/test_install.py similarity index 100% rename from tests/test_install.py rename to tests/unit/test_install.py diff --git a/tests/test_os_compat.py b/tests/unit/test_os_compat.py similarity index 100% rename from tests/test_os_compat.py rename to tests/unit/test_os_compat.py diff --git a/tests/test_override.py b/tests/unit/test_override.py similarity index 100% rename from tests/test_override.py rename to tests/unit/test_override.py diff --git a/tests/test_parse_args.py b/tests/unit/test_parse_args.py similarity index 100% rename from tests/test_parse_args.py rename to tests/unit/test_parse_args.py diff --git a/tests/test_time.py b/tests/unit/test_time.py similarity index 100% rename from tests/test_time.py rename to tests/unit/test_time.py