From e0855bc51a0c8b4936a40d7daa106a3e65e98e21 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 31 Jul 2025 17:16:31 -0700 Subject: [PATCH 1/3] init unittest github action --- .github/workflows/tests.yml | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 .github/workflows/tests.yml diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..118eaf0 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,39 @@ +name: Tests + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +permissions: + contents: read + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Set up Python 3.10 + uses: actions/setup-python@v3 + with: + python-version: "3.10" + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install flake8 pytest pytest-asyncio mypy + pip install .[dev] + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 devlog --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 devlog --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + - name: Check typing with mypy + run: | + mypy devlog + - name: Test with pytest + run: | + pytest -v \ No newline at end of file From ba03162f72b88c7c54a6a6d2ac34e80276df0564 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 31 Jul 2025 17:16:51 -0700 Subject: [PATCH 2/3] init cli file unittest --- tests/test_cli.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tests/test_cli.py diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..768a8ae --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,44 @@ +import pytest + +from typer.testing import CliRunner +from devlog.cli import app +import devlog.cli as cli + +runner = CliRunner() + + +@pytest.fixture(autouse=True) +def clean_env(tmp_path, monkeypatch): + sessions_dir = tmp_path / "sessions" + monkeypatch.setattr("devlog.cli.DATA_DIR", sessions_dir) + monkeypatch.setattr("devlog.cli.CURRENT", tmp_path / "current.json") + sessions_dir.mkdir(parents=True) + + yield + + for file in tmp_path.iterdir(): + if file.is_file(): + file.unlink() + + +def test_start_create_session(): + result = runner.invoke(app, ["start"]) + assert result.exit_code == 0 + assert "✅ Session started" in result.stdout + assert cli.CURRENT.exists() + + +def test_start_already_active_session(): + cli.CURRENT.write_text("hello world") + result = runner.invoke(app, ["start"]) + assert "[DEVLOG] session already in progress." in result.stdout + + +def test_note_no_session(): + result = runner.invoke(app, ["note", "Hello Test"]) + assert "[DEVLOG] No current session active." in result.stdout + + +def test_stop_no_session(): + result = runner.invoke(app, ["stop"]) + assert "[DEVLOG] No current session active." in result.stdout From fafecf7bad43efd4856acb231b06b78307af74d0 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 31 Jul 2025 18:08:53 -0700 Subject: [PATCH 3/3] added more test for cli.py test for: - note - stop - export md - export html --- tests/test_cli.py | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 768a8ae..f4ab057 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,8 +1,7 @@ import pytest +import devlog.cli as cli from typer.testing import CliRunner -from devlog.cli import app -import devlog.cli as cli runner = CliRunner() @@ -10,9 +9,13 @@ @pytest.fixture(autouse=True) def clean_env(tmp_path, monkeypatch): sessions_dir = tmp_path / "sessions" + + monkeypatch.setattr("pathlib.Path.home", lambda: tmp_path) + monkeypatch.setattr("devlog.cli.DATA_DIR", sessions_dir) monkeypatch.setattr("devlog.cli.CURRENT", tmp_path / "current.json") - sessions_dir.mkdir(parents=True) + + sessions_dir.mkdir(parents=True, exist_ok=True) yield @@ -22,7 +25,7 @@ def clean_env(tmp_path, monkeypatch): def test_start_create_session(): - result = runner.invoke(app, ["start"]) + result = runner.invoke(cli.app, ["start"]) assert result.exit_code == 0 assert "✅ Session started" in result.stdout assert cli.CURRENT.exists() @@ -30,15 +33,45 @@ def test_start_create_session(): def test_start_already_active_session(): cli.CURRENT.write_text("hello world") - result = runner.invoke(app, ["start"]) + result = runner.invoke(cli.app, ["start"]) assert "[DEVLOG] session already in progress." in result.stdout +def test_note(): + runner.invoke(cli.app, ["start"]) + result = runner.invoke(cli.app, ["note", "Hello Test"]) + assert "[LOG]📝 Note recorded." in result.stdout + + def test_note_no_session(): - result = runner.invoke(app, ["note", "Hello Test"]) + result = runner.invoke(cli.app, ["note", "Hello Test"]) assert "[DEVLOG] No current session active." in result.stdout +def test_stop(): + runner.invoke(cli.app, ["start"]) + result = runner.invoke(cli.app, ["stop"]) + assert "[DEVLOG] ✅ Session ended." in result.stdout + + def test_stop_no_session(): - result = runner.invoke(app, ["stop"]) + result = runner.invoke(cli.app, ["stop"]) assert "[DEVLOG] No current session active." in result.stdout + + +def test_export_markdown(): + result = runner.invoke(cli.app, ["export", "md"]) + assert "[LOG] 🚀 Logs exported" in result.stdout + + +def test_export_html(): + result = runner.invoke(cli.app, ["export", "html"]) + assert "[LOG] ✅ Data moved to HTML" in result.stdout + + +def test_export_fail(): + cli.CURRENT.write_text("hello world") + result = runner.invoke(cli.app, ["export", "md"]) + + assert "[DEVLOG] Session active. Stop it before exporting." \ + in result.stdout