File: test_config.py

package info (click to toggle)
python-sphinx-chango 0.6.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,840 kB
  • sloc: python: 4,909; javascript: 74; makefile: 23
file content (40 lines) | stat: -rw-r--r-- 1,063 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#  SPDX-FileCopyrightText: 2024-present Hinrich Mahler <chango@mahlerhome.de>
#
#  SPDX-License-Identifier: MIT
import os
from contextlib import contextmanager
from pathlib import Path
from types import SimpleNamespace

from chango.config import ChanGoConfig, get_chango_instance


@contextmanager
def temporary_chdir(path: Path):
    current_dir = Path.cwd()
    try:
        os.chdir(path)
        yield
    finally:
        os.chdir(current_dir)


class TestConfigModule:
    def test_get_chango_instance_caching(self, monkeypatch):
        call_count = 0

        def import_chango_instance():
            nonlocal call_count
            call_count += 1
            return call_count

        def load(*_, **__):
            return SimpleNamespace(import_chango_instance=import_chango_instance)

        monkeypatch.setattr(ChanGoConfig, "load", load)

        for _ in range(10):
            assert get_chango_instance() == 1, "The ChanGo instance should be cached!"

        # to ensure that other tests still pass
        get_chango_instance.cache_clear()