File: conftest.py

package info (click to toggle)
python-changelogd 0.1.9-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 448 kB
  • sloc: python: 1,921; makefile: 21
file content (76 lines) | stat: -rw-r--r-- 1,786 bytes parent folder | download | duplicates (3)
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
import datetime
import getpass
import os
import typing
from pathlib import Path

import pytest
from click.testing import CliRunner

from changelogd import config

old_invoke = CliRunner.invoke


def invoke(*args, **kwargs):
    result = old_invoke(*args, **kwargs)
    config.Config.settings = {}
    return result


CliRunner.invoke = invoke


@pytest.fixture
def setup_env(fake_process, monkeypatch, tmpdir, fake_date):
    fake_process.allow_unregistered(True)
    fake_process.keep_last_process(True)
    fake_process.register_subprocess(
        ["git", "config", "--list"],
        stdout=("user.name=Some User\n" "user.email=user@example.com\n"),
    )
    monkeypatch.setattr(getpass, "getuser", lambda: "test-user")
    monkeypatch.setattr(config, "DEFAULT_PATH", Path(tmpdir) / "changelog.d")
    monkeypatch.chdir(tmpdir)
    monkeypatch.setattr(datetime, "date", fake_date)
    monkeypatch.setattr(os.path, "getmtime", lambda _: fake_date.EPOCH_02_02_2020)
    fake_date.set_date(datetime.date(2020, 2, 2))
    yield tmpdir


class FakeDate(datetime.date):
    EPOCH_02_02_2020 = 1580608922
    EPOCH_03_02_2020 = 1580695322
    _date = None

    @classmethod
    def today(cls) -> datetime.date:
        if not cls._date:
            return super().today()
        return cls._date

    @classmethod
    def set_date(cls, date: datetime.date) -> None:
        cls._date = date


class FakeNow:
    def __init__(self, timestamp):
        self._timestamp = timestamp

    def timestamp(self):
        return self._timestamp


class FakeDateTime(datetime.datetime):
    timestamp = 0

    @classmethod
    def now(cls, tz=None):
        cls.timestamp += 1
        return FakeNow(cls.timestamp)


@pytest.fixture()
def fake_date() -> typing.Type[FakeDate]:
    return FakeDate