1
# -*- coding: utf-8 -*-
3
# Copyright 2013 Jaap Karssenberg <jaap.karssenberg@gmail.com>
5
from __future__ import with_statement
11
from zim.environ import environ
12
from zim.fs import File, Dir
15
class EnvironmentContext(object):
16
'''Context manager to be able to run test cases for
17
environment parameters and restore the previous values on
18
exit or error. Usage as::
20
with EnvironmentContext({
26
# Use os.environ here for to avoid any errors with
27
# our environ object - may bite when testing with non-ascii data.
28
# But overloaded in EnvironmentConfigContext()
32
def __init__(self, environ_context):
33
self.environ_context = environ_context
34
self.environ_backup = {}
37
for k, v in self.environ_context.items():
38
self.environ_backup[k] = self.environ.get(k)
41
elif k in self.environ:
46
def __exit__(self, *exc_info):
47
for k, v in self.environ_backup.items():
50
elif k in self.environ:
59
class TestEnviron(tests.TestCase):
62
k = 'TEST_ZIM_ENVIRON_MODULE'
64
with EnvironmentContext({k: None}):
65
self.assertNotIn(k, environ)
66
self.assertRaises(KeyError, environ.__getitem__, k)
67
self.assertIsNone(environ.get(k))
68
self.assertEqual(environ.get(k, 'FOO'), 'FOO')
71
self.assertEqual(environ.get(k, 'FOO'), 'BAR')
72
self.assertEqual(environ[k], 'BAR')
73
self.assertGreater(len(environ), 0)
74
self.assertIn(k, environ)
77
self.assertNotIn(k, environ)
79
def testGetListPath(self):
80
path = environ.get_list('PATH')
81
self.assertGreater(len(path), 0)
82
for dir in map(Dir, path):
86
raise AssertionError, 'No existing dirs found in PATH: %s' % path
88
def testHomeAndUser(self):
89
user = environ.get('USER')
90
self.assertIsNotNone(user)
92
home = environ.get('HOME')
93
self.assertIsNotNone(home)
94
self.assertTrue(Dir(home).exists())
97
appdata = environ.get('APPDATA')
98
self.assertIsNotNone(appdata)
99
self.assertTrue(Dir(appdata).exists())