diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..14ab5a5 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,14 @@ +name: tests +on: + pull_request: + push: + branches: [main] +jobs: + test: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-python@v7 + with: + python-version: "3.8" + - run: python -m unittest discover -s tests 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/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 diff --git a/tests/test_commands.py b/tests/test_commands.py new file mode 100644 index 0000000..145fc75 --- /dev/null +++ b/tests/test_commands.py @@ -0,0 +1,151 @@ +import contextlib +import io +import os +import tempfile +import unittest +from datetime import datetime +from pathlib import Path +from unittest import mock + +from timelog import commands + +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.dict(os.environ, {"HOME": str(self.home), "USERPROFILE": str(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_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)) + 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"): + 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): + code, _, err = self.run_cmd(commands.note, []) + self.assertEqual(code, 1) + self.assertIn("description is missing", err) diff --git a/tests/test_model.py b/tests/test_model.py new file mode 100644 index 0000000..d95ec46 --- /dev/null +++ b/tests/test_model.py @@ -0,0 +1,39 @@ +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") diff --git a/tests/test_render.py b/tests/test_render.py new file mode 100644 index 0000000..6e06fc2 --- /dev/null +++ b/tests/test_render.py @@ -0,0 +1,29 @@ +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.") diff --git a/tests/test_storage.py b/tests/test_storage.py new file mode 100644 index 0000000..9830de0 --- /dev/null +++ b/tests/test_storage.py @@ -0,0 +1,16 @@ +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()) diff --git a/tests/test_tl.py b/tests/test_tl.py new file mode 100644 index 0000000..007c5a9 --- /dev/null +++ b/tests/test_tl.py @@ -0,0 +1,41 @@ +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): + 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) + + def test_planned_commands_are_not_registered(self): + self.assertFalse(tl.PLANNED & set(tl.COMMANDS))