Coverage for tests/unit_tests/managers/test_profile.py: 100%
27 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-23 03:53 +0000
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-23 03:53 +0000
1"""
2Tests of profile.py
3"""
5import json
6from pytest import raises
7from tests.conftest import ApioRunner
8from apio.managers.profile import (
9 Profile,
10)
12TEST_DATA = {
13 "preferences": {"theme": "light"},
14}
17def test_profile_loading(apio_runner: ApioRunner):
18 """Tests the loading and validation of a profile file."""
20 with apio_runner.in_sandbox() as sb:
22 # -- Write a test profile.json file.
23 path = sb.home_dir / "profile.json"
24 # test_data = get_test_data(apio_ctx, util.get_apio_version_str(), 0)
25 sb.write_file(
26 path,
27 json.dumps(
28 TEST_DATA,
29 indent=2,
30 ),
31 exists_ok=True,
32 )
34 # -- Read back the content.
35 profile = Profile(sb.home_dir)
37 # -- Verify
38 assert profile.preferences == TEST_DATA["preferences"]
41def test_profile_with_corrupt_profile_file(apio_runner: ApioRunner):
42 """Tests that a corrupt profile.json results in a clean error message
43 instead of an unhandled JSONDecodeError on every command."""
45 bad_contents = [
46 "{ corrupt json", # -- Not a valid json.
47 "[1, 2, 3]", # -- Not a json dict.
48 '{"preferences": "corrupt"}', # -- Field is not a dict.
49 ]
51 with apio_runner.in_sandbox() as sb:
53 for bad_content in bad_contents:
55 # -- Write a corrupt profile.json file.
56 sb.write_file(
57 sb.home_dir / "profile.json", bad_content, exists_ok=True
58 )
60 # -- The theme reader should quietly fall back to the default.
61 assert Profile.read_preferences_theme(default="dark") == "dark"
63 # -- Loading the profile should exit with a clean error message.
64 with apio_runner.with_logger() as log:
65 with raises(SystemExit) as e:
66 Profile(sb.home_dir)
67 assert e.value.code == 1, bad_content
68 assert "Invalid profile file" in log.out
71def test_profile_with_unknown_theme(apio_runner: ApioRunner):
72 """Tests that an unknown theme name in profile.json falls back to the
73 default theme instead of crashing every command with an
74 AssertionError."""
76 with apio_runner.in_sandbox() as sb:
77 for bogus_theme in ["no-such-theme", ["dark"], 5, None]:
79 # -- Write a profile.json file with a bogus theme value.
80 sb.write_file(
81 sb.home_dir / "profile.json",
82 json.dumps({"preferences": {"theme": bogus_theme}}),
83 exists_ok=True,
84 )
86 # -- The theme reader should quietly fall back to the default.
87 assert Profile.read_preferences_theme(default="light") == "light"