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
|
# Mock OS functions and check that they are called properly
import os
from sys import version_info
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 ##
args = "yarsync init".split()
ys0 = YARsync(args)
conffile = ys0.CONFIGFILE
repofile = ys0.REPOFILE.format("my_repo")
def _os_path_exists(filepath):
if filepath == YSDIR:
return True
elif filepath == conffile:
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()
mocker.patch("builtins.open", m)
# the user inputs "my_host"
mocker.patch("builtins.input", lambda _: "my_repo")
mocker.patch("os.path.exists", _os_path_exists)
# otherwise listdir will complain that .ys doesn't exist
mocker.patch("os.listdir", lambda _: [])
# call _init
res = ys0()
assert res == 0
call = mocker.call
if version_info.minor >= 13:
assert m.mock_calls == [
# call(conffile, "w"), call().__enter__(),
# call().write(CONFIG_EXAMPLE), call().write(''),
# call().__exit__(None, None, None),
call(repofile, "x"), call().__enter__(),
call().__exit__(None, None, None),
call().close()
]
else:
assert m.mock_calls == [
# call(conffile, "w"), call().__enter__(),
# call().write(CONFIG_EXAMPLE), call().write(''),
# call().__exit__(None, None, None),
call(repofile, "x"), call().__enter__(),
call().__exit__(None, None, None)
]
# old_calls = m.mock_calls[:]
# clear the calls
m.reset_mock()
# the user inputs nothing, and hostname is taken
mocker.patch("builtins.input", lambda _: "")
mocker.patch("socket.gethostname", lambda: "my_host")
# don't forget to call the function
ys1 = YARsync(["yarsync", "init"])
ys1._init()
repofile = ys1.REPOFILE.format("my_host")
assert call(repofile, "x") in m.mock_calls
def test_init_non_existent(mocker):
def _os_path_exists(filepath):
return False
m = mocker.mock_open()
mocker.patch("builtins.open", m)
mocker.patch("os.path.exists", _os_path_exists)
mkdir = mocker.patch("os.mkdir")
mocker.patch("os.listdir", lambda _: [])
args = "yarsync init myhost".split()
ys = YARsync(args)
conffile = ys.CONFIGFILE
repofile = ys.REPOFILE.format("myhost")
res = ys()
assert res == 0
call = mocker.call
assert mkdir.mock_calls == [call(YSDIR)]
# assert mkdir.mock_calls == [call(YSDIR, ys.DIRMODE)]
if version_info.minor >= 13:
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().close(),
call(repofile, "x"), call().__enter__(),
call().__exit__(None, None, None),
call().close(),
]
else:
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, "x"), call().__enter__(),
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
args = "yarsync init myhost".split()
m = mocker.mock_open()
mocker.patch("builtins.open", m)
# no input is prompted when we provide repo name in CL args
input_ = mocker.patch("builtins.input")
mocker.patch("os.path.exists", _os_path_exists)
mocker.patch("os.listdir", lambda _: ["repo_myhost.txt"])
mkdir = mocker.patch("os.mkdir")
ys = YARsync(args)
res = ys()
assert res == 0
assert input_.mock_calls == []
assert mkdir.mock_calls == []
assert m.mock_calls == []
|