From 88be618e8c19debb2ec7df3215a26d32a200eef5 Mon Sep 17 00:00:00 2001 From: markspl <14055798+markspl@users.noreply.github.com> Date: Tue, 13 Oct 2026 23:54:08 +0300 Subject: [PATCH 1/7] feat: add unit tests - cover model, render, commands, storage and the tl.py dispatcher with unittest - tests use a temp home dir and a frozen clock - document how to run them in README and AGENTS. --- AGENTS.md | 2 +- README.md | 10 +++ tests/test_commands.py | 161 +++++++++++++++++++++++++++++++++++++++++ tests/test_model.py | 43 +++++++++++ tests/test_render.py | 33 +++++++++ tests/test_tl.py | 42 +++++++++++ 6 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 tests/test_commands.py create mode 100644 tests/test_model.py create mode 100644 tests/test_render.py create mode 100644 tests/test_tl.py diff --git a/AGENTS.md b/AGENTS.md index bca32c1..a35314c 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -4,7 +4,7 @@ Guidance for AI coding agents working in this repository. ## What this is -`tl` is a small command-line time logger written in pure Python (3.8+, standard library only). There is no build step, no linter config and no test suite yet. See [README.md](README.md) for the command list, the design rules (how durations work) and the log file format. +`tl` is a small command-line time logger written in pure Python (3.8+, standard library only). There is no build step and no linter config. Tests use `unittest` and live in `tests/`; run them from the repo root with `python -m unittest discover -s tests`. They use a temp home dir and a frozen clock, so they never touch the real log. See [README.md](README.md) for the command list, the design rules (how durations work) and the log file format. ## Running diff --git a/README.md b/README.md index 1a9e63a..ef8c40d 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,16 @@ Try it with `tl version`. > **Windows:** run the steps in Git Bash (it comes with Git for Windows). If `python3` is not found, use `python` or `py` in the alias instead. +## Tests + +Run the tests from the repo root (standard library only, no install needed): + +```bash +python -m unittest discover -s tests +``` + +They use a temporary home folder, so your real `~/.timelog` is never touched. + ## Design ideas - A task lasts from its own `add` line to the next `add` or `stop` line. diff --git a/tests/test_commands.py b/tests/test_commands.py new file mode 100644 index 0000000..732f305 --- /dev/null +++ b/tests/test_commands.py @@ -0,0 +1,161 @@ +import contextlib +import io +import tempfile +import unittest +from datetime import datetime +from pathlib import Path +from unittest import mock + +from timelog import commands, storage +from timelog.model import Entry + +NOW = datetime(2026, 9, 23, 12, 0) + + +class CommandTestCase(unittest.TestCase): + """Runs commands against a temp home dir and a frozen clock.""" + + def setUp(self): + tmp = tempfile.TemporaryDirectory() + self.addCleanup(tmp.cleanup) + self.home = Path(tmp.name) + for patcher in ( + mock.patch.object(Path, "home", return_value=self.home), + mock.patch.object(commands, "_now", return_value=NOW), + ): + patcher.start() + self.addCleanup(patcher.stop) + + def run_cmd(self, func, args): + out, err = io.StringIO(), io.StringIO() + with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): + code = func(args) + return code, out.getvalue(), err.getvalue() + + def day_file(self, day="2026-09-23"): + return self.home / ".timelog" / (day + ".tsv") + + def lines(self, day="2026-09-23"): + return self.day_file(day).read_text(encoding="utf-8").splitlines() + + +class ParseTimeFlagTest(unittest.TestCase): + def test_clock_time(self): + self.assertEqual(commands._parse_time_flag("09:30", NOW), datetime(2026, 9, 23, 9, 30)) + + def test_durations(self): + parse = commands._parse_time_flag + self.assertEqual(parse("1h30m", NOW), datetime(2026, 9, 23, 10, 30)) + self.assertEqual(parse("1h", NOW), datetime(2026, 9, 23, 11, 0)) + self.assertEqual(parse("15m", NOW), datetime(2026, 9, 23, 11, 45)) + + def test_crossing_midnight_goes_to_previous_day(self): + self.assertEqual(commands._parse_time_flag("13h", NOW), datetime(2026, 9, 22, 23, 0)) + + def test_invalid(self): + for bad in ("", "abc", "1x", "ab:cd", "25:00", "1:2:3"): + with self.subTest(bad=bad), self.assertRaises(ValueError): + commands._parse_time_flag(bad, NOW) + + +class GetTimeFlagTest(unittest.TestCase): + def test_no_flag(self): + self.assertEqual(commands._get_time_flag(["a", "b"]), (None, ["a", "b"])) + + def test_flag_only_first(self): + self.assertEqual(commands._get_time_flag(["a", "-t", "5m"]), (None, ["a", "-t", "5m"])) + + def test_short_and_long(self): + self.assertEqual(commands._get_time_flag(["-t", "5m", "x"]), ("5m", ["x"])) + self.assertEqual(commands._get_time_flag(["--time", "5m", "x"]), ("5m", ["x"])) + + def test_missing_value(self): + with contextlib.redirect_stderr(io.StringIO()): + self.assertEqual(commands._get_time_flag(["-t"]), (None, None)) + + +class AddTest(CommandTestCase): + def test_add_now(self): + code, out, _ = self.run_cmd(commands.add, ["Team", "daily"]) + self.assertEqual(code, 0) + self.assertEqual(out, "Started: Team daily (12:00)\n") + self.assertEqual(self.lines(), ["2026-09-23 12:00\tadd\tTeam daily"]) + + def test_add_with_clock_time(self): + code, out, _ = self.run_cmd(commands.add, ["-t", "09:30", "Meeting"]) + self.assertEqual(code, 0) + self.assertEqual(out, "Added: Meeting (09:30)\n") + self.assertEqual(self.lines(), ["2026-09-23 09:30\tadd\tMeeting"]) + + def test_add_with_duration(self): + self.run_cmd(commands.add, ["--time", "1h30m", "Lunch"]) + self.assertEqual(self.lines(), ["2026-09-23 10:30\tadd\tLunch"]) + + def test_backdated_entry_appended_at_end(self): + self.run_cmd(commands.add, ["Later"]) + self.run_cmd(commands.add, ["-t", "08:00", "Earlier"]) + self.assertEqual( + self.lines(), + ["2026-09-23 12:00\tadd\tLater", "2026-09-23 08:00\tadd\tEarlier"], + ) + + def test_backdated_to_previous_day_uses_that_days_file(self): + self.run_cmd(commands.add, ["-t", "13h", "Late"]) + self.assertEqual(self.lines("2026-09-22"), ["2026-09-22 23:00\tadd\tLate"]) + self.assertFalse(self.day_file().exists()) + + def test_bad_time_flag(self): + code, out, err = self.run_cmd(commands.add, ["-t", "nope", "x"]) + self.assertEqual(code, 1) + self.assertEqual(out, "") + self.assertIn("can't parse time flag 'nope'", err) + self.assertFalse(self.day_file().exists()) + + def test_flag_without_value(self): + code, _, err = self.run_cmd(commands.add, ["-t"]) + self.assertEqual(code, 1) + self.assertIn("needs a value", err) + + def test_missing_description(self): + for args in ([], ["-t", "5m"], [" "]): + with self.subTest(args=args): + code, out, err = self.run_cmd(commands.add, args) + self.assertEqual(code, 1) + self.assertEqual(out, "") + self.assertIn("description is missing", err) + self.assertFalse(self.day_file().exists()) + + +class NoteTest(CommandTestCase): + def test_note(self): + code, out, _ = self.run_cmd(commands.note, ["PR", "created"]) + self.assertEqual(code, 0) + self.assertEqual(out, "Note added: PR created (12:00)\n") + self.assertEqual(self.lines(), ["2026-09-23 12:00\tnote\tPR created"]) + + def test_time_flag_rejected(self): + for flag in ("-t", "--time"): + code, _, err = self.run_cmd(commands.note, [flag, "09:00", "x"]) + self.assertEqual(code, 1) + self.assertIn("not allowed", err) + self.assertFalse(self.day_file().exists()) + + def test_missing_text(self): + code, _, err = self.run_cmd(commands.note, []) + self.assertEqual(code, 1) + self.assertIn("description is missing", err) + + +class StorageTest(CommandTestCase): + def test_appends_and_creates_dir(self): + storage.append_entry(Entry(NOW, "add", "one")) + storage.append_entry(Entry(NOW, "note", "two")) + self.assertEqual(self.lines(), ["2026-09-23 12:00\tadd\tone", "2026-09-23 12:00\tnote\ttwo"]) + + def test_uses_lf_line_endings(self): + storage.append_entry(Entry(NOW, "add", "one")) + self.assertNotIn(b"\r", self.day_file().read_bytes()) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_model.py b/tests/test_model.py new file mode 100644 index 0000000..6e7d0b1 --- /dev/null +++ b/tests/test_model.py @@ -0,0 +1,43 @@ +import unittest +from datetime import datetime + +from timelog.model import Entry, clean_text + + +class CleanTextTest(unittest.TestCase): + def test_collapses_whitespace_and_newlines(self): + self.assertEqual(clean_text(" a \t b\n\nc "), "a b c") + + +class EntryTest(unittest.TestCase): + def test_unknown_type_rejected(self): + with self.assertRaises(ValueError): + Entry(datetime(2026, 9, 23, 9, 15), "bogus", "x") + + def test_text_is_normalized(self): + e = Entry(datetime(2026, 9, 23, 9, 15), "add", "a\tb\nc") + self.assertEqual(e.text, "a b c") + + def test_to_line(self): + e = Entry(datetime(2026, 9, 23, 9, 15), "add", "Team daily") + self.assertEqual(e.to_line(), "2026-09-23 09:15\tadd\tTeam daily") + + def test_round_trip(self): + e = Entry(datetime(2026, 9, 23, 9, 15), "note", "PR created") + self.assertEqual(Entry.from_line(e.to_line() + "\n"), e) + + def test_from_line_wrong_field_count(self): + with self.assertRaises(ValueError): + Entry.from_line("2026-09-23 09:15\tadd") + + def test_from_line_bad_time(self): + with self.assertRaises(ValueError): + Entry.from_line("yesterday\tadd\tx") + + def test_from_line_unknown_type(self): + with self.assertRaises(ValueError): + Entry.from_line("2026-09-23 09:15\tstop\tx") + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_render.py b/tests/test_render.py new file mode 100644 index 0000000..8e82dd3 --- /dev/null +++ b/tests/test_render.py @@ -0,0 +1,33 @@ +import unittest +from datetime import datetime + +from timelog import render +from timelog.model import Entry + + +def _entry(kind="add"): + return Entry(datetime(2026, 9, 23, 9, 5), kind, "Team daily") + + +class RenderTest(unittest.TestCase): + def test_format_duration(self): + self.assertEqual(render.format_duration(0), "0m") + self.assertEqual(render.format_duration(45), "45m") + self.assertEqual(render.format_duration(60), "1h 00m") + self.assertEqual(render.format_duration(65), "1h 05m") + + def test_action_messages(self): + self.assertEqual(render.time_started(_entry()), "Started: Team daily (09:05)") + self.assertEqual(render.time_added(_entry()), "Added: Team daily (09:05)") + self.assertEqual(render.note_added(_entry("note")), "Note added: Team daily (09:05)") + + def test_errors(self): + self.assertEqual( + render.missing_argument_error("tl add", "description", "tl add "), + "tl add: description is missing. Usage: tl add ", + ) + self.assertEqual(render.flag_not_allowed("tl note", "-t"), "tl note: flag '-t' is not allowed.") + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_tl.py b/tests/test_tl.py new file mode 100644 index 0000000..14268e2 --- /dev/null +++ b/tests/test_tl.py @@ -0,0 +1,42 @@ +import contextlib +import io +import unittest + +import tl +from timelog import __version__ + + +def run_main(argv): + out, err = io.StringIO(), io.StringIO() + with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): + code = tl.main(argv) + return code, out.getvalue(), err.getvalue() + + +class MainTest(unittest.TestCase): + def test_help(self): + for argv in ([], ["help"], ["-h"], ["--help"]): + with self.subTest(argv=argv): + code, out, _ = run_main(argv) + self.assertEqual(code, 0) + self.assertIn("Usage: tl ", out) + + def test_version(self): + self.assertEqual(run_main(["version"]), (0, "timelog-cli " + __version__ + "\n", "")) + + def test_unknown_command(self): + code, out, err = run_main(["frobnicate"]) + self.assertEqual((code, out), (1, "")) + self.assertIn("unknown command 'frobnicate'", err) + + def test_planned_command(self): + code, _, err = run_main(["list"]) + self.assertEqual(code, 1) + self.assertIn("not implemented yet", err) + + def test_planned_commands_are_not_registered(self): + self.assertFalse(tl.PLANNED & set(tl.COMMANDS)) + + +if __name__ == "__main__": + unittest.main() From 363beedda890b6f9b86093787036278c4cd59f57 Mon Sep 17 00:00:00 2001 From: markspl <14055798+markspl@users.noreply.github.com> Date: Tue, 13 Oct 2026 23:57:58 +0300 Subject: [PATCH 2/7] docs: use feat/ prefix for work branches --- RELEASING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RELEASING.md b/RELEASING.md index e8f7457..b5d35f9 100644 --- a/RELEASING.md +++ b/RELEASING.md @@ -12,7 +12,7 @@ The release notes are written once and used in two places: `CHANGELOG.md` and th - A release needs a tag, not a branch. Tags are made on `main`. - Release branches (`release/0.1`) are only for patching an old version. -- Keep `main` always working using work branches (`feature/...`, `fix/...`). +- Keep `main` always working using work branches (`feat/...`, `fix/...`). ## Version From f62534f86faeaff0fd34689fe4c9620052a901ce Mon Sep 17 00:00:00 2001 From: markspl <14055798+markspl@users.noreply.github.com> Date: Wed, 14 Oct 2026 00:08:50 +0300 Subject: [PATCH 3/7] fix: also redirect HOME and USERPROFILE in command tests --- tests/test_commands.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_commands.py b/tests/test_commands.py index 732f305..bca93a4 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1,5 +1,6 @@ import contextlib import io +import os import tempfile import unittest from datetime import datetime @@ -20,6 +21,7 @@ def setUp(self): self.addCleanup(tmp.cleanup) self.home = Path(tmp.name) for patcher in ( + mock.patch.dict(os.environ, {"HOME": str(self.home), "USERPROFILE": str(self.home)}), mock.patch.object(Path, "home", return_value=self.home), mock.patch.object(commands, "_now", return_value=NOW), ): From 5b866f14fdc15298fd70f2dbbbcbd162047c5374 Mon Sep 17 00:00:00 2001 From: markspl <14055798+markspl@users.noreply.github.com> Date: Wed, 14 Oct 2026 00:16:14 +0300 Subject: [PATCH 4/7] feat: run tests on pull requests --- .github/workflows/test.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..ca2ad54 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,11 @@ +name: tests +on: pull_request +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-python@v7 + with: + python-version: "3.8" + - run: python -m unittest discover -s tests From f77c26a87b2e269f4f1a2009679470c67ab41158 Mon Sep 17 00:00:00 2001 From: markspl <14055798+markspl@users.noreply.github.com> Date: Sun, 8 Nov 2026 20:01:13 +0200 Subject: [PATCH 5/7] fix: pin ubuntu-22.04 for Python 3.8 and run tests on push to main --- .github/workflows/test.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ca2ad54..14ab5a5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,8 +1,11 @@ name: tests -on: pull_request +on: + pull_request: + push: + branches: [main] jobs: test: - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v7 - uses: actions/setup-python@v7 From b6224683c639eef4285b7c2f98d0c897f0132b56 Mon Sep 17 00:00:00 2001 From: markspl <14055798+markspl@users.noreply.github.com> Date: Sun, 8 Nov 2026 20:01:13 +0200 Subject: [PATCH 6/7] refactor: make tests independent of planned features --- tests/test_commands.py | 22 +++++----------------- tests/test_storage.py | 20 ++++++++++++++++++++ tests/test_tl.py | 5 ++++- 3 files changed, 29 insertions(+), 18 deletions(-) create mode 100644 tests/test_storage.py diff --git a/tests/test_commands.py b/tests/test_commands.py index bca93a4..71ad6fe 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -7,8 +7,7 @@ from pathlib import Path from unittest import mock -from timelog import commands, storage -from timelog.model import Entry +from timelog import commands NOW = datetime(2026, 9, 23, 12, 0) @@ -22,7 +21,6 @@ def setUp(self): self.home = Path(tmp.name) for patcher in ( mock.patch.dict(os.environ, {"HOME": str(self.home), "USERPROFILE": str(self.home)}), - mock.patch.object(Path, "home", return_value=self.home), mock.patch.object(commands, "_now", return_value=NOW), ): patcher.start() @@ -137,9 +135,10 @@ def test_note(self): def test_time_flag_rejected(self): for flag in ("-t", "--time"): - code, _, err = self.run_cmd(commands.note, [flag, "09:00", "x"]) - self.assertEqual(code, 1) - self.assertIn("not allowed", err) + with self.subTest(flag=flag): + code, _, err = self.run_cmd(commands.note, [flag, "09:00", "x"]) + self.assertEqual(code, 1) + self.assertIn("not allowed", err) self.assertFalse(self.day_file().exists()) def test_missing_text(self): @@ -148,16 +147,5 @@ def test_missing_text(self): self.assertIn("description is missing", err) -class StorageTest(CommandTestCase): - def test_appends_and_creates_dir(self): - storage.append_entry(Entry(NOW, "add", "one")) - storage.append_entry(Entry(NOW, "note", "two")) - self.assertEqual(self.lines(), ["2026-09-23 12:00\tadd\tone", "2026-09-23 12:00\tnote\ttwo"]) - - def test_uses_lf_line_endings(self): - storage.append_entry(Entry(NOW, "add", "one")) - self.assertNotIn(b"\r", self.day_file().read_bytes()) - - if __name__ == "__main__": unittest.main() diff --git a/tests/test_storage.py b/tests/test_storage.py new file mode 100644 index 0000000..cf72976 --- /dev/null +++ b/tests/test_storage.py @@ -0,0 +1,20 @@ +import unittest + +from tests.test_commands import NOW, CommandTestCase +from timelog import storage +from timelog.model import Entry + + +class StorageTest(CommandTestCase): + def test_appends_and_creates_dir(self): + storage.append_entry(Entry(NOW, "add", "one")) + storage.append_entry(Entry(NOW, "note", "two")) + self.assertEqual(self.lines(), ["2026-09-23 12:00\tadd\tone", "2026-09-23 12:00\tnote\ttwo"]) + + def test_uses_lf_line_endings(self): + storage.append_entry(Entry(NOW, "add", "one")) + self.assertNotIn(b"\r", self.day_file().read_bytes()) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_tl.py b/tests/test_tl.py index 14268e2..de5e542 100644 --- a/tests/test_tl.py +++ b/tests/test_tl.py @@ -30,7 +30,10 @@ def test_unknown_command(self): self.assertIn("unknown command 'frobnicate'", err) def test_planned_command(self): - code, _, err = run_main(["list"]) + if not tl.PLANNED: + self.skipTest("no planned commands left") + name = sorted(tl.PLANNED)[0] + code, _, err = run_main([name]) self.assertEqual(code, 1) self.assertIn("not implemented yet", err) From 1726134658f44ab125c73c9ebcae77a86d189ac8 Mon Sep 17 00:00:00 2001 From: markspl <14055798+markspl@users.noreply.github.com> Date: Sun, 8 Nov 2026 20:01:13 +0200 Subject: [PATCH 7/7] refactor: remove main blocks from tests, test future clock time --- tests/test_commands.py | 8 ++++---- tests/test_model.py | 4 ---- tests/test_render.py | 4 ---- tests/test_storage.py | 4 ---- tests/test_tl.py | 4 ---- 5 files changed, 4 insertions(+), 20 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index 71ad6fe..145fc75 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -43,6 +43,10 @@ class ParseTimeFlagTest(unittest.TestCase): def test_clock_time(self): self.assertEqual(commands._parse_time_flag("09:30", NOW), datetime(2026, 9, 23, 9, 30)) + def test_clock_time_later_today_is_allowed(self): + # logging what will happen later today is intended + self.assertEqual(commands._parse_time_flag("23:30", NOW), datetime(2026, 9, 23, 23, 30)) + def test_durations(self): parse = commands._parse_time_flag self.assertEqual(parse("1h30m", NOW), datetime(2026, 9, 23, 10, 30)) @@ -145,7 +149,3 @@ def test_missing_text(self): code, _, err = self.run_cmd(commands.note, []) self.assertEqual(code, 1) self.assertIn("description is missing", err) - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/test_model.py b/tests/test_model.py index 6e7d0b1..d95ec46 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -37,7 +37,3 @@ def test_from_line_bad_time(self): def test_from_line_unknown_type(self): with self.assertRaises(ValueError): Entry.from_line("2026-09-23 09:15\tstop\tx") - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/test_render.py b/tests/test_render.py index 8e82dd3..6e06fc2 100644 --- a/tests/test_render.py +++ b/tests/test_render.py @@ -27,7 +27,3 @@ def test_errors(self): "tl add: description is missing. Usage: tl add ", ) self.assertEqual(render.flag_not_allowed("tl note", "-t"), "tl note: flag '-t' is not allowed.") - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/test_storage.py b/tests/test_storage.py index cf72976..9830de0 100644 --- a/tests/test_storage.py +++ b/tests/test_storage.py @@ -14,7 +14,3 @@ def test_appends_and_creates_dir(self): def test_uses_lf_line_endings(self): storage.append_entry(Entry(NOW, "add", "one")) self.assertNotIn(b"\r", self.day_file().read_bytes()) - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/test_tl.py b/tests/test_tl.py index de5e542..007c5a9 100644 --- a/tests/test_tl.py +++ b/tests/test_tl.py @@ -39,7 +39,3 @@ def test_planned_command(self): def test_planned_commands_are_not_registered(self): self.assertFalse(tl.PLANNED & set(tl.COMMANDS)) - - -if __name__ == "__main__": - unittest.main()