File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1111import json
1212import os
1313import sys
14+ import logging
1415from datetime import datetime
1516
1617# Reportlab is required for PDF generation. If it's missing, emit a clear
@@ -38,8 +39,13 @@ def _load_profile(base_dir):
3839 try :
3940 with open (path , "r" , encoding = "utf-8" ) as fh :
4041 return json .load (fh )
41- except Exception :
42+ except FileNotFoundError :
4243 return {}
44+ except json .JSONDecodeError as exc :
45+ logging .error ("Failed to parse %s: %s" , path , exc )
46+ except OSError as exc :
47+ logging .error ("Unable to read %s: %s" , path , exc )
48+ return {}
4349
4450
4551def _profile_sections (profile ):
Original file line number Diff line number Diff line change @@ -11,3 +11,4 @@ PYTHONPATH=src python test/unit/billing_summary.test.py
1111PYTHONPATH=src python test/unit/invoice_retrieval.test.py
1212PYTHONPATH=src python test/unit/auth_boundaries.test.py
1313PYTHONPATH=src python test/unit/accounts_listing.test.py
14+ PYTHONPATH=src python test/unit/profile_loading.test.py
Original file line number Diff line number Diff line change 1+ import os
2+ import tempfile
3+ import unittest
4+ from unittest import mock
5+ import logging
6+
7+ from invoice import _load_profile
8+
9+
10+ class LoadProfileTests (unittest .TestCase ):
11+ def test_missing_file_returns_empty_profile (self ):
12+ with tempfile .TemporaryDirectory () as td :
13+ self .assertEqual (_load_profile (td ), {})
14+
15+ def test_invalid_json_logs_error (self ):
16+ with tempfile .TemporaryDirectory () as td :
17+ path = os .path .join (td , "institution.json" )
18+ with open (path , "w" , encoding = "utf-8" ) as fh :
19+ fh .write ("{invalid" )
20+ with self .assertLogs (level = "ERROR" ) as cm :
21+ self .assertEqual (_load_profile (td ), {})
22+ self .assertIn ("Failed to parse" , cm .output [0 ])
23+
24+ def test_os_error_logs_error (self ):
25+ with tempfile .TemporaryDirectory () as td :
26+ with mock .patch ("builtins.open" , side_effect = PermissionError ("denied" )):
27+ with self .assertLogs (level = "ERROR" ) as cm :
28+ self .assertEqual (_load_profile (td ), {})
29+ self .assertIn ("Unable to read" , cm .output [0 ])
30+
31+
32+ if __name__ == "__main__" :
33+ unittest .main ()
You can’t perform that action at this time.
0 commit comments