File: test_init.py

package info (click to toggle)
yarsync 0.1.1%2Bdeb-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 624 kB
  • sloc: python: 1,905; makefile: 13
file content (113 lines) | stat: -rw-r--r-- 3,252 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
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
# -*- coding: utf-8 -*-
# Mock OS functions and check that they are called properly

import os
import subprocess
import sys

from yarsync import YARsync
from yarsync.yarsync import CONFIG_EXAMPLE

from .settings import YSDIR


def test_init_mixed(mocker):
    ## Mock existing directory and non-existent files ###

    def _os_path_exists(filepath):
        if filepath == YSDIR:
            return True
        elif filepath.startswith(YSDIR):
            print(filepath)
            return False
        else:
            return False  # won't get access to real os.path.exists(filepath)

    m = mocker.mock_open()
    if sys.version[0] == "2":
        mocker.patch("__builtin__.open", m)
    else:
        mocker.patch("builtins.open", m)
    mocker.patch("os.path.exists", _os_path_exists)

    args = "yarsync init myhost".split()
    ys = YARsync(args)
    conffile = ys.CONFIGFILE
    repofile = ys.REPOFILE
    # call _init
    res = ys()
    assert res == 0

    call = mocker.call
    assert m.mock_calls == [
        call(conffile, "w"), call().__enter__(),
        call().write(CONFIG_EXAMPLE), call().write(''),
        call().__exit__(None, None, None),
        call(repofile, "w"), call().__enter__(),
        call().write("myhost"),
        call().write(''),  # this write is because of print(end='').
        call().__exit__(None, None, None)
    ]
    old_calls = m.mock_calls[:]
    # To clear the calls use m.reset_mock()


def test_init_non_existent(mocker):
    def _os_path_exists(filepath):
        return False

    m = mocker.mock_open()
    if sys.version[0] == "2":
        mocker.patch("__builtin__.open", m)
    else:
        mocker.patch("builtins.open", m)
    mocker.patch("os.path.exists", _os_path_exists)
    mkdir = mocker.patch("os.mkdir")

    args = "yarsync init myhost".split()
    ys = YARsync(args)
    conffile = ys.CONFIGFILE
    repofile = ys.REPOFILE

    res = ys()
    assert res == 0
    call = mocker.call
    assert mkdir.mock_calls == [call(YSDIR)]
    # assert mkdir.mock_calls == [call(YSDIR, ys.DIRMODE)]
    assert m.mock_calls == [
        # mkdir is recorded separately
        call(conffile, "w"), call().__enter__(),
        call().write(CONFIG_EXAMPLE), call().write(''),
        call().__exit__(None, None, None),
        call(repofile, "w"), call().__enter__(),
        call().write("myhost"),
        call().write(''),  # this write is because of print(end='').
        call().__exit__(None, None, None)
    ]


def test_init_existent(mocker):
    def _os_path_exists(filepath):
        # assume only files within YSDIR exist,
        # otherwise you'll have problems with gettext
        if os.path.commonprefix([filepath, YSDIR]) == YSDIR:
            return True
        # otherwise os.path.exists(filepath)
        # would cause infinite recursion here
        return False

    m = mocker.mock_open()
    if sys.version[0] == "2":
        mocker.patch("__builtin__.open", m)
    else:
        mocker.patch("builtins.open", m)
    mocker.patch("os.path.exists", _os_path_exists)
    mkdir = mocker.patch("os.mkdir")

    args = "yarsync init myhost".split()
    ys = YARsync(args)

    res = ys()
    assert res == 0
    assert mkdir.mock_calls == []
    assert m.mock_calls == []