Repository: commit-check/commit-check (language: python)

Issue:
add unit test for error handler
              need to add error handler unit test

_Originally posted by @shenxianpeng in https://github.com/commit-check/commit-check/issues/60#issuecomment-1407377689_
            
add unit test for error handler
              need to add error handler unit test

_Originally posted by @shenxianpeng in https://github.com/commit-check/commit-check/issues/60#issuecomment-1407377689_
            


Discussion / hints:

Fix patch:
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 8b0388d..dc85280 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -47,7 +47,7 @@ jobs:
 
       - name: Collect Coverage
         run: |
-          coverage run --source . -m pytest
+          coverage run --source commit_check -m pytest
           coverage report && coverage xml
       - uses: codecov/codecov-action@v3
         with:


Test patch:
diff --git a/tests/error_test.py b/tests/error_test.py
new file mode 100644
index 0000000..188a2b1
--- /dev/null
+++ b/tests/error_test.py
@@ -0,0 +1,46 @@
+import os
+import pytest
+from commit_check.error import error_handler, log_and_exit
+
+
+def test_error_handler_RuntimeError():
+    with pytest.raises(SystemExit) as exit_info:
+        with error_handler():
+            raise RuntimeError("Test error")
+    assert exit_info.value.code == 1
+
+
+def test_error_handler_KeyboardInterrupt():
+    with pytest.raises(SystemExit) as exit_info:
+        with error_handler():
+            raise KeyboardInterrupt
+    assert exit_info.value.code == 130
+
+
+def test_error_handler_unexpected_error():
+    with pytest.raises(SystemExit) as exit_info:
+        with error_handler():
+            raise Exception("Test error")
+    assert exit_info.value.code == 3
+
+
+@pytest.mark.xfail
+def test_log_and_exit(monkeypatch):
+    monkeypatch.setenv("COMMIT_CHECK_HOME", "")
+    monkeypatch.setenv("XDG_CACHE_HOME", "")
+
+    error_msg = "Test error message"
+    ret_code = 123
+    exc = Exception("Test error")
+    formatted = "Test formatted traceback"
+
+    log_and_exit(error_msg, ret_code, exc, formatted)
+
+    log_path = os.path.expanduser("~/.cache/commit-check/commit-check.log")
+    with open(log_path) as file:
+        log_content = file.read()
+
+    assert error_msg in log_content
+    assert str(ret_code) in log_content
+    assert str(exc) in log_content
+    assert formatted in log_content