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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# test_imaputils.py
# This file is part of isbg.
#
# Copyright 2018 Carles Muñoz Gorriz <carlesmu@internautas.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
"""Test cases for __main__ file."""
import os
import sys
try:
import pytest
except ImportError:
pass
from unittest import mock
# We add the upper dir to the path
sys.path.insert(0, os.path.abspath(os.path.join(
os.path.dirname(__file__), '..')))
from isbg import __main__, isbg # noqa: E402
def test_parse_args():
"""Test parse_args."""
# Remove pytest options:
orig_args = sys.argv[:]
# Parse command line (it should work):
del sys.argv[1:]
for op in ["--imaphost", "localhost", "--imapuser", "anonymous",
"--imappasswd", "none", "--dryrun", "--flag",
"--noninteractive"]:
sys.argv.append(op)
sbg = isbg.ISBG()
__main__.parse_args(sbg)
assert sbg.imapsets.host == "localhost"
assert sbg.imapsets.user == "anonymous"
assert sbg.imapsets.passwd == "none"
# Parse with unknown option
del sys.argv[1:]
for op in ["--foo"]:
sys.argv.append(op)
sbg = isbg.ISBG()
with pytest.raises(isbg.ISBGError, match="[options]"):
__main__.parse_args(sbg)
pytest.fail("It should rise a docopt SystemExit")
# Parse with bogus deletehigherthan
del sys.argv[1:]
for op in ["--imaphost", "localhost", "--imapuser", "anonymous",
"--imappasswd", "none", "--dryrun", "--deletehigherthan",
"0"]:
sys.argv.append(op)
sbg = isbg.ISBG()
with pytest.raises(isbg.ISBGError, match="too small"):
__main__.parse_args(sbg)
pytest.fail("It should rise a too small ISBGError")
del sys.argv[1:]
for op in ["--imaphost", "localhost", "--imapuser", "anonymous",
"--imappasswd", "none", "--dryrun", "--deletehigherthan",
"foo"]:
sys.argv.append(op)
sbg = isbg.ISBG()
with pytest.raises(isbg.ISBGError, match="Unrecognized score"):
__main__.parse_args(sbg)
pytest.fail("It should rise a unrecognized score" + "ISBGError")
# Parse with ok deletehigherthan
del sys.argv[1:]
for op in ["--imaphost", "localhost", "--imapuser", "anonymous",
"--imappasswd", "none", "--dryrun", "--deletehigherthan",
"8"]:
sys.argv.append(op)
sbg = isbg.ISBG()
__main__.parse_args(sbg)
assert sbg.deletehigherthan == 8.0
# Parse with bogus maxsize
del sys.argv[1:]
for op in ["--imaphost", "localhost", "--imapuser", "anonymous",
"--imappasswd", "none", "--dryrun", "--maxsize", "0"]:
sys.argv.append(op)
sbg = isbg.ISBG()
with pytest.raises(isbg.ISBGError, match="too small"):
__main__.parse_args(sbg)
pytest.fail("It should rise a too small ISBGError")
del sys.argv[1:]
for op in ["--imaphost", "localhost", "--imapuser", "anonymous",
"--imappasswd", "none", "--dryrun", "--maxsize", "foo"]:
sys.argv.append(op)
sbg = isbg.ISBG()
with pytest.raises(isbg.ISBGError, match="Unrecognised size"):
__main__.parse_args(sbg)
pytest.fail("It should rise a Unrecognised size" + "ISBGError")
# Parse with ok maxsize
del sys.argv[1:]
for op in ["--imaphost", "localhost", "--imapuser", "anonymous",
"--imappasswd", "none", "--dryrun", "--maxsize", "12000"]:
sys.argv.append(op)
sbg = isbg.ISBG()
__main__.parse_args(sbg)
assert sbg.maxsize == 12000
# Parse with bogus partialrun
del sys.argv[1:]
for op in ["--imaphost", "localhost", "--imapuser", "anonymous",
"--imappasswd", "none", "--dryrun", "--partialrun", "-10"]:
sys.argv.append(op)
sbg = isbg.ISBG()
with pytest.raises(isbg.ISBGError, match="equal to 0 or higher"):
__main__.parse_args(sbg)
pytest.fail("It should rise a equal to 0 or higher " + "ISBGError")
# Parse with 0 partialrun
del sys.argv[1:]
for op in ["--imaphost", "localhost", "--imapuser", "anonymous",
"--imappasswd", "none", "--dryrun", "--partialrun", "0"]:
sys.argv.append(op)
sbg = isbg.ISBG()
__main__.parse_args(sbg)
assert sbg.partialrun is None
del sys.argv[1:]
for op in ["--imaphost", "localhost", "--imapuser", "anonymous",
"--imappasswd", "none", "--dryrun", "--partialrun", "foo"]:
sys.argv.append(op)
sbg = isbg.ISBG()
with pytest.raises(isbg.ISBGError, match="must be a integer"):
__main__.parse_args(sbg)
pytest.fail("It should rise a invalid literal " + "ValueError")
# Parse with ok partialrun and verbose and nossl
del sys.argv[1:]
for op in ["--imaphost", "localhost", "--imapuser", "anonymous",
"--imappasswd", "none", "--verbose", "--partialrun", "10",
"--nossl"]:
sys.argv.append(op)
sbg = isbg.ISBG()
__main__.parse_args(sbg)
assert sbg.partialrun == 10
# Restore pytest options:
del sys.argv[1:]
sys.argv = orig_args[:]
def test_main():
"""Test main()."""
# Remove pytest options:
args = sys.argv[:]
del sys.argv[1:]
sys.argv.append("--ignorelockfile")
with mock.patch.object(isbg, "__name__", "__main__"):
with pytest.raises(SystemExit, match="10"):
__main__.main()
pytest.fail("Not error or unexpected error")
with pytest.raises(SystemExit, match="10"):
with pytest.raises(isbg.ISBGError, match="10"):
__main__.main()
pytest.fail("Not error or unexpected error message")
pytest.fail("Not error or unexpected error")
sys.argv.append("--version")
with mock.patch.object(isbg, "__name__", "__main__"):
with pytest.raises(SystemExit):
__main__.main()
pytest.fail("Not error or unexpected error")
sys.argv.append("--exitcodes")
with mock.patch.object(isbg, "__name__", "__main__"):
with pytest.raises(SystemExit):
__main__.main()
pytest.fail("Not error or unexpected error")
del sys.argv[1:]
sys.argv.append("--usage")
with mock.patch.object(isbg, "__name__", "__main__"):
with pytest.raises(SystemExit):
__main__.main()
pytest.fail("Not Exit or unexpected error")
# Restore pytest options:
sys.argv = args[:]
|