File: conftest.py

package info (click to toggle)
khal 1%3A0.13.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,664 kB
  • sloc: python: 13,297; makefile: 191; awk: 33; sh: 1
file content (153 lines) | stat: -rw-r--r-- 4,774 bytes parent folder | download | duplicates (2)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import logging
import os
from time import sleep

import pytest

from khal.custom_types import CalendarConfiguration
from khal.khalendar import CalendarCollection
from khal.khalendar.vdir import Vdir

from .utils import LOCALE_BERLIN, CollVdirType, cal1, example_cals


@pytest.fixture
def metavdirs(tmpdir):
    tmpdir = str(tmpdir)
    dirstructure = [
        '/cal1/public/',
        '/cal1/private/',
        '/cal2/public/',
        '/cal3/public/',
        '/cal3/work/',
        '/cal3/home/',
        '/cal4/cfgcolor/',
        '/cal4/dircolor/',
        '/cal4/cfgcolor_again/',
        '/cal4/cfgcolor_once_more/',
        '/singlecollection/',
    ]
    for one in dirstructure:
        os.makedirs(tmpdir + one)
    filestructure = [
        ('/cal1/public/displayname', 'my calendar'),
        ('/cal1/public/color', 'dark blue'),
        ('/cal1/private/displayname', 'my private calendar'),
        ('/cal1/private/color', '#FF00FF'),
        ('/cal4/dircolor/color', 'dark blue'),
    ]
    for filename, content in filestructure:
        with open(tmpdir + filename, 'w') as metafile:
            metafile.write(content)
    return tmpdir


@pytest.fixture
def coll_vdirs(tmpdir) -> CollVdirType:
    calendars, vdirs = {}, {}
    for name in example_cals:
        path = str(tmpdir) + '/' + name
        os.makedirs(path, mode=0o770)
        readonly = True if name == 'a_calendar' else False
        calendars[name] = CalendarConfiguration(
            name=name,
            path=path,
            readonly=readonly,
            color='dark blue',
            priority=10,
            ctype='calendar',
            addresses='user@example.com',
        )
        vdirs[name] = Vdir(path, '.ics')
    coll = CalendarCollection(calendars=calendars, dbpath=':memory:', locale=LOCALE_BERLIN)
    coll.default_calendar_name = cal1
    return coll, vdirs


@pytest.fixture
def coll_vdirs_birthday(tmpdir):
    calendars, vdirs = {}, {}
    for name in example_cals:
        path = str(tmpdir) + '/' + name
        os.makedirs(path, mode=0o770)
        readonly = True if name == 'a_calendar' else False
        calendars[name] = {'name': name, 'path': path, 'color': 'dark blue',
                           'readonly': readonly, 'unicode_symbols': True, 'ctype': 'birthdays',
                           'addresses': 'user@example.com'}
        vdirs[name] = Vdir(path, '.vcf')
    coll = CalendarCollection(calendars=calendars, dbpath=':memory:', locale=LOCALE_BERLIN)
    coll.default_calendar_name = cal1
    return coll, vdirs


@pytest.fixture(autouse=True)
def never_echo_bytes(monkeypatch):
    '''Click's echo function will not strip colorcodes if we call `click.echo`
    with a bytestring message. The reason for this that bytestrings may contain
    arbitrary binary data (such as images).

    Khal is not concerned with such data at all, but may contain a few
    instances where it explicitly encodes its output into the configured
    locale. This in turn would break the functionality of the global
    `--color/--no-color` flag.
    '''
    from click import echo as old_echo

    def echo(msg=None, *a, **kw):
        assert not isinstance(msg, bytes)
        return old_echo(msg, *a, **kw)

    monkeypatch.setattr('click.echo', echo)

    class Result:
        @staticmethod
        def undo():
            monkeypatch.setattr('click.echo', old_echo)

    return Result


@pytest.fixture(scope='session')
def sleep_time(tmpdir_factory):
    """
    Returns the filesystem's mtime precision

    Returns how long we need to sleep for the filesystem's mtime precision to
    pick up differences.

    This keeps test fast on systems with high precisions, but makes them pass
    on those that don't.
    """
    tmpfile = tmpdir_factory.mktemp('sleep').join('touch_me')

    def touch_and_mtime():
        tmpfile.open('w').close()
        stat = os.stat(str(tmpfile))
        return getattr(stat, 'st_mtime_ns', stat.st_mtime)

    i = 0.00001
    while i < 100:
        # Measure three times to avoid things like 12::18:11.9994 [mis]passing
        first = touch_and_mtime()
        sleep(i)
        second = touch_and_mtime()
        sleep(i)
        third = touch_and_mtime()

        if first != second != third:
            return i * 1.1
        i = i * 10

    # This should never happen, but oh, well:
    raise Exception(
        'Filesystem does not seem to save modified times of files. \n'
        'Cannot run tests that depend on this.'
    )


@pytest.fixture
def fix_caplog(monkeypatch):
    """Temporarily undoes the logging setup by click-log such that the caplog fixture can be used"""
    logger = logging.getLogger('khal')
    monkeypatch.setattr(logger, 'handlers', [])
    monkeypatch.setattr(logger, 'propagate', True)