Coverage for tests/unit_tests/managers/test_profile.py: 100%
27 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 05:08 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 05:08 +0000
1# This file is part of the Apio project
2# (C) 2016-present FPGAwars
3# Project authors:
4# * Jesús Arroyo (Jesus89) (2016-2019)
5# * Juan Gonzalez (obijuan) (2019-2024)
6# * The Apio Team (2024-present)
7# License: GPLv2
9"""
10Tests of profile.py
11"""
13import json
14from pytest import raises
15from tests.conftest import ApioRunner
16from apio.managers.profile import (
17 Profile,
18)
20TEST_DATA = {
21 "preferences": {"theme": "light"},
22}
25def test_profile_loading(apio_runner: ApioRunner):
26 """Tests the loading and validation of a profile file."""
28 with apio_runner.in_sandbox() as sb:
30 # -- Write a test profile.json file.
31 path = sb.home_dir / "profile.json"
32 # test_data = get_test_data(apio_ctx, util.get_apio_version_str(), 0)
33 sb.write_file(
34 path,
35 json.dumps(
36 TEST_DATA,
37 indent=2,
38 ),
39 exists_ok=True,
40 )
42 # -- Read back the content.
43 profile = Profile(sb.home_dir)
45 # -- Verify
46 assert profile.preferences == TEST_DATA["preferences"]
49def test_profile_with_corrupt_profile_file(apio_runner: ApioRunner):
50 """Tests that a corrupt profile.json results in a clean error message
51 instead of an unhandled JSONDecodeError on every command."""
53 bad_contents = [
54 "{ corrupt json", # -- Not a valid json.
55 "[1, 2, 3]", # -- Not a json dict.
56 '{"preferences": "corrupt"}', # -- Field is not a dict.
57 ]
59 with apio_runner.in_sandbox() as sb:
61 for bad_content in bad_contents:
63 # -- Write a corrupt profile.json file.
64 sb.write_file(
65 sb.home_dir / "profile.json", bad_content, exists_ok=True
66 )
68 # -- The theme reader should quietly fall back to the default.
69 assert Profile.read_preferences_theme(default="dark") == "dark"
71 # -- Loading the profile should exit with a clean error message.
72 with apio_runner.with_logger() as log:
73 with raises(SystemExit) as e:
74 Profile(sb.home_dir)
75 assert e.value.code == 1, bad_content
76 assert "Invalid profile file" in log.out
79def test_profile_with_unknown_theme(apio_runner: ApioRunner):
80 """Tests that an unknown theme name in profile.json falls back to the
81 default theme instead of crashing every command with an
82 AssertionError."""
84 with apio_runner.in_sandbox() as sb:
85 for bogus_theme in ["no-such-theme", ["dark"], 5, None]:
87 # -- Write a profile.json file with a bogus theme value.
88 sb.write_file(
89 sb.home_dir / "profile.json",
90 json.dumps({"preferences": {"theme": bogus_theme}}),
91 exists_ok=True,
92 )
94 # -- The theme reader should quietly fall back to the default.
95 assert Profile.read_preferences_theme(default="light") == "light"