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
|
import json
import os
import unittest
from io import StringIO
from tempfile import mkstemp
from molotov.util import _VARS, OptionError, expand_options, get_var, set_var
_HERE = os.path.dirname(__file__)
config = os.path.join(_HERE, "..", "..", "molotov.json")
class Args:
pass
class TestUtil(unittest.TestCase):
def setUp(self):
super().setUp()
_VARS.clear()
def test_config(self):
args = Args()
expand_options(config, "test", args)
self.assertEqual(args.duration, 1)
def _get_config(self, data):
data = json.dumps(data)
data = StringIO(data)
data.seek(0)
return data
def test_bad_config(self):
args = Args()
fd, badfile = mkstemp()
os.close(fd)
with open(badfile, "w") as f:
f.write("'1")
try:
self.assertRaises(OptionError, expand_options, badfile, "", args)
finally:
os.remove(badfile)
self.assertRaises(OptionError, expand_options, 1, "", args)
self.assertRaises(OptionError, expand_options, "", "", args)
bad_data = [
({}, "test"),
({"molotov": {}}, "test"),
({"molotov": {"tests": {}}}, "test"),
]
for data, scenario in bad_data:
self.assertRaises(
OptionError, expand_options, self._get_config(data), scenario, args
)
def test_setget_var(self):
me = object()
set_var("me", me)
self.assertTrue(get_var("me") is me)
def test_get_var_factory(self):
me = object()
def factory():
return me
self.assertTrue(get_var("me", factory) is me)
|